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',
46 'showredirects' => false,
55 'linktable' => 'pagelinks',
56 'from_namespace' => true,
57 'showredirects' => true,
59 'transcludedin' => array(
62 'linktable' => 'templatelinks',
63 'from_namespace' => true,
64 'showredirects' => true,
69 'linktable' => 'imagelinks',
70 'from_namespace' => true,
71 'to_namespace' => NS_FILE
,
72 'exampletitle' => 'File:Example.jpg',
73 'showredirects' => true,
77 public function __construct( ApiQuery
$query, $moduleName ) {
78 parent
::__construct( $query, $moduleName, self
::$settings[$moduleName]['code'] );
81 public function execute() {
85 public function executeGenerator( $resultPageSet ) {
86 $this->run( $resultPageSet );
90 * @param ApiPageSet $resultPageSet
92 private function run( ApiPageSet
$resultPageSet = null ) {
93 $settings = self
::$settings[$this->getModuleName()];
96 $params = $this->extractRequestParams();
97 $prop = array_flip( $params['prop'] );
98 $emptyString = $db->addQuotes( '' );
100 $pageSet = $this->getPageSet();
101 $titles = $pageSet->getGoodAndMissingTitles();
102 $map = $pageSet->getGoodAndMissingTitlesByNamespace();
104 // Determine our fields to query on
105 $p = $settings['prefix'];
106 $hasNS = !isset( $settings['to_namespace'] );
108 $bl_namespace = "{$p}_namespace";
109 $bl_title = "{$p}_title";
111 $bl_namespace = $settings['to_namespace'];
112 $bl_title = "{$p}_to";
114 $titles = array_filter( $titles, function ( $t ) use ( $bl_namespace ) {
115 return $t->getNamespace() === $bl_namespace;
117 $map = array_intersect_key( $map, array( $bl_namespace => true ) );
119 $bl_from = "{$p}_from";
122 return; // nothing to do
125 // Figure out what we're sorting by, and add associated WHERE clauses.
126 // MySQL's query planner screws up if we include a field in ORDER BY
127 // when it's constant in WHERE, so we have to test that for each field.
129 if ( $hasNS && count( $map ) > 1 ) {
130 $sortby[$bl_namespace] = 'ns';
133 foreach ( $map as $nsTitles ) {
135 $key = key( $nsTitles );
136 if ( $theTitle === null ) {
139 if ( count( $nsTitles ) > 1 ||
$key !== $theTitle ) {
140 $sortby[$bl_title] = 'title';
145 if ( $params['namespace'] !== null ) {
146 if ( empty( $settings['from_namespace'] ) && $this->getConfig()->get( 'MiserMode' ) ) {
147 $miser_ns = $params['namespace'];
149 $this->addWhereFld( "{$p}_from_namespace", $params['namespace'] );
150 if ( !empty( $settings['from_namespace'] ) && count( $params['namespace'] ) > 1 ) {
151 $sortby["{$p}_from_namespace"] = 'int';
155 $sortby[$bl_from] = 'int';
157 // Now use the $sortby to figure out the continuation
158 if ( !is_null( $params['continue'] ) ) {
159 $cont = explode( '|', $params['continue'] );
160 $this->dieContinueUsageIf( count( $cont ) != count( $sortby ) );
162 $i = count( $sortby ) - 1;
165 foreach ( array_reverse( $sortby, true ) as $field => $type ) {
173 $this->dieContinueUsageIf( $v != $cont[$i] );
180 $v = $db->addQuotes( $v );
184 if ( $where === '' ) {
185 $where = "$field >= $v";
187 $where = "$field > $v OR ($field = $v AND ($where))";
192 $this->addWhere( $where );
195 // Populate the rest of the query
196 $this->addTables( array( $settings['linktable'], 'page' ) );
197 $this->addWhere( "$bl_from = page_id" );
199 if ( $this->getModuleName() === 'redirects' ) {
200 $this->addWhere( "rd_interwiki = $emptyString OR rd_interwiki IS NULL" );
203 $this->addFields( array_keys( $sortby ) );
204 $this->addFields( array( 'bl_namespace' => $bl_namespace, 'bl_title' => $bl_title ) );
205 if ( is_null( $resultPageSet ) ) {
206 $fld_pageid = isset( $prop['pageid'] );
207 $fld_title = isset( $prop['title'] );
208 $fld_redirect = isset( $prop['redirect'] );
210 $this->addFieldsIf( 'page_id', $fld_pageid );
211 $this->addFieldsIf( array( 'page_title', 'page_namespace' ), $fld_title );
212 $this->addFieldsIf( 'page_is_redirect', $fld_redirect );
215 $fld_fragment = isset( $prop['fragment'] );
216 $this->addFieldsIf( 'rd_fragment', $fld_fragment );
218 $this->addFields( $resultPageSet->getPageTableFields() );
221 $this->addFieldsIf( 'page_namespace', $miser_ns !== null );
224 $lb = new LinkBatch( $titles );
225 $this->addWhere( $lb->constructSet( $p, $db ) );
228 foreach ( $titles as $t ) {
229 if ( $t->getNamespace() == $bl_namespace ) {
230 $where[] = "$bl_title = " . $db->addQuotes( $t->getDBkey() );
233 $this->addWhere( $db->makeList( $where, LIST_OR
) );
236 if ( $params['show'] !== null ) {
237 // prop=redirects only
238 $show = array_flip( $params['show'] );
239 if ( isset( $show['fragment'] ) && isset( $show['!fragment'] ) ||
240 isset( $show['redirect'] ) && isset( $show['!redirect'] )
242 $this->dieUsageMsg( 'show' );
244 $this->addWhereIf( "rd_fragment != $emptyString", isset( $show['fragment'] ) );
246 "rd_fragment = $emptyString OR rd_fragment IS NULL",
247 isset( $show['!fragment'] )
249 $this->addWhereIf( array( 'page_is_redirect' => 1 ), isset( $show['redirect'] ) );
250 $this->addWhereIf( array( 'page_is_redirect' => 0 ), isset( $show['!redirect'] ) );
253 // Override any ORDER BY from above with what we calculated earlier.
254 $this->addOption( 'ORDER BY', array_keys( $sortby ) );
256 $this->addOption( 'LIMIT', $params['limit'] +
1 );
258 $res = $this->select( __METHOD__
);
260 if ( is_null( $resultPageSet ) ) {
262 foreach ( $res as $row ) {
263 if ( ++
$count > $params['limit'] ) {
264 // We've reached the one extra which shows that
265 // there are additional pages to be had. Stop here...
266 $this->setContinue( $row, $sortby );
270 if ( $miser_ns !== null && !in_array( $row->page_namespace
, $miser_ns ) ) {
271 // Miser mode namespace check
275 // Get the ID of the current page
276 $id = $map[$row->bl_namespace
][$row->bl_title
];
280 $vals['pageid'] = $row->page_id
;
283 ApiQueryBase
::addTitleInfo( $vals,
284 Title
::makeTitle( $row->page_namespace
, $row->page_title
)
287 if ( $fld_fragment && $row->rd_fragment
!== null && $row->rd_fragment
!== '' ) {
288 $vals['fragment'] = $row->rd_fragment
;
290 if ( $fld_redirect && $row->page_is_redirect
) {
291 $vals['redirect'] = '';
293 $fit = $this->addPageSubItem( $id, $vals );
295 $this->setContinue( $row, $sortby );
302 foreach ( $res as $row ) {
303 if ( ++
$count > $params['limit'] ) {
304 // We've reached the one extra which shows that
305 // there are additional pages to be had. Stop here...
306 $this->setContinue( $row, $sortby );
309 $titles[] = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
311 $resultPageSet->populateFromTitles( $titles );
315 private function setContinue( $row, $sortby ) {
317 foreach ( $sortby as $field => $v ) {
318 $cont[] = $row->$field;
320 $this->setContinueEnumParameter( 'continue', join( '|', $cont ) );
323 public function getCacheMode( $params ) {
327 public function getAllowedParams() {
328 $settings = self
::$settings[$this->getModuleName()];
332 ApiBase
::PARAM_TYPE
=> array(
336 ApiBase
::PARAM_ISMULTI
=> true,
337 ApiBase
::PARAM_DFLT
=> 'pageid|title',
339 'namespace' => array(
340 ApiBase
::PARAM_ISMULTI
=> true,
341 ApiBase
::PARAM_TYPE
=> 'namespace',
343 'show' => null, // Will be filled/removed below
345 ApiBase
::PARAM_DFLT
=> 10,
346 ApiBase
::PARAM_TYPE
=> 'limit',
347 ApiBase
::PARAM_MIN
=> 1,
348 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
349 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
352 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
356 if ( empty( $settings['from_namespace'] ) && $this->getConfig()->get( 'MiserMode' ) ) {
357 $ret['namespace'][ApiBase
::PARAM_HELP_MSG_APPEND
] = array(
358 'api-help-param-limited-in-miser-mode',
362 if ( !empty( $settings['showredirects'] ) ) {
363 $ret['prop'][ApiBase
::PARAM_TYPE
][] = 'redirect';
364 $ret['prop'][ApiBase
::PARAM_DFLT
] .= '|redirect';
366 if ( isset( $settings['props'] ) ) {
367 $ret['prop'][ApiBase
::PARAM_TYPE
] = array_merge(
368 $ret['prop'][ApiBase
::PARAM_TYPE
], $settings['props']
373 if ( !empty( $settings['showredirects'] ) ) {
374 $show[] = 'redirect';
375 $show[] = '!redirect';
377 if ( isset( $settings['show'] ) ) {
378 $show = array_merge( $show, $settings['show'] );
381 $ret['show'] = array(
382 ApiBase
::PARAM_TYPE
=> $show,
383 ApiBase
::PARAM_ISMULTI
=> true,
386 unset( $ret['show'] );
392 protected function getExamplesMessages() {
393 $settings = self
::$settings[$this->getModuleName()];
394 $name = $this->getModuleName();
395 $path = $this->getModulePath();
396 $title = isset( $settings['exampletitle'] ) ?
$settings['exampletitle'] : 'Main Page';
397 $etitle = rawurlencode( $title );
400 "action=query&prop={$name}&titles={$etitle}"
401 => "apihelp-$path-example-simple",
402 "action=query&generator={$name}&titles={$etitle}&prop=info"
403 => "apihelp-$path-example-generator",
407 public function getHelpUrls() {
408 $name = $this->getModuleName();
409 $prefix = $this->getModulePrefix();
410 return "https://www.mediawiki.org/wiki/API:Properties#{$name}_.2F_{$prefix}";