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  $services = MediaWikiServices::getInstance();
89 
90  $this->linkCache = $linkCache ?? $services->getLinkCache();
91  $this->titleFormatter = $titleFormatter ?? $services->getTitleFormatter();
92  $this->contentLanguage = $contentLanguage ?? $services->getContentLanguage();
93  $this->genderCache = $genderCache ?? $services->getGenderCache();
94  $this->loadBalancer = $loadBalancer ?? $services->getDBLoadBalancer();
95 
96  foreach ( $arr as $item ) {
97  $this->addObj( $item );
98  }
99  }
100 
109  public function setCaller( $caller ) {
110  $this->caller = $caller;
111 
112  return $this;
113  }
114 
118  public function addObj( $linkTarget ) {
119  if ( is_object( $linkTarget ) ) {
120  $this->add( $linkTarget->getNamespace(), $linkTarget->getDBkey() );
121  } else {
122  wfDebug( "Warning: LinkBatch::addObj got invalid LinkTarget object\n" );
123  }
124  }
125 
130  public function add( $ns, $dbkey ) {
131  if ( $ns < 0 || $dbkey === '' ) {
132  return; // T137083
133  }
134  if ( !array_key_exists( $ns, $this->data ) ) {
135  $this->data[$ns] = [];
136  }
137 
138  $this->data[$ns][strtr( $dbkey, ' ', '_' )] = 1;
139  }
140 
147  public function setArray( $array ) {
148  $this->data = $array;
149  }
150 
156  public function isEmpty() {
157  return $this->getSize() == 0;
158  }
159 
165  public function getSize() {
166  return count( $this->data );
167  }
168 
174  public function execute() {
175  return $this->executeInto( $this->linkCache );
176  }
177 
185  protected function executeInto( $cache ) {
186  $res = $this->doQuery();
187  $this->doGenderQuery();
188  $ids = $this->addResultToCache( $cache, $res );
189 
190  return $ids;
191  }
192 
203  public function addResultToCache( $cache, $res ) {
204  if ( !$res ) {
205  return [];
206  }
207 
208  // For each returned entry, add it to the list of good links, and remove it from $remaining
209 
210  $ids = [];
211  $remaining = $this->data;
212  foreach ( $res as $row ) {
213  $title = TitleValue::tryNew( (int)$row->page_namespace, $row->page_title );
214  if ( $title ) {
215  $cache->addGoodLinkObjFromRow( $title, $row );
216  $pdbk = $this->titleFormatter->getPrefixedDBkey( $title );
217  $ids[$pdbk] = $row->page_id;
218  } else {
219  wfLogWarning( __METHOD__ . ': encountered invalid title: ' . $row->page_title );
220  }
221 
222  unset( $remaining[$row->page_namespace][$row->page_title] );
223  }
224 
225  // The remaining links in $data are bad links, register them as such
226  foreach ( $remaining as $ns => $dbkeys ) {
227  foreach ( $dbkeys as $dbkey => $unused ) {
228  $title = TitleValue::tryNew( (int)$ns, (string)$dbkey );
229  if ( $title ) {
230  $cache->addBadLinkObj( $title );
231  $pdbk = $this->titleFormatter->getPrefixedDBkey( $title );
232  } else {
233  wfLogWarning( __METHOD__ . ': encountered invalid title: ' . $row->page_title );
234  }
235 
236  $ids[$pdbk] = 0;
237  }
238  }
239 
240  return $ids;
241  }
242 
247  public function doQuery() {
248  if ( $this->isEmpty() ) {
249  return false;
250  }
251 
252  // This is similar to LinkHolderArray::replaceInternal
253  $dbr = $this->loadBalancer->getConnectionRef( DB_REPLICA );
254  $table = 'page';
255  $fields = array_merge(
257  [ 'page_namespace', 'page_title' ]
258  );
259 
260  $conds = $this->constructSet( 'page', $dbr );
261 
262  // Do query
263  $caller = __METHOD__;
264  if ( strval( $this->caller ) !== '' ) {
265  $caller .= " (for {$this->caller})";
266  }
267  $res = $dbr->select( $table, $fields, $conds, $caller );
268 
269  return $res;
270  }
271 
277  public function doGenderQuery() {
278  if ( $this->isEmpty() ) {
279  return false;
280  }
281 
282  if ( !$this->contentLanguage->needsGenderDistinction() ) {
283  return false;
284  }
285 
286  $this->genderCache->doLinkBatch( $this->data, $this->caller );
287 
288  return true;
289  }
290 
298  public function constructSet( $prefix, $db ) {
299  return $db->makeWhereFrom2d( $this->data, "{$prefix}_namespace", "{$prefix}_title" );
300  }
301 }
LinkCache
Cache for article titles (prefixed DB keys) and ids linked from one source.
Definition: LinkCache.php:34
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:277
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:130
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:130
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:109
GenderCache
Caches user genders when needed to use correct namespace aliases.
Definition: GenderCache.php:34
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:1078
LinkBatch\addObj
addObj( $linkTarget)
Definition: LinkBatch.php:118
$res
$res
Definition: testCompression.php:54
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:147
LinkCache\getSelectFields
static getSelectFields()
Fields that LinkCache needs to select.
Definition: LinkCache.php:219
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:185
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:174
$dbr
$dbr
Definition: testCompression.php:52
LinkBatch\getSize
getSize()
Returns the size of the batch.
Definition: LinkBatch.php:165
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:298
TitleValue\tryNew
static tryNew( $namespace, $title, $fragment='', $interwiki='')
Constructs a TitleValue, or returns null if the parameters are not valid.
Definition: TitleValue.php:91
$title
$title
Definition: testCompression.php:36
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:913
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:247
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:156
$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:203
Language
Internationalisation code.
Definition: Language.php:39
Wikimedia\Rdbms\ILoadBalancer
Database cluster connection, tracking, load balancing, and transaction manager interface.
Definition: ILoadBalancer.php:81