MediaWiki REL1_35
LinkBatch.php
Go to the documentation of this file.
1<?php
28
35class LinkBatch {
39 public $data = [];
40
44 protected $caller;
45
49 private $linkCache;
50
55
60
64 private $genderCache;
65
70
80 public function __construct(
81 iterable $arr = [],
82 ?LinkCache $linkCache = null,
83 ?TitleFormatter $titleFormatter = null,
84 ?Language $contentLanguage = null,
85 ?GenderCache $genderCache = null,
86 ?ILoadBalancer $loadBalancer = 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" );
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: ' .
220 $row->page_namespace . '-' . $row->page_title );
221 }
222
223 unset( $remaining[$row->page_namespace][$row->page_title] );
224 }
225
226 // The remaining links in $data are bad links, register them as such
227 foreach ( $remaining as $ns => $dbkeys ) {
228 foreach ( $dbkeys as $dbkey => $unused ) {
229 $title = TitleValue::tryNew( (int)$ns, (string)$dbkey );
230 if ( $title ) {
231 $cache->addBadLinkObj( $title );
232 $pdbk = $this->titleFormatter->getPrefixedDBkey( $title );
233 $ids[$pdbk] = 0;
234 } else {
235 wfLogWarning( __METHOD__ . ': encountered invalid title: ' . $ns . '-' . $dbkey );
236 }
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(
256 LinkCache::getSelectFields(),
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}
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfLogWarning( $msg, $callerOffset=1, $level=E_USER_WARNING)
Send a warning as a PHP error and the debug log.
Caches user genders when needed to use correct namespace aliases.
Internationalisation code See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more...
Definition Language.php:41
Class representing a list of titles The execute() method checks them all for existence and adds them ...
Definition LinkBatch.php:35
Language null $contentLanguage
Definition LinkBatch.php:59
__construct(iterable $arr=[], ?LinkCache $linkCache=null, ?TitleFormatter $titleFormatter=null, ?Language $contentLanguage=null, ?GenderCache $genderCache=null, ?ILoadBalancer $loadBalancer=null)
Definition LinkBatch.php:80
ILoadBalancer null $loadBalancer
Definition LinkBatch.php:69
doGenderQuery()
Do (and cache) {{GENDER:...}} information for userpages in this LinkBatch.
TitleFormatter null $titleFormatter
Definition LinkBatch.php:54
add( $ns, $dbkey)
$data
2-d array, first index namespace, second index dbkey, value arbitrary
Definition LinkBatch.php:39
getSize()
Returns the size of the batch.
addResultToCache( $cache, $res)
Add a result wrapper containing IDs and titles to a LinkCache object.
$caller
For debugging which method is using this class.
Definition LinkBatch.php:44
isEmpty()
Returns true if no pages have been added, false otherwise.
setCaller( $caller)
Use ->setCaller( METHOD ) to indicate which code is using this class.
constructSet( $prefix, $db)
Construct a WHERE clause which will match all the given titles.
execute()
Do the query and add the results to the LinkCache object.
LinkCache null $linkCache
Definition LinkBatch.php:49
setArray( $array)
Set the link list to a given 2-d array First key is the namespace, second is the DB key,...
GenderCache null $genderCache
Definition LinkBatch.php:64
addObj( $linkTarget)
executeInto( $cache)
Do the query and add the results to a given LinkCache object Return an array mapping PDBK to ID.
doQuery()
Perform the existence test query, return a result wrapper with page_id fields.
Cache for article titles (prefixed DB keys) and ids linked from one source.
Definition LinkCache.php:34
MediaWikiServices is the service locator for the application scope of MediaWiki.
static tryNew( $namespace, $title, $fragment='', $interwiki='')
Constructs a TitleValue, or returns null if the parameters are not valid.
A title formatter service for MediaWiki.
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:38
Database cluster connection, tracking, load balancing, and transaction manager interface.
Result wrapper for grabbing data queried from an IDatabase object.
$cache
Definition mcc.php:33
const DB_REPLICA
Definition defines.php:25