Merge "Add method parameter type documentation"
[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, $contID, $redirID, $redirect;
43 private $bl_ns, $bl_from, $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( $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_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 = array(
95 $this->bl_ns,
96 $this->bl_title
97 );
98 } else {
99 $this->bl_title = $prefix . '_to';
100 $this->bl_fields = array(
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 $resultPageSet ApiPageSet
120 * @return void
121 */
122 private function prepareFirstQuery( $resultPageSet = null ) {
123 /* SELECT page_id, page_title, page_namespace, page_is_redirect
124 * FROM pagelinks, page WHERE pl_from=page_id
125 * AND pl_title='Foo' AND pl_namespace=0
126 * LIMIT 11 ORDER BY pl_from
127 */
128 $this->addTables( array( $this->bl_table, 'page' ) );
129 $this->addWhere( "{$this->bl_from}=page_id" );
130 if ( is_null( $resultPageSet ) ) {
131 $this->addFields( array( 'page_id', 'page_title', 'page_namespace' ) );
132 } else {
133 $this->addFields( $resultPageSet->getPageTableFields() );
134 }
135
136 $this->addFields( 'page_is_redirect' );
137 $this->addWhereFld( $this->bl_title, $this->rootTitle->getDBkey() );
138
139 if ( $this->hasNS ) {
140 $this->addWhereFld( $this->bl_ns, $this->rootTitle->getNamespace() );
141 }
142 $this->addWhereFld( 'page_namespace', $this->params['namespace'] );
143
144 if ( !is_null( $this->contID ) ) {
145 $op = $this->params['dir'] == 'descending' ? '<' : '>';
146 $this->addWhere( "{$this->bl_from}$op={$this->contID}" );
147 }
148
149 if ( $this->params['filterredir'] == 'redirects' ) {
150 $this->addWhereFld( 'page_is_redirect', 1 );
151 } elseif ( $this->params['filterredir'] == 'nonredirects' && !$this->redirect ) {
152 // bug 22245 - Check for !redirect, as filtering nonredirects, when getting what links to them is contradictory
153 $this->addWhereFld( 'page_is_redirect', 0 );
154 }
155
156 $this->addOption( 'LIMIT', $this->params['limit'] + 1 );
157 $sort = ( $this->params['dir'] == 'descending' ? ' DESC' : '' );
158 $this->addOption( 'ORDER BY', $this->bl_from . $sort );
159 $this->addOption( 'STRAIGHT_JOIN' );
160 }
161
162 /**
163 * @param $resultPageSet ApiPageSet
164 * @return void
165 */
166 private function prepareSecondQuery( $resultPageSet = null ) {
167 /* SELECT page_id, page_title, page_namespace, page_is_redirect, pl_title, pl_namespace
168 FROM pagelinks, page WHERE pl_from=page_id
169 AND (pl_title='Foo' AND pl_namespace=0) OR (pl_title='Bar' AND pl_namespace=1)
170 ORDER BY pl_namespace, pl_title, pl_from LIMIT 11
171 */
172 $db = $this->getDB();
173 $this->addTables( array( 'page', $this->bl_table ) );
174 $this->addWhere( "{$this->bl_from}=page_id" );
175
176 if ( is_null( $resultPageSet ) ) {
177 $this->addFields( array( 'page_id', 'page_title', 'page_namespace', 'page_is_redirect' ) );
178 } else {
179 $this->addFields( $resultPageSet->getPageTableFields() );
180 }
181
182 $this->addFields( $this->bl_title );
183 if ( $this->hasNS ) {
184 $this->addFields( $this->bl_ns );
185 }
186
187 // We can't use LinkBatch here because $this->hasNS may be false
188 $titleWhere = array();
189 $allRedirNs = array();
190 $allRedirDBkey = array();
191 /** @var $t Title */
192 foreach ( $this->redirTitles as $t ) {
193 $redirNs = $t->getNamespace();
194 $redirDBkey = $t->getDBkey();
195 $titleWhere[] = "{$this->bl_title} = " . $db->addQuotes( $redirDBkey ) .
196 ( $this->hasNS ? " AND {$this->bl_ns} = {$redirNs}" : '' );
197 $allRedirNs[] = $redirNs;
198 $allRedirDBkey[] = $redirDBkey;
199 }
200 $this->addWhere( $db->makeList( $titleWhere, LIST_OR ) );
201 $this->addWhereFld( 'page_namespace', $this->params['namespace'] );
202
203 if ( !is_null( $this->redirID ) ) {
204 $op = $this->params['dir'] == 'descending' ? '<' : '>';
205 /** @var $first Title */
206 $first = $this->redirTitles[0];
207 $title = $db->addQuotes( $first->getDBkey() );
208 $ns = $first->getNamespace();
209 $from = $this->redirID;
210 if ( $this->hasNS ) {
211 $this->addWhere( "{$this->bl_ns} $op $ns OR " .
212 "({$this->bl_ns} = $ns AND " .
213 "({$this->bl_title} $op $title OR " .
214 "({$this->bl_title} = $title AND " .
215 "{$this->bl_from} $op= $from)))" );
216 } else {
217 $this->addWhere( "{$this->bl_title} $op $title OR " .
218 "({$this->bl_title} = $title AND " .
219 "{$this->bl_from} $op= $from)" );
220 }
221 }
222 if ( $this->params['filterredir'] == 'redirects' ) {
223 $this->addWhereFld( 'page_is_redirect', 1 );
224 } elseif ( $this->params['filterredir'] == 'nonredirects' ) {
225 $this->addWhereFld( 'page_is_redirect', 0 );
226 }
227
228 $this->addOption( 'LIMIT', $this->params['limit'] + 1 );
229 $orderBy = array();
230 $sort = ( $this->params['dir'] == 'descending' ? ' DESC' : '' );
231 // Don't order by namespace/title if it's constant in the WHERE clause
232 if ( $this->hasNS && count( array_unique( $allRedirNs ) ) != 1 ) {
233 $orderBy[] = $this->bl_ns . $sort;
234 }
235 if ( count( array_unique( $allRedirDBkey ) ) != 1 ) {
236 $orderBy[] = $this->bl_title . $sort;
237 }
238 $orderBy[] = $this->bl_from . $sort;
239 $this->addOption( 'ORDER BY', $orderBy );
240 $this->addOption( 'USE INDEX', array( 'page' => 'PRIMARY' ) );
241 }
242
243 /**
244 * @param $resultPageSet ApiPageSet
245 * @return void
246 */
247 private function run( $resultPageSet = null ) {
248 $this->params = $this->extractRequestParams( false );
249 $this->redirect = isset( $this->params['redirect'] ) && $this->params['redirect'];
250 $userMax = ( $this->redirect ? ApiBase::LIMIT_BIG1 / 2 : ApiBase::LIMIT_BIG1 );
251 $botMax = ( $this->redirect ? ApiBase::LIMIT_BIG2 / 2 : ApiBase::LIMIT_BIG2 );
252
253 $result = $this->getResult();
254
255 if ( $this->params['limit'] == 'max' ) {
256 $this->params['limit'] = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
257 $result->setParsedLimit( $this->getModuleName(), $this->params['limit'] );
258 } else {
259 $this->params['limit'] = intval( $this->params['limit'] );
260 $this->validateLimit( 'limit', $this->params['limit'], 1, $userMax, $botMax );
261 }
262
263 $this->processContinue();
264 $this->prepareFirstQuery( $resultPageSet );
265
266 $res = $this->select( __METHOD__ . '::firstQuery' );
267
268 $count = 0;
269
270 foreach ( $res as $row ) {
271 if ( ++ $count > $this->params['limit'] ) {
272 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
273 // Continue string preserved in case the redirect query doesn't pass the limit
274 $this->continueStr = $this->getContinueStr( $row->page_id );
275 break;
276 }
277
278 if ( is_null( $resultPageSet ) ) {
279 $this->extractRowInfo( $row );
280 } else {
281 $this->pageMap[$row->page_namespace][$row->page_title] = $row->page_id;
282 if ( $row->page_is_redirect ) {
283 $this->redirTitles[] = Title::makeTitle( $row->page_namespace, $row->page_title );
284 }
285
286 $resultPageSet->processDbRow( $row );
287 }
288 }
289
290 if ( $this->redirect && count( $this->redirTitles ) ) {
291 $this->resetQueryParams();
292 $this->prepareSecondQuery( $resultPageSet );
293 $res = $this->select( __METHOD__ . '::secondQuery' );
294 $count = 0;
295 foreach ( $res as $row ) {
296 if ( ++$count > $this->params['limit'] ) {
297 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
298 // We need to keep the parent page of this redir in
299 if ( $this->hasNS ) {
300 $parentID = $this->pageMap[$row->{$this->bl_ns}][$row->{$this->bl_title}];
301 } else {
302 $parentID = $this->pageMap[NS_FILE][$row->{$this->bl_title}];
303 }
304 $this->continueStr = $this->getContinueRedirStr( $parentID, $row->page_id );
305 break;
306 }
307
308 if ( is_null( $resultPageSet ) ) {
309 $this->extractRedirRowInfo( $row );
310 } else {
311 $resultPageSet->processDbRow( $row );
312 }
313 }
314 }
315 if ( is_null( $resultPageSet ) ) {
316 // Try to add the result data in one go and pray that it fits
317 $fit = $result->addValue( 'query', $this->getModuleName(), array_values( $this->resultArr ) );
318 if ( !$fit ) {
319 // It didn't fit. Add elements one by one until the
320 // result is full.
321 foreach ( $this->resultArr as $pageID => $arr ) {
322 // Add the basic entry without redirlinks first
323 $fit = $result->addValue(
324 array( 'query', $this->getModuleName() ),
325 null, array_diff_key( $arr, array( 'redirlinks' => '' ) ) );
326 if ( !$fit ) {
327 $this->continueStr = $this->getContinueStr( $pageID );
328 break;
329 }
330
331 $hasRedirs = false;
332 $redirLinks = isset( $arr['redirlinks'] ) ? $arr['redirlinks'] : array();
333 foreach ( (array)$redirLinks as $key => $redir ) {
334 $fit = $result->addValue(
335 array( 'query', $this->getModuleName(), $pageID, 'redirlinks' ),
336 $key, $redir );
337 if ( !$fit ) {
338 $this->continueStr = $this->getContinueRedirStr( $pageID, $redir['pageid'] );
339 break;
340 }
341 $hasRedirs = true;
342 }
343 if ( $hasRedirs ) {
344 $result->setIndexedTagName_internal(
345 array( 'query', $this->getModuleName(), $pageID, 'redirlinks' ),
346 $this->bl_code );
347 }
348 if ( !$fit ) {
349 break;
350 }
351 }
352 }
353
354 $result->setIndexedTagName_internal(
355 array( 'query', $this->getModuleName() ),
356 $this->bl_code
357 );
358 }
359 if ( !is_null( $this->continueStr ) ) {
360 $this->setContinueEnumParameter( 'continue', $this->continueStr );
361 }
362 }
363
364 private function extractRowInfo( $row ) {
365 $this->pageMap[$row->page_namespace][$row->page_title] = $row->page_id;
366 $t = Title::makeTitle( $row->page_namespace, $row->page_title );
367 $a = array( 'pageid' => intval( $row->page_id ) );
368 ApiQueryBase::addTitleInfo( $a, $t );
369 if ( $row->page_is_redirect ) {
370 $a['redirect'] = '';
371 $this->redirTitles[] = $t;
372 }
373 // Put all the results in an array first
374 $this->resultArr[$a['pageid']] = $a;
375 }
376
377 private function extractRedirRowInfo( $row ) {
378 $a['pageid'] = intval( $row->page_id );
379 ApiQueryBase::addTitleInfo( $a, Title::makeTitle( $row->page_namespace, $row->page_title ) );
380 if ( $row->page_is_redirect ) {
381 $a['redirect'] = '';
382 }
383 $ns = $this->hasNS ? $row->{$this->bl_ns} : NS_FILE;
384 $parentID = $this->pageMap[$ns][$row->{$this->bl_title}];
385 // Put all the results in an array first
386 $this->resultArr[$parentID]['redirlinks'][] = $a;
387 $this->getResult()->setIndexedTagName( $this->resultArr[$parentID]['redirlinks'], $this->bl_code );
388 }
389
390 protected function processContinue() {
391 if ( !is_null( $this->params['continue'] ) ) {
392 $this->parseContinueParam();
393 } else {
394 $this->rootTitle = $this->getTitleOrPageId( $this->params )->getTitle();
395 }
396
397 // only image titles are allowed for the root in imageinfo mode
398 if ( !$this->hasNS && $this->rootTitle->getNamespace() !== NS_FILE ) {
399 $this->dieUsage( "The title for {$this->getModuleName()} query must be an image", 'bad_image_title' );
400 }
401 }
402
403 protected function parseContinueParam() {
404 $continueList = explode( '|', $this->params['continue'] );
405 // expected format:
406 // ns | key | id1 [| id2]
407 // ns+key: root title
408 // id1: first-level page ID to continue from
409 // id2: second-level page ID to continue from
410
411 // null stuff out now so we know what's set and what isn't
412 $this->rootTitle = $this->contID = $this->redirID = null;
413 $rootNs = intval( $continueList[0] );
414 $this->dieContinueUsageIf( $rootNs === 0 && $continueList[0] !== '0' );
415
416 $this->rootTitle = Title::makeTitleSafe( $rootNs, $continueList[1] );
417 $this->dieContinueUsageIf( !$this->rootTitle );
418
419 $contID = intval( $continueList[2] );
420 $this->dieContinueUsageIf( $contID === 0 && $continueList[2] !== '0' );
421
422 $this->contID = $contID;
423 $id2 = isset( $continueList[3] ) ? $continueList[3] : null;
424 $redirID = intval( $id2 );
425
426 if ( $redirID === 0 && $id2 !== '0' ) {
427 // This one isn't required
428 return;
429 }
430 $this->redirID = $redirID;
431
432 }
433
434 protected function getContinueStr( $lastPageID ) {
435 return $this->rootTitle->getNamespace() .
436 '|' . $this->rootTitle->getDBkey() .
437 '|' . $lastPageID;
438 }
439
440 protected function getContinueRedirStr( $lastPageID, $lastRedirID ) {
441 return $this->getContinueStr( $lastPageID ) . '|' . $lastRedirID;
442 }
443
444 public function getAllowedParams() {
445 $retval = array(
446 'title' => array(
447 ApiBase::PARAM_TYPE => 'string',
448 ),
449 'pageid' => array(
450 ApiBase::PARAM_TYPE => 'integer',
451 ),
452 'continue' => null,
453 'namespace' => array(
454 ApiBase::PARAM_ISMULTI => true,
455 ApiBase::PARAM_TYPE => 'namespace'
456 ),
457 'dir' => array(
458 ApiBase::PARAM_DFLT => 'ascending',
459 ApiBase::PARAM_TYPE => array(
460 'ascending',
461 'descending'
462 )
463 ),
464 'filterredir' => array(
465 ApiBase::PARAM_DFLT => 'all',
466 ApiBase::PARAM_TYPE => array(
467 'all',
468 'redirects',
469 'nonredirects'
470 )
471 ),
472 'limit' => array(
473 ApiBase::PARAM_DFLT => 10,
474 ApiBase::PARAM_TYPE => 'limit',
475 ApiBase::PARAM_MIN => 1,
476 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
477 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
478 )
479 );
480 if ( $this->getModuleName() == 'embeddedin' ) {
481 return $retval;
482 }
483 $retval['redirect'] = false;
484 return $retval;
485 }
486
487 public function getParamDescription() {
488 $retval = array(
489 'title' => "Title to search. Cannot be used together with {$this->bl_code}pageid",
490 'pageid' => "Pageid to search. Cannot be used together with {$this->bl_code}title",
491 'continue' => 'When more results are available, use this to continue',
492 'namespace' => 'The namespace to enumerate',
493 'dir' => 'The direction in which to list',
494 );
495 if ( $this->getModuleName() != 'embeddedin' ) {
496 return array_merge( $retval, array(
497 'redirect' => 'If linking page is a redirect, find all pages that link to that redirect as well. Maximum limit is halved.',
498 'filterredir' => "How to filter for redirects. If set to nonredirects when {$this->bl_code}redirect is enabled, this is only applied to the second level",
499 'limit' => "How many total pages to return. If {$this->bl_code}redirect is enabled, limit applies to each level separately (which means you may get up to 2 * limit results)."
500 ) );
501 }
502 return array_merge( $retval, array(
503 'filterredir' => 'How to filter for redirects',
504 'limit' => 'How many total pages to return'
505 ) );
506 }
507
508 public function getResultProperties() {
509 return array(
510 '' => array(
511 'pageid' => 'integer',
512 'ns' => 'namespace',
513 'title' => 'string',
514 'redirect' => 'boolean'
515 )
516 );
517 }
518
519 public function getDescription() {
520 switch ( $this->getModuleName() ) {
521 case 'backlinks':
522 return 'Find all pages that link to the given page';
523 case 'embeddedin':
524 return 'Find all pages that embed (transclude) the given title';
525 case 'imageusage':
526 return 'Find all pages that use the given image title.';
527 default:
528 ApiBase::dieDebug( __METHOD__, 'Unknown module name' );
529 }
530 }
531
532 public function getPossibleErrors() {
533 return array_merge( parent::getPossibleErrors(),
534 $this->getTitleOrPageIdErrorMessage(),
535 array(
536 array( 'code' => 'bad_image_title', 'info' => "The title for {$this->getModuleName()} query must be an image" ),
537 )
538 );
539 }
540
541 public function getExamples() {
542 static $examples = array(
543 'backlinks' => array(
544 'api.php?action=query&list=backlinks&bltitle=Main%20Page',
545 'api.php?action=query&generator=backlinks&gbltitle=Main%20Page&prop=info'
546 ),
547 'embeddedin' => array(
548 'api.php?action=query&list=embeddedin&eititle=Template:Stub',
549 'api.php?action=query&generator=embeddedin&geititle=Template:Stub&prop=info'
550 ),
551 'imageusage' => array(
552 'api.php?action=query&list=imageusage&iutitle=File:Albert%20Einstein%20Head.jpg',
553 'api.php?action=query&generator=imageusage&giutitle=File:Albert%20Einstein%20Head.jpg&prop=info'
554 )
555 );
556
557 return $examples[$this->getModuleName()];
558 }
559
560 public function getHelpUrls() {
561 return $this->helpUrl;
562 }
563 }