* Removed lots of explicit require_once statements. The autoloader should theoretical...
[lhc/web/wiklou.git] / includes / ExternalStoreDB.php
1 <?php
2 /**
3 *
4 * @package MediaWiki
5 *
6 * DB accessable external objects
7 *
8 */
9
10
11 /** @package MediaWiki */
12
13 /**
14 * External database storage will use one (or more) separate connection pools
15 * from what the main wiki uses. If we load many revisions, such as when doing
16 * bulk backups or maintenance, we want to keep them around over the lifetime
17 * of the script.
18 *
19 * Associative array of LoadBalancer objects, indexed by cluster name.
20 */
21 global $wgExternalLoadBalancers;
22 $wgExternalLoadBalancers = array();
23
24 /**
25 * One-step cache variable to hold base blobs; operations that
26 * pull multiple revisions may often pull multiple times from
27 * the same blob. By keeping the last-used one open, we avoid
28 * redundant unserialization and decompression overhead.
29 */
30 global $wgExternalBlobCache;
31 $wgExternalBlobCache = array();
32
33 class ExternalStoreDB {
34
35 /** @todo Document.*/
36 function &getLoadBalancer( $cluster ) {
37 global $wgExternalServers, $wgExternalLoadBalancers;
38 if ( !array_key_exists( $cluster, $wgExternalLoadBalancers ) ) {
39 $wgExternalLoadBalancers[$cluster] = LoadBalancer::newFromParams( $wgExternalServers[$cluster] );
40 }
41 $wgExternalLoadBalancers[$cluster]->allowLagged(true);
42 return $wgExternalLoadBalancers[$cluster];
43 }
44
45 /** @todo Document.*/
46 function &getSlave( $cluster ) {
47 $lb =& $this->getLoadBalancer( $cluster );
48 return $lb->getConnection( DB_SLAVE );
49 }
50
51 /** @todo Document.*/
52 function &getMaster( $cluster ) {
53 $lb =& $this->getLoadBalancer( $cluster );
54 return $lb->getConnection( DB_MASTER );
55 }
56
57 /** @todo Document.*/
58 function getTable( &$db ) {
59 $table = $db->getLBInfo( 'blobs table' );
60 if ( is_null( $table ) ) {
61 $table = 'blobs';
62 }
63 return $table;
64 }
65
66 /**
67 * Fetch data from given URL
68 * @param string $url An url of the form DB://cluster/id or DB://cluster/id/itemid for concatened storage.
69 */
70 function fetchFromURL($url) {
71 $path = explode( '/', $url );
72 $cluster = $path[2];
73 $id = $path[3];
74 if ( isset( $path[4] ) ) {
75 $itemID = $path[4];
76 } else {
77 $itemID = false;
78 }
79
80 $ret =& $this->fetchBlob( $cluster, $id, $itemID );
81
82 if ( $itemID !== false && $ret !== false ) {
83 return $ret->getItem( $itemID );
84 }
85 return $ret;
86 }
87
88 /**
89 * Fetch a blob item out of the database; a cache of the last-loaded
90 * blob will be kept so that multiple loads out of a multi-item blob
91 * can avoid redundant database access and decompression.
92 * @param $cluster
93 * @param $id
94 * @param $itemID
95 * @return mixed
96 * @private
97 */
98 function &fetchBlob( $cluster, $id, $itemID ) {
99 global $wgExternalBlobCache;
100 $cacheID = ( $itemID === false ) ? "$cluster/$id" : "$cluster/$id/";
101 if( isset( $wgExternalBlobCache[$cacheID] ) ) {
102 wfDebug( "ExternalStoreDB::fetchBlob cache hit on $cacheID\n" );
103 return $wgExternalBlobCache[$cacheID];
104 }
105
106 wfDebug( "ExternalStoreDB::fetchBlob cache miss on $cacheID\n" );
107
108 $dbr =& $this->getSlave( $cluster );
109 $ret = $dbr->selectField( $this->getTable( $dbr ), 'blob_text', array( 'blob_id' => $id ) );
110 if ( $ret === false ) {
111 wfDebugLog( 'ExternalStoreDB', "ExternalStoreDB::fetchBlob master fallback on $cacheID\n" );
112 // Try the master
113 $dbw =& $this->getMaster( $cluster );
114 $ret = $dbw->selectField( $this->getTable( $dbw ), 'blob_text', array( 'blob_id' => $id ) );
115 if( $ret === false) {
116 wfDebugLog( 'ExternalStoreDB', "ExternalStoreDB::fetchBlob master failed to find $cacheID\n" );
117 }
118 }
119 if( $itemID !== false && $ret !== false ) {
120 // Unserialise object; caller extracts item
121 $ret = unserialize( $ret );
122 }
123
124 $wgExternalBlobCache = array( $cacheID => &$ret );
125 return $ret;
126 }
127
128 /**
129 * Insert a data item into a given cluster
130 *
131 * @param $cluster String: the cluster name
132 * @param $data String: the data item
133 * @return string URL
134 */
135 function store( $cluster, $data ) {
136 $fname = 'ExternalStoreDB::store';
137
138 $dbw =& $this->getMaster( $cluster );
139
140 $id = $dbw->nextSequenceValue( 'blob_blob_id_seq' );
141 $dbw->insert( $this->getTable( $dbw ), array( 'blob_id' => $id, 'blob_text' => $data ), $fname );
142 $id = $dbw->insertId();
143 if ( $dbw->getFlag( DBO_TRX ) ) {
144 $dbw->immediateCommit();
145 }
146 return "DB://$cluster/$id";
147 }
148 }
149 ?>