Merge "Use local context to get messages"
[lhc/web/wiklou.git] / includes / ExternalStoreDB.php
1 <?php
2
3 /**
4 * DB accessable external objects
5 * @ingroup ExternalStorage
6 */
7 class ExternalStoreDB {
8
9 function __construct( $params = array() ) {
10 $this->mParams = $params;
11 }
12
13 /**
14 * Get a LoadBalancer for the specified cluster
15 *
16 * @param $cluster String: cluster name
17 * @return LoadBalancer object
18 */
19 function &getLoadBalancer( $cluster ) {
20 $wiki = isset($this->mParams['wiki']) ? $this->mParams['wiki'] : false;
21
22 return wfGetLBFactory()->getExternalLB( $cluster, $wiki );
23 }
24
25 /**
26 * Get a slave database connection for the specified cluster
27 *
28 * @param $cluster String: cluster name
29 * @return DatabaseBase object
30 */
31 function &getSlave( $cluster ) {
32 global $wgDefaultExternalStore;
33
34 $wiki = isset($this->mParams['wiki']) ? $this->mParams['wiki'] : false;
35 $lb =& $this->getLoadBalancer( $cluster );
36
37 if ( !in_array( "DB://" . $cluster, (array)$wgDefaultExternalStore ) ) {
38 wfDebug( "read only external store" );
39 $lb->allowLagged(true);
40 } else {
41 wfDebug( "writable external store" );
42 }
43
44 return $lb->getConnection( DB_SLAVE, array(), $wiki );
45 }
46
47 /**
48 * Get a master database connection for the specified cluster
49 *
50 * @param $cluster String: cluster name
51 * @return DatabaseBase object
52 */
53 function &getMaster( $cluster ) {
54 $wiki = isset($this->mParams['wiki']) ? $this->mParams['wiki'] : false;
55 $lb =& $this->getLoadBalancer( $cluster );
56 return $lb->getConnection( DB_MASTER, array(), $wiki );
57 }
58
59 /**
60 * Get the 'blobs' table name for this database
61 *
62 * @param $db DatabaseBase
63 * @return String: table name ('blobs' by default)
64 */
65 function getTable( &$db ) {
66 $table = $db->getLBInfo( 'blobs table' );
67 if ( is_null( $table ) ) {
68 $table = 'blobs';
69 }
70 return $table;
71 }
72
73 /**
74 * Fetch data from given URL
75 * @param $url String: an url of the form DB://cluster/id or DB://cluster/id/itemid for concatened storage.
76 * @return mixed
77 */
78 function fetchFromURL( $url ) {
79 $path = explode( '/', $url );
80 $cluster = $path[2];
81 $id = $path[3];
82 if ( isset( $path[4] ) ) {
83 $itemID = $path[4];
84 } else {
85 $itemID = false;
86 }
87
88 $ret =& $this->fetchBlob( $cluster, $id, $itemID );
89
90 if ( $itemID !== false && $ret !== false ) {
91 return $ret->getItem( $itemID );
92 }
93 return $ret;
94 }
95
96 /**
97 * Fetch a blob item out of the database; a cache of the last-loaded
98 * blob will be kept so that multiple loads out of a multi-item blob
99 * can avoid redundant database access and decompression.
100 * @param $cluster
101 * @param $id
102 * @param $itemID
103 * @return mixed
104 * @private
105 */
106 function &fetchBlob( $cluster, $id, $itemID ) {
107 /**
108 * One-step cache variable to hold base blobs; operations that
109 * pull multiple revisions may often pull multiple times from
110 * the same blob. By keeping the last-used one open, we avoid
111 * redundant unserialization and decompression overhead.
112 */
113 static $externalBlobCache = array();
114
115 $cacheID = ( $itemID === false ) ? "$cluster/$id" : "$cluster/$id/";
116 if( isset( $externalBlobCache[$cacheID] ) ) {
117 wfDebug( "ExternalStoreDB::fetchBlob cache hit on $cacheID\n" );
118 return $externalBlobCache[$cacheID];
119 }
120
121 wfDebug( "ExternalStoreDB::fetchBlob cache miss on $cacheID\n" );
122
123 $dbr =& $this->getSlave( $cluster );
124 $ret = $dbr->selectField( $this->getTable( $dbr ), 'blob_text', array( 'blob_id' => $id ), __METHOD__ );
125 if ( $ret === false ) {
126 wfDebugLog( 'ExternalStoreDB', "ExternalStoreDB::fetchBlob master fallback on $cacheID\n" );
127 // Try the master
128 $dbw =& $this->getMaster( $cluster );
129 $ret = $dbw->selectField( $this->getTable( $dbw ), 'blob_text', array( 'blob_id' => $id ), __METHOD__ );
130 if( $ret === false) {
131 wfDebugLog( 'ExternalStoreDB', "ExternalStoreDB::fetchBlob master failed to find $cacheID\n" );
132 }
133 }
134 if( $itemID !== false && $ret !== false ) {
135 // Unserialise object; caller extracts item
136 $ret = unserialize( $ret );
137 }
138
139 $externalBlobCache = array( $cacheID => &$ret );
140 return $ret;
141 }
142
143 /**
144 * Insert a data item into a given cluster
145 *
146 * @param $cluster String: the cluster name
147 * @param $data String: the data item
148 * @return string URL
149 */
150 function store( $cluster, $data ) {
151 $dbw = $this->getMaster( $cluster );
152 $id = $dbw->nextSequenceValue( 'blob_blob_id_seq' );
153 $dbw->insert( $this->getTable( $dbw ),
154 array( 'blob_id' => $id, 'blob_text' => $data ),
155 __METHOD__ );
156 $id = $dbw->insertId();
157 if ( !$id ) {
158 throw new MWException( __METHOD__.': no insert ID' );
159 }
160 if ( $dbw->getFlag( DBO_TRX ) ) {
161 $dbw->commit( __METHOD__ );
162 }
163 return "DB://$cluster/$id";
164 }
165 }