Run executeGenderCacheFromResultWrapper with titles in prop=linkshere
[lhc/web/wiklou.git] / includes / api / ApiQueryBacklinksprop.php
1 <?php
2 /**
3 * API module to handle links table back-queries
4 *
5 * Copyright © 2014 Wikimedia Foundation and contributors
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
21 *
22 * @file
23 * @since 1.24
24 */
25
26 /**
27 * This implements prop=redirects, prop=linkshere, prop=catmembers,
28 * prop=transcludedin, and prop=fileusage
29 *
30 * @ingroup API
31 * @since 1.24
32 */
33 class ApiQueryBacklinksprop extends ApiQueryGeneratorBase {
34
35 // Data for the various modules implemented by this class
36 private static $settings = [
37 'redirects' => [
38 'code' => 'rd',
39 'prefix' => 'rd',
40 'linktable' => 'redirect',
41 'props' => [
42 'fragment',
43 ],
44 'showredirects' => false,
45 'show' => [
46 'fragment',
47 '!fragment',
48 ],
49 ],
50 'linkshere' => [
51 'code' => 'lh',
52 'prefix' => 'pl',
53 'linktable' => 'pagelinks',
54 'indexes' => [ 'pl_namespace', 'pl_backlinks_namespace' ],
55 'from_namespace' => true,
56 'showredirects' => true,
57 ],
58 'transcludedin' => [
59 'code' => 'ti',
60 'prefix' => 'tl',
61 'linktable' => 'templatelinks',
62 'indexes' => [ 'tl_namespace', 'tl_backlinks_namespace' ],
63 'from_namespace' => true,
64 'showredirects' => true,
65 ],
66 'fileusage' => [
67 'code' => 'fu',
68 'prefix' => 'il',
69 'linktable' => 'imagelinks',
70 'indexes' => [ 'il_to', 'il_backlinks_namespace' ],
71 'from_namespace' => true,
72 'to_namespace' => NS_FILE,
73 'exampletitle' => 'File:Example.jpg',
74 'showredirects' => true,
75 ],
76 ];
77
78 public function __construct( ApiQuery $query, $moduleName ) {
79 parent::__construct( $query, $moduleName, self::$settings[$moduleName]['code'] );
80 }
81
82 public function execute() {
83 $this->run();
84 }
85
86 public function executeGenerator( $resultPageSet ) {
87 $this->run( $resultPageSet );
88 }
89
90 /**
91 * @param ApiPageSet|null $resultPageSet
92 */
93 private function run( ApiPageSet $resultPageSet = null ) {
94 $settings = self::$settings[$this->getModuleName()];
95
96 $db = $this->getDB();
97 $params = $this->extractRequestParams();
98 $prop = array_flip( $params['prop'] );
99 $emptyString = $db->addQuotes( '' );
100
101 $pageSet = $this->getPageSet();
102 $titles = $pageSet->getGoodAndMissingTitles();
103 $map = $pageSet->getGoodAndMissingTitlesByNamespace();
104
105 // Add in special pages, they can theoretically have backlinks too.
106 // (although currently they only do for prop=redirects)
107 foreach ( $pageSet->getSpecialTitles() as $id => $title ) {
108 $titles[] = $title;
109 $map[$title->getNamespace()][$title->getDBkey()] = $id;
110 }
111
112 // Determine our fields to query on
113 $p = $settings['prefix'];
114 $hasNS = !isset( $settings['to_namespace'] );
115 if ( $hasNS ) {
116 $bl_namespace = "{$p}_namespace";
117 $bl_title = "{$p}_title";
118 } else {
119 $bl_namespace = $settings['to_namespace'];
120 $bl_title = "{$p}_to";
121
122 $titles = array_filter( $titles, function ( $t ) use ( $bl_namespace ) {
123 return $t->getNamespace() === $bl_namespace;
124 } );
125 $map = array_intersect_key( $map, [ $bl_namespace => true ] );
126 }
127 $bl_from = "{$p}_from";
128
129 if ( !$titles ) {
130 return; // nothing to do
131 }
132 if ( $params['namespace'] !== null && count( $params['namespace'] ) === 0 ) {
133 return; // nothing to do
134 }
135
136 // Figure out what we're sorting by, and add associated WHERE clauses.
137 // MySQL's query planner screws up if we include a field in ORDER BY
138 // when it's constant in WHERE, so we have to test that for each field.
139 $sortby = [];
140 if ( $hasNS && count( $map ) > 1 ) {
141 $sortby[$bl_namespace] = 'ns';
142 }
143 $theTitle = null;
144 foreach ( $map as $nsTitles ) {
145 reset( $nsTitles );
146 $key = key( $nsTitles );
147 if ( $theTitle === null ) {
148 $theTitle = $key;
149 }
150 if ( count( $nsTitles ) > 1 || $key !== $theTitle ) {
151 $sortby[$bl_title] = 'title';
152 break;
153 }
154 }
155 $miser_ns = null;
156 if ( $params['namespace'] !== null ) {
157 if ( empty( $settings['from_namespace'] ) ) {
158 if ( $this->getConfig()->get( 'MiserMode' ) ) {
159 $miser_ns = $params['namespace'];
160 } else {
161 $this->addWhereFld( 'page_namespace', $params['namespace'] );
162 }
163 } else {
164 $this->addWhereFld( "{$p}_from_namespace", $params['namespace'] );
165 if ( !empty( $settings['from_namespace'] )
166 && $params['namespace'] !== null && count( $params['namespace'] ) > 1
167 ) {
168 $sortby["{$p}_from_namespace"] = 'int';
169 }
170 }
171 }
172 $sortby[$bl_from] = 'int';
173
174 // Now use the $sortby to figure out the continuation
175 if ( !is_null( $params['continue'] ) ) {
176 $cont = explode( '|', $params['continue'] );
177 $this->dieContinueUsageIf( count( $cont ) != count( $sortby ) );
178 $where = '';
179 $i = count( $sortby ) - 1;
180 foreach ( array_reverse( $sortby, true ) as $field => $type ) {
181 $v = $cont[$i];
182 switch ( $type ) {
183 case 'ns':
184 case 'int':
185 $v = (int)$v;
186 $this->dieContinueUsageIf( $v != $cont[$i] );
187 break;
188 default:
189 $v = $db->addQuotes( $v );
190 break;
191 }
192
193 if ( $where === '' ) {
194 $where = "$field >= $v";
195 } else {
196 $where = "$field > $v OR ($field = $v AND ($where))";
197 }
198
199 $i--;
200 }
201 $this->addWhere( $where );
202 }
203
204 // Populate the rest of the query
205 $this->addTables( [ $settings['linktable'], 'page' ] );
206 $this->addWhere( "$bl_from = page_id" );
207
208 if ( $this->getModuleName() === 'redirects' ) {
209 $this->addWhere( "rd_interwiki = $emptyString OR rd_interwiki IS NULL" );
210 }
211
212 $this->addFields( array_keys( $sortby ) );
213 $this->addFields( [ 'bl_namespace' => $bl_namespace, 'bl_title' => $bl_title ] );
214 if ( is_null( $resultPageSet ) ) {
215 $fld_pageid = isset( $prop['pageid'] );
216 $fld_title = isset( $prop['title'] );
217 $fld_redirect = isset( $prop['redirect'] );
218
219 $this->addFieldsIf( 'page_id', $fld_pageid );
220 $this->addFieldsIf( [ 'page_title', 'page_namespace' ], $fld_title );
221 $this->addFieldsIf( 'page_is_redirect', $fld_redirect );
222
223 // prop=redirects
224 $fld_fragment = isset( $prop['fragment'] );
225 $this->addFieldsIf( 'rd_fragment', $fld_fragment );
226 } else {
227 $this->addFields( $resultPageSet->getPageTableFields() );
228 }
229
230 $this->addFieldsIf( 'page_namespace', $miser_ns !== null );
231
232 if ( $hasNS ) {
233 // Can't use LinkBatch because it throws away Special titles.
234 // And we already have the needed data structure anyway.
235 $this->addWhere( $db->makeWhereFrom2d( $map, $bl_namespace, $bl_title ) );
236 } else {
237 $where = [];
238 foreach ( $titles as $t ) {
239 if ( $t->getNamespace() == $bl_namespace ) {
240 $where[] = "$bl_title = " . $db->addQuotes( $t->getDBkey() );
241 }
242 }
243 $this->addWhere( $db->makeList( $where, LIST_OR ) );
244 }
245
246 if ( $params['show'] !== null ) {
247 // prop=redirects only
248 $show = array_flip( $params['show'] );
249 if ( isset( $show['fragment'] ) && isset( $show['!fragment'] ) ||
250 isset( $show['redirect'] ) && isset( $show['!redirect'] )
251 ) {
252 $this->dieWithError( 'apierror-show' );
253 }
254 $this->addWhereIf( "rd_fragment != $emptyString", isset( $show['fragment'] ) );
255 $this->addWhereIf(
256 "rd_fragment = $emptyString OR rd_fragment IS NULL",
257 isset( $show['!fragment'] )
258 );
259 $this->addWhereIf( [ 'page_is_redirect' => 1 ], isset( $show['redirect'] ) );
260 $this->addWhereIf( [ 'page_is_redirect' => 0 ], isset( $show['!redirect'] ) );
261 }
262
263 // Override any ORDER BY from above with what we calculated earlier.
264 $this->addOption( 'ORDER BY', array_keys( $sortby ) );
265
266 // MySQL's optimizer chokes if we have too many values in "$bl_title IN
267 // (...)" and chooses the wrong index, so specify the correct index to
268 // use for the query. See T139056 for details.
269 if ( !empty( $settings['indexes'] ) ) {
270 list( $idxNoFromNS, $idxWithFromNS ) = $settings['indexes'];
271 if ( $params['namespace'] !== null && !empty( $settings['from_namespace'] ) ) {
272 $this->addOption( 'USE INDEX', [ $settings['linktable'] => $idxWithFromNS ] );
273 } else {
274 $this->addOption( 'USE INDEX', [ $settings['linktable'] => $idxNoFromNS ] );
275 }
276 }
277
278 // MySQL (or at least 5.5.5-10.0.23-MariaDB) chooses a really bad query
279 // plan if it thinks there will be more matching rows in the linktable
280 // than are in page. Use STRAIGHT_JOIN here to force it to use the
281 // intended, fast plan. See T145079 for details.
282 $this->addOption( 'STRAIGHT_JOIN' );
283
284 $this->addOption( 'LIMIT', $params['limit'] + 1 );
285
286 $res = $this->select( __METHOD__ );
287
288 if ( is_null( $resultPageSet ) ) {
289 if ( $fld_title ) {
290 $this->executeGenderCacheFromResultWrapper( $res, __METHOD__ );
291 }
292
293 $count = 0;
294 foreach ( $res as $row ) {
295 if ( ++$count > $params['limit'] ) {
296 // We've reached the one extra which shows that
297 // there are additional pages to be had. Stop here...
298 $this->setContinue( $row, $sortby );
299 break;
300 }
301
302 if ( $miser_ns !== null && !in_array( $row->page_namespace, $miser_ns ) ) {
303 // Miser mode namespace check
304 continue;
305 }
306
307 // Get the ID of the current page
308 $id = $map[$row->bl_namespace][$row->bl_title];
309
310 $vals = [];
311 if ( $fld_pageid ) {
312 $vals['pageid'] = (int)$row->page_id;
313 }
314 if ( $fld_title ) {
315 ApiQueryBase::addTitleInfo( $vals,
316 Title::makeTitle( $row->page_namespace, $row->page_title )
317 );
318 }
319 if ( $fld_fragment && $row->rd_fragment !== null && $row->rd_fragment !== '' ) {
320 $vals['fragment'] = $row->rd_fragment;
321 }
322 if ( $fld_redirect ) {
323 $vals['redirect'] = (bool)$row->page_is_redirect;
324 }
325 $fit = $this->addPageSubItem( $id, $vals );
326 if ( !$fit ) {
327 $this->setContinue( $row, $sortby );
328 break;
329 }
330 }
331 } else {
332 $titles = [];
333 $count = 0;
334 foreach ( $res as $row ) {
335 if ( ++$count > $params['limit'] ) {
336 // We've reached the one extra which shows that
337 // there are additional pages to be had. Stop here...
338 $this->setContinue( $row, $sortby );
339 break;
340 }
341
342 if ( $miser_ns !== null && !in_array( $row->page_namespace, $miser_ns ) ) {
343 // Miser mode namespace check
344 continue;
345 }
346
347 $titles[] = Title::makeTitle( $row->page_namespace, $row->page_title );
348 }
349 $resultPageSet->populateFromTitles( $titles );
350 }
351 }
352
353 private function setContinue( $row, $sortby ) {
354 $cont = [];
355 foreach ( $sortby as $field => $v ) {
356 $cont[] = $row->$field;
357 }
358 $this->setContinueEnumParameter( 'continue', implode( '|', $cont ) );
359 }
360
361 public function getCacheMode( $params ) {
362 return 'public';
363 }
364
365 public function getAllowedParams() {
366 $settings = self::$settings[$this->getModuleName()];
367
368 $ret = [
369 'prop' => [
370 ApiBase::PARAM_TYPE => [
371 'pageid',
372 'title',
373 ],
374 ApiBase::PARAM_ISMULTI => true,
375 ApiBase::PARAM_DFLT => 'pageid|title',
376 ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
377 ],
378 'namespace' => [
379 ApiBase::PARAM_ISMULTI => true,
380 ApiBase::PARAM_TYPE => 'namespace',
381 ],
382 'show' => null, // Will be filled/removed below
383 'limit' => [
384 ApiBase::PARAM_DFLT => 10,
385 ApiBase::PARAM_TYPE => 'limit',
386 ApiBase::PARAM_MIN => 1,
387 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
388 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
389 ],
390 'continue' => [
391 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
392 ],
393 ];
394
395 if ( empty( $settings['from_namespace'] ) && $this->getConfig()->get( 'MiserMode' ) ) {
396 $ret['namespace'][ApiBase::PARAM_HELP_MSG_APPEND] = [
397 'api-help-param-limited-in-miser-mode',
398 ];
399 }
400
401 if ( !empty( $settings['showredirects'] ) ) {
402 $ret['prop'][ApiBase::PARAM_TYPE][] = 'redirect';
403 $ret['prop'][ApiBase::PARAM_DFLT] .= '|redirect';
404 }
405 if ( isset( $settings['props'] ) ) {
406 $ret['prop'][ApiBase::PARAM_TYPE] = array_merge(
407 $ret['prop'][ApiBase::PARAM_TYPE], $settings['props']
408 );
409 }
410
411 $show = [];
412 if ( !empty( $settings['showredirects'] ) ) {
413 $show[] = 'redirect';
414 $show[] = '!redirect';
415 }
416 if ( isset( $settings['show'] ) ) {
417 $show = array_merge( $show, $settings['show'] );
418 }
419 if ( $show ) {
420 $ret['show'] = [
421 ApiBase::PARAM_TYPE => $show,
422 ApiBase::PARAM_ISMULTI => true,
423 ];
424 } else {
425 unset( $ret['show'] );
426 }
427
428 return $ret;
429 }
430
431 protected function getExamplesMessages() {
432 $settings = self::$settings[$this->getModuleName()];
433 $name = $this->getModuleName();
434 $path = $this->getModulePath();
435 $title = $settings['exampletitle'] ?? 'Main Page';
436 $etitle = rawurlencode( $title );
437
438 return [
439 "action=query&prop={$name}&titles={$etitle}"
440 => "apihelp-$path-example-simple",
441 "action=query&generator={$name}&titles={$etitle}&prop=info"
442 => "apihelp-$path-example-generator",
443 ];
444 }
445
446 public function getHelpUrls() {
447 $name = ucfirst( $this->getModuleName() );
448 return "https://www.mediawiki.org/wiki/Special:MyLanguage/API:{$name}";
449 }
450 }