9use InvalidArgumentException;
18use Psr\Log\LoggerInterface;
20use Wikimedia\Assert\Assert;
45 private array $users = [];
50 private $pageIdentities =
null;
65 private $titleFormatter;
70 private $contentLanguage;
83 private $linksMigration;
113 LoggerInterface $logger
115 $this->linkCache = $linkCache;
116 $this->titleFormatter = $titleFormatter;
117 $this->contentLanguage = $contentLanguage;
118 $this->genderCache = $genderCache;
119 $this->dbProvider = $dbProvider;
120 $this->linksMigration = $linksMigration;
121 $this->tempUserDetailsLookup = $tempUserDetailsLookup;
122 $this->logger = $logger;
124 foreach ( $arr as $item ) {
151 $this->users[$user->getName()] = $user;
164 $this->logger->warning(
165 'Skipping null link, probably due to a bad title.',
166 [
'exception' =>
new RuntimeException() ]
171 $this->logger->warning(
172 'Skipping interwiki link',
173 [
'exception' =>
new RuntimeException() ]
178 Assert::parameterType( [ LinkTarget::class, PageReference::class ], $link,
'$link' );
179 $this->add( $link->getNamespace(), $link->getDBkey() );
186 public function add( $ns, $dbkey ) {
187 if ( $ns < 0 || $dbkey ===
'' ) {
191 $this->data[$ns][strtr( $dbkey,
' ',
'_' )] = 1;
202 $this->data = $array;
211 return $this->getSize() == 0;
220 return count( $this->data );
229 return $this->executeInto( $this->linkCache );
240 if ( $this->pageIdentities === null ) {
244 return $this->pageIdentities;
254 $res = $this->doQuery();
255 $this->doGenderQuery();
259 if ( count( $this->users ) > 0 ) {
260 $this->tempUserDetailsLookup->preloadExpirationStatus( $this->users );
263 return $this->addResultToCache( $cache, $res );
282 $this->pageIdentities ??= [];
285 $remaining = $this->data;
288 foreach ( $res as $row ) {
290 $title =
new TitleValue( (
int)$row->page_namespace, $row->page_title );
292 $cache->addGoodLinkObjFromRow( $title, $row );
293 $pdbk = $this->titleFormatter->getPrefixedDBkey( $title );
294 $ids[$pdbk] = $row->page_id;
296 $pageIdentity = PageIdentityValue::localIdentity(
298 (
int)$row->page_namespace,
302 $key = CacheKeyHelper::getKeyForPage( $pageIdentity );
303 $this->pageIdentities[$key] = $pageIdentity;
304 }
catch ( InvalidArgumentException ) {
305 $this->logger->warning(
306 'Encountered invalid title',
307 [
'title_namespace' => $row->page_namespace,
'title_dbkey' => $row->page_title ]
311 unset( $remaining[$row->page_namespace][$row->page_title] );
315 foreach ( $remaining as $ns => $dbkeys ) {
316 foreach ( $dbkeys as $dbkey => $unused ) {
318 $title =
new TitleValue( (
int)$ns, (
string)$dbkey );
320 $cache->addBadLinkObj( $title );
321 $pdbk = $this->titleFormatter->getPrefixedDBkey( $title );
324 $pageIdentity = PageIdentityValue::localIdentity( 0, (
int)$ns, $dbkey );
325 $key = CacheKeyHelper::getKeyForPage( $pageIdentity );
326 $this->pageIdentities[$key] = $pageIdentity;
327 }
catch ( InvalidArgumentException ) {
328 $this->logger->warning(
329 'Encountered invalid title',
330 [
'title_namespace' => $ns,
'title_dbkey' => $dbkey ]
345 if ( $this->isEmpty() ) {
349 $caller = __METHOD__;
350 if ( strval( $this->caller ) !==
'' ) {
351 $caller .=
" (for {$this->caller})";
355 $dbr = $this->dbProvider->getReplicaDatabase();
356 return $dbr->newSelectQueryBuilder()
357 ->select( LinkCache::getSelectFields() )
359 ->where( $this->constructSet(
'page', $dbr ) )
370 if ( $this->isEmpty() || !$this->contentLanguage->needsGenderDistinction() ) {
374 $this->genderCache->doLinkBatch( $this->data, $this->caller );
391 if ( isset( $this->linksMigration::$prefixToTableMapping[$prefix] ) ) {
392 [ $blNamespace, $blTitle ] = $this->linksMigration->getTitleFields(
393 $this->linksMigration::$prefixToTableMapping[$prefix]
396 $blNamespace =
"{$prefix}_namespace";
397 $blTitle =
"{$prefix}_title";
399 return $db->makeWhereFrom2d( $this->data, $blNamespace, $blTitle );
404class_alias( LinkBatch::class,
'LinkBatch' );
407class_alias( LinkBatch::class,
'MediaWiki\Cache\LinkBatch' );
if(!defined('MW_SETUP_CALLBACK'))
Batch query for page metadata and feed to LinkCache.
getPageIdentities()
Do the query, add the results to the LinkCache object, and return ProperPageIdentity instances corres...
addResultToCache( $cache, $res)
Add a database result with page rows to the LinkCache.
setCaller( $caller)
Set the function name to attribute database queries to, in debug logs.
doQuery()
Perform the existence test query.
getSize()
Return the size of the batch.
array< int, array< string, mixed > > $data
2-d array, first index namespace, second index dbkey, value arbitrary
addUser(UserIdentity $user)
Add user page and user talk page for a given user to this batch.
__construct(iterable $arr, LinkCache $linkCache, TitleFormatter $titleFormatter, Language $contentLanguage, GenderCache $genderCache, IConnectionProvider $dbProvider, LinksMigration $linksMigration, TempUserDetailsLookup $tempUserDetailsLookup, LoggerInterface $logger)
setArray( $array)
Replace the link batch with a given 2-d array.
constructSet( $prefix, $db)
Construct a WHERE clause which will match all the given titles.
isEmpty()
Whether no pages have been added.
string null $caller
For debugging which method is using this class.
execute()
Do the query and add the results to the LinkCache.
executeInto( $cache)
Do the query and add the results to a given LinkCache object.
doGenderQuery()
Execute and cache {{GENDER:...}} information for user pages in this LinkBatch.
Page existence and metadata cache.
Interface for a page that is (or could be, or used to be) an editable wiki page.