32use Psr\Log\LoggerInterface;
33use Wikimedia\Assert\Assert;
53 private $pageIdentities =
null;
68 private $titleFormatter;
73 private $contentLanguage;
83 private $loadBalancer;
86 private $linksMigration;
110 ?LoggerInterface $logger =
null
112 $getServices =
static function () {
114 return MediaWikiServices::getInstance();
117 $this->linkCache = $linkCache ?? $getServices()->getLinkCache();
118 $this->titleFormatter = $titleFormatter ?? $getServices()->getTitleFormatter();
119 $this->contentLanguage = $contentLanguage ?? $getServices()->getContentLanguage();
120 $this->genderCache = $genderCache ?? $getServices()->getGenderCache();
121 $this->loadBalancer = $loadBalancer ?? $getServices()->getDBLoadBalancer();
122 $this->linksMigration = $linksMigration ?? $getServices()->getLinksMigration();
123 $this->logger = $logger ?? LoggerFactory::getInstance(
'LinkBatch' );
125 foreach ( $arr as $item ) {
151 $this->logger->warning(
152 'Skipping null link, probably due to a bad title.',
153 [
'exception' =>
new RuntimeException() ]
158 $this->logger->warning(
159 'Skipping interwiki link',
160 [
'exception' =>
new RuntimeException() ]
165 Assert::parameterType( [ LinkTarget::class, PageReference::class ], $link,
'$link' );
166 $this->
add( $link->getNamespace(), $link->getDBkey() );
173 public function add( $ns, $dbkey ) {
174 if ( $ns < 0 || $dbkey ===
'' ) {
178 if ( !array_key_exists( $ns, $this->data ) ) {
179 $this->data[$ns] = [];
182 $this->data[$ns][strtr( $dbkey,
' ',
'_' )] = 1;
192 $this->data = $array;
210 return count( $this->data );
230 if ( $this->pageIdentities === null ) {
234 return $this->pageIdentities;
245 $res = $this->doQuery();
246 $this->doGenderQuery();
247 return $this->addResultToCache(
$cache,
$res );
267 if ( $this->pageIdentities ===
null ) {
268 $this->pageIdentities = [];
272 $remaining = $this->data;
273 foreach (
$res as $row ) {
278 $pdbk = $this->titleFormatter->getPrefixedDBkey(
$title );
279 $ids[$pdbk] = $row->page_id;
283 (
int)$row->page_namespace,
285 ProperPageIdentity::LOCAL
288 $key = CacheKeyHelper::getKeyForPage( $pageIdentity );
289 $this->pageIdentities[$key] = $pageIdentity;
290 }
catch ( InvalidArgumentException $ex ) {
291 $this->logger->warning(
292 'Encountered invalid title',
293 [
'title_namespace' => $row->page_namespace,
'title_dbkey' => $row->page_title ]
297 unset( $remaining[$row->page_namespace][$row->page_title] );
301 foreach ( $remaining as $ns => $dbkeys ) {
302 foreach ( $dbkeys as $dbkey => $unused ) {
307 $pdbk = $this->titleFormatter->getPrefixedDBkey(
$title );
310 $pageIdentity =
new PageIdentityValue( 0, (
int)$ns, $dbkey, ProperPageIdentity::LOCAL );
311 $key = CacheKeyHelper::getKeyForPage( $pageIdentity );
312 $this->pageIdentities[$key] = $pageIdentity;
313 }
catch ( InvalidArgumentException $ex ) {
314 $this->logger->warning(
315 'Encountered invalid title',
316 [
'title_namespace' => $ns,
'title_dbkey' => $dbkey ]
330 if ( $this->isEmpty() ) {
337 $fields = LinkCache::getSelectFields();
339 $conds = $this->constructSet(
'page',
$dbr );
342 $caller = __METHOD__;
343 if ( strval( $this->caller ) !==
'' ) {
344 $caller .=
" (for {$this->caller})";
347 return $dbr->select( $table, $fields, $conds, $caller );
356 if ( $this->isEmpty() ) {
360 if ( !$this->contentLanguage->needsGenderDistinction() ) {
364 $this->genderCache->doLinkBatch( $this->data, $this->caller );
377 if ( isset( $this->linksMigration::$prefixToTableMapping[$prefix] ) ) {
378 list( $blNamespace, $blTitle ) = $this->linksMigration->getTitleFields(
379 $this->linksMigration::$prefixToTableMapping[$prefix]
382 $blNamespace =
"{$prefix}_namespace";
383 $blTitle =
"{$prefix}_title";
385 return $db->makeWhereFrom2d( $this->data, $blNamespace, $blTitle );
if(!defined('MW_SETUP_CALLBACK'))
The persistent session ID (if any) loaded at startup.
Caches user genders when needed to use correct namespace aliases.
Base class for language-specific code.
Class representing a list of titles The execute() method checks them all for existence and adds them ...
doGenderQuery()
Do (and cache) {{GENDER:...}} information for userpages in this LinkBatch.
getSize()
Returns the size of the batch.
__construct(iterable $arr=[], ?LinkCache $linkCache=null, ?TitleFormatter $titleFormatter=null, ?Language $contentLanguage=null, ?GenderCache $genderCache=null, ?ILoadBalancer $loadBalancer=null, ?LinksMigration $linksMigration=null, ?LoggerInterface $logger=null)
addResultToCache( $cache, $res)
Add a result wrapper containing IDs and titles to a LinkCache object.
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.
getPageIdentities()
Do the query, add the results to the LinkCache object, and return ProperPageIdentity instances corres...
execute()
Do the query and add the results to the LinkCache object.
setArray( $array)
Set the link list to a given 2-d array First key is the namespace, second is the DB key,...
string null $caller
For debugging which method is using this class.
array[] $data
2-d array, first index namespace, second index dbkey, value arbitrary
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.
Immutable value object representing a page identity.
Represents a page (or page fragment) title within MediaWiki.
Interface for a page that is (or could be, or used to be) an editable wiki page.