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