Merge "DatabaseMssql: Don't duplicate body of makeList()"
[mediawiki.git] / includes / api / ApiQueryBacklinksprop.php
blobb4752ae9d4b7fe111d2fb2bdf93870c991cc0e4b
1 <?php
2 /**
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
24 * @file
25 * @since 1.24
28 /**
29 * This implements prop=redirects, prop=linkshere, prop=catmembers,
30 * prop=transcludedin, and prop=fileusage
32 * @ingroup API
33 * @since 1.24
35 class ApiQueryBacklinksprop extends ApiQueryGeneratorBase {
37 // Data for the various modules implemented by this class
38 private static $settings = array(
39 'redirects' => array(
40 'code' => 'rd',
41 'prefix' => 'rd',
42 'linktable' => 'redirect',
43 'props' => array(
44 'fragment',
46 'showredirects' => false,
47 'show' => array(
48 'fragment',
49 '!fragment',
52 'linkshere' => array(
53 'code' => 'lh',
54 'prefix' => 'pl',
55 'linktable' => 'pagelinks',
56 'from_namespace' => true,
57 'showredirects' => true,
59 'transcludedin' => array(
60 'code' => 'ti',
61 'prefix' => 'tl',
62 'linktable' => 'templatelinks',
63 'from_namespace' => true,
64 'showredirects' => true,
66 'fileusage' => array(
67 'code' => 'fu',
68 'prefix' => 'il',
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() {
82 $this->run();
85 public function executeGenerator( $resultPageSet ) {
86 $this->run( $resultPageSet );
89 /**
90 * @param ApiPageSet $resultPageSet
92 private function run( ApiPageSet $resultPageSet = null ) {
93 $settings = self::$settings[$this->getModuleName()];
95 $db = $this->getDB();
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'] );
107 if ( $hasNS ) {
108 $bl_namespace = "{$p}_namespace";
109 $bl_title = "{$p}_title";
110 } else {
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;
116 } );
117 $map = array_intersect_key( $map, array( $bl_namespace => true ) );
119 $bl_from = "{$p}_from";
121 if ( !$titles ) {
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.
128 $sortby = array();
129 if ( $hasNS && count( $map ) > 1 ) {
130 $sortby[$bl_namespace] = 'ns';
132 $theTitle = null;
133 foreach ( $map as $nsTitles ) {
134 reset( $nsTitles );
135 $key = key( $nsTitles );
136 if ( $theTitle === null ) {
137 $theTitle = $key;
139 if ( count( $nsTitles ) > 1 || $key !== $theTitle ) {
140 $sortby[$bl_title] = 'title';
141 break;
144 $miser_ns = null;
145 if ( $params['namespace'] !== null ) {
146 if ( empty( $settings['from_namespace'] ) && $this->getConfig()->get( 'MiserMode' ) ) {
147 $miser_ns = $params['namespace'];
148 } else {
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 ) );
161 $where = '';
162 $i = count( $sortby ) - 1;
163 $cont_ns = 0;
164 $cont_title = '';
165 foreach ( array_reverse( $sortby, true ) as $field => $type ) {
166 $v = $cont[$i];
167 switch ( $type ) {
168 case 'ns':
169 $cont_ns = (int)$v;
170 /* fall through */
171 case 'int':
172 $v = (int)$v;
173 $this->dieContinueUsageIf( $v != $cont[$i] );
174 break;
176 case 'title':
177 $cont_title = $v;
178 /* fall through */
179 default:
180 $v = $db->addQuotes( $v );
181 break;
184 if ( $where === '' ) {
185 $where = "$field >= $v";
186 } else {
187 $where = "$field > $v OR ($field = $v AND ($where))";
190 $i--;
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 );
214 // prop=redirects
215 $fld_fragment = isset( $prop['fragment'] );
216 $this->addFieldsIf( 'rd_fragment', $fld_fragment );
217 } else {
218 $this->addFields( $resultPageSet->getPageTableFields() );
221 $this->addFieldsIf( 'page_namespace', $miser_ns !== null );
223 if ( $hasNS ) {
224 $lb = new LinkBatch( $titles );
225 $this->addWhere( $lb->constructSet( $p, $db ) );
226 } else {
227 $where = array();
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'] ) );
245 $this->addWhereIf(
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 ) ) {
261 $count = 0;
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 );
267 break;
270 if ( $miser_ns !== null && !in_array( $row->page_namespace, $miser_ns ) ) {
271 // Miser mode namespace check
272 continue;
275 // Get the ID of the current page
276 $id = $map[$row->bl_namespace][$row->bl_title];
278 $vals = array();
279 if ( $fld_pageid ) {
280 $vals['pageid'] = $row->page_id;
282 if ( $fld_title ) {
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 );
294 if ( !$fit ) {
295 $this->setContinue( $row, $sortby );
296 break;
299 } else {
300 $titles = array();
301 $count = 0;
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 );
307 break;
309 $titles[] = Title::makeTitle( $row->page_namespace, $row->page_title );
311 $resultPageSet->populateFromTitles( $titles );
315 private function setContinue( $row, $sortby ) {
316 $cont = array();
317 foreach ( $sortby as $field => $v ) {
318 $cont[] = $row->$field;
320 $this->setContinueEnumParameter( 'continue', join( '|', $cont ) );
323 public function getCacheMode( $params ) {
324 return 'public';
327 public function getAllowedParams() {
328 $settings = self::$settings[$this->getModuleName()];
330 $ret = array(
331 'prop' => array(
332 ApiBase::PARAM_TYPE => array(
333 'pageid',
334 'title',
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
344 'limit' => array(
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
351 'continue' => array(
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']
372 $show = array();
373 if ( !empty( $settings['showredirects'] ) ) {
374 $show[] = 'redirect';
375 $show[] = '!redirect';
377 if ( isset( $settings['show'] ) ) {
378 $show = array_merge( $show, $settings['show'] );
380 if ( $show ) {
381 $ret['show'] = array(
382 ApiBase::PARAM_TYPE => $show,
383 ApiBase::PARAM_ISMULTI => true,
385 } else {
386 unset( $ret['show'] );
389 return $ret;
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 );
399 return array(
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}";