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