getParamValue defaults to current page only if url undefined
[mediawiki.git] / includes / ExternalStoreDB.php
blob37b1b9335dba7fdbe5dfef107cc926ebd3a0375f
1 <?php
2 /**
3 * External storage in SQL database.
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.
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.
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
20 * @file
23 /**
24 * DB accessable external objects
25 * @ingroup ExternalStorage
27 class ExternalStoreDB {
29 /**
30 * @param $params array
32 function __construct( $params = array() ) {
33 $this->mParams = $params;
36 /**
37 * Get a LoadBalancer for the specified cluster
39 * @param $cluster String: cluster name
40 * @return LoadBalancer object
42 function &getLoadBalancer( $cluster ) {
43 $wiki = isset($this->mParams['wiki']) ? $this->mParams['wiki'] : false;
45 return wfGetLBFactory()->getExternalLB( $cluster, $wiki );
48 /**
49 * Get a slave database connection for the specified cluster
51 * @param $cluster String: cluster name
52 * @return DatabaseBase object
54 function &getSlave( $cluster ) {
55 global $wgDefaultExternalStore;
57 $wiki = isset($this->mParams['wiki']) ? $this->mParams['wiki'] : false;
58 $lb =& $this->getLoadBalancer( $cluster );
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" );
67 return $lb->getConnection( DB_SLAVE, array(), $wiki );
70 /**
71 * Get a master database connection for the specified cluster
73 * @param $cluster String: cluster name
74 * @return DatabaseBase object
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 );
82 /**
83 * Get the 'blobs' table name for this database
85 * @param $db DatabaseBase
86 * @return String: table name ('blobs' by default)
88 function getTable( &$db ) {
89 $table = $db->getLBInfo( 'blobs table' );
90 if ( is_null( $table ) ) {
91 $table = 'blobs';
93 return $table;
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
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;
111 $ret =& $this->fetchBlob( $cluster, $id, $itemID );
113 if ( $itemID !== false && $ret !== false ) {
114 return $ret->getItem( $itemID );
116 return $ret;
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
129 function &fetchBlob( $cluster, $id, $itemID ) {
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.
136 static $externalBlobCache = array();
138 $cacheID = ( $itemID === false ) ? "$cluster/$id" : "$cluster/$id/";
139 if( isset( $externalBlobCache[$cacheID] ) ) {
140 wfDebug( "ExternalStoreDB::fetchBlob cache hit on $cacheID\n" );
141 return $externalBlobCache[$cacheID];
144 wfDebug( "ExternalStoreDB::fetchBlob cache miss on $cacheID\n" );
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" );
157 if( $itemID !== false && $ret !== false ) {
158 // Unserialise object; caller extracts item
159 $ret = unserialize( $ret );
162 $externalBlobCache = array( $cacheID => &$ret );
163 return $ret;
167 * Insert a data item into a given cluster
169 * @param $cluster String: the cluster name
170 * @param $data String: the data item
171 * @throws MWException
172 * @return string URL
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' );
184 if ( $dbw->getFlag( DBO_TRX ) ) {
185 $dbw->commit( __METHOD__ );
187 return "DB://$cluster/$id";