Merge "Set namespaces for dtp"
[mediawiki.git] / includes / api / ApiQueryBacklinksprop.php
blob819a608964280d0a70fb3496be82cfdc1fadaec1
1 <?php
2 /**
3 * API module to handle links table back-queries
5 * Copyright © 2014 Wikimedia Foundation and contributors
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
22 * @file
23 * @since 1.24
26 namespace MediaWiki\Api;
28 use MediaWiki\Linker\LinksMigration;
29 use MediaWiki\MainConfigNames;
30 use MediaWiki\Title\Title;
31 use Wikimedia\ParamValidator\ParamValidator;
32 use Wikimedia\ParamValidator\TypeDef\IntegerDef;
34 /**
35 * This implements prop=redirects, prop=linkshere, prop=catmembers,
36 * prop=transcludedin, and prop=fileusage
38 * @ingroup API
39 * @since 1.24
41 class ApiQueryBacklinksprop extends ApiQueryGeneratorBase {
43 /** @var array Data for the various modules implemented by this class */
44 private static $settings = [
45 'redirects' => [
46 'code' => 'rd',
47 'prefix' => 'rd',
48 'linktable' => 'redirect',
49 'props' => [
50 'fragment',
52 'showredirects' => false,
53 'show' => [
54 'fragment',
55 '!fragment',
58 'linkshere' => [
59 'code' => 'lh',
60 'prefix' => 'pl',
61 'linktable' => 'pagelinks',
62 'indexes' => [ 'pl_namespace', 'pl_backlinks_namespace' ],
63 'from_namespace' => true,
64 'showredirects' => true,
66 'transcludedin' => [
67 'code' => 'ti',
68 'prefix' => 'tl',
69 'linktable' => 'templatelinks',
70 'from_namespace' => true,
71 'showredirects' => true,
73 'fileusage' => [
74 'code' => 'fu',
75 'prefix' => 'il',
76 'linktable' => 'imagelinks',
77 'indexes' => [ 'il_to', 'il_backlinks_namespace' ],
78 'from_namespace' => true,
79 'to_namespace' => NS_FILE,
80 'exampletitle' => 'File:Example.jpg',
81 'showredirects' => true,
85 private LinksMigration $linksMigration;
87 public function __construct(
88 ApiQuery $query,
89 string $moduleName,
90 LinksMigration $linksMigration
91 ) {
92 parent::__construct( $query, $moduleName, self::$settings[$moduleName]['code'] );
93 $this->linksMigration = $linksMigration;
96 public function execute() {
97 $this->run();
100 public function executeGenerator( $resultPageSet ) {
101 $this->run( $resultPageSet );
105 * @param ApiPageSet|null $resultPageSet
107 private function run( ?ApiPageSet $resultPageSet = null ) {
108 $settings = self::$settings[$this->getModuleName()];
110 $db = $this->getDB();
111 $params = $this->extractRequestParams();
112 $prop = array_fill_keys( $params['prop'], true );
114 $pageSet = $this->getPageSet();
115 $titles = $pageSet->getGoodAndMissingPages();
116 $map = $pageSet->getGoodAndMissingTitlesByNamespace();
118 // Add in special pages, they can theoretically have backlinks too.
119 // (although currently they only do for prop=redirects)
120 foreach ( $pageSet->getSpecialPages() as $id => $title ) {
121 $titles[] = $title;
122 $map[$title->getNamespace()][$title->getDBkey()] = $id;
125 // Determine our fields to query on
126 $p = $settings['prefix'];
127 $hasNS = !isset( $settings['to_namespace'] );
128 if ( $hasNS ) {
129 if ( isset( $this->linksMigration::$mapping[$settings['linktable']] ) ) {
130 [ $bl_namespace, $bl_title ] = $this->linksMigration->getTitleFields( $settings['linktable'] );
131 } else {
132 $bl_namespace = "{$p}_namespace";
133 $bl_title = "{$p}_title";
135 } else {
136 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive
137 $bl_namespace = $settings['to_namespace'];
138 $bl_title = "{$p}_to";
140 $titles = array_filter( $titles, static function ( $t ) use ( $bl_namespace ) {
141 return $t->getNamespace() === $bl_namespace;
142 } );
143 $map = array_intersect_key( $map, [ $bl_namespace => true ] );
145 $bl_from = "{$p}_from";
147 if ( !$titles ) {
148 return; // nothing to do
150 if ( $params['namespace'] !== null && count( $params['namespace'] ) === 0 ) {
151 return; // nothing to do
154 // Figure out what we're sorting by, and add associated WHERE clauses.
155 // MySQL's query planner screws up if we include a field in ORDER BY
156 // when it's constant in WHERE, so we have to test that for each field.
157 $sortby = [];
158 if ( $hasNS && count( $map ) > 1 ) {
159 $sortby[$bl_namespace] = 'int';
161 $theTitle = null;
162 foreach ( $map as $nsTitles ) {
163 $key = array_key_first( $nsTitles );
164 $theTitle ??= $key;
165 if ( count( $nsTitles ) > 1 || $key !== $theTitle ) {
166 $sortby[$bl_title] = 'string';
167 break;
170 $miser_ns = null;
171 if ( $params['namespace'] !== null ) {
172 if ( empty( $settings['from_namespace'] ) ) {
173 if ( $this->getConfig()->get( MainConfigNames::MiserMode ) ) {
174 $miser_ns = $params['namespace'];
175 } else {
176 $this->addWhereFld( 'page_namespace', $params['namespace'] );
178 } else {
179 $this->addWhereFld( "{$p}_from_namespace", $params['namespace'] );
180 if ( !empty( $settings['from_namespace'] )
181 && $params['namespace'] !== null && count( $params['namespace'] ) > 1
183 $sortby["{$p}_from_namespace"] = 'int';
187 $sortby[$bl_from] = 'int';
189 // Now use the $sortby to figure out the continuation
190 $continueFields = array_keys( $sortby );
191 $continueTypes = array_values( $sortby );
192 if ( $params['continue'] !== null ) {
193 $continueValues = $this->parseContinueParamOrDie( $params['continue'], $continueTypes );
194 $conds = array_combine( $continueFields, $continueValues );
195 $this->addWhere( $db->buildComparison( '>=', $conds ) );
198 // Populate the rest of the query
199 [ $idxNoFromNS, $idxWithFromNS ] = $settings['indexes'] ?? [ '', '' ];
200 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive
201 if ( isset( $this->linksMigration::$mapping[$settings['linktable']] ) ) {
202 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive
203 $queryInfo = $this->linksMigration->getQueryInfo( $settings['linktable'] );
204 $this->addTables( array_merge( [ 'page' ], $queryInfo['tables'] ) );
205 $this->addJoinConds( $queryInfo['joins'] );
206 // TODO: Move to links migration
207 if ( in_array( 'linktarget', $queryInfo['tables'] ) ) {
208 $idxWithFromNS .= '_target_id';
210 } else {
211 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive
212 $this->addTables( [ $settings['linktable'], 'page' ] );
214 $this->addWhere( "$bl_from = page_id" );
216 if ( $this->getModuleName() === 'redirects' ) {
217 $this->addWhereFld( 'rd_interwiki', '' );
220 $this->addFields( array_keys( $sortby ) );
221 $this->addFields( [ 'bl_namespace' => $bl_namespace, 'bl_title' => $bl_title ] );
222 if ( $resultPageSet === null ) {
223 $fld_pageid = isset( $prop['pageid'] );
224 $fld_title = isset( $prop['title'] );
225 $fld_redirect = isset( $prop['redirect'] );
227 $this->addFieldsIf( 'page_id', $fld_pageid );
228 $this->addFieldsIf( [ 'page_title', 'page_namespace' ], $fld_title );
229 $this->addFieldsIf( 'page_is_redirect', $fld_redirect );
231 // prop=redirects
232 $fld_fragment = isset( $prop['fragment'] );
233 $this->addFieldsIf( 'rd_fragment', $fld_fragment );
234 } else {
235 $this->addFields( $resultPageSet->getPageTableFields() );
238 $this->addFieldsIf( 'page_namespace', $miser_ns !== null );
240 if ( $hasNS && $map ) {
241 // Can't use LinkBatch because it throws away Special titles.
242 // And we already have the needed data structure anyway.
243 $this->addWhere( $db->makeWhereFrom2d( $map, $bl_namespace, $bl_title ) );
244 } else {
245 $where = [];
246 foreach ( $titles as $t ) {
247 if ( $t->getNamespace() == $bl_namespace ) {
248 $where[] = $db->expr( $bl_title, '=', $t->getDBkey() );
251 $this->addWhere( $db->orExpr( $where ) );
254 if ( $params['show'] !== null ) {
255 // prop=redirects only
256 $show = array_fill_keys( $params['show'], true );
257 if ( ( isset( $show['fragment'] ) && isset( $show['!fragment'] ) ) ||
258 ( isset( $show['redirect'] ) && isset( $show['!redirect'] ) )
260 $this->dieWithError( 'apierror-show' );
262 $this->addWhereIf( $db->expr( 'rd_fragment', '!=', '' ), isset( $show['fragment'] ) );
263 $this->addWhereIf( [ 'rd_fragment' => '' ], isset( $show['!fragment'] ) );
264 $this->addWhereIf( [ 'page_is_redirect' => 1 ], isset( $show['redirect'] ) );
265 $this->addWhereIf( [ 'page_is_redirect' => 0 ], isset( $show['!redirect'] ) );
268 // Override any ORDER BY from above with what we calculated earlier.
269 $this->addOption( 'ORDER BY', array_keys( $sortby ) );
271 // MySQL's optimizer chokes if we have too many values in "$bl_title IN
272 // (...)" and chooses the wrong index, so specify the correct index to
273 // use for the query. See T139056 for details.
274 if ( !empty( $settings['indexes'] ) ) {
275 if (
276 $params['namespace'] !== null &&
277 count( $params['namespace'] ) == 1 &&
278 !empty( $settings['from_namespace'] )
280 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive
281 $this->addOption( 'USE INDEX', [ $settings['linktable'] => $idxWithFromNS ] );
282 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive
283 } elseif ( !isset( $this->linksMigration::$mapping[$settings['linktable']] ) ) {
284 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive
285 $this->addOption( 'USE INDEX', [ $settings['linktable'] => $idxNoFromNS ] );
289 $this->addOption( 'LIMIT', $params['limit'] + 1 );
291 $res = $this->select( __METHOD__ );
293 if ( $resultPageSet === null ) {
294 // @phan-suppress-next-line PhanPossiblyUndeclaredVariable set when used
295 if ( $fld_title ) {
296 $this->executeGenderCacheFromResultWrapper( $res, __METHOD__ );
299 $count = 0;
300 foreach ( $res as $row ) {
301 if ( ++$count > $params['limit'] ) {
302 // We've reached the one extra which shows that
303 // there are additional pages to be had. Stop here...
304 $this->setContinue( $row, $sortby );
305 break;
308 if ( $miser_ns !== null && !in_array( $row->page_namespace, $miser_ns ) ) {
309 // Miser mode namespace check
310 continue;
313 // Get the ID of the current page
314 $id = $map[$row->bl_namespace][$row->bl_title];
316 $vals = [];
317 // @phan-suppress-next-line PhanPossiblyUndeclaredVariable set when used
318 if ( $fld_pageid ) {
319 $vals['pageid'] = (int)$row->page_id;
321 if ( $fld_title ) {
322 ApiQueryBase::addTitleInfo( $vals,
323 Title::makeTitle( $row->page_namespace, $row->page_title )
326 // @phan-suppress-next-line PhanPossiblyUndeclaredVariable set when used
327 if ( $fld_fragment && $row->rd_fragment !== '' ) {
328 $vals['fragment'] = $row->rd_fragment;
330 // @phan-suppress-next-line PhanPossiblyUndeclaredVariable set when used
331 if ( $fld_redirect ) {
332 $vals['redirect'] = (bool)$row->page_is_redirect;
334 $fit = $this->addPageSubItem( $id, $vals );
335 if ( !$fit ) {
336 $this->setContinue( $row, $sortby );
337 break;
340 } else {
341 $titles = [];
342 $count = 0;
343 foreach ( $res as $row ) {
344 if ( ++$count > $params['limit'] ) {
345 // We've reached the one extra which shows that
346 // there are additional pages to be had. Stop here...
347 $this->setContinue( $row, $sortby );
348 break;
351 if ( $miser_ns !== null && !in_array( $row->page_namespace, $miser_ns ) ) {
352 // Miser mode namespace check
353 continue;
356 $titles[] = Title::makeTitle( $row->page_namespace, $row->page_title );
358 $resultPageSet->populateFromTitles( $titles );
362 private function setContinue( $row, $sortby ) {
363 $cont = [];
364 foreach ( $sortby as $field => $v ) {
365 $cont[] = $row->$field;
367 $this->setContinueEnumParameter( 'continue', implode( '|', $cont ) );
370 public function getCacheMode( $params ) {
371 return 'public';
374 public function getAllowedParams() {
375 $settings = self::$settings[$this->getModuleName()];
377 $ret = [
378 'prop' => [
379 ParamValidator::PARAM_TYPE => [
380 'pageid',
381 'title',
383 ParamValidator::PARAM_ISMULTI => true,
384 ParamValidator::PARAM_DEFAULT => 'pageid|title',
385 ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
387 'namespace' => [
388 ParamValidator::PARAM_ISMULTI => true,
389 ParamValidator::PARAM_TYPE => 'namespace',
391 'show' => null, // Will be filled/removed below
392 'limit' => [
393 ParamValidator::PARAM_DEFAULT => 10,
394 ParamValidator::PARAM_TYPE => 'limit',
395 IntegerDef::PARAM_MIN => 1,
396 IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
397 IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
399 'continue' => [
400 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
404 if ( empty( $settings['from_namespace'] ) &&
405 $this->getConfig()->get( MainConfigNames::MiserMode ) ) {
406 $ret['namespace'][ApiBase::PARAM_HELP_MSG_APPEND] = [
407 'api-help-param-limited-in-miser-mode',
411 if ( !empty( $settings['showredirects'] ) ) {
412 $ret['prop'][ParamValidator::PARAM_TYPE][] = 'redirect';
413 $ret['prop'][ParamValidator::PARAM_DEFAULT] .= '|redirect';
415 if ( isset( $settings['props'] ) ) {
416 $ret['prop'][ParamValidator::PARAM_TYPE] = array_merge(
417 $ret['prop'][ParamValidator::PARAM_TYPE], $settings['props']
421 $show = [];
422 if ( !empty( $settings['showredirects'] ) ) {
423 $show[] = 'redirect';
424 $show[] = '!redirect';
426 if ( isset( $settings['show'] ) ) {
427 $show = array_merge( $show, $settings['show'] );
429 if ( $show ) {
430 $ret['show'] = [
431 ParamValidator::PARAM_TYPE => $show,
432 ParamValidator::PARAM_ISMULTI => true,
433 ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
435 } else {
436 unset( $ret['show'] );
439 return $ret;
442 protected function getExamplesMessages() {
443 $settings = self::$settings[$this->getModuleName()];
444 $name = $this->getModuleName();
445 $path = $this->getModulePath();
446 $title = $settings['exampletitle'] ?? Title::newMainPage()->getPrefixedText();
447 $etitle = rawurlencode( $title );
449 return [
450 "action=query&prop={$name}&titles={$etitle}"
451 => "apihelp-$path-example-simple",
452 "action=query&generator={$name}&titles={$etitle}&prop=info"
453 => "apihelp-$path-example-generator",
457 public function getHelpUrls() {
458 $name = ucfirst( $this->getModuleName() );
459 return "https://www.mediawiki.org/wiki/Special:MyLanguage/API:{$name}";
463 /** @deprecated class alias since 1.43 */
464 class_alias( ApiQueryBacklinksprop::class, 'ApiQueryBacklinksprop' );