6c1eb0f48913a5b5e5810a907f49e69c6312e27f
[lhc/web/wiklou.git] / includes / api / ApiQueryBacklinks.php
1 <?php
2 /**
3 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 /**
24 * This is a three-in-one module to query:
25 * * backlinks - links pointing to the given page,
26 * * embeddedin - what pages transclude the given page within themselves,
27 * * imageusage - what pages use the given image
28 *
29 * @ingroup API
30 */
31 class ApiQueryBacklinks extends ApiQueryGeneratorBase {
32
33 /**
34 * @var Title
35 */
36 private $rootTitle;
37
38 private $params;
39 /** @var array */
40 private $cont;
41 private $redirect;
42 private $bl_ns, $bl_from, $bl_from_ns, $bl_table, $bl_code, $bl_title, $bl_fields, $hasNS;
43
44 /**
45 * Maps ns and title to pageid
46 *
47 * @var array
48 */
49 private $pageMap = [];
50 private $resultArr;
51
52 private $redirTitles = [];
53 private $continueStr = null;
54
55 // output element name, database column field prefix, database table
56 private $backlinksSettings = [
57 'backlinks' => [
58 'code' => 'bl',
59 'prefix' => 'pl',
60 'linktbl' => 'pagelinks',
61 'helpurl' => 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Backlinks',
62 ],
63 'embeddedin' => [
64 'code' => 'ei',
65 'prefix' => 'tl',
66 'linktbl' => 'templatelinks',
67 'helpurl' => 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Embeddedin',
68 ],
69 'imageusage' => [
70 'code' => 'iu',
71 'prefix' => 'il',
72 'linktbl' => 'imagelinks',
73 'helpurl' => 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Imageusage',
74 ]
75 ];
76
77 public function __construct( ApiQuery $query, $moduleName ) {
78 $settings = $this->backlinksSettings[$moduleName];
79 $prefix = $settings['prefix'];
80 $code = $settings['code'];
81 $this->resultArr = [];
82
83 parent::__construct( $query, $moduleName, $code );
84 $this->bl_ns = $prefix . '_namespace';
85 $this->bl_from = $prefix . '_from';
86 $this->bl_from_ns = $prefix . '_from_namespace';
87 $this->bl_table = $settings['linktbl'];
88 $this->bl_code = $code;
89 $this->helpUrl = $settings['helpurl'];
90
91 $this->hasNS = $moduleName !== 'imageusage';
92 if ( $this->hasNS ) {
93 $this->bl_title = $prefix . '_title';
94 $this->bl_fields = [
95 $this->bl_ns,
96 $this->bl_title
97 ];
98 } else {
99 $this->bl_title = $prefix . '_to';
100 $this->bl_fields = [
101 $this->bl_title
102 ];
103 }
104 }
105
106 public function execute() {
107 $this->run();
108 }
109
110 public function getCacheMode( $params ) {
111 return 'public';
112 }
113
114 public function executeGenerator( $resultPageSet ) {
115 $this->run( $resultPageSet );
116 }
117
118 /**
119 * @param ApiPageSet $resultPageSet
120 * @return void
121 */
122 private function runFirstQuery( $resultPageSet = null ) {
123 $this->addTables( [ $this->bl_table, 'page' ] );
124 $this->addWhere( "{$this->bl_from}=page_id" );
125 if ( is_null( $resultPageSet ) ) {
126 $this->addFields( [ 'page_id', 'page_title', 'page_namespace' ] );
127 } else {
128 $this->addFields( $resultPageSet->getPageTableFields() );
129 }
130 $this->addFields( [ 'page_is_redirect', 'from_ns' => 'page_namespace' ] );
131
132 $this->addWhereFld( $this->bl_title, $this->rootTitle->getDBkey() );
133 if ( $this->hasNS ) {
134 $this->addWhereFld( $this->bl_ns, $this->rootTitle->getNamespace() );
135 }
136 $this->addWhereFld( $this->bl_from_ns, $this->params['namespace'] );
137
138 if ( count( $this->cont ) >= 2 ) {
139 $op = $this->params['dir'] == 'descending' ? '<' : '>';
140 if ( $this->params['namespace'] !== null && count( $this->params['namespace'] ) > 1 ) {
141 $this->addWhere(
142 "{$this->bl_from_ns} $op {$this->cont[0]} OR " .
143 "({$this->bl_from_ns} = {$this->cont[0]} AND " .
144 "{$this->bl_from} $op= {$this->cont[1]})"
145 );
146 } else {
147 $this->addWhere( "{$this->bl_from} $op= {$this->cont[1]}" );
148 }
149 }
150
151 if ( $this->params['filterredir'] == 'redirects' ) {
152 $this->addWhereFld( 'page_is_redirect', 1 );
153 } elseif ( $this->params['filterredir'] == 'nonredirects' && !$this->redirect ) {
154 // T24245 - Check for !redirect, as filtering nonredirects, when
155 // getting what links to them is contradictory
156 $this->addWhereFld( 'page_is_redirect', 0 );
157 }
158
159 $this->addOption( 'LIMIT', $this->params['limit'] + 1 );
160 $sort = ( $this->params['dir'] == 'descending' ? ' DESC' : '' );
161 $orderBy = [];
162 if ( $this->params['namespace'] !== null && count( $this->params['namespace'] ) > 1 ) {
163 $orderBy[] = $this->bl_from_ns . $sort;
164 }
165 $orderBy[] = $this->bl_from . $sort;
166 $this->addOption( 'ORDER BY', $orderBy );
167 $this->addOption( 'STRAIGHT_JOIN' );
168
169 $res = $this->select( __METHOD__ );
170 $count = 0;
171 foreach ( $res as $row ) {
172 if ( ++$count > $this->params['limit'] ) {
173 // We've reached the one extra which shows that there are
174 // additional pages to be had. Stop here...
175 // Continue string may be overridden at a later step
176 $this->continueStr = "{$row->from_ns}|{$row->page_id}";
177 break;
178 }
179
180 // Fill in continuation fields for later steps
181 if ( count( $this->cont ) < 2 ) {
182 $this->cont[] = $row->from_ns;
183 $this->cont[] = $row->page_id;
184 }
185
186 $this->pageMap[$row->page_namespace][$row->page_title] = $row->page_id;
187 $t = Title::makeTitle( $row->page_namespace, $row->page_title );
188 if ( $row->page_is_redirect ) {
189 $this->redirTitles[] = $t;
190 }
191
192 if ( is_null( $resultPageSet ) ) {
193 $a = [ 'pageid' => (int)$row->page_id ];
194 ApiQueryBase::addTitleInfo( $a, $t );
195 if ( $row->page_is_redirect ) {
196 $a['redirect'] = true;
197 }
198 // Put all the results in an array first
199 $this->resultArr[$a['pageid']] = $a;
200 } else {
201 $resultPageSet->processDbRow( $row );
202 }
203 }
204 }
205
206 /**
207 * @param ApiPageSet $resultPageSet
208 * @return void
209 */
210 private function runSecondQuery( $resultPageSet = null ) {
211 $db = $this->getDB();
212 $this->addTables( [ 'page', $this->bl_table ] );
213 $this->addWhere( "{$this->bl_from}=page_id" );
214
215 if ( is_null( $resultPageSet ) ) {
216 $this->addFields( [ 'page_id', 'page_title', 'page_namespace', 'page_is_redirect' ] );
217 } else {
218 $this->addFields( $resultPageSet->getPageTableFields() );
219 }
220
221 $this->addFields( [ $this->bl_title, 'from_ns' => 'page_namespace' ] );
222 if ( $this->hasNS ) {
223 $this->addFields( $this->bl_ns );
224 }
225
226 // We can't use LinkBatch here because $this->hasNS may be false
227 $titleWhere = [];
228 $allRedirNs = [];
229 $allRedirDBkey = [];
230 /** @var Title $t */
231 foreach ( $this->redirTitles as $t ) {
232 $redirNs = $t->getNamespace();
233 $redirDBkey = $t->getDBkey();
234 $titleWhere[] = "{$this->bl_title} = " . $db->addQuotes( $redirDBkey ) .
235 ( $this->hasNS ? " AND {$this->bl_ns} = {$redirNs}" : '' );
236 $allRedirNs[$redirNs] = true;
237 $allRedirDBkey[$redirDBkey] = true;
238 }
239 $this->addWhere( $db->makeList( $titleWhere, LIST_OR ) );
240 $this->addWhereFld( 'page_namespace', $this->params['namespace'] );
241
242 if ( count( $this->cont ) >= 6 ) {
243 $op = $this->params['dir'] == 'descending' ? '<' : '>';
244
245 $where = "{$this->bl_from} $op= {$this->cont[5]}";
246 // Don't bother with namespace, title, or from_namespace if it's
247 // otherwise constant in the where clause.
248 if ( $this->params['namespace'] !== null && count( $this->params['namespace'] ) > 1 ) {
249 $where = "{$this->bl_from_ns} $op {$this->cont[4]} OR " .
250 "({$this->bl_from_ns} = {$this->cont[4]} AND ($where))";
251 }
252 if ( count( $allRedirDBkey ) > 1 ) {
253 $title = $db->addQuotes( $this->cont[3] );
254 $where = "{$this->bl_title} $op $title OR " .
255 "({$this->bl_title} = $title AND ($where))";
256 }
257 if ( $this->hasNS && count( $allRedirNs ) > 1 ) {
258 $where = "{$this->bl_ns} $op {$this->cont[2]} OR " .
259 "({$this->bl_ns} = {$this->cont[2]} AND ($where))";
260 }
261
262 $this->addWhere( $where );
263 }
264 if ( $this->params['filterredir'] == 'redirects' ) {
265 $this->addWhereFld( 'page_is_redirect', 1 );
266 } elseif ( $this->params['filterredir'] == 'nonredirects' ) {
267 $this->addWhereFld( 'page_is_redirect', 0 );
268 }
269
270 $this->addOption( 'LIMIT', $this->params['limit'] + 1 );
271 $orderBy = [];
272 $sort = ( $this->params['dir'] == 'descending' ? ' DESC' : '' );
273 // Don't order by namespace/title/from_namespace if it's constant in the WHERE clause
274 if ( $this->hasNS && count( $allRedirNs ) > 1 ) {
275 $orderBy[] = $this->bl_ns . $sort;
276 }
277 if ( count( $allRedirDBkey ) > 1 ) {
278 $orderBy[] = $this->bl_title . $sort;
279 }
280 if ( $this->params['namespace'] !== null && count( $this->params['namespace'] ) > 1 ) {
281 $orderBy[] = $this->bl_from_ns . $sort;
282 }
283 $orderBy[] = $this->bl_from . $sort;
284 $this->addOption( 'ORDER BY', $orderBy );
285 $this->addOption( 'USE INDEX', [ 'page' => 'PRIMARY' ] );
286
287 $res = $this->select( __METHOD__ );
288 $count = 0;
289 foreach ( $res as $row ) {
290 $ns = $this->hasNS ? $row->{$this->bl_ns} : NS_FILE;
291
292 if ( ++$count > $this->params['limit'] ) {
293 // We've reached the one extra which shows that there are
294 // additional pages to be had. Stop here...
295 // Note we must keep the parameters for the first query constant
296 // This may be overridden at a later step
297 $title = $row->{$this->bl_title};
298 $this->continueStr = implode( '|', array_slice( $this->cont, 0, 2 ) ) .
299 "|$ns|$title|{$row->from_ns}|{$row->page_id}";
300 break;
301 }
302
303 // Fill in continuation fields for later steps
304 if ( count( $this->cont ) < 6 ) {
305 $this->cont[] = $ns;
306 $this->cont[] = $row->{$this->bl_title};
307 $this->cont[] = $row->from_ns;
308 $this->cont[] = $row->page_id;
309 }
310
311 if ( is_null( $resultPageSet ) ) {
312 $a = [ 'pageid' => (int)$row->page_id ];
313 ApiQueryBase::addTitleInfo( $a, Title::makeTitle( $row->page_namespace, $row->page_title ) );
314 if ( $row->page_is_redirect ) {
315 $a['redirect'] = true;
316 }
317 $parentID = $this->pageMap[$ns][$row->{$this->bl_title}];
318 // Put all the results in an array first
319 $this->resultArr[$parentID]['redirlinks'][$row->page_id] = $a;
320 } else {
321 $resultPageSet->processDbRow( $row );
322 }
323 }
324 }
325
326 /**
327 * @param ApiPageSet $resultPageSet
328 * @return void
329 * @suppress PhanTypeInvalidDimOffset
330 */
331 private function run( $resultPageSet = null ) {
332 $this->params = $this->extractRequestParams( false );
333 $this->redirect = isset( $this->params['redirect'] ) && $this->params['redirect'];
334 $userMax = ( $this->redirect ? ApiBase::LIMIT_BIG1 / 2 : ApiBase::LIMIT_BIG1 );
335 $botMax = ( $this->redirect ? ApiBase::LIMIT_BIG2 / 2 : ApiBase::LIMIT_BIG2 );
336
337 $result = $this->getResult();
338
339 if ( $this->params['limit'] == 'max' ) {
340 $this->params['limit'] = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
341 $result->addParsedLimit( $this->getModuleName(), $this->params['limit'] );
342 } else {
343 $this->params['limit'] = (int)$this->params['limit'];
344 $this->validateLimit( 'limit', $this->params['limit'], 1, $userMax, $botMax );
345 }
346
347 $this->rootTitle = $this->getTitleFromTitleOrPageId( $this->params );
348
349 // only image titles are allowed for the root in imageinfo mode
350 if ( !$this->hasNS && $this->rootTitle->getNamespace() !== NS_FILE ) {
351 $this->dieWithError(
352 [ 'apierror-imageusage-badtitle', $this->getModuleName() ],
353 'bad_image_title'
354 );
355 }
356
357 // Parse and validate continuation parameter
358 $this->cont = [];
359 if ( $this->params['continue'] !== null ) {
360 $cont = explode( '|', $this->params['continue'] );
361
362 switch ( count( $cont ) ) {
363 case 8:
364 // redirect page ID for result adding
365 $this->cont[7] = (int)$cont[7];
366 $this->dieContinueUsageIf( $cont[7] !== (string)$this->cont[7] );
367
368 /* Fall through */
369
370 case 7:
371 // top-level page ID for result adding
372 $this->cont[6] = (int)$cont[6];
373 $this->dieContinueUsageIf( $cont[6] !== (string)$this->cont[6] );
374
375 /* Fall through */
376
377 case 6:
378 // ns for 2nd query (even for imageusage)
379 $this->cont[2] = (int)$cont[2];
380 $this->dieContinueUsageIf( $cont[2] !== (string)$this->cont[2] );
381
382 // title for 2nd query
383 $this->cont[3] = $cont[3];
384
385 // from_ns for 2nd query
386 $this->cont[4] = (int)$cont[4];
387 $this->dieContinueUsageIf( $cont[4] !== (string)$this->cont[4] );
388
389 // from_id for 1st query
390 $this->cont[5] = (int)$cont[5];
391 $this->dieContinueUsageIf( $cont[5] !== (string)$this->cont[5] );
392
393 /* Fall through */
394
395 case 2:
396 // from_ns for 1st query
397 $this->cont[0] = (int)$cont[0];
398 $this->dieContinueUsageIf( $cont[0] !== (string)$this->cont[0] );
399
400 // from_id for 1st query
401 $this->cont[1] = (int)$cont[1];
402 $this->dieContinueUsageIf( $cont[1] !== (string)$this->cont[1] );
403
404 break;
405
406 default:
407 $this->dieContinueUsageIf( true );
408 }
409
410 ksort( $this->cont );
411 }
412
413 $this->runFirstQuery( $resultPageSet );
414 if ( $this->redirect && count( $this->redirTitles ) ) {
415 $this->resetQueryParams();
416 $this->runSecondQuery( $resultPageSet );
417 }
418
419 // Fill in any missing fields in case it's needed below
420 $this->cont += [ 0, 0, 0, '', 0, 0, 0 ];
421
422 if ( is_null( $resultPageSet ) ) {
423 // Try to add the result data in one go and pray that it fits
424 $code = $this->bl_code;
425 $data = array_map( function ( $arr ) use ( $code ) {
426 if ( isset( $arr['redirlinks'] ) ) {
427 $arr['redirlinks'] = array_values( $arr['redirlinks'] );
428 ApiResult::setIndexedTagName( $arr['redirlinks'], $code );
429 }
430 return $arr;
431 }, array_values( $this->resultArr ) );
432 $fit = $result->addValue( 'query', $this->getModuleName(), $data );
433 if ( !$fit ) {
434 // It didn't fit. Add elements one by one until the
435 // result is full.
436 ksort( $this->resultArr );
437 if ( count( $this->cont ) >= 7 ) {
438 $startAt = $this->cont[6];
439 } else {
440 reset( $this->resultArr );
441 $startAt = key( $this->resultArr );
442 }
443 $idx = 0;
444 foreach ( $this->resultArr as $pageID => $arr ) {
445 if ( $pageID < $startAt ) {
446 continue;
447 }
448
449 // Add the basic entry without redirlinks first
450 $fit = $result->addValue(
451 [ 'query', $this->getModuleName() ],
452 $idx, array_diff_key( $arr, [ 'redirlinks' => '' ] ) );
453 if ( !$fit ) {
454 $this->continueStr = implode( '|', array_slice( $this->cont, 0, 6 ) ) .
455 "|$pageID";
456 break;
457 }
458
459 $hasRedirs = false;
460 $redirLinks = isset( $arr['redirlinks'] ) ? (array)$arr['redirlinks'] : [];
461 ksort( $redirLinks );
462 if ( count( $this->cont ) >= 8 && $pageID == $startAt ) {
463 $redirStartAt = $this->cont[7];
464 } else {
465 reset( $redirLinks );
466 $redirStartAt = key( $redirLinks );
467 }
468 foreach ( $redirLinks as $key => $redir ) {
469 if ( $key < $redirStartAt ) {
470 continue;
471 }
472
473 $fit = $result->addValue(
474 [ 'query', $this->getModuleName(), $idx, 'redirlinks' ],
475 null, $redir );
476 if ( !$fit ) {
477 $this->continueStr = implode( '|', array_slice( $this->cont, 0, 6 ) ) .
478 "|$pageID|$key";
479 break;
480 }
481 $hasRedirs = true;
482 }
483 if ( $hasRedirs ) {
484 $result->addIndexedTagName(
485 [ 'query', $this->getModuleName(), $idx, 'redirlinks' ],
486 $this->bl_code );
487 }
488 if ( !$fit ) {
489 break;
490 }
491
492 $idx++;
493 }
494 }
495
496 $result->addIndexedTagName(
497 [ 'query', $this->getModuleName() ],
498 $this->bl_code
499 );
500 }
501 if ( !is_null( $this->continueStr ) ) {
502 $this->setContinueEnumParameter( 'continue', $this->continueStr );
503 }
504 }
505
506 public function getAllowedParams() {
507 $retval = [
508 'title' => [
509 ApiBase::PARAM_TYPE => 'string',
510 ],
511 'pageid' => [
512 ApiBase::PARAM_TYPE => 'integer',
513 ],
514 'continue' => [
515 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
516 ],
517 'namespace' => [
518 ApiBase::PARAM_ISMULTI => true,
519 ApiBase::PARAM_TYPE => 'namespace'
520 ],
521 'dir' => [
522 ApiBase::PARAM_DFLT => 'ascending',
523 ApiBase::PARAM_TYPE => [
524 'ascending',
525 'descending'
526 ]
527 ],
528 'filterredir' => [
529 ApiBase::PARAM_DFLT => 'all',
530 ApiBase::PARAM_TYPE => [
531 'all',
532 'redirects',
533 'nonredirects'
534 ]
535 ],
536 'limit' => [
537 ApiBase::PARAM_DFLT => 10,
538 ApiBase::PARAM_TYPE => 'limit',
539 ApiBase::PARAM_MIN => 1,
540 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
541 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
542 ]
543 ];
544 if ( $this->getModuleName() == 'embeddedin' ) {
545 return $retval;
546 }
547 $retval['redirect'] = false;
548
549 return $retval;
550 }
551
552 protected function getExamplesMessages() {
553 static $examples = [
554 'backlinks' => [
555 'action=query&list=backlinks&bltitle=Main%20Page'
556 => 'apihelp-query+backlinks-example-simple',
557 'action=query&generator=backlinks&gbltitle=Main%20Page&prop=info'
558 => 'apihelp-query+backlinks-example-generator',
559 ],
560 'embeddedin' => [
561 'action=query&list=embeddedin&eititle=Template:Stub'
562 => 'apihelp-query+embeddedin-example-simple',
563 'action=query&generator=embeddedin&geititle=Template:Stub&prop=info'
564 => 'apihelp-query+embeddedin-example-generator',
565 ],
566 'imageusage' => [
567 'action=query&list=imageusage&iutitle=File:Albert%20Einstein%20Head.jpg'
568 => 'apihelp-query+imageusage-example-simple',
569 'action=query&generator=imageusage&giutitle=File:Albert%20Einstein%20Head.jpg&prop=info'
570 => 'apihelp-query+imageusage-example-generator',
571 ]
572 ];
573
574 return $examples[$this->getModuleName()];
575 }
576
577 public function getHelpUrls() {
578 return $this->helpUrl;
579 }
580 }