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 = [
42 'linktable' => 'redirect',
46 'showredirects' => false,
55 'linktable' => 'pagelinks',
56 'from_namespace' => true,
57 'showredirects' => true,
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, [ $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'] ) ) {
147 if ( $this->getConfig()->get( 'MiserMode' ) ) {
148 $miser_ns = $params['namespace'];
150 $this->addWhereFld( 'page_namespace', $params['namespace'] );
153 $this->addWhereFld( "{$p}_from_namespace", $params['namespace'] );
154 if ( !empty( $settings['from_namespace'] ) && count( $params['namespace'] ) > 1 ) {
155 $sortby["{$p}_from_namespace"] = 'int';
159 $sortby[$bl_from] = 'int';
161 // Now use the $sortby to figure out the continuation
162 if ( !is_null( $params['continue'] ) ) {
163 $cont = explode( '|', $params['continue'] );
164 $this->dieContinueUsageIf( count( $cont ) != count( $sortby ) );
166 $i = count( $sortby ) - 1;
167 foreach ( array_reverse( $sortby, true ) as $field => $type ) {
173 $this->dieContinueUsageIf( $v != $cont[$i] );
176 $v = $db->addQuotes( $v );
180 if ( $where === '' ) {
181 $where = "$field >= $v";
183 $where = "$field > $v OR ($field = $v AND ($where))";
188 $this->addWhere( $where );
191 // Populate the rest of the query
192 $this->addTables( [ $settings['linktable'], 'page' ] );
193 $this->addWhere( "$bl_from = page_id" );
195 if ( $this->getModuleName() === 'redirects' ) {
196 $this->addWhere( "rd_interwiki = $emptyString OR rd_interwiki IS NULL" );
199 $this->addFields( array_keys( $sortby ) );
200 $this->addFields( [ 'bl_namespace' => $bl_namespace, 'bl_title' => $bl_title ] );
201 if ( is_null( $resultPageSet ) ) {
202 $fld_pageid = isset( $prop['pageid'] );
203 $fld_title = isset( $prop['title'] );
204 $fld_redirect = isset( $prop['redirect'] );
206 $this->addFieldsIf( 'page_id', $fld_pageid );
207 $this->addFieldsIf( [ 'page_title', 'page_namespace' ], $fld_title );
208 $this->addFieldsIf( 'page_is_redirect', $fld_redirect );
211 $fld_fragment = isset( $prop['fragment'] );
212 $this->addFieldsIf( 'rd_fragment', $fld_fragment );
214 $this->addFields( $resultPageSet->getPageTableFields() );
217 $this->addFieldsIf( 'page_namespace', $miser_ns !== null );
220 $lb = new LinkBatch( $titles );
221 $this->addWhere( $lb->constructSet( $p, $db ) );
224 foreach ( $titles as $t ) {
225 if ( $t->getNamespace() == $bl_namespace ) {
226 $where[] = "$bl_title = " . $db->addQuotes( $t->getDBkey() );
229 $this->addWhere( $db->makeList( $where, LIST_OR
) );
232 if ( $params['show'] !== null ) {
233 // prop=redirects only
234 $show = array_flip( $params['show'] );
235 if ( isset( $show['fragment'] ) && isset( $show['!fragment'] ) ||
236 isset( $show['redirect'] ) && isset( $show['!redirect'] )
238 $this->dieUsageMsg( 'show' );
240 $this->addWhereIf( "rd_fragment != $emptyString", isset( $show['fragment'] ) );
242 "rd_fragment = $emptyString OR rd_fragment IS NULL",
243 isset( $show['!fragment'] )
245 $this->addWhereIf( [ 'page_is_redirect' => 1 ], isset( $show['redirect'] ) );
246 $this->addWhereIf( [ 'page_is_redirect' => 0 ], isset( $show['!redirect'] ) );
249 // Override any ORDER BY from above with what we calculated earlier.
250 $this->addOption( 'ORDER BY', array_keys( $sortby ) );
252 $this->addOption( 'LIMIT', $params['limit'] +
1 );
254 $res = $this->select( __METHOD__
);
256 if ( is_null( $resultPageSet ) ) {
258 foreach ( $res as $row ) {
259 if ( ++
$count > $params['limit'] ) {
260 // We've reached the one extra which shows that
261 // there are additional pages to be had. Stop here...
262 $this->setContinue( $row, $sortby );
266 if ( $miser_ns !== null && !in_array( $row->page_namespace
, $miser_ns ) ) {
267 // Miser mode namespace check
271 // Get the ID of the current page
272 $id = $map[$row->bl_namespace
][$row->bl_title
];
276 $vals['pageid'] = (int)$row->page_id
;
279 ApiQueryBase
::addTitleInfo( $vals,
280 Title
::makeTitle( $row->page_namespace
, $row->page_title
)
283 if ( $fld_fragment && $row->rd_fragment
!== null && $row->rd_fragment
!== '' ) {
284 $vals['fragment'] = $row->rd_fragment
;
286 if ( $fld_redirect ) {
287 $vals['redirect'] = (bool)$row->page_is_redirect
;
289 $fit = $this->addPageSubItem( $id, $vals );
291 $this->setContinue( $row, $sortby );
298 foreach ( $res as $row ) {
299 if ( ++
$count > $params['limit'] ) {
300 // We've reached the one extra which shows that
301 // there are additional pages to be had. Stop here...
302 $this->setContinue( $row, $sortby );
305 $titles[] = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
307 $resultPageSet->populateFromTitles( $titles );
311 private function setContinue( $row, $sortby ) {
313 foreach ( $sortby as $field => $v ) {
314 $cont[] = $row->$field;
316 $this->setContinueEnumParameter( 'continue', implode( '|', $cont ) );
319 public function getCacheMode( $params ) {
323 public function getAllowedParams() {
324 $settings = self
::$settings[$this->getModuleName()];
328 ApiBase
::PARAM_TYPE
=> [
332 ApiBase
::PARAM_ISMULTI
=> true,
333 ApiBase
::PARAM_DFLT
=> 'pageid|title',
334 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> [],
337 ApiBase
::PARAM_ISMULTI
=> true,
338 ApiBase
::PARAM_TYPE
=> 'namespace',
340 'show' => null, // Will be filled/removed below
342 ApiBase
::PARAM_DFLT
=> 10,
343 ApiBase
::PARAM_TYPE
=> 'limit',
344 ApiBase
::PARAM_MIN
=> 1,
345 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
346 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
349 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
353 if ( empty( $settings['from_namespace'] ) && $this->getConfig()->get( 'MiserMode' ) ) {
354 $ret['namespace'][ApiBase
::PARAM_HELP_MSG_APPEND
] = [
355 'api-help-param-limited-in-miser-mode',
359 if ( !empty( $settings['showredirects'] ) ) {
360 $ret['prop'][ApiBase
::PARAM_TYPE
][] = 'redirect';
361 $ret['prop'][ApiBase
::PARAM_DFLT
] .= '|redirect';
363 if ( isset( $settings['props'] ) ) {
364 $ret['prop'][ApiBase
::PARAM_TYPE
] = array_merge(
365 $ret['prop'][ApiBase
::PARAM_TYPE
], $settings['props']
370 if ( !empty( $settings['showredirects'] ) ) {
371 $show[] = 'redirect';
372 $show[] = '!redirect';
374 if ( isset( $settings['show'] ) ) {
375 $show = array_merge( $show, $settings['show'] );
379 ApiBase
::PARAM_TYPE
=> $show,
380 ApiBase
::PARAM_ISMULTI
=> true,
383 unset( $ret['show'] );
389 protected function getExamplesMessages() {
390 $settings = self
::$settings[$this->getModuleName()];
391 $name = $this->getModuleName();
392 $path = $this->getModulePath();
393 $title = isset( $settings['exampletitle'] ) ?
$settings['exampletitle'] : 'Main Page';
394 $etitle = rawurlencode( $title );
397 "action=query&prop={$name}&titles={$etitle}"
398 => "apihelp-$path-example-simple",
399 "action=query&generator={$name}&titles={$etitle}&prop=info"
400 => "apihelp-$path-example-generator",
404 public function getHelpUrls() {
405 $name = ucfirst( $this->getModuleName() );
406 return "https://www.mediawiki.org/wiki/API:{$name}";