3 * API module to handle links table back-queries
5 * Created on Aug 19, 2014
7 * Copyright © 2014 Brad Jorsch <bjorsch@wikimedia.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
29 * This implements prop=redirects, prop=linkshere, prop=catmembers,
30 * prop=transcludedin, and prop=fileusage
35 class ApiQueryBacklinksprop
extends ApiQueryGeneratorBase
{
37 // Data for the various modules implemented by this class
38 private static $settings = array(
42 'linktable' => 'redirect',
43 'what' => 'redirects to',
44 'description' => 'Returns all redirects to the given pages.',
46 'fragment' => 'Fragment of each redirect, if any',
48 'showredirects' => false,
50 'fragment' => 'Only show redirects with a fragment',
51 '!fragment' => 'Only show redirects without a fragment',
57 'linktable' => 'pagelinks',
58 'from_namespace' => true,
59 'what' => 'pages linking to',
60 'description' => 'Find all pages that link to the given pages.',
61 'showredirects' => true,
63 'transcludedin' => array(
66 'linktable' => 'templatelinks',
67 'from_namespace' => true,
68 'what' => 'pages transcluding',
69 'description' => 'Find all pages that transclude the given pages.',
70 'showredirects' => true,
75 'linktable' => 'imagelinks',
76 'from_namespace' => true,
77 'to_namespace' => NS_FILE
,
78 'what' => 'pages using',
79 'exampletitle' => 'File:Example.jpg',
80 'description' => 'Find all pages that use the given files.',
81 'showredirects' => true,
85 public function __construct( ApiQuery
$query, $moduleName ) {
86 parent
::__construct( $query, $moduleName, self
::$settings[$moduleName]['code'] );
89 public function execute() {
93 public function executeGenerator( $resultPageSet ) {
94 $this->run( $resultPageSet );
98 * @param ApiPageSet $resultPageSet
100 private function run( ApiPageSet
$resultPageSet = null ) {
101 $settings = self
::$settings[$this->getModuleName()];
103 $db = $this->getDB();
104 $params = $this->extractRequestParams();
105 $prop = array_flip( $params['prop'] );
106 $emptyString = $db->addQuotes( '' );
108 $pageSet = $this->getPageSet();
109 $titles = $pageSet->getGoodTitles() +
$pageSet->getMissingTitles();
110 $map = $pageSet->getAllTitlesByNamespace();
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, array( $bl_namespace => true ) );
127 $bl_from = "{$p}_from";
130 return; // nothing to do
133 // Figure out what we're sorting by, and add associated WHERE clauses.
134 // MySQL's query planner screws up if we include a field in ORDER BY
135 // when it's constant in WHERE, so we have to test that for each field.
137 if ( $hasNS && count( $map ) > 1 ) {
138 $sortby[$bl_namespace] = 'ns';
141 foreach ( $map as $nsTitles ) {
143 $key = key( $nsTitles );
144 if ( $theTitle === null ) {
147 if ( count( $nsTitles ) > 1 ||
$key !== $theTitle ) {
148 $sortby[$bl_title] = 'title';
153 if ( $params['namespace'] !== null ) {
154 if ( empty( $settings['from_namespace'] ) && $this->getConfig()->get( 'MiserMode' ) ) {
155 $miser_ns = $params['namespace'];
157 $this->addWhereFld( "{$p}_from_namespace", $params['namespace'] );
158 if ( !empty( $settings['from_namespace'] ) && count( $params['namespace'] ) > 1 ) {
159 $sortby["{$p}_from_namespace"] = 'int';
163 $sortby[$bl_from] = 'int';
165 // Now use the $sortby to figure out the continuation
166 if ( !is_null( $params['continue'] ) ) {
167 $cont = explode( '|', $params['continue'] );
168 $this->dieContinueUsageIf( count( $cont ) != count( $sortby ) );
170 $i = count( $sortby ) - 1;
173 foreach ( array_reverse( $sortby, true ) as $field => $type ) {
181 $this->dieContinueUsageIf( $v != $cont[$i] );
188 $v = $db->addQuotes( $v );
192 if ( $where === '' ) {
193 $where = "$field >= $v";
195 $where = "$field > $v OR ($field = $v AND ($where))";
200 $this->addWhere( $where );
203 // Populate the rest of the query
204 $this->addTables( array( $settings['linktable'], 'page' ) );
205 $this->addWhere( "$bl_from = page_id" );
207 if ( $this->getModuleName() === 'redirects' ) {
208 $this->addWhere( "rd_interwiki = $emptyString OR rd_interwiki IS NULL" );
211 $this->addFields( array_keys( $sortby ) );
212 $this->addFields( array( 'bl_namespace' => $bl_namespace, 'bl_title' => $bl_title ) );
213 if ( is_null( $resultPageSet ) ) {
214 $fld_pageid = isset( $prop['pageid'] );
215 $fld_title = isset( $prop['title'] );
216 $fld_redirect = isset( $prop['redirect'] );
218 $this->addFieldsIf( 'page_id', $fld_pageid );
219 $this->addFieldsIf( array( 'page_title', 'page_namespace' ), $fld_title );
220 $this->addFieldsIf( 'page_is_redirect', $fld_redirect );
223 $fld_fragment = isset( $prop['fragment'] );
224 $this->addFieldsIf( 'rd_fragment', $fld_fragment );
226 $this->addFields( $resultPageSet->getPageTableFields() );
229 $this->addFieldsIf( 'page_namespace', $miser_ns !== null );
232 $lb = new LinkBatch( $titles );
233 $this->addWhere( $lb->constructSet( $p, $db ) );
236 foreach ( $titles as $t ) {
237 if ( $t->getNamespace() == $bl_namespace ) {
238 $where[] = "$bl_title = " . $db->addQuotes( $t->getDBkey() );
241 $this->addWhere( $db->makeList( $where, LIST_OR
) );
244 if ( $params['show'] !== null ) {
245 // prop=redirects only
246 $show = array_flip( $params['show'] );
247 if ( isset( $show['fragment'] ) && isset( $show['!fragment'] ) ||
248 isset( $show['redirect'] ) && isset( $show['!redirect'] )
250 $this->dieUsageMsg( 'show' );
252 $this->addWhereIf( "rd_fragment != $emptyString", isset( $show['fragment'] ) );
254 "rd_fragment = $emptyString OR rd_fragment IS NULL",
255 isset( $show['!fragment'] )
257 $this->addWhereIf( array( 'page_is_redirect' => 1 ), isset( $show['redirect'] ) );
258 $this->addWhereIf( array( 'page_is_redirect' => 0 ), isset( $show['!redirect'] ) );
261 // Override any ORDER BY from above with what we calculated earlier.
262 $this->addOption( 'ORDER BY', array_keys( $sortby ) );
264 $this->addOption( 'LIMIT', $params['limit'] +
1 );
266 $res = $this->select( __METHOD__
);
268 if ( is_null( $resultPageSet ) ) {
270 foreach ( $res as $row ) {
271 if ( ++
$count > $params['limit'] ) {
272 // We've reached the one extra which shows that
273 // there are additional pages to be had. Stop here...
274 $this->setContinue( $row, $sortby );
278 if ( $miser_ns !== null && !in_array( $row->page_namespace
, $miser_ns ) ) {
279 // Miser mode namespace check
283 // Get the ID of the current page
284 $id = $map[$row->bl_namespace
][$row->bl_title
];
288 $vals['pageid'] = $row->page_id
;
291 ApiQueryBase
::addTitleInfo( $vals,
292 Title
::makeTitle( $row->page_namespace
, $row->page_title
)
295 if ( $fld_fragment && $row->rd_fragment
!== null && $row->rd_fragment
!== '' ) {
296 $vals['fragment'] = $row->rd_fragment
;
298 if ( $fld_redirect && $row->page_is_redirect
) {
299 $vals['redirect'] = '';
301 $fit = $this->addPageSubItem( $id, $vals );
303 $this->setContinue( $row, $sortby );
310 foreach ( $res as $row ) {
311 if ( ++
$count > $params['limit'] ) {
312 // We've reached the one extra which shows that
313 // there are additional pages to be had. Stop here...
314 $this->setContinue( $row, $sortby );
317 $titles[] = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
319 $resultPageSet->populateFromTitles( $titles );
323 private function setContinue( $row, $sortby ) {
325 foreach ( $sortby as $field => $v ) {
326 $cont[] = $row->$field;
328 $this->setContinueEnumParameter( 'continue', join( '|', $cont ) );
331 public function getCacheMode( $params ) {
335 public function getAllowedParams() {
336 $settings = self
::$settings[$this->getModuleName()];
340 ApiBase
::PARAM_TYPE
=> array(
344 ApiBase
::PARAM_ISMULTI
=> true,
345 ApiBase
::PARAM_DFLT
=> 'pageid|title',
347 'namespace' => array(
348 ApiBase
::PARAM_ISMULTI
=> true,
349 ApiBase
::PARAM_TYPE
=> 'namespace',
352 ApiBase
::PARAM_DFLT
=> 10,
353 ApiBase
::PARAM_TYPE
=> 'limit',
354 ApiBase
::PARAM_MIN
=> 1,
355 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
356 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
361 if ( !empty( $settings['showredirects'] ) ) {
362 $ret['prop'][ApiBase
::PARAM_TYPE
][] = 'redirect';
363 $ret['prop'][ApiBase
::PARAM_DFLT
] .= '|redirect';
365 if ( isset( $settings['props'] ) ) {
366 $ret['prop'][ApiBase
::PARAM_TYPE
] = array_merge(
367 $ret['prop'][ApiBase
::PARAM_TYPE
], array_keys( $settings['props'] )
372 if ( !empty( $settings['showredirects'] ) ) {
373 $show[] = 'redirect';
374 $show[] = '!redirect';
376 if ( isset( $settings['show'] ) ) {
377 $show = array_merge( $show, array_keys( $settings['show'] ) );
380 $ret['show'] = array(
381 ApiBase
::PARAM_TYPE
=> $show,
382 ApiBase
::PARAM_ISMULTI
=> true,
389 public function getParamDescription() {
390 $settings = self
::$settings[$this->getModuleName()];
391 $p = $this->getModulePrefix();
395 'Which properties to get:',
398 'Show only items that meet this criteria.',
400 'namespace' => 'Only include pages in these namespaces',
401 'limit' => 'How many to return',
402 'continue' => 'When more results are available, use this to continue',
405 if ( empty( $settings['from_namespace'] ) && $this->getConfig()->get( 'MiserMode' ) ) {
406 $ret['namespace'] = array(
408 "NOTE: Due to \$wgMiserMode, using this may result in fewer than \"{$p}limit\" results",
409 'returned before continuing; in extreme cases, zero results may be returned.',
411 if ( isset( $ret['type'] ) ) {
412 $ret['namespace'][] = "Note that you can use {$p}type=subcat or {$p}type=file " .
413 "instead of {$p}namespace=14 or 6.";
418 'pageid' => 'Adds the ID of page',
419 'title' => 'Adds the title and namespace ID of the page',
421 if ( !empty( $settings['showredirects'] ) ) {
422 $props['redirect'] = 'Indicate if the page is a redirect';
424 if ( isset( $settings['props'] ) ) {
425 $props +
= $settings['props'];
427 foreach ( $props as $k => $v ) {
428 $ret['props'][] = sprintf( "%-9s - %s", $k, $v );
432 if ( !empty( $settings['showredirects'] ) ) {
434 'redirect' => 'Only show redirects',
435 '!redirect' => 'Only show non-redirects',
438 if ( isset( $settings['show'] ) ) {
439 $show +
= $settings['show'];
441 foreach ( $show as $k => $v ) {
442 $ret['show'][] = sprintf( "%-9s - %s", $k, $v );
448 public function getDescription() {
449 return self
::$settings[$this->getModuleName()]['description'];
452 public function getExamples() {
453 $settings = self
::$settings[$this->getModuleName()];
454 $name = $this->getModuleName();
455 $what = $settings['what'];
456 $title = isset( $settings['exampletitle'] ) ?
$settings['exampletitle'] : 'Main Page';
457 $etitle = rawurlencode( $title );
460 "api.php?action=query&prop={$name}&titles={$etitle}"
461 => "Get a list of $what [[$title]]",
462 "api.php?action=query&generator={$name}&titles={$etitle}&prop=info"
463 => "Get information about $what [[$title]]",
467 public function getHelpUrls() {
468 $name = $this->getModuleName();
469 $prefix = $this->getModulePrefix();
470 return "https://www.mediawiki.org/wiki/API:Properties#{$name}_.2F_{$prefix}";