(minor) use wfDebugLog consistently.
[lhc/web/wiklou.git] / includes / externalstore / ExternalStoreDB.php
1 <?php
2 /**
3 * External storage in SQL database.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 /**
24 * DB accessable external objects
25 * @ingroup ExternalStorage
26 */
27 class ExternalStoreDB {
28
29 /**
30 * @param $params array
31 */
32 function __construct( $params = array() ) {
33 $this->mParams = $params;
34 }
35
36 /**
37 * Get a LoadBalancer for the specified cluster
38 *
39 * @param $cluster String: cluster name
40 * @return LoadBalancer object
41 */
42 function &getLoadBalancer( $cluster ) {
43 $wiki = isset($this->mParams['wiki']) ? $this->mParams['wiki'] : false;
44
45 return wfGetLBFactory()->getExternalLB( $cluster, $wiki );
46 }
47
48 /**
49 * Get a slave database connection for the specified cluster
50 *
51 * @param $cluster String: cluster name
52 * @return DatabaseBase object
53 */
54 function &getSlave( $cluster ) {
55 global $wgDefaultExternalStore;
56
57 $wiki = isset($this->mParams['wiki']) ? $this->mParams['wiki'] : false;
58 $lb =& $this->getLoadBalancer( $cluster );
59
60 if ( !in_array( "DB://" . $cluster, (array)$wgDefaultExternalStore ) ) {
61 wfDebug( "read only external store" );
62 $lb->allowLagged(true);
63 } else {
64 wfDebug( "writable external store" );
65 }
66
67 return $lb->getConnection( DB_SLAVE, array(), $wiki );
68 }
69
70 /**
71 * Get a master database connection for the specified cluster
72 *
73 * @param $cluster String: cluster name
74 * @return DatabaseBase object
75 */
76 function &getMaster( $cluster ) {
77 $wiki = isset($this->mParams['wiki']) ? $this->mParams['wiki'] : false;
78 $lb =& $this->getLoadBalancer( $cluster );
79 return $lb->getConnection( DB_MASTER, array(), $wiki );
80 }
81
82 /**
83 * Get the 'blobs' table name for this database
84 *
85 * @param $db DatabaseBase
86 * @return String: table name ('blobs' by default)
87 */
88 function getTable( &$db ) {
89 $table = $db->getLBInfo( 'blobs table' );
90 if ( is_null( $table ) ) {
91 $table = 'blobs';
92 }
93 return $table;
94 }
95
96 /**
97 * Fetch data from given URL
98 * @param $url String: an url of the form DB://cluster/id or DB://cluster/id/itemid for concatened storage.
99 * @return mixed
100 */
101 function fetchFromURL( $url ) {
102 $path = explode( '/', $url );
103 $cluster = $path[2];
104 $id = $path[3];
105 if ( isset( $path[4] ) ) {
106 $itemID = $path[4];
107 } else {
108 $itemID = false;
109 }
110
111 $ret =& $this->fetchBlob( $cluster, $id, $itemID );
112
113 if ( $itemID !== false && $ret !== false ) {
114 return $ret->getItem( $itemID );
115 }
116 return $ret;
117 }
118
119 /**
120 * Fetch a blob item out of the database; a cache of the last-loaded
121 * blob will be kept so that multiple loads out of a multi-item blob
122 * can avoid redundant database access and decompression.
123 * @param $cluster
124 * @param $id
125 * @param $itemID
126 * @return mixed
127 * @private
128 */
129 function &fetchBlob( $cluster, $id, $itemID ) {
130 /**
131 * One-step cache variable to hold base blobs; operations that
132 * pull multiple revisions may often pull multiple times from
133 * the same blob. By keeping the last-used one open, we avoid
134 * redundant unserialization and decompression overhead.
135 */
136 static $externalBlobCache = array();
137
138 $cacheID = ( $itemID === false ) ? "$cluster/$id" : "$cluster/$id/";
139 if( isset( $externalBlobCache[$cacheID] ) ) {
140 wfDebugLog( 'ExternalStoreDB-cache', "ExternalStoreDB::fetchBlob cache hit on $cacheID\n" );
141 return $externalBlobCache[$cacheID];
142 }
143
144 wfDebugLog( 'ExternalStoreDB-cache', "ExternalStoreDB::fetchBlob cache miss on $cacheID\n" );
145
146 $dbr =& $this->getSlave( $cluster );
147 $ret = $dbr->selectField( $this->getTable( $dbr ), 'blob_text', array( 'blob_id' => $id ), __METHOD__ );
148 if ( $ret === false ) {
149 wfDebugLog( 'ExternalStoreDB', "ExternalStoreDB::fetchBlob master fallback on $cacheID\n" );
150 // Try the master
151 $dbw =& $this->getMaster( $cluster );
152 $ret = $dbw->selectField( $this->getTable( $dbw ), 'blob_text', array( 'blob_id' => $id ), __METHOD__ );
153 if( $ret === false) {
154 wfDebugLog( 'ExternalStoreDB', "ExternalStoreDB::fetchBlob master failed to find $cacheID\n" );
155 }
156 }
157 if( $itemID !== false && $ret !== false ) {
158 // Unserialise object; caller extracts item
159 $ret = unserialize( $ret );
160 }
161
162 $externalBlobCache = array( $cacheID => &$ret );
163 return $ret;
164 }
165
166 /**
167 * Insert a data item into a given cluster
168 *
169 * @param $cluster String: the cluster name
170 * @param $data String: the data item
171 * @throws MWException
172 * @return string URL
173 */
174 function store( $cluster, $data ) {
175 $dbw = $this->getMaster( $cluster );
176 $id = $dbw->nextSequenceValue( 'blob_blob_id_seq' );
177 $dbw->insert( $this->getTable( $dbw ),
178 array( 'blob_id' => $id, 'blob_text' => $data ),
179 __METHOD__ );
180 $id = $dbw->insertId();
181 if ( !$id ) {
182 throw new MWException( __METHOD__.': no insert ID' );
183 }
184 if ( $dbw->getFlag( DBO_TRX ) ) {
185 $dbw->commit( __METHOD__ );
186 }
187 return "DB://$cluster/$id";
188 }
189 }