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
27 * This implements prop=redirects, prop=linkshere, prop=catmembers,
28 * prop=transcludedin, and prop=fileusage
33 class ApiQueryBacklinksprop
extends ApiQueryGeneratorBase
{
35 // Data for the various modules implemented by this class
36 private static $settings = [
40 'linktable' => 'redirect',
44 'showredirects' => false,
53 'linktable' => 'pagelinks',
54 'indexes' => [ 'pl_namespace', 'pl_backlinks_namespace' ],
55 'from_namespace' => true,
56 'showredirects' => true,
61 'linktable' => 'templatelinks',
62 'indexes' => [ 'tl_namespace', 'tl_backlinks_namespace' ],
63 'from_namespace' => true,
64 'showredirects' => true,
69 'linktable' => 'imagelinks',
70 'indexes' => [ 'il_to', 'il_backlinks_namespace' ],
71 'from_namespace' => true,
72 'to_namespace' => NS_FILE
,
73 'exampletitle' => 'File:Example.jpg',
74 'showredirects' => true,
78 public function __construct( ApiQuery
$query, $moduleName ) {
79 parent
::__construct( $query, $moduleName, self
::$settings[$moduleName]['code'] );
82 public function execute() {
86 public function executeGenerator( $resultPageSet ) {
87 $this->run( $resultPageSet );
91 * @param ApiPageSet|null $resultPageSet
93 private function run( ApiPageSet
$resultPageSet = null ) {
94 $settings = self
::$settings[$this->getModuleName()];
97 $params = $this->extractRequestParams();
98 $prop = array_flip( $params['prop'] );
99 $emptyString = $db->addQuotes( '' );
101 $pageSet = $this->getPageSet();
102 $titles = $pageSet->getGoodAndMissingTitles();
103 $map = $pageSet->getGoodAndMissingTitlesByNamespace();
105 // Add in special pages, they can theoretically have backlinks too.
106 // (although currently they only do for prop=redirects)
107 foreach ( $pageSet->getSpecialTitles() as $id => $title ) {
109 $map[$title->getNamespace()][$title->getDBkey()] = $id;
112 // Determine our fields to query on
113 $p = $settings['prefix'];
114 $hasNS = !isset( $settings['to_namespace'] );
116 $bl_namespace = "{$p}_namespace";
117 $bl_title = "{$p}_title";
119 $bl_namespace = $settings['to_namespace'];
120 $bl_title = "{$p}_to";
122 $titles = array_filter( $titles, function ( $t ) use ( $bl_namespace ) {
123 return $t->getNamespace() === $bl_namespace;
125 $map = array_intersect_key( $map, [ $bl_namespace => true ] );
127 $bl_from = "{$p}_from";
130 return; // nothing to do
132 if ( $params['namespace'] !== null && count( $params['namespace'] ) === 0 ) {
133 return; // nothing to do
136 // Figure out what we're sorting by, and add associated WHERE clauses.
137 // MySQL's query planner screws up if we include a field in ORDER BY
138 // when it's constant in WHERE, so we have to test that for each field.
140 if ( $hasNS && count( $map ) > 1 ) {
141 $sortby[$bl_namespace] = 'ns';
144 foreach ( $map as $nsTitles ) {
146 $key = key( $nsTitles );
147 if ( $theTitle === null ) {
150 if ( count( $nsTitles ) > 1 ||
$key !== $theTitle ) {
151 $sortby[$bl_title] = 'title';
156 if ( $params['namespace'] !== null ) {
157 if ( empty( $settings['from_namespace'] ) ) {
158 if ( $this->getConfig()->get( 'MiserMode' ) ) {
159 $miser_ns = $params['namespace'];
161 $this->addWhereFld( 'page_namespace', $params['namespace'] );
164 $this->addWhereFld( "{$p}_from_namespace", $params['namespace'] );
165 if ( !empty( $settings['from_namespace'] )
166 && $params['namespace'] !== null && count( $params['namespace'] ) > 1
168 $sortby["{$p}_from_namespace"] = 'int';
172 $sortby[$bl_from] = 'int';
174 // Now use the $sortby to figure out the continuation
175 if ( !is_null( $params['continue'] ) ) {
176 $cont = explode( '|', $params['continue'] );
177 $this->dieContinueUsageIf( count( $cont ) != count( $sortby ) );
179 $i = count( $sortby ) - 1;
180 foreach ( array_reverse( $sortby, true ) as $field => $type ) {
186 $this->dieContinueUsageIf( $v != $cont[$i] );
189 $v = $db->addQuotes( $v );
193 if ( $where === '' ) {
194 $where = "$field >= $v";
196 $where = "$field > $v OR ($field = $v AND ($where))";
201 $this->addWhere( $where );
204 // Populate the rest of the query
205 $this->addTables( [ $settings['linktable'], 'page' ] );
206 $this->addWhere( "$bl_from = page_id" );
208 if ( $this->getModuleName() === 'redirects' ) {
209 $this->addWhere( "rd_interwiki = $emptyString OR rd_interwiki IS NULL" );
212 $this->addFields( array_keys( $sortby ) );
213 $this->addFields( [ 'bl_namespace' => $bl_namespace, 'bl_title' => $bl_title ] );
214 if ( is_null( $resultPageSet ) ) {
215 $fld_pageid = isset( $prop['pageid'] );
216 $fld_title = isset( $prop['title'] );
217 $fld_redirect = isset( $prop['redirect'] );
219 $this->addFieldsIf( 'page_id', $fld_pageid );
220 $this->addFieldsIf( [ 'page_title', 'page_namespace' ], $fld_title );
221 $this->addFieldsIf( 'page_is_redirect', $fld_redirect );
224 $fld_fragment = isset( $prop['fragment'] );
225 $this->addFieldsIf( 'rd_fragment', $fld_fragment );
227 $this->addFields( $resultPageSet->getPageTableFields() );
230 $this->addFieldsIf( 'page_namespace', $miser_ns !== null );
233 // Can't use LinkBatch because it throws away Special titles.
234 // And we already have the needed data structure anyway.
235 $this->addWhere( $db->makeWhereFrom2d( $map, $bl_namespace, $bl_title ) );
238 foreach ( $titles as $t ) {
239 if ( $t->getNamespace() == $bl_namespace ) {
240 $where[] = "$bl_title = " . $db->addQuotes( $t->getDBkey() );
243 $this->addWhere( $db->makeList( $where, LIST_OR
) );
246 if ( $params['show'] !== null ) {
247 // prop=redirects only
248 $show = array_flip( $params['show'] );
249 if ( isset( $show['fragment'] ) && isset( $show['!fragment'] ) ||
250 isset( $show['redirect'] ) && isset( $show['!redirect'] )
252 $this->dieWithError( 'apierror-show' );
254 $this->addWhereIf( "rd_fragment != $emptyString", isset( $show['fragment'] ) );
256 "rd_fragment = $emptyString OR rd_fragment IS NULL",
257 isset( $show['!fragment'] )
259 $this->addWhereIf( [ 'page_is_redirect' => 1 ], isset( $show['redirect'] ) );
260 $this->addWhereIf( [ 'page_is_redirect' => 0 ], isset( $show['!redirect'] ) );
263 // Override any ORDER BY from above with what we calculated earlier.
264 $this->addOption( 'ORDER BY', array_keys( $sortby ) );
266 // MySQL's optimizer chokes if we have too many values in "$bl_title IN
267 // (...)" and chooses the wrong index, so specify the correct index to
268 // use for the query. See T139056 for details.
269 if ( !empty( $settings['indexes'] ) ) {
270 list( $idxNoFromNS, $idxWithFromNS ) = $settings['indexes'];
271 if ( $params['namespace'] !== null && !empty( $settings['from_namespace'] ) ) {
272 $this->addOption( 'USE INDEX', [ $settings['linktable'] => $idxWithFromNS ] );
274 $this->addOption( 'USE INDEX', [ $settings['linktable'] => $idxNoFromNS ] );
278 // MySQL (or at least 5.5.5-10.0.23-MariaDB) chooses a really bad query
279 // plan if it thinks there will be more matching rows in the linktable
280 // than are in page. Use STRAIGHT_JOIN here to force it to use the
281 // intended, fast plan. See T145079 for details.
282 $this->addOption( 'STRAIGHT_JOIN' );
284 $this->addOption( 'LIMIT', $params['limit'] +
1 );
286 $res = $this->select( __METHOD__
);
288 if ( is_null( $resultPageSet ) ) {
290 foreach ( $res as $row ) {
291 if ( ++
$count > $params['limit'] ) {
292 // We've reached the one extra which shows that
293 // there are additional pages to be had. Stop here...
294 $this->setContinue( $row, $sortby );
298 if ( $miser_ns !== null && !in_array( $row->page_namespace
, $miser_ns ) ) {
299 // Miser mode namespace check
303 // Get the ID of the current page
304 $id = $map[$row->bl_namespace
][$row->bl_title
];
308 $vals['pageid'] = (int)$row->page_id
;
311 ApiQueryBase
::addTitleInfo( $vals,
312 Title
::makeTitle( $row->page_namespace
, $row->page_title
)
315 if ( $fld_fragment && $row->rd_fragment
!== null && $row->rd_fragment
!== '' ) {
316 $vals['fragment'] = $row->rd_fragment
;
318 if ( $fld_redirect ) {
319 $vals['redirect'] = (bool)$row->page_is_redirect
;
321 $fit = $this->addPageSubItem( $id, $vals );
323 $this->setContinue( $row, $sortby );
330 foreach ( $res as $row ) {
331 if ( ++
$count > $params['limit'] ) {
332 // We've reached the one extra which shows that
333 // there are additional pages to be had. Stop here...
334 $this->setContinue( $row, $sortby );
337 $titles[] = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
339 $resultPageSet->populateFromTitles( $titles );
343 private function setContinue( $row, $sortby ) {
345 foreach ( $sortby as $field => $v ) {
346 $cont[] = $row->$field;
348 $this->setContinueEnumParameter( 'continue', implode( '|', $cont ) );
351 public function getCacheMode( $params ) {
355 public function getAllowedParams() {
356 $settings = self
::$settings[$this->getModuleName()];
360 ApiBase
::PARAM_TYPE
=> [
364 ApiBase
::PARAM_ISMULTI
=> true,
365 ApiBase
::PARAM_DFLT
=> 'pageid|title',
366 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> [],
369 ApiBase
::PARAM_ISMULTI
=> true,
370 ApiBase
::PARAM_TYPE
=> 'namespace',
372 'show' => null, // Will be filled/removed below
374 ApiBase
::PARAM_DFLT
=> 10,
375 ApiBase
::PARAM_TYPE
=> 'limit',
376 ApiBase
::PARAM_MIN
=> 1,
377 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
378 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
381 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
385 if ( empty( $settings['from_namespace'] ) && $this->getConfig()->get( 'MiserMode' ) ) {
386 $ret['namespace'][ApiBase
::PARAM_HELP_MSG_APPEND
] = [
387 'api-help-param-limited-in-miser-mode',
391 if ( !empty( $settings['showredirects'] ) ) {
392 $ret['prop'][ApiBase
::PARAM_TYPE
][] = 'redirect';
393 $ret['prop'][ApiBase
::PARAM_DFLT
] .= '|redirect';
395 if ( isset( $settings['props'] ) ) {
396 $ret['prop'][ApiBase
::PARAM_TYPE
] = array_merge(
397 $ret['prop'][ApiBase
::PARAM_TYPE
], $settings['props']
402 if ( !empty( $settings['showredirects'] ) ) {
403 $show[] = 'redirect';
404 $show[] = '!redirect';
406 if ( isset( $settings['show'] ) ) {
407 $show = array_merge( $show, $settings['show'] );
411 ApiBase
::PARAM_TYPE
=> $show,
412 ApiBase
::PARAM_ISMULTI
=> true,
415 unset( $ret['show'] );
421 protected function getExamplesMessages() {
422 $settings = self
::$settings[$this->getModuleName()];
423 $name = $this->getModuleName();
424 $path = $this->getModulePath();
425 $title = $settings['exampletitle'] ??
'Main Page';
426 $etitle = rawurlencode( $title );
429 "action=query&prop={$name}&titles={$etitle}"
430 => "apihelp-$path-example-simple",
431 "action=query&generator={$name}&titles={$etitle}&prop=info"
432 => "apihelp-$path-example-generator",
436 public function getHelpUrls() {
437 $name = ucfirst( $this->getModuleName() );
438 return "https://www.mediawiki.org/wiki/Special:MyLanguage/API:{$name}";