9use InvalidArgumentException;
18use Psr\Log\LoggerInterface;
20use Wikimedia\Assert\Assert;
45 private array $users = [];
50 private $pageIdentities =
null;
73 private LoggerInterface $logger
75 foreach ( $arr as $item ) {
102 $this->users[$user->getName()] = $user;
115 $this->logger->warning(
116 'Skipping null link, probably due to a bad title.',
117 [
'exception' =>
new RuntimeException() ]
122 $this->logger->warning(
123 'Skipping interwiki link',
124 [
'exception' =>
new RuntimeException() ]
129 Assert::parameterType( [ LinkTarget::class, PageReference::class ], $link,
'$link' );
130 $this->add( $link->getNamespace(), $link->getDBkey() );
137 public function add( $ns, $dbkey ) {
138 if ( $ns < 0 || $dbkey ===
'' ) {
142 $this->data[$ns][strtr( $dbkey,
' ',
'_' )] = 1;
153 $this->data = $array;
162 return $this->getSize() == 0;
171 return count( $this->data );
180 return $this->executeInto( $this->linkCache );
191 if ( $this->pageIdentities === null ) {
195 return $this->pageIdentities;
205 $res = $this->doQuery();
206 $this->doGenderQuery();
207 $this->doIsAlwaysKnownQuery();
211 if ( count( $this->users ) > 0 ) {
212 $this->tempUserDetailsLookup->preloadExpirationStatus( $this->users );
215 return $this->addResultToCache( $cache, $res );
234 $this->pageIdentities ??= [];
237 $remaining = $this->data;
240 foreach ( $res as $row ) {
242 $title =
new TitleValue( (
int)$row->page_namespace, $row->page_title );
244 $cache->addGoodLinkObjFromRow( $title, $row );
245 $pdbk = $this->titleFormatter->getPrefixedDBkey( $title );
246 $ids[$pdbk] = $row->page_id;
248 $pageIdentity = PageIdentityValue::localIdentity(
250 (
int)$row->page_namespace,
254 $key = CacheKeyHelper::getKeyForPage( $pageIdentity );
255 $this->pageIdentities[$key] = $pageIdentity;
256 }
catch ( InvalidArgumentException ) {
257 $this->logger->warning(
258 'Encountered invalid title',
259 [
'title_namespace' => $row->page_namespace,
'title_dbkey' => $row->page_title ]
263 unset( $remaining[$row->page_namespace][$row->page_title] );
267 foreach ( $remaining as $ns => $dbkeys ) {
268 foreach ( $dbkeys as $dbkey => $unused ) {
270 $title =
new TitleValue( (
int)$ns, (
string)$dbkey );
272 $cache->addBadLinkObj( $title );
273 $pdbk = $this->titleFormatter->getPrefixedDBkey( $title );
276 $pageIdentity = PageIdentityValue::localIdentity( 0, (
int)$ns, $dbkey );
277 $key = CacheKeyHelper::getKeyForPage( $pageIdentity );
278 $this->pageIdentities[$key] = $pageIdentity;
279 }
catch ( InvalidArgumentException ) {
280 $this->logger->warning(
281 'Encountered invalid title',
282 [
'title_namespace' => $ns,
'title_dbkey' => $dbkey ]
297 if ( $this->isEmpty() ) {
301 $caller = __METHOD__;
302 if ( strval( $this->caller ) !==
'' ) {
303 $caller .=
" (for {$this->caller})";
307 $dbr = $this->dbProvider->getReplicaDatabase();
308 return $dbr->newSelectQueryBuilder()
309 ->select( LinkCache::getSelectFields() )
311 ->where( $this->constructSet(
'page', $dbr ) )
322 if ( $this->isEmpty() || !$this->contentLanguage->needsGenderDistinction() ) {
326 $this->genderCache->doLinkBatch( $this->data, $this->caller );
332 if ( $this->isEmpty() ) {
337 foreach ( $this->data as $ns => $dbkeys ) {
338 foreach ( $dbkeys as $dbkey => $_ ) {
340 $links[] =
new TitleValue( (
int)$ns, (
string)$dbkey );
341 }
catch ( InvalidArgumentException ) {
342 $this->logger->warning(
343 'Encountered invalid title',
345 'title_namespace' => $ns,
346 'title_dbkey' => $dbkey,
347 'exception' =>
new RuntimeException
354 $this->linkAlwaysKnownLookup->preload( $links );
369 if ( isset( $this->linksMigration::$prefixToTableMapping[$prefix] ) ) {
370 [ $blNamespace, $blTitle ] = $this->linksMigration->getTitleFields(
371 $this->linksMigration::$prefixToTableMapping[$prefix]
374 $blNamespace =
"{$prefix}_namespace";
375 $blTitle =
"{$prefix}_title";
377 return $db->makeWhereFrom2d( $this->data, $blNamespace, $blTitle );
382class_alias( LinkBatch::class,
'LinkBatch' );
385class_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.
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.
__construct(iterable $arr, private LinkCache $linkCache, private TitleFormatter $titleFormatter, private Language $contentLanguage, private GenderCache $genderCache, private IConnectionProvider $dbProvider, private LinksMigration $linksMigration, private TempUserDetailsLookup $tempUserDetailsLookup, private LinkAlwaysKnownLookup $linkAlwaysKnownLookup, private LoggerInterface $logger)
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.