MediaWiki  master
LinkBatch.php
Go to the documentation of this file.
1 <?php
27 
34 class LinkBatch {
38  public $data = [];
39 
43  protected $caller;
44 
48  public function __construct( $arr = [] ) {
49  foreach ( $arr as $item ) {
50  $this->addObj( $item );
51  }
52  }
53 
62  public function setCaller( $caller ) {
63  $this->caller = $caller;
64 
65  return $this;
66  }
67 
71  public function addObj( $linkTarget ) {
72  if ( is_object( $linkTarget ) ) {
73  $this->add( $linkTarget->getNamespace(), $linkTarget->getDBkey() );
74  } else {
75  wfDebug( "Warning: LinkBatch::addObj got invalid LinkTarget object\n" );
76  }
77  }
78 
83  public function add( $ns, $dbkey ) {
84  if ( $ns < 0 || $dbkey === '' ) {
85  return; // T137083
86  }
87  if ( !array_key_exists( $ns, $this->data ) ) {
88  $this->data[$ns] = [];
89  }
90 
91  $this->data[$ns][strtr( $dbkey, ' ', '_' )] = 1;
92  }
93 
100  public function setArray( $array ) {
101  $this->data = $array;
102  }
103 
109  public function isEmpty() {
110  return $this->getSize() == 0;
111  }
112 
118  public function getSize() {
119  return count( $this->data );
120  }
121 
127  public function execute() {
128  $linkCache = MediaWikiServices::getInstance()->getLinkCache();
129 
130  return $this->executeInto( $linkCache );
131  }
132 
140  protected function executeInto( &$cache ) {
141  $res = $this->doQuery();
142  $this->doGenderQuery();
143  $ids = $this->addResultToCache( $cache, $res );
144 
145  return $ids;
146  }
147 
158  public function addResultToCache( $cache, $res ) {
159  if ( !$res ) {
160  return [];
161  }
162 
163  $titleFormatter = MediaWikiServices::getInstance()->getTitleFormatter();
164  // For each returned entry, add it to the list of good links, and remove it from $remaining
165 
166  $ids = [];
167  $remaining = $this->data;
168  foreach ( $res as $row ) {
169  $title = TitleValue::tryNew( (int)$row->page_namespace, $row->page_title );
170  if ( $title ) {
171  $cache->addGoodLinkObjFromRow( $title, $row );
172  $pdbk = $titleFormatter->getPrefixedDBkey( $title );
173  $ids[$pdbk] = $row->page_id;
174  } else {
175  wfLogWarning( __METHOD__ . ': encountered invalid title: ' . $row->page_title );
176  }
177 
178  unset( $remaining[$row->page_namespace][$row->page_title] );
179  }
180 
181  // The remaining links in $data are bad links, register them as such
182  foreach ( $remaining as $ns => $dbkeys ) {
183  foreach ( $dbkeys as $dbkey => $unused ) {
184  $title = TitleValue::tryNew( (int)$ns, (string)$dbkey );
185  if ( $title ) {
186  $cache->addBadLinkObj( $title );
187  $pdbk = $titleFormatter->getPrefixedDBkey( $title );
188  } else {
189  wfLogWarning( __METHOD__ . ': encountered invalid title: ' . $row->page_title );
190  }
191 
192  $ids[$pdbk] = 0;
193  }
194  }
195 
196  return $ids;
197  }
198 
203  public function doQuery() {
204  if ( $this->isEmpty() ) {
205  return false;
206  }
207 
208  // This is similar to LinkHolderArray::replaceInternal
209  $dbr = wfGetDB( DB_REPLICA );
210  $table = 'page';
211  $fields = array_merge(
213  [ 'page_namespace', 'page_title' ]
214  );
215 
216  $conds = $this->constructSet( 'page', $dbr );
217 
218  // Do query
219  $caller = __METHOD__;
220  if ( strval( $this->caller ) !== '' ) {
221  $caller .= " (for {$this->caller})";
222  }
223  $res = $dbr->select( $table, $fields, $conds, $caller );
224 
225  return $res;
226  }
227 
233  public function doGenderQuery() {
234  if ( $this->isEmpty() ) {
235  return false;
236  }
237  $services = MediaWikiServices::getInstance();
238 
239  if ( !$services->getContentLanguage()->needsGenderDistinction() ) {
240  return false;
241  }
242 
243  $genderCache = $services->getGenderCache();
244  $genderCache->doLinkBatch( $this->data, $this->caller );
245 
246  return true;
247  }
248 
256  public function constructSet( $prefix, $db ) {
257  return $db->makeWhereFrom2d( $this->data, "{$prefix}_namespace", "{$prefix}_title" );
258  }
259 }
setCaller( $caller)
Use ->setCaller( METHOD ) to indicate which code is using this class.
Definition: LinkBatch.php:62
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:100
add( $ns, $dbkey)
Definition: LinkBatch.php:83
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
addResultToCache( $cache, $res)
Add a result wrapper containing IDs and titles to a LinkCache object.
Definition: LinkBatch.php:158
doGenderQuery()
Do (and cache) {{GENDER:...}} information for userpages in this LinkBatch.
Definition: LinkBatch.php:233
wfLogWarning( $msg, $callerOffset=1, $level=E_USER_WARNING)
Send a warning as a PHP error and the debug log.
addObj( $linkTarget)
Definition: LinkBatch.php:71
Class representing a list of titles The execute() method checks them all for existence and adds them ...
Definition: LinkBatch.php:34
isEmpty()
Returns true if no pages have been added, false otherwise.
Definition: LinkBatch.php:109
__construct( $arr=[])
Definition: LinkBatch.php:48
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
$caller
For debugging which method is using this class.
Definition: LinkBatch.php:43
constructSet( $prefix, $db)
Construct a WHERE clause which will match all the given titles.
Definition: LinkBatch.php:256
doQuery()
Perform the existence test query, return a result wrapper with page_id fields.
Definition: LinkBatch.php:203
$cache
Definition: mcc.php:33
static getSelectFields()
Fields that LinkCache needs to select.
Definition: LinkCache.php:219
$data
2-d array, first index namespace, second index dbkey, value arbitrary
Definition: LinkBatch.php:38
execute()
Do the query and add the results to the LinkCache object.
Definition: LinkBatch.php:127
executeInto(&$cache)
Do the query and add the results to a given LinkCache object Return an array mapping PDBK to ID...
Definition: LinkBatch.php:140
const DB_REPLICA
Definition: defines.php:25
static tryNew( $namespace, $title, $fragment='', $interwiki='')
Constructs a TitleValue, or returns null if the parameters are not valid.
Definition: TitleValue.php:91
getSize()
Returns the size of the batch.
Definition: LinkBatch.php:118