MediaWiki  1.29.1
ExternalStoreDB.php
Go to the documentation of this file.
1 <?php
27 
45  public function fetchFromURL( $url ) {
46  list( $cluster, $id, $itemID ) = $this->parseURL( $url );
47  $ret = $this->fetchBlob( $cluster, $id, $itemID );
48 
49  if ( $itemID !== false && $ret !== false ) {
50  return $ret->getItem( $itemID );
51  }
52 
53  return $ret;
54  }
55 
65  public function batchFetchFromURLs( array $urls ) {
66  $batched = $inverseUrlMap = [];
67  foreach ( $urls as $url ) {
68  list( $cluster, $id, $itemID ) = $this->parseURL( $url );
69  $batched[$cluster][$id][] = $itemID;
70  // false $itemID gets cast to int, but should be ok
71  // since we do === from the $itemID in $batched
72  $inverseUrlMap[$cluster][$id][$itemID] = $url;
73  }
74  $ret = [];
75  foreach ( $batched as $cluster => $batchByCluster ) {
76  $res = $this->batchFetchBlobs( $cluster, $batchByCluster );
78  foreach ( $res as $id => $blob ) {
79  foreach ( $batchByCluster[$id] as $itemID ) {
80  $url = $inverseUrlMap[$cluster][$id][$itemID];
81  if ( $itemID === false ) {
82  $ret[$url] = $blob;
83  } else {
84  $ret[$url] = $blob->getItem( $itemID );
85  }
86  }
87  }
88  }
89 
90  return $ret;
91  }
92 
93  public function store( $location, $data ) {
94  $dbw = $this->getMaster( $location );
95  $id = $dbw->nextSequenceValue( 'blob_blob_id_seq' );
96  $dbw->insert( $this->getTable( $dbw ),
97  [ 'blob_id' => $id, 'blob_text' => $data ],
98  __METHOD__ );
99  $id = $dbw->insertId();
100  if ( !$id ) {
101  throw new MWException( __METHOD__ . ': no insert ID' );
102  }
103 
104  return "DB://$location/$id";
105  }
106 
113  private function getLoadBalancer( $cluster ) {
114  return wfGetLBFactory()->getExternalLB( $cluster );
115  }
116 
123  public function getSlave( $cluster ) {
125 
126  $wiki = isset( $this->params['wiki'] ) ? $this->params['wiki'] : false;
127  $lb = $this->getLoadBalancer( $cluster );
128 
129  if ( !in_array( "DB://" . $cluster, (array)$wgDefaultExternalStore ) ) {
130  wfDebug( "read only external store\n" );
131  $lb->allowLagged( true );
132  } else {
133  wfDebug( "writable external store\n" );
134  }
135 
136  $db = $lb->getConnectionRef( DB_REPLICA, [], $wiki );
137  $db->clearFlag( DBO_TRX ); // sanity
138 
139  return $db;
140  }
141 
148  public function getMaster( $cluster ) {
149  $wiki = isset( $this->params['wiki'] ) ? $this->params['wiki'] : false;
150  $lb = $this->getLoadBalancer( $cluster );
151 
152  $db = $lb->getMaintenanceConnectionRef( DB_MASTER, [], $wiki );
153  $db->clearFlag( DBO_TRX ); // sanity
154 
155  return $db;
156  }
157 
164  public function getTable( $db ) {
165  $table = $db->getLBInfo( 'blobs table' );
166  if ( is_null( $table ) ) {
167  $table = 'blobs';
168  }
169 
170  return $table;
171  }
172 
182  private function fetchBlob( $cluster, $id, $itemID ) {
189  static $externalBlobCache = [];
190 
191  $cacheID = ( $itemID === false ) ? "$cluster/$id" : "$cluster/$id/";
192  if ( isset( $externalBlobCache[$cacheID] ) ) {
193  wfDebugLog( 'ExternalStoreDB-cache',
194  "ExternalStoreDB::fetchBlob cache hit on $cacheID" );
195 
196  return $externalBlobCache[$cacheID];
197  }
198 
199  wfDebugLog( 'ExternalStoreDB-cache',
200  "ExternalStoreDB::fetchBlob cache miss on $cacheID" );
201 
202  $dbr = $this->getSlave( $cluster );
203  $ret = $dbr->selectField( $this->getTable( $dbr ),
204  'blob_text', [ 'blob_id' => $id ], __METHOD__ );
205  if ( $ret === false ) {
206  wfDebugLog( 'ExternalStoreDB',
207  "ExternalStoreDB::fetchBlob master fallback on $cacheID" );
208  // Try the master
209  $dbw = $this->getMaster( $cluster );
210  $ret = $dbw->selectField( $this->getTable( $dbw ),
211  'blob_text', [ 'blob_id' => $id ], __METHOD__ );
212  if ( $ret === false ) {
213  wfDebugLog( 'ExternalStoreDB',
214  "ExternalStoreDB::fetchBlob master failed to find $cacheID" );
215  }
216  }
217  if ( $itemID !== false && $ret !== false ) {
218  // Unserialise object; caller extracts item
219  $ret = unserialize( $ret );
220  }
221 
222  $externalBlobCache = [ $cacheID => $ret ];
223 
224  return $ret;
225  }
226 
235  private function batchFetchBlobs( $cluster, array $ids ) {
236  $dbr = $this->getSlave( $cluster );
237  $res = $dbr->select( $this->getTable( $dbr ),
238  [ 'blob_id', 'blob_text' ], [ 'blob_id' => array_keys( $ids ) ], __METHOD__ );
239  $ret = [];
240  if ( $res !== false ) {
241  $this->mergeBatchResult( $ret, $ids, $res );
242  }
243  if ( $ids ) {
244  wfDebugLog( __CLASS__, __METHOD__ .
245  " master fallback on '$cluster' for: " .
246  implode( ',', array_keys( $ids ) ) );
247  // Try the master
248  $dbw = $this->getMaster( $cluster );
249  $res = $dbw->select( $this->getTable( $dbr ),
250  [ 'blob_id', 'blob_text' ],
251  [ 'blob_id' => array_keys( $ids ) ],
252  __METHOD__ );
253  if ( $res === false ) {
254  wfDebugLog( __CLASS__, __METHOD__ . " master failed on '$cluster'" );
255  } else {
256  $this->mergeBatchResult( $ret, $ids, $res );
257  }
258  }
259  if ( $ids ) {
260  wfDebugLog( __CLASS__, __METHOD__ .
261  " master on '$cluster' failed locating items: " .
262  implode( ',', array_keys( $ids ) ) );
263  }
264 
265  return $ret;
266  }
267 
274  private function mergeBatchResult( array &$ret, array &$ids, $res ) {
275  foreach ( $res as $row ) {
276  $id = $row->blob_id;
277  $itemIDs = $ids[$id];
278  unset( $ids[$id] ); // to track if everything is found
279  if ( count( $itemIDs ) === 1 && reset( $itemIDs ) === false ) {
280  // single result stored per blob
281  $ret[$id] = $row->blob_text;
282  } else {
283  // multi result stored per blob
284  $ret[$id] = unserialize( $row->blob_text );
285  }
286  }
287  }
288 
293  protected function parseURL( $url ) {
294  $path = explode( '/', $url );
295 
296  return [
297  $path[2], // cluster
298  $path[3], // id
299  isset( $path[4] ) ? $path[4] : false // itemID
300  ];
301  }
302 }
ExternalStoreDB\batchFetchBlobs
batchFetchBlobs( $cluster, array $ids)
Fetch multiple blob items out of the database.
Definition: ExternalStoreDB.php:235
ExternalStoreMedium
Accessable external objects in a particular storage medium.
Definition: ExternalStoreMedium.php:31
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:189
ExternalStoreDB\getSlave
getSlave( $cluster)
Get a replica DB connection for the specified cluster.
Definition: ExternalStoreDB.php:123
ExternalStoreDB\parseURL
parseURL( $url)
Definition: ExternalStoreDB.php:293
ExternalStoreDB
DB accessable external objects.
Definition: ExternalStoreDB.php:36
ExternalStoreDB\store
store( $location, $data)
Insert a data item into a given location.
Definition: ExternalStoreDB.php:93
captcha-old.count
count
Definition: captcha-old.py:225
ExternalStoreDB\batchFetchFromURLs
batchFetchFromURLs(array $urls)
Fetch data from given external store URLs.
Definition: ExternalStoreDB.php:65
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
unserialize
unserialize( $serialized)
Definition: ApiMessage.php:185
$res
$res
Definition: database.txt:21
$wgDefaultExternalStore
array $wgDefaultExternalStore
The place to put new revisions, false to put them in the local text table.
Definition: DefaultSettings.php:2124
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:182
wfDebugLog
wfDebugLog( $logGroup, $text, $dest='all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not.
Definition: GlobalFunctions.php:1092
DBO_TRX
const DBO_TRX
Definition: defines.php:12
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:40
Wikimedia\Rdbms\MaintainableDBConnRef
Helper class to handle automatically marking connections as reusable (via RAII pattern) as well handl...
Definition: MaintainableDBConnRef.php:13
ExternalStoreDB\getLoadBalancer
getLoadBalancer( $cluster)
Get a LoadBalancer for the specified cluster.
Definition: ExternalStoreDB.php:113
MWException
MediaWiki exception.
Definition: MWException.php:26
$blob
$blob
Definition: testCompression.php:63
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
DB_MASTER
const DB_MASTER
Definition: defines.php:26
wfDebug
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:999
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
ExternalStoreDB\getTable
getTable( $db)
Get the 'blobs' table name for this database.
Definition: ExternalStoreDB.php:164
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:45
Wikimedia\Rdbms\LoadBalancer
Database connection, tracking, load balancing, and transaction manager for a cluster.
Definition: LoadBalancer.php:41
$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:1956
wfGetLBFactory
wfGetLBFactory()
Get the load balancer factory object.
Definition: GlobalFunctions.php:3089
Wikimedia\Rdbms\DBConnRef
Helper class to handle automatically marking connections as reusable (via RAII pattern) as well handl...
Definition: DBConnRef.php:15
$dbr
if(! $regexes) $dbr
Definition: cleanup.php:94
$path
$path
Definition: NoLocalSettings.php:26
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
ExternalStoreDB\mergeBatchResult
mergeBatchResult(array &$ret, array &$ids, $res)
Helper function for self::batchFetchBlobs for merging master/replica DB results.
Definition: ExternalStoreDB.php:274
ExternalStoreDB\getMaster
getMaster( $cluster)
Get a master database connection for the specified cluster.
Definition: ExternalStoreDB.php:148
array
the array() calling protocol came about after MediaWiki 1.4rc1.