Merge "Don't check namespace in SpecialWantedtemplates"
[lhc/web/wiklou.git] / includes / api / ApiQueryBacklinksprop.php
1 <?php
2 /**
3 * API module to handle links table back-queries
4 *
5 * Created on Aug 19, 2014
6 *
7 * Copyright © 2014 Brad Jorsch <bjorsch@wikimedia.org>
8 *
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.
13 *
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.
18 *
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
23 *
24 * @file
25 * @since 1.24
26 */
27
28 /**
29 * This implements prop=redirects, prop=linkshere, prop=catmembers,
30 * prop=transcludedin, and prop=fileusage
31 *
32 * @ingroup API
33 * @since 1.24
34 */
35 class ApiQueryBacklinksprop extends ApiQueryGeneratorBase {
36
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',
45 ),
46 'showredirects' => false,
47 'show' => array(
48 'fragment',
49 '!fragment',
50 ),
51 ),
52 'linkshere' => array(
53 'code' => 'lh',
54 'prefix' => 'pl',
55 'linktable' => 'pagelinks',
56 'from_namespace' => true,
57 'showredirects' => true,
58 ),
59 'transcludedin' => array(
60 'code' => 'ti',
61 'prefix' => 'tl',
62 'linktable' => 'templatelinks',
63 'from_namespace' => true,
64 'showredirects' => true,
65 ),
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,
74 ),
75 );
76
77 public function __construct( ApiQuery $query, $moduleName ) {
78 parent::__construct( $query, $moduleName, self::$settings[$moduleName]['code'] );
79 }
80
81 public function execute() {
82 $this->run();
83 }
84
85 public function executeGenerator( $resultPageSet ) {
86 $this->run( $resultPageSet );
87 }
88
89 /**
90 * @param ApiPageSet $resultPageSet
91 */
92 private function run( ApiPageSet $resultPageSet = null ) {
93 $settings = self::$settings[$this->getModuleName()];
94
95 $db = $this->getDB();
96 $params = $this->extractRequestParams();
97 $prop = array_flip( $params['prop'] );
98 $emptyString = $db->addQuotes( '' );
99
100 $pageSet = $this->getPageSet();
101 $titles = $pageSet->getGoodAndMissingTitles();
102 $map = $pageSet->getGoodAndMissingTitlesByNamespace();
103
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";
113
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 ) );
118 }
119 $bl_from = "{$p}_from";
120
121 if ( !$titles ) {
122 return; // nothing to do
123 }
124
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';
131 }
132 $theTitle = null;
133 foreach ( $map as $nsTitles ) {
134 reset( $nsTitles );
135 $key = key( $nsTitles );
136 if ( $theTitle === null ) {
137 $theTitle = $key;
138 }
139 if ( count( $nsTitles ) > 1 || $key !== $theTitle ) {
140 $sortby[$bl_title] = 'title';
141 break;
142 }
143 }
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';
152 }
153 }
154 }
155 $sortby[$bl_from] = 'int';
156
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;
175
176 case 'title':
177 $cont_title = $v;
178 /* fall through */
179 default:
180 $v = $db->addQuotes( $v );
181 break;
182 }
183
184 if ( $where === '' ) {
185 $where = "$field >= $v";
186 } else {
187 $where = "$field > $v OR ($field = $v AND ($where))";
188 }
189
190 $i--;
191 }
192 $this->addWhere( $where );
193 }
194
195 // Populate the rest of the query
196 $this->addTables( array( $settings['linktable'], 'page' ) );
197 $this->addWhere( "$bl_from = page_id" );
198
199 if ( $this->getModuleName() === 'redirects' ) {
200 $this->addWhere( "rd_interwiki = $emptyString OR rd_interwiki IS NULL" );
201 }
202
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'] );
209
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 );
213
214 // prop=redirects
215 $fld_fragment = isset( $prop['fragment'] );
216 $this->addFieldsIf( 'rd_fragment', $fld_fragment );
217 } else {
218 $this->addFields( $resultPageSet->getPageTableFields() );
219 }
220
221 $this->addFieldsIf( 'page_namespace', $miser_ns !== null );
222
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() );
231 }
232 }
233 $this->addWhere( $db->makeList( $where, LIST_OR ) );
234 }
235
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'] )
241 ) {
242 $this->dieUsageMsg( 'show' );
243 }
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'] )
248 );
249 $this->addWhereIf( array( 'page_is_redirect' => 1 ), isset( $show['redirect'] ) );
250 $this->addWhereIf( array( 'page_is_redirect' => 0 ), isset( $show['!redirect'] ) );
251 }
252
253 // Override any ORDER BY from above with what we calculated earlier.
254 $this->addOption( 'ORDER BY', array_keys( $sortby ) );
255
256 $this->addOption( 'LIMIT', $params['limit'] + 1 );
257
258 $res = $this->select( __METHOD__ );
259
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;
268 }
269
270 if ( $miser_ns !== null && !in_array( $row->page_namespace, $miser_ns ) ) {
271 // Miser mode namespace check
272 continue;
273 }
274
275 // Get the ID of the current page
276 $id = $map[$row->bl_namespace][$row->bl_title];
277
278 $vals = array();
279 if ( $fld_pageid ) {
280 $vals['pageid'] = (int)$row->page_id;
281 }
282 if ( $fld_title ) {
283 ApiQueryBase::addTitleInfo( $vals,
284 Title::makeTitle( $row->page_namespace, $row->page_title )
285 );
286 }
287 if ( $fld_fragment && $row->rd_fragment !== null && $row->rd_fragment !== '' ) {
288 $vals['fragment'] = $row->rd_fragment;
289 }
290 if ( $fld_redirect ) {
291 $vals['redirect'] = (bool)$row->page_is_redirect;
292 }
293 $fit = $this->addPageSubItem( $id, $vals );
294 if ( !$fit ) {
295 $this->setContinue( $row, $sortby );
296 break;
297 }
298 }
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;
308 }
309 $titles[] = Title::makeTitle( $row->page_namespace, $row->page_title );
310 }
311 $resultPageSet->populateFromTitles( $titles );
312 }
313 }
314
315 private function setContinue( $row, $sortby ) {
316 $cont = array();
317 foreach ( $sortby as $field => $v ) {
318 $cont[] = $row->$field;
319 }
320 $this->setContinueEnumParameter( 'continue', join( '|', $cont ) );
321 }
322
323 public function getCacheMode( $params ) {
324 return 'public';
325 }
326
327 public function getAllowedParams() {
328 $settings = self::$settings[$this->getModuleName()];
329
330 $ret = array(
331 'prop' => array(
332 ApiBase::PARAM_TYPE => array(
333 'pageid',
334 'title',
335 ),
336 ApiBase::PARAM_ISMULTI => true,
337 ApiBase::PARAM_DFLT => 'pageid|title',
338 ApiBase::PARAM_HELP_MSG_PER_VALUE => array(),
339 ),
340 'namespace' => array(
341 ApiBase::PARAM_ISMULTI => true,
342 ApiBase::PARAM_TYPE => 'namespace',
343 ),
344 'show' => null, // Will be filled/removed below
345 'limit' => array(
346 ApiBase::PARAM_DFLT => 10,
347 ApiBase::PARAM_TYPE => 'limit',
348 ApiBase::PARAM_MIN => 1,
349 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
350 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
351 ),
352 'continue' => array(
353 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
354 ),
355 );
356
357 if ( empty( $settings['from_namespace'] ) && $this->getConfig()->get( 'MiserMode' ) ) {
358 $ret['namespace'][ApiBase::PARAM_HELP_MSG_APPEND] = array(
359 'api-help-param-limited-in-miser-mode',
360 );
361 }
362
363 if ( !empty( $settings['showredirects'] ) ) {
364 $ret['prop'][ApiBase::PARAM_TYPE][] = 'redirect';
365 $ret['prop'][ApiBase::PARAM_DFLT] .= '|redirect';
366 }
367 if ( isset( $settings['props'] ) ) {
368 $ret['prop'][ApiBase::PARAM_TYPE] = array_merge(
369 $ret['prop'][ApiBase::PARAM_TYPE], $settings['props']
370 );
371 }
372
373 $show = array();
374 if ( !empty( $settings['showredirects'] ) ) {
375 $show[] = 'redirect';
376 $show[] = '!redirect';
377 }
378 if ( isset( $settings['show'] ) ) {
379 $show = array_merge( $show, $settings['show'] );
380 }
381 if ( $show ) {
382 $ret['show'] = array(
383 ApiBase::PARAM_TYPE => $show,
384 ApiBase::PARAM_ISMULTI => true,
385 );
386 } else {
387 unset( $ret['show'] );
388 }
389
390 return $ret;
391 }
392
393 protected function getExamplesMessages() {
394 $settings = self::$settings[$this->getModuleName()];
395 $name = $this->getModuleName();
396 $path = $this->getModulePath();
397 $title = isset( $settings['exampletitle'] ) ? $settings['exampletitle'] : 'Main Page';
398 $etitle = rawurlencode( $title );
399
400 return array(
401 "action=query&prop={$name}&titles={$etitle}"
402 => "apihelp-$path-example-simple",
403 "action=query&generator={$name}&titles={$etitle}&prop=info"
404 => "apihelp-$path-example-generator",
405 );
406 }
407
408 public function getHelpUrls() {
409 $name = ucfirst( $this->getModuleName() );
410 return "https://www.mediawiki.org/wiki/API:{$name}";
411 }
412 }