3 * Batch query to determine page existence.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
24 namespace MediaWiki\Cache
;
26 use InvalidArgumentException
;
27 use MediaWiki\Language\Language
;
28 use MediaWiki\Linker\LinksMigration
;
29 use MediaWiki\Linker\LinkTarget
;
30 use MediaWiki\Page\PageIdentityValue
;
31 use MediaWiki\Page\PageReference
;
32 use MediaWiki\Page\ProperPageIdentity
;
33 use MediaWiki\Title\TitleFormatter
;
34 use MediaWiki\Title\TitleValue
;
35 use Psr\Log\LoggerInterface
;
37 use Wikimedia\Assert\Assert
;
38 use Wikimedia\Rdbms\IConnectionProvider
;
39 use Wikimedia\Rdbms\IResultWrapper
;
40 use Wikimedia\Rdbms\Platform\ISQLPlatform
;
43 * Class representing a list of titles
44 * The execute() method checks them all for existence and adds them to a LinkCache object
50 * @var array<int,array<string,mixed>> 2-d array, first index namespace, second index dbkey, value arbitrary
55 * @var ProperPageIdentity[]|null page identity objects corresponding to the links in the batch
57 private $pageIdentities = null;
60 * @var string|null For debugging which method is using this class.
72 private $titleFormatter;
77 private $contentLanguage;
85 * @var IConnectionProvider
89 /** @var LinksMigration */
90 private $linksMigration;
92 /** @var LoggerInterface */
96 * @see \MediaWiki\Cache\LinkBatchFactory
99 * @param iterable<LinkTarget>|iterable<PageReference> $arr Initial items to be added to the batch
100 * @param LinkCache $linkCache
101 * @param TitleFormatter $titleFormatter
102 * @param Language $contentLanguage
103 * @param GenderCache $genderCache
104 * @param IConnectionProvider $dbProvider
105 * @param LinksMigration $linksMigration
106 * @param LoggerInterface $logger
108 public function __construct(
110 LinkCache
$linkCache,
111 TitleFormatter
$titleFormatter,
112 Language
$contentLanguage,
113 GenderCache
$genderCache,
114 IConnectionProvider
$dbProvider,
115 LinksMigration
$linksMigration,
116 LoggerInterface
$logger
118 $this->linkCache
= $linkCache;
119 $this->titleFormatter
= $titleFormatter;
120 $this->contentLanguage
= $contentLanguage;
121 $this->genderCache
= $genderCache;
122 $this->dbProvider
= $dbProvider;
123 $this->linksMigration
= $linksMigration;
124 $this->logger
= $logger;
126 foreach ( $arr as $item ) {
127 $this->addObj( $item );
132 * Use ->setCaller( __METHOD__ ) to indicate which code is using this
133 * class. Only used in debugging output.
136 * @param string $caller
137 * @return self (since 1.32)
139 public function setCaller( $caller ) {
140 $this->caller
= $caller;
146 * @param LinkTarget|PageReference $link
148 public function addObj( $link ) {
150 // Don't die if we got null, just skip. There is nothing to do anyway.
151 // For now, let's avoid things like T282180. We should be more strict in the future.
152 $this->logger
->warning(
153 'Skipping null link, probably due to a bad title.',
154 [ 'exception' => new RuntimeException() ]
158 if ( $link instanceof LinkTarget
&& $link->isExternal() ) {
159 $this->logger
->warning(
160 'Skipping interwiki link',
161 [ 'exception' => new RuntimeException() ]
166 Assert
::parameterType( [ LinkTarget
::class, PageReference
::class ], $link, '$link' );
167 $this->add( $link->getNamespace(), $link->getDBkey() );
172 * @param string $dbkey
174 public function add( $ns, $dbkey ) {
175 if ( $ns < 0 ||
$dbkey === '' ) {
179 $this->data
[$ns][strtr( $dbkey, ' ', '_' )] = 1;
183 * Set the link list to a given 2-d array
184 * First key is the namespace, second is the DB key, value arbitrary
186 * @param array<int,array<string,mixed>> $array
188 public function setArray( $array ) {
189 $this->data
= $array;
193 * Returns true if no pages have been added, false otherwise.
197 public function isEmpty() {
198 return $this->getSize() == 0;
202 * Returns the size of the batch.
206 public function getSize() {
207 return count( $this->data
);
211 * Do the query and add the results to the LinkCache object
213 * @return int[] Mapping PDBK to ID
215 public function execute() {
216 return $this->executeInto( $this->linkCache
);
220 * Do the query, add the results to the LinkCache object,
221 * and return ProperPageIdentity instances corresponding to the pages in the batch.
224 * @return ProperPageIdentity[] A list of ProperPageIdentities
226 public function getPageIdentities(): array {
227 if ( $this->pageIdentities
=== null ) {
231 return $this->pageIdentities
;
235 * Do the query and add the results to a given LinkCache object
236 * Return an array mapping PDBK to ID
238 * @param LinkCache $cache
239 * @return int[] Remaining IDs
241 protected function executeInto( $cache ) {
242 $res = $this->doQuery();
243 $this->doGenderQuery();
244 return $this->addResultToCache( $cache, $res );
248 * Add a result wrapper containing IDs and titles to a LinkCache object.
249 * As normal, titles will go into the static Title cache field.
250 * This function *also* stores extra fields of the title used for link
251 * parsing to avoid extra DB queries.
253 * @param LinkCache $cache
254 * @param IResultWrapper $res
255 * @return int[] Array of remaining titles
257 public function addResultToCache( $cache, $res ) {
262 // For each returned entry, add it to the list of good links, and remove it from $remaining
264 $this->pageIdentities ??
= [];
267 $remaining = $this->data
;
268 foreach ( $res as $row ) {
270 $title = new TitleValue( (int)$row->page_namespace
, $row->page_title
);
272 $cache->addGoodLinkObjFromRow( $title, $row );
273 $pdbk = $this->titleFormatter
->getPrefixedDBkey( $title );
274 $ids[$pdbk] = $row->page_id
;
276 $pageIdentity = new PageIdentityValue(
278 (int)$row->page_namespace
,
280 ProperPageIdentity
::LOCAL
283 $key = CacheKeyHelper
::getKeyForPage( $pageIdentity );
284 $this->pageIdentities
[$key] = $pageIdentity;
285 } catch ( InvalidArgumentException
$ex ) {
286 $this->logger
->warning(
287 'Encountered invalid title',
288 [ 'title_namespace' => $row->page_namespace
, 'title_dbkey' => $row->page_title
]
292 unset( $remaining[$row->page_namespace
][$row->page_title
] );
295 // The remaining links in $data are bad links, register them as such
296 foreach ( $remaining as $ns => $dbkeys ) {
297 foreach ( $dbkeys as $dbkey => $unused ) {
299 $title = new TitleValue( (int)$ns, (string)$dbkey );
301 $cache->addBadLinkObj( $title );
302 $pdbk = $this->titleFormatter
->getPrefixedDBkey( $title );
305 $pageIdentity = new PageIdentityValue( 0, (int)$ns, $dbkey, ProperPageIdentity
::LOCAL
);
306 $key = CacheKeyHelper
::getKeyForPage( $pageIdentity );
307 $this->pageIdentities
[$key] = $pageIdentity;
308 } catch ( InvalidArgumentException
$ex ) {
309 $this->logger
->warning(
310 'Encountered invalid title',
311 [ 'title_namespace' => $ns, 'title_dbkey' => $dbkey ]
321 * Perform the existence test query, return a result wrapper with page_id fields
322 * @return IResultWrapper|false
324 public function doQuery() {
325 if ( $this->isEmpty() ) {
329 // This is similar to LinkHolderArray::replaceInternal
330 $dbr = $this->dbProvider
->getReplicaDatabase();
331 $queryBuilder = $dbr->newSelectQueryBuilder()
332 ->select( LinkCache
::getSelectFields() )
334 ->where( $this->constructSet( 'page', $dbr ) );
336 $caller = __METHOD__
;
337 if ( strval( $this->caller
) !== '' ) {
338 $caller .= " (for {$this->caller})";
341 return $queryBuilder->caller( $caller )->fetchResultSet();
345 * Do (and cache) {{GENDER:...}} information for userpages in this LinkBatch
347 * @return bool Whether the query was successful
349 public function doGenderQuery() {
350 if ( $this->isEmpty() ||
!$this->contentLanguage
->needsGenderDistinction() ) {
354 $this->genderCache
->doLinkBatch( $this->data
, $this->caller
);
360 * Construct a WHERE clause which will match all the given titles.
362 * It is the caller's responsibility to only call this if the LinkBatch is
363 * not empty, because there is no safe way to represent a SQL conditional
366 * @param string $prefix The appropriate table's field name prefix ('page', 'pl', etc)
367 * @param ISQLPlatform $db DB object to use
368 * @return string String with SQL where clause fragment
370 public function constructSet( $prefix, $db ) {
371 if ( isset( $this->linksMigration
::$prefixToTableMapping[$prefix] ) ) {
372 [ $blNamespace, $blTitle ] = $this->linksMigration
->getTitleFields(
373 $this->linksMigration
::$prefixToTableMapping[$prefix]
376 $blNamespace = "{$prefix}_namespace";
377 $blTitle = "{$prefix}_title";
379 return $db->makeWhereFrom2d( $this->data
, $blNamespace, $blTitle );
383 /** @deprecated class alias since 1.42 */
384 class_alias( LinkBatch
::class, 'LinkBatch' );