MediaWiki  master
LinkBatch.php
Go to the documentation of this file.
1 <?php
28 
35 class LinkBatch {
39  public $data = [];
40 
44  protected $caller;
45 
49  private $linkCache;
50 
54  private $titleFormatter;
55 
60 
64  private $genderCache;
65 
69  private $loadBalancer;
70 
80  public function __construct(
81  iterable $arr = [],
82  ?LinkCache $linkCache = null,
84  ?Language $contentLanguage = null,
85  ?GenderCache $genderCache = null,
87  ) {
88  $getServices = function () {
89  // BC hack. Use a closure so this can be unit-tested.
90  return MediaWikiServices::getInstance();
91  };
92 
93  $this->linkCache = $linkCache ?? $getServices()->getLinkCache();
94  $this->titleFormatter = $titleFormatter ?? $getServices()->getTitleFormatter();
95  $this->contentLanguage = $contentLanguage ?? $getServices()->getContentLanguage();
96  $this->genderCache = $genderCache ?? $getServices()->getGenderCache();
97  $this->loadBalancer = $loadBalancer ?? $getServices()->getDBLoadBalancer();
98 
99  foreach ( $arr as $item ) {
100  $this->addObj( $item );
101  }
102  }
103 
112  public function setCaller( $caller ) {
113  $this->caller = $caller;
114 
115  return $this;
116  }
117 
121  public function addObj( $linkTarget ) {
122  if ( is_object( $linkTarget ) ) {
123  $this->add( $linkTarget->getNamespace(), $linkTarget->getDBkey() );
124  } else {
125  wfDebug( "Warning: LinkBatch::addObj got invalid LinkTarget object" );
126  }
127  }
128 
133  public function add( $ns, $dbkey ) {
134  if ( $ns < 0 || $dbkey === '' ) {
135  return; // T137083
136  }
137  if ( !array_key_exists( $ns, $this->data ) ) {
138  $this->data[$ns] = [];
139  }
140 
141  $this->data[$ns][strtr( $dbkey, ' ', '_' )] = 1;
142  }
143 
150  public function setArray( $array ) {
151  $this->data = $array;
152  }
153 
159  public function isEmpty() {
160  return $this->getSize() == 0;
161  }
162 
168  public function getSize() {
169  return count( $this->data );
170  }
171 
177  public function execute() {
178  return $this->executeInto( $this->linkCache );
179  }
180 
188  protected function executeInto( $cache ) {
189  $res = $this->doQuery();
190  $this->doGenderQuery();
191  $ids = $this->addResultToCache( $cache, $res );
192 
193  return $ids;
194  }
195 
206  public function addResultToCache( $cache, $res ) {
207  if ( !$res ) {
208  return [];
209  }
210 
211  // For each returned entry, add it to the list of good links, and remove it from $remaining
212 
213  $ids = [];
214  $remaining = $this->data;
215  foreach ( $res as $row ) {
216  $title = TitleValue::tryNew( (int)$row->page_namespace, $row->page_title );
217  if ( $title ) {
218  $cache->addGoodLinkObjFromRow( $title, $row );
219  $pdbk = $this->titleFormatter->getPrefixedDBkey( $title );
220  $ids[$pdbk] = $row->page_id;
221  } else {
222  wfLogWarning( __METHOD__ . ': encountered invalid title: ' .
223  $row->page_namespace . '-' . $row->page_title );
224  }
225 
226  unset( $remaining[$row->page_namespace][$row->page_title] );
227  }
228 
229  // The remaining links in $data are bad links, register them as such
230  foreach ( $remaining as $ns => $dbkeys ) {
231  foreach ( $dbkeys as $dbkey => $unused ) {
232  $title = TitleValue::tryNew( (int)$ns, (string)$dbkey );
233  if ( $title ) {
234  $cache->addBadLinkObj( $title );
235  $pdbk = $this->titleFormatter->getPrefixedDBkey( $title );
236  $ids[$pdbk] = 0;
237  } else {
238  wfLogWarning( __METHOD__ . ': encountered invalid title: ' . $ns . '-' . $dbkey );
239  }
240  }
241  }
242 
243  return $ids;
244  }
245 
250  public function doQuery() {
251  if ( $this->isEmpty() ) {
252  return false;
253  }
254 
255  // This is similar to LinkHolderArray::replaceInternal
256  $dbr = $this->loadBalancer->getConnectionRef( DB_REPLICA );
257  $table = 'page';
258  $fields = array_merge(
260  [ 'page_namespace', 'page_title' ]
261  );
262 
263  $conds = $this->constructSet( 'page', $dbr );
264 
265  // Do query
266  $caller = __METHOD__;
267  if ( strval( $this->caller ) !== '' ) {
268  $caller .= " (for {$this->caller})";
269  }
270  $res = $dbr->select( $table, $fields, $conds, $caller );
271 
272  return $res;
273  }
274 
280  public function doGenderQuery() {
281  if ( $this->isEmpty() ) {
282  return false;
283  }
284 
285  if ( !$this->contentLanguage->needsGenderDistinction() ) {
286  return false;
287  }
288 
289  $this->genderCache->doLinkBatch( $this->data, $this->caller );
290 
291  return true;
292  }
293 
301  public function constructSet( $prefix, $db ) {
302  return $db->makeWhereFrom2d( $this->data, "{$prefix}_namespace", "{$prefix}_title" );
303  }
304 }
LinkCache
Cache for article titles (prefixed DB keys) and ids linked from one source.
Definition: LinkCache.php:35
LinkBatch\$loadBalancer
ILoadBalancer null $loadBalancer
Definition: LinkBatch.php:69
LinkBatch\doGenderQuery
doGenderQuery()
Do (and cache) {{GENDER:...}} information for userpages in this LinkBatch.
Definition: LinkBatch.php:280
LinkBatch
Class representing a list of titles The execute() method checks them all for existence and adds them ...
Definition: LinkBatch.php:35
LinkBatch\add
add( $ns, $dbkey)
Definition: LinkBatch.php:133
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:166
LinkBatch\__construct
__construct(iterable $arr=[], ?LinkCache $linkCache=null, ?TitleFormatter $titleFormatter=null, ?Language $contentLanguage=null, ?GenderCache $genderCache=null, ?ILoadBalancer $loadBalancer=null)
Definition: LinkBatch.php:80
LinkBatch\setCaller
setCaller( $caller)
Use ->setCaller( METHOD ) to indicate which code is using this class.
Definition: LinkBatch.php:112
GenderCache
Caches user genders when needed to use correct namespace aliases.
Definition: GenderCache.php:36
LinkBatch\$linkCache
LinkCache null $linkCache
Definition: LinkBatch.php:49
LinkBatch\$contentLanguage
Language null $contentLanguage
Definition: LinkBatch.php:59
wfLogWarning
wfLogWarning( $msg, $callerOffset=1, $level=E_USER_WARNING)
Send a warning as a PHP error and the debug log.
Definition: GlobalFunctions.php:1093
LinkBatch\addObj
addObj( $linkTarget)
Definition: LinkBatch.php:121
$res
$res
Definition: testCompression.php:57
LinkBatch\setArray
setArray( $array)
Set the link list to a given 2-d array First key is the namespace, second is the DB key,...
Definition: LinkBatch.php:150
LinkCache\getSelectFields
static getSelectFields()
Fields that LinkCache needs to select.
Definition: LinkCache.php:231
LinkBatch\$titleFormatter
TitleFormatter null $titleFormatter
Definition: LinkBatch.php:54
LinkBatch\executeInto
executeInto( $cache)
Do the query and add the results to a given LinkCache object Return an array mapping PDBK to ID.
Definition: LinkBatch.php:188
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
LinkBatch\execute
execute()
Do the query and add the results to the LinkCache object.
Definition: LinkBatch.php:177
$dbr
$dbr
Definition: testCompression.php:54
LinkBatch\getSize
getSize()
Returns the size of the batch.
Definition: LinkBatch.php:168
Wikimedia\Rdbms\IResultWrapper
Result wrapper for grabbing data queried from an IDatabase object.
Definition: IResultWrapper.php:24
LinkBatch\constructSet
constructSet( $prefix, $db)
Construct a WHERE clause which will match all the given titles.
Definition: LinkBatch.php:301
TitleValue\tryNew
static tryNew( $namespace, $title, $fragment='', $interwiki='')
Constructs a TitleValue, or returns null if the parameters are not valid.
Definition: TitleValue.php:94
$title
$title
Definition: testCompression.php:38
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
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:914
LinkBatch\$caller
$caller
For debugging which method is using this class.
Definition: LinkBatch.php:44
LinkBatch\$data
$data
2-d array, first index namespace, second index dbkey, value arbitrary
Definition: LinkBatch.php:39
LinkBatch\doQuery
doQuery()
Perform the existence test query, return a result wrapper with page_id fields.
Definition: LinkBatch.php:250
LinkBatch\$genderCache
GenderCache null $genderCache
Definition: LinkBatch.php:64
LinkBatch\isEmpty
isEmpty()
Returns true if no pages have been added, false otherwise.
Definition: LinkBatch.php:159
$cache
$cache
Definition: mcc.php:33
TitleFormatter
A title formatter service for MediaWiki.
Definition: TitleFormatter.php:34
MediaWiki\Linker\LinkTarget
Definition: LinkTarget.php:26
LinkBatch\addResultToCache
addResultToCache( $cache, $res)
Add a result wrapper containing IDs and titles to a LinkCache object.
Definition: LinkBatch.php:206
Language
Internationalisation code See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more...
Definition: Language.php:42
Wikimedia\Rdbms\ILoadBalancer
Database cluster connection, tracking, load balancing, and transaction manager interface.
Definition: ILoadBalancer.php:81