Import: Handle uploads with sha1 starting with 0 properly
[mediawiki.git] / includes / api / ApiQueryBacklinksprop.php
blobf1a2271c1284383d158b457bf974a833f4129a40
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'] ) ) {
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 $cont_ns = 0;
168 $cont_title = '';
169 foreach ( array_reverse( $sortby, true ) as $field => $type ) {
170 $v = $cont[$i];
171 switch ( $type ) {
172 case 'ns':
173 $cont_ns = (int)$v;
174 /* fall through */
175 case 'int':
176 $v = (int)$v;
177 $this->dieContinueUsageIf( $v != $cont[$i] );
178 break;
180 case 'title':
181 $cont_title = $v;
182 /* fall through */
183 default:
184 $v = $db->addQuotes( $v );
185 break;
188 if ( $where === '' ) {
189 $where = "$field >= $v";
190 } else {
191 $where = "$field > $v OR ($field = $v AND ($where))";
194 $i--;
196 $this->addWhere( $where );
199 // Populate the rest of the query
200 $this->addTables( array( $settings['linktable'], 'page' ) );
201 $this->addWhere( "$bl_from = page_id" );
203 if ( $this->getModuleName() === 'redirects' ) {
204 $this->addWhere( "rd_interwiki = $emptyString OR rd_interwiki IS NULL" );
207 $this->addFields( array_keys( $sortby ) );
208 $this->addFields( array( 'bl_namespace' => $bl_namespace, 'bl_title' => $bl_title ) );
209 if ( is_null( $resultPageSet ) ) {
210 $fld_pageid = isset( $prop['pageid'] );
211 $fld_title = isset( $prop['title'] );
212 $fld_redirect = isset( $prop['redirect'] );
214 $this->addFieldsIf( 'page_id', $fld_pageid );
215 $this->addFieldsIf( array( 'page_title', 'page_namespace' ), $fld_title );
216 $this->addFieldsIf( 'page_is_redirect', $fld_redirect );
218 // prop=redirects
219 $fld_fragment = isset( $prop['fragment'] );
220 $this->addFieldsIf( 'rd_fragment', $fld_fragment );
221 } else {
222 $this->addFields( $resultPageSet->getPageTableFields() );
225 $this->addFieldsIf( 'page_namespace', $miser_ns !== null );
227 if ( $hasNS ) {
228 $lb = new LinkBatch( $titles );
229 $this->addWhere( $lb->constructSet( $p, $db ) );
230 } else {
231 $where = array();
232 foreach ( $titles as $t ) {
233 if ( $t->getNamespace() == $bl_namespace ) {
234 $where[] = "$bl_title = " . $db->addQuotes( $t->getDBkey() );
237 $this->addWhere( $db->makeList( $where, LIST_OR ) );
240 if ( $params['show'] !== null ) {
241 // prop=redirects only
242 $show = array_flip( $params['show'] );
243 if ( isset( $show['fragment'] ) && isset( $show['!fragment'] ) ||
244 isset( $show['redirect'] ) && isset( $show['!redirect'] )
246 $this->dieUsageMsg( 'show' );
248 $this->addWhereIf( "rd_fragment != $emptyString", isset( $show['fragment'] ) );
249 $this->addWhereIf(
250 "rd_fragment = $emptyString OR rd_fragment IS NULL",
251 isset( $show['!fragment'] )
253 $this->addWhereIf( array( 'page_is_redirect' => 1 ), isset( $show['redirect'] ) );
254 $this->addWhereIf( array( 'page_is_redirect' => 0 ), isset( $show['!redirect'] ) );
257 // Override any ORDER BY from above with what we calculated earlier.
258 $this->addOption( 'ORDER BY', array_keys( $sortby ) );
260 $this->addOption( 'LIMIT', $params['limit'] + 1 );
262 $res = $this->select( __METHOD__ );
264 if ( is_null( $resultPageSet ) ) {
265 $count = 0;
266 foreach ( $res as $row ) {
267 if ( ++$count > $params['limit'] ) {
268 // We've reached the one extra which shows that
269 // there are additional pages to be had. Stop here...
270 $this->setContinue( $row, $sortby );
271 break;
274 if ( $miser_ns !== null && !in_array( $row->page_namespace, $miser_ns ) ) {
275 // Miser mode namespace check
276 continue;
279 // Get the ID of the current page
280 $id = $map[$row->bl_namespace][$row->bl_title];
282 $vals = array();
283 if ( $fld_pageid ) {
284 $vals['pageid'] = (int)$row->page_id;
286 if ( $fld_title ) {
287 ApiQueryBase::addTitleInfo( $vals,
288 Title::makeTitle( $row->page_namespace, $row->page_title )
291 if ( $fld_fragment && $row->rd_fragment !== null && $row->rd_fragment !== '' ) {
292 $vals['fragment'] = $row->rd_fragment;
294 if ( $fld_redirect ) {
295 $vals['redirect'] = (bool)$row->page_is_redirect;
297 $fit = $this->addPageSubItem( $id, $vals );
298 if ( !$fit ) {
299 $this->setContinue( $row, $sortby );
300 break;
303 } else {
304 $titles = array();
305 $count = 0;
306 foreach ( $res as $row ) {
307 if ( ++$count > $params['limit'] ) {
308 // We've reached the one extra which shows that
309 // there are additional pages to be had. Stop here...
310 $this->setContinue( $row, $sortby );
311 break;
313 $titles[] = Title::makeTitle( $row->page_namespace, $row->page_title );
315 $resultPageSet->populateFromTitles( $titles );
319 private function setContinue( $row, $sortby ) {
320 $cont = array();
321 foreach ( $sortby as $field => $v ) {
322 $cont[] = $row->$field;
324 $this->setContinueEnumParameter( 'continue', join( '|', $cont ) );
327 public function getCacheMode( $params ) {
328 return 'public';
331 public function getAllowedParams() {
332 $settings = self::$settings[$this->getModuleName()];
334 $ret = array(
335 'prop' => array(
336 ApiBase::PARAM_TYPE => array(
337 'pageid',
338 'title',
340 ApiBase::PARAM_ISMULTI => true,
341 ApiBase::PARAM_DFLT => 'pageid|title',
342 ApiBase::PARAM_HELP_MSG_PER_VALUE => array(),
344 'namespace' => array(
345 ApiBase::PARAM_ISMULTI => true,
346 ApiBase::PARAM_TYPE => 'namespace',
348 'show' => null, // Will be filled/removed below
349 'limit' => array(
350 ApiBase::PARAM_DFLT => 10,
351 ApiBase::PARAM_TYPE => 'limit',
352 ApiBase::PARAM_MIN => 1,
353 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
354 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
356 'continue' => array(
357 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
361 if ( empty( $settings['from_namespace'] ) && $this->getConfig()->get( 'MiserMode' ) ) {
362 $ret['namespace'][ApiBase::PARAM_HELP_MSG_APPEND] = array(
363 'api-help-param-limited-in-miser-mode',
367 if ( !empty( $settings['showredirects'] ) ) {
368 $ret['prop'][ApiBase::PARAM_TYPE][] = 'redirect';
369 $ret['prop'][ApiBase::PARAM_DFLT] .= '|redirect';
371 if ( isset( $settings['props'] ) ) {
372 $ret['prop'][ApiBase::PARAM_TYPE] = array_merge(
373 $ret['prop'][ApiBase::PARAM_TYPE], $settings['props']
377 $show = array();
378 if ( !empty( $settings['showredirects'] ) ) {
379 $show[] = 'redirect';
380 $show[] = '!redirect';
382 if ( isset( $settings['show'] ) ) {
383 $show = array_merge( $show, $settings['show'] );
385 if ( $show ) {
386 $ret['show'] = array(
387 ApiBase::PARAM_TYPE => $show,
388 ApiBase::PARAM_ISMULTI => true,
390 } else {
391 unset( $ret['show'] );
394 return $ret;
397 protected function getExamplesMessages() {
398 $settings = self::$settings[$this->getModuleName()];
399 $name = $this->getModuleName();
400 $path = $this->getModulePath();
401 $title = isset( $settings['exampletitle'] ) ? $settings['exampletitle'] : 'Main Page';
402 $etitle = rawurlencode( $title );
404 return array(
405 "action=query&prop={$name}&titles={$etitle}"
406 => "apihelp-$path-example-simple",
407 "action=query&generator={$name}&titles={$etitle}&prop=info"
408 => "apihelp-$path-example-generator",
412 public function getHelpUrls() {
413 $name = ucfirst( $this->getModuleName() );
414 return "https://www.mediawiki.org/wiki/API:{$name}";