Merge "Improve how slashes are stripped from filenames"
[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 = [
39 'redirects' => [
40 'code' => 'rd',
41 'prefix' => 'rd',
42 'linktable' => 'redirect',
43 'props' => [
44 'fragment',
45 ],
46 'showredirects' => false,
47 'show' => [
48 'fragment',
49 '!fragment',
50 ],
51 ],
52 'linkshere' => [
53 'code' => 'lh',
54 'prefix' => 'pl',
55 'linktable' => 'pagelinks',
56 'from_namespace' => true,
57 'showredirects' => true,
58 ],
59 'transcludedin' => [
60 'code' => 'ti',
61 'prefix' => 'tl',
62 'linktable' => 'templatelinks',
63 'from_namespace' => true,
64 'showredirects' => true,
65 ],
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,
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, [ $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 = [];
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'] ) ) {
147 if ( $this->getConfig()->get( 'MiserMode' ) ) {
148 $miser_ns = $params['namespace'];
149 } else {
150 $this->addWhereFld( 'page_namespace', $params['namespace'] );
151 }
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';
156 }
157 }
158 }
159 $sortby[$bl_from] = 'int';
160
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;
178 }
179
180 if ( $where === '' ) {
181 $where = "$field >= $v";
182 } else {
183 $where = "$field > $v OR ($field = $v AND ($where))";
184 }
185
186 $i--;
187 }
188 $this->addWhere( $where );
189 }
190
191 // Populate the rest of the query
192 $this->addTables( [ $settings['linktable'], 'page' ] );
193 $this->addWhere( "$bl_from = page_id" );
194
195 if ( $this->getModuleName() === 'redirects' ) {
196 $this->addWhere( "rd_interwiki = $emptyString OR rd_interwiki IS NULL" );
197 }
198
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'] );
205
206 $this->addFieldsIf( 'page_id', $fld_pageid );
207 $this->addFieldsIf( [ 'page_title', 'page_namespace' ], $fld_title );
208 $this->addFieldsIf( 'page_is_redirect', $fld_redirect );
209
210 // prop=redirects
211 $fld_fragment = isset( $prop['fragment'] );
212 $this->addFieldsIf( 'rd_fragment', $fld_fragment );
213 } else {
214 $this->addFields( $resultPageSet->getPageTableFields() );
215 }
216
217 $this->addFieldsIf( 'page_namespace', $miser_ns !== null );
218
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() );
227 }
228 }
229 $this->addWhere( $db->makeList( $where, LIST_OR ) );
230 }
231
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'] )
237 ) {
238 $this->dieUsageMsg( 'show' );
239 }
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'] )
244 );
245 $this->addWhereIf( [ 'page_is_redirect' => 1 ], isset( $show['redirect'] ) );
246 $this->addWhereIf( [ 'page_is_redirect' => 0 ], isset( $show['!redirect'] ) );
247 }
248
249 // Override any ORDER BY from above with what we calculated earlier.
250 $this->addOption( 'ORDER BY', array_keys( $sortby ) );
251
252 $this->addOption( 'LIMIT', $params['limit'] + 1 );
253
254 $res = $this->select( __METHOD__ );
255
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;
264 }
265
266 if ( $miser_ns !== null && !in_array( $row->page_namespace, $miser_ns ) ) {
267 // Miser mode namespace check
268 continue;
269 }
270
271 // Get the ID of the current page
272 $id = $map[$row->bl_namespace][$row->bl_title];
273
274 $vals = [];
275 if ( $fld_pageid ) {
276 $vals['pageid'] = (int)$row->page_id;
277 }
278 if ( $fld_title ) {
279 ApiQueryBase::addTitleInfo( $vals,
280 Title::makeTitle( $row->page_namespace, $row->page_title )
281 );
282 }
283 if ( $fld_fragment && $row->rd_fragment !== null && $row->rd_fragment !== '' ) {
284 $vals['fragment'] = $row->rd_fragment;
285 }
286 if ( $fld_redirect ) {
287 $vals['redirect'] = (bool)$row->page_is_redirect;
288 }
289 $fit = $this->addPageSubItem( $id, $vals );
290 if ( !$fit ) {
291 $this->setContinue( $row, $sortby );
292 break;
293 }
294 }
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;
304 }
305 $titles[] = Title::makeTitle( $row->page_namespace, $row->page_title );
306 }
307 $resultPageSet->populateFromTitles( $titles );
308 }
309 }
310
311 private function setContinue( $row, $sortby ) {
312 $cont = [];
313 foreach ( $sortby as $field => $v ) {
314 $cont[] = $row->$field;
315 }
316 $this->setContinueEnumParameter( 'continue', implode( '|', $cont ) );
317 }
318
319 public function getCacheMode( $params ) {
320 return 'public';
321 }
322
323 public function getAllowedParams() {
324 $settings = self::$settings[$this->getModuleName()];
325
326 $ret = [
327 'prop' => [
328 ApiBase::PARAM_TYPE => [
329 'pageid',
330 'title',
331 ],
332 ApiBase::PARAM_ISMULTI => true,
333 ApiBase::PARAM_DFLT => 'pageid|title',
334 ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
335 ],
336 'namespace' => [
337 ApiBase::PARAM_ISMULTI => true,
338 ApiBase::PARAM_TYPE => 'namespace',
339 ],
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
347 ],
348 'continue' => [
349 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
350 ],
351 ];
352
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',
356 ];
357 }
358
359 if ( !empty( $settings['showredirects'] ) ) {
360 $ret['prop'][ApiBase::PARAM_TYPE][] = 'redirect';
361 $ret['prop'][ApiBase::PARAM_DFLT] .= '|redirect';
362 }
363 if ( isset( $settings['props'] ) ) {
364 $ret['prop'][ApiBase::PARAM_TYPE] = array_merge(
365 $ret['prop'][ApiBase::PARAM_TYPE], $settings['props']
366 );
367 }
368
369 $show = [];
370 if ( !empty( $settings['showredirects'] ) ) {
371 $show[] = 'redirect';
372 $show[] = '!redirect';
373 }
374 if ( isset( $settings['show'] ) ) {
375 $show = array_merge( $show, $settings['show'] );
376 }
377 if ( $show ) {
378 $ret['show'] = [
379 ApiBase::PARAM_TYPE => $show,
380 ApiBase::PARAM_ISMULTI => true,
381 ];
382 } else {
383 unset( $ret['show'] );
384 }
385
386 return $ret;
387 }
388
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 );
395
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",
401 ];
402 }
403
404 public function getHelpUrls() {
405 $name = ucfirst( $this->getModuleName() );
406 return "https://www.mediawiki.org/wiki/API:{$name}";
407 }
408 }