MediaWiki  1.23.6
ExternalStoreDB.php
Go to the documentation of this file.
1 <?php
38  public function fetchFromURL( $url ) {
39  list( $cluster, $id, $itemID ) = $this->parseURL( $url );
40  $ret = $this->fetchBlob( $cluster, $id, $itemID );
41 
42  if ( $itemID !== false && $ret !== false ) {
43  return $ret->getItem( $itemID );
44  }
45 
46  return $ret;
47  }
48 
58  public function batchFetchFromURLs( array $urls ) {
59  $batched = $inverseUrlMap = array();
60  foreach ( $urls as $url ) {
61  list( $cluster, $id, $itemID ) = $this->parseURL( $url );
62  $batched[$cluster][$id][] = $itemID;
63  // false $itemID gets cast to int, but should be ok
64  // since we do === from the $itemID in $batched
65  $inverseUrlMap[$cluster][$id][$itemID] = $url;
66  }
67  $ret = array();
68  foreach ( $batched as $cluster => $batchByCluster ) {
69  $res = $this->batchFetchBlobs( $cluster, $batchByCluster );
71  foreach ( $res as $id => $blob ) {
72  foreach ( $batchByCluster[$id] as $itemID ) {
73  $url = $inverseUrlMap[$cluster][$id][$itemID];
74  if ( $itemID === false ) {
75  $ret[$url] = $blob;
76  } else {
77  $ret[$url] = $blob->getItem( $itemID );
78  }
79  }
80  }
81  }
82 
83  return $ret;
84  }
85 
89  public function store( $cluster, $data ) {
90  $dbw = $this->getMaster( $cluster );
91  $id = $dbw->nextSequenceValue( 'blob_blob_id_seq' );
92  $dbw->insert( $this->getTable( $dbw ),
93  array( 'blob_id' => $id, 'blob_text' => $data ),
94  __METHOD__ );
95  $id = $dbw->insertId();
96  if ( !$id ) {
97  throw new MWException( __METHOD__ . ': no insert ID' );
98  }
99  if ( $dbw->getFlag( DBO_TRX ) ) {
100  $dbw->commit( __METHOD__ );
101  }
102 
103  return "DB://$cluster/$id";
104  }
105 
112  function getLoadBalancer( $cluster ) {
113  $wiki = isset( $this->params['wiki'] ) ? $this->params['wiki'] : false;
114 
115  return wfGetLBFactory()->getExternalLB( $cluster, $wiki );
116  }
117 
124  function getSlave( $cluster ) {
125  global $wgDefaultExternalStore;
126 
127  $wiki = isset( $this->params['wiki'] ) ? $this->params['wiki'] : false;
128  $lb = $this->getLoadBalancer( $cluster );
129 
130  if ( !in_array( "DB://" . $cluster, (array)$wgDefaultExternalStore ) ) {
131  wfDebug( "read only external store\n" );
132  $lb->allowLagged( true );
133  } else {
134  wfDebug( "writable external store\n" );
135  }
136 
137  return $lb->getConnection( DB_SLAVE, array(), $wiki );
138  }
139 
146  function getMaster( $cluster ) {
147  $wiki = isset( $this->params['wiki'] ) ? $this->params['wiki'] : false;
148  $lb = $this->getLoadBalancer( $cluster );
149 
150  return $lb->getConnection( DB_MASTER, array(), $wiki );
151  }
152 
159  function getTable( $db ) {
160  $table = $db->getLBInfo( 'blobs table' );
161  if ( is_null( $table ) ) {
162  $table = 'blobs';
163  }
164 
165  return $table;
166  }
167 
178  function fetchBlob( $cluster, $id, $itemID ) {
185  static $externalBlobCache = array();
186 
187  $cacheID = ( $itemID === false ) ? "$cluster/$id" : "$cluster/$id/";
188  if ( isset( $externalBlobCache[$cacheID] ) ) {
189  wfDebugLog( 'ExternalStoreDB-cache',
190  "ExternalStoreDB::fetchBlob cache hit on $cacheID" );
191 
192  return $externalBlobCache[$cacheID];
193  }
194 
195  wfDebugLog( 'ExternalStoreDB-cache',
196  "ExternalStoreDB::fetchBlob cache miss on $cacheID" );
197 
198  $dbr = $this->getSlave( $cluster );
199  $ret = $dbr->selectField( $this->getTable( $dbr ),
200  'blob_text', array( 'blob_id' => $id ), __METHOD__ );
201  if ( $ret === false ) {
202  wfDebugLog( 'ExternalStoreDB',
203  "ExternalStoreDB::fetchBlob master fallback on $cacheID" );
204  // Try the master
205  $dbw = $this->getMaster( $cluster );
206  $ret = $dbw->selectField( $this->getTable( $dbw ),
207  'blob_text', array( 'blob_id' => $id ), __METHOD__ );
208  if ( $ret === false ) {
209  wfDebugLog( 'ExternalStoreDB',
210  "ExternalStoreDB::fetchBlob master failed to find $cacheID" );
211  }
212  }
213  if ( $itemID !== false && $ret !== false ) {
214  // Unserialise object; caller extracts item
215  $ret = unserialize( $ret );
216  }
217 
218  $externalBlobCache = array( $cacheID => $ret );
219 
220  return $ret;
221  }
222 
231  function batchFetchBlobs( $cluster, array $ids ) {
232  $dbr = $this->getSlave( $cluster );
233  $res = $dbr->select( $this->getTable( $dbr ),
234  array( 'blob_id', 'blob_text' ), array( 'blob_id' => array_keys( $ids ) ), __METHOD__ );
235  $ret = array();
236  if ( $res !== false ) {
237  $this->mergeBatchResult( $ret, $ids, $res );
238  }
239  if ( $ids ) {
240  wfDebugLog( __CLASS__, __METHOD__ .
241  " master fallback on '$cluster' for: " .
242  implode( ',', array_keys( $ids ) ) );
243  // Try the master
244  $dbw = $this->getMaster( $cluster );
245  $res = $dbw->select( $this->getTable( $dbr ),
246  array( 'blob_id', 'blob_text' ),
247  array( 'blob_id' => array_keys( $ids ) ),
248  __METHOD__ );
249  if ( $res === false ) {
250  wfDebugLog( __CLASS__, __METHOD__ . " master failed on '$cluster'" );
251  } else {
252  $this->mergeBatchResult( $ret, $ids, $res );
253  }
254  }
255  if ( $ids ) {
256  wfDebugLog( __CLASS__, __METHOD__ .
257  " master on '$cluster' failed locating items: " .
258  implode( ',', array_keys( $ids ) ) );
259  }
260 
261  return $ret;
262  }
263 
270  private function mergeBatchResult( array &$ret, array &$ids, $res ) {
271  foreach ( $res as $row ) {
272  $id = $row->blob_id;
273  $itemIDs = $ids[$id];
274  unset( $ids[$id] ); // to track if everything is found
275  if ( count( $itemIDs ) === 1 && reset( $itemIDs ) === false ) {
276  // single result stored per blob
277  $ret[$id] = $row->blob_text;
278  } else {
279  // multi result stored per blob
280  $ret[$id] = unserialize( $row->blob_text );
281  }
282  }
283  }
284 
285  protected function parseURL( $url ) {
286  $path = explode( '/', $url );
287 
288  return array(
289  $path[2], // cluster
290  $path[3], // id
291  isset( $path[4] ) ? $path[4] : false // itemID
292  );
293  }
294 }
ExternalStoreDB\batchFetchBlobs
batchFetchBlobs( $cluster, array $ids)
Fetch multiple blob items out of the database.
Definition: ExternalStoreDB.php:231
ExternalStoreMedium
Accessable external objects in a particular storage medium.
Definition: ExternalStoreMedium.php:31
DB_MASTER
const DB_MASTER
Definition: Defines.php:56
ExternalStoreDB\getSlave
getSlave( $cluster)
Get a slave database connection for the specified cluster.
Definition: ExternalStoreDB.php:124
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
ExternalStoreDB\parseURL
parseURL( $url)
Definition: ExternalStoreDB.php:285
ExternalStoreDB
DB accessable external objects.
Definition: ExternalStoreDB.php:31
ExternalStoreDB\batchFetchFromURLs
batchFetchFromURLs(array $urls)
Fetch data from given external store URLs.
Definition: ExternalStoreDB.php:58
wfDebugLog
wfDebugLog( $logGroup, $text, $dest='all')
Send a line to a supplementary debug log file, if configured, or main debug log if not.
Definition: GlobalFunctions.php:1040
$ret
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition: hooks.txt:1530
ExternalStoreDB\fetchBlob
fetchBlob( $cluster, $id, $itemID)
Fetch a blob item out of the database; a cache of the last-loaded blob will be kept so that multiple ...
Definition: ExternalStoreDB.php:178
$dbr
$dbr
Definition: testCompression.php:48
ExternalStoreDB\store
store( $cluster, $data)
Definition: ExternalStoreDB.php:89
ExternalStoreDB\getLoadBalancer
getLoadBalancer( $cluster)
Get a LoadBalancer for the specified cluster.
Definition: ExternalStoreDB.php:112
$lb
if( $wgAPIRequestLog) $lb
Definition: api.php:126
MWException
MediaWiki exception.
Definition: MWException.php:26
$blob
$blob
Definition: testCompression.php:61
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
list
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:188
ExternalStoreDB\getTable
getTable( $db)
Get the 'blobs' table name for this database.
Definition: ExternalStoreDB.php:159
ExternalStoreDB\fetchFromURL
fetchFromURL( $url)
The provided URL is in the form of DB://cluster/id or DB://cluster/id/itemid for concatened storage.
Definition: ExternalStoreDB.php:38
wfDebug
wfDebug( $text, $dest='all')
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:933
DB_SLAVE
const DB_SLAVE
Definition: Defines.php:55
wfGetLBFactory
& wfGetLBFactory()
Get the load balancer factory object.
Definition: GlobalFunctions.php:3678
$path
$path
Definition: NoLocalSettings.php:35
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
$res
$res
Definition: database.txt:21
ExternalStoreDB\mergeBatchResult
mergeBatchResult(array &$ret, array &$ids, $res)
Helper function for self::batchFetchBlobs for merging master/slave results.
Definition: ExternalStoreDB.php:270
DBO_TRX
const DBO_TRX
Definition: Defines.php:42
ExternalStoreDB\getMaster
getMaster( $cluster)
Get a master database connection for the specified cluster.
Definition: ExternalStoreDB.php:146