Expose $wgMaxArticleSize in siteinfo query api
[mediawiki.git] / includes / api / ApiQueryBacklinksprop.php
blob3810e90f139745d2dd3b73b80ca0d03fa0abc6f8
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 = [
39 'redirects' => [
40 'code' => 'rd',
41 'prefix' => 'rd',
42 'linktable' => 'redirect',
43 'props' => [
44 'fragment',
46 'showredirects' => false,
47 'show' => [
48 'fragment',
49 '!fragment',
52 'linkshere' => [
53 'code' => 'lh',
54 'prefix' => 'pl',
55 'linktable' => 'pagelinks',
56 'from_namespace' => true,
57 'showredirects' => true,
59 'transcludedin' => [
60 'code' => 'ti',
61 'prefix' => 'tl',
62 'linktable' => 'templatelinks',
63 'from_namespace' => true,
64 'showredirects' => true,
66 'fileusage' => [
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, [ $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 = [];
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'] ) ) {
147 if ( $this->getConfig()->get( 'MiserMode' ) ) {
148 $miser_ns = $params['namespace'];
149 } else {
150 $this->addWhereFld( 'page_namespace', $params['namespace'] );
152 } else {
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 ) );
165 $where = '';
166 $i = count( $sortby ) - 1;
167 foreach ( array_reverse( $sortby, true ) as $field => $type ) {
168 $v = $cont[$i];
169 switch ( $type ) {
170 case 'ns':
171 case 'int':
172 $v = (int)$v;
173 $this->dieContinueUsageIf( $v != $cont[$i] );
174 break;
175 default:
176 $v = $db->addQuotes( $v );
177 break;
180 if ( $where === '' ) {
181 $where = "$field >= $v";
182 } else {
183 $where = "$field > $v OR ($field = $v AND ($where))";
186 $i--;
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 );
210 // prop=redirects
211 $fld_fragment = isset( $prop['fragment'] );
212 $this->addFieldsIf( 'rd_fragment', $fld_fragment );
213 } else {
214 $this->addFields( $resultPageSet->getPageTableFields() );
217 $this->addFieldsIf( 'page_namespace', $miser_ns !== null );
219 if ( $hasNS ) {
220 $lb = new LinkBatch( $titles );
221 $this->addWhere( $lb->constructSet( $p, $db ) );
222 } else {
223 $where = [];
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'] ) );
241 $this->addWhereIf(
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 ) ) {
257 $count = 0;
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 );
263 break;
266 if ( $miser_ns !== null && !in_array( $row->page_namespace, $miser_ns ) ) {
267 // Miser mode namespace check
268 continue;
271 // Get the ID of the current page
272 $id = $map[$row->bl_namespace][$row->bl_title];
274 $vals = [];
275 if ( $fld_pageid ) {
276 $vals['pageid'] = (int)$row->page_id;
278 if ( $fld_title ) {
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 );
290 if ( !$fit ) {
291 $this->setContinue( $row, $sortby );
292 break;
295 } else {
296 $titles = [];
297 $count = 0;
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 );
303 break;
305 $titles[] = Title::makeTitle( $row->page_namespace, $row->page_title );
307 $resultPageSet->populateFromTitles( $titles );
311 private function setContinue( $row, $sortby ) {
312 $cont = [];
313 foreach ( $sortby as $field => $v ) {
314 $cont[] = $row->$field;
316 $this->setContinueEnumParameter( 'continue', implode( '|', $cont ) );
319 public function getCacheMode( $params ) {
320 return 'public';
323 public function getAllowedParams() {
324 $settings = self::$settings[$this->getModuleName()];
326 $ret = [
327 'prop' => [
328 ApiBase::PARAM_TYPE => [
329 'pageid',
330 'title',
332 ApiBase::PARAM_ISMULTI => true,
333 ApiBase::PARAM_DFLT => 'pageid|title',
334 ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
336 'namespace' => [
337 ApiBase::PARAM_ISMULTI => true,
338 ApiBase::PARAM_TYPE => 'namespace',
340 'show' => null, // Will be filled/removed below
341 'limit' => [
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
348 'continue' => [
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']
369 $show = [];
370 if ( !empty( $settings['showredirects'] ) ) {
371 $show[] = 'redirect';
372 $show[] = '!redirect';
374 if ( isset( $settings['show'] ) ) {
375 $show = array_merge( $show, $settings['show'] );
377 if ( $show ) {
378 $ret['show'] = [
379 ApiBase::PARAM_TYPE => $show,
380 ApiBase::PARAM_ISMULTI => true,
382 } else {
383 unset( $ret['show'] );
386 return $ret;
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 );
396 return [
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}";