MediaWiki  1.30.2
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  $dbw->insert( $this->getTable( $dbw ),
96  [ 'blob_text' => $data ],
97  __METHOD__ );
98  $id = $dbw->insertId();
99  if ( !$id ) {
100  throw new MWException( __METHOD__ . ': no insert ID' );
101  }
102 
103  return "DB://$location/$id";
104  }
105 
112  private function getLoadBalancer( $cluster ) {
113  return wfGetLBFactory()->getExternalLB( $cluster );
114  }
115 
122  public function getSlave( $cluster ) {
124 
125  $wiki = isset( $this->params['wiki'] ) ? $this->params['wiki'] : false;
126  $lb = $this->getLoadBalancer( $cluster );
127 
128  if ( !in_array( "DB://" . $cluster, (array)$wgDefaultExternalStore ) ) {
129  wfDebug( "read only external store\n" );
130  $lb->allowLagged( true );
131  } else {
132  wfDebug( "writable external store\n" );
133  }
134 
135  $db = $lb->getConnectionRef( DB_REPLICA, [], $wiki );
136  $db->clearFlag( DBO_TRX ); // sanity
137 
138  return $db;
139  }
140 
147  public function getMaster( $cluster ) {
148  $wiki = isset( $this->params['wiki'] ) ? $this->params['wiki'] : false;
149  $lb = $this->getLoadBalancer( $cluster );
150 
151  $db = $lb->getMaintenanceConnectionRef( DB_MASTER, [], $wiki );
152  $db->clearFlag( DBO_TRX ); // sanity
153 
154  return $db;
155  }
156 
163  public function getTable( $db ) {
164  $table = $db->getLBInfo( 'blobs table' );
165  if ( is_null( $table ) ) {
166  $table = 'blobs';
167  }
168 
169  return $table;
170  }
171 
181  private function fetchBlob( $cluster, $id, $itemID ) {
188  static $externalBlobCache = [];
189 
190  $cacheID = ( $itemID === false ) ? "$cluster/$id" : "$cluster/$id/";
191  if ( isset( $externalBlobCache[$cacheID] ) ) {
192  wfDebugLog( 'ExternalStoreDB-cache',
193  "ExternalStoreDB::fetchBlob cache hit on $cacheID" );
194 
195  return $externalBlobCache[$cacheID];
196  }
197 
198  wfDebugLog( 'ExternalStoreDB-cache',
199  "ExternalStoreDB::fetchBlob cache miss on $cacheID" );
200 
201  $dbr = $this->getSlave( $cluster );
202  $ret = $dbr->selectField( $this->getTable( $dbr ),
203  'blob_text', [ 'blob_id' => $id ], __METHOD__ );
204  if ( $ret === false ) {
205  wfDebugLog( 'ExternalStoreDB',
206  "ExternalStoreDB::fetchBlob master fallback on $cacheID" );
207  // Try the master
208  $dbw = $this->getMaster( $cluster );
209  $ret = $dbw->selectField( $this->getTable( $dbw ),
210  'blob_text', [ 'blob_id' => $id ], __METHOD__ );
211  if ( $ret === false ) {
212  wfDebugLog( 'ExternalStoreDB',
213  "ExternalStoreDB::fetchBlob master failed to find $cacheID" );
214  }
215  }
216  if ( $itemID !== false && $ret !== false ) {
217  // Unserialise object; caller extracts item
218  $ret = unserialize( $ret );
219  }
220 
221  $externalBlobCache = [ $cacheID => $ret ];
222 
223  return $ret;
224  }
225 
234  private function batchFetchBlobs( $cluster, array $ids ) {
235  $dbr = $this->getSlave( $cluster );
236  $res = $dbr->select( $this->getTable( $dbr ),
237  [ 'blob_id', 'blob_text' ], [ 'blob_id' => array_keys( $ids ) ], __METHOD__ );
238  $ret = [];
239  if ( $res !== false ) {
240  $this->mergeBatchResult( $ret, $ids, $res );
241  }
242  if ( $ids ) {
243  wfDebugLog( __CLASS__, __METHOD__ .
244  " master fallback on '$cluster' for: " .
245  implode( ',', array_keys( $ids ) ) );
246  // Try the master
247  $dbw = $this->getMaster( $cluster );
248  $res = $dbw->select( $this->getTable( $dbr ),
249  [ 'blob_id', 'blob_text' ],
250  [ 'blob_id' => array_keys( $ids ) ],
251  __METHOD__ );
252  if ( $res === false ) {
253  wfDebugLog( __CLASS__, __METHOD__ . " master failed on '$cluster'" );
254  } else {
255  $this->mergeBatchResult( $ret, $ids, $res );
256  }
257  }
258  if ( $ids ) {
259  wfDebugLog( __CLASS__, __METHOD__ .
260  " master on '$cluster' failed locating items: " .
261  implode( ',', array_keys( $ids ) ) );
262  }
263 
264  return $ret;
265  }
266 
273  private function mergeBatchResult( array &$ret, array &$ids, $res ) {
274  foreach ( $res as $row ) {
275  $id = $row->blob_id;
276  $itemIDs = $ids[$id];
277  unset( $ids[$id] ); // to track if everything is found
278  if ( count( $itemIDs ) === 1 && reset( $itemIDs ) === false ) {
279  // single result stored per blob
280  $ret[$id] = $row->blob_text;
281  } else {
282  // multi result stored per blob
283  $ret[$id] = unserialize( $row->blob_text );
284  }
285  }
286  }
287 
292  protected function parseURL( $url ) {
293  $path = explode( '/', $url );
294 
295  return [
296  $path[2], // cluster
297  $path[3], // id
298  isset( $path[4] ) ? $path[4] : false // itemID
299  ];
300  }
301 }
ExternalStoreDB\batchFetchBlobs
batchFetchBlobs( $cluster, array $ids)
Fetch multiple blob items out of the database.
Definition: ExternalStoreDB.php:234
ExternalStoreMedium
Accessable external objects in a particular storage medium.
Definition: ExternalStoreMedium.php:30
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187
ExternalStoreDB\getSlave
getSlave( $cluster)
Get a replica DB connection for the specified cluster.
Definition: ExternalStoreDB.php:122
ExternalStoreDB\parseURL
parseURL( $url)
Definition: ExternalStoreDB.php:292
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:249
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:2146
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:181
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:1140
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:112
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:1047
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:163
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:1965
wfGetLBFactory
wfGetLBFactory()
Get the load balancer factory object.
Definition: GlobalFunctions.php:2885
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:273
ExternalStoreDB\getMaster
getMaster( $cluster)
Get a master database connection for the specified cluster.
Definition: ExternalStoreDB.php:147
array
the array() calling protocol came about after MediaWiki 1.4rc1.