Moved contribs rev parent ID batch query into doBatchLookups()
[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 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( "ApiQueryBase.php" );
30 }
31
32 /**
33 * This is a three-in-one module to query:
34 * * backlinks - links pointing to the given page,
35 * * embeddedin - what pages transclude the given page within themselves,
36 * * imageusage - what pages use the given image
37 *
38 * @ingroup API
39 */
40 class ApiQueryBacklinks extends ApiQueryGeneratorBase {
41
42 /**
43 * @var Title
44 */
45 private $rootTitle;
46
47 private $params, $contID, $redirID, $redirect;
48 private $bl_ns, $bl_from, $bl_table, $bl_code, $bl_title, $bl_sort, $bl_fields, $hasNS;
49
50 /**
51 * Maps ns and title to pageid
52 *
53 * @var array
54 */
55 private $pageMap = array();
56 private $resultArr;
57
58 private $redirTitles = array();
59 private $continueStr = null;
60
61 // output element name, database column field prefix, database table
62 private $backlinksSettings = array(
63 'backlinks' => array(
64 'code' => 'bl',
65 'prefix' => 'pl',
66 'linktbl' => 'pagelinks',
67 'helpurl' => 'http://www.mediawiki.org/wiki/API:Backlinks',
68 ),
69 'embeddedin' => array(
70 'code' => 'ei',
71 'prefix' => 'tl',
72 'linktbl' => 'templatelinks',
73 'helpurl' => 'http://www.mediawiki.org/wiki/API:Embeddedin',
74 ),
75 'imageusage' => array(
76 'code' => 'iu',
77 'prefix' => 'il',
78 'linktbl' => 'imagelinks',
79 'helpurl' => 'http://www.mediawiki.org/wiki/API:Imageusage',
80 )
81 );
82
83 public function __construct( $query, $moduleName ) {
84 $settings = $this->backlinksSettings[$moduleName];
85 $prefix = $settings['prefix'];
86 $code = $settings['code'];
87 $this->resultArr = array();
88
89 parent::__construct( $query, $moduleName, $code );
90 $this->bl_ns = $prefix . '_namespace';
91 $this->bl_from = $prefix . '_from';
92 $this->bl_table = $settings['linktbl'];
93 $this->bl_code = $code;
94 $this->helpUrl = $settings['helpurl'];
95
96 $this->hasNS = $moduleName !== 'imageusage';
97 if ( $this->hasNS ) {
98 $this->bl_title = $prefix . '_title';
99 $this->bl_sort = "{$this->bl_ns}, {$this->bl_title}, {$this->bl_from}";
100 $this->bl_fields = array(
101 $this->bl_ns,
102 $this->bl_title
103 );
104 } else {
105 $this->bl_title = $prefix . '_to';
106 $this->bl_sort = "{$this->bl_title}, {$this->bl_from}";
107 $this->bl_fields = array(
108 $this->bl_title
109 );
110 }
111 }
112
113 public function execute() {
114 $this->run();
115 }
116
117 public function getCacheMode( $params ) {
118 return 'public';
119 }
120
121 public function executeGenerator( $resultPageSet ) {
122 $this->run( $resultPageSet );
123 }
124
125 /**
126 * @param $resultPageSet ApiPageSet
127 * @return void
128 */
129 private function prepareFirstQuery( $resultPageSet = null ) {
130 /* SELECT page_id, page_title, page_namespace, page_is_redirect
131 * FROM pagelinks, page WHERE pl_from=page_id
132 * AND pl_title='Foo' AND pl_namespace=0
133 * LIMIT 11 ORDER BY pl_from
134 */
135 $this->addTables( array( $this->bl_table, 'page' ) );
136 $this->addWhere( "{$this->bl_from}=page_id" );
137 if ( is_null( $resultPageSet ) ) {
138 $this->addFields( array( 'page_id', 'page_title', 'page_namespace' ) );
139 } else {
140 $this->addFields( $resultPageSet->getPageTableFields() );
141 }
142
143 $this->addFields( 'page_is_redirect' );
144 $this->addWhereFld( $this->bl_title, $this->rootTitle->getDBkey() );
145
146 if ( $this->hasNS ) {
147 $this->addWhereFld( $this->bl_ns, $this->rootTitle->getNamespace() );
148 }
149 $this->addWhereFld( 'page_namespace', $this->params['namespace'] );
150
151 if ( !is_null( $this->contID ) ) {
152 $this->addWhere( "{$this->bl_from}>={$this->contID}" );
153 }
154
155 if ( $this->params['filterredir'] == 'redirects' ) {
156 $this->addWhereFld( 'page_is_redirect', 1 );
157 } elseif ( $this->params['filterredir'] == 'nonredirects' && !$this->redirect ) {
158 // bug 22245 - Check for !redirect, as filtering nonredirects, when getting what links to them is contradictory
159 $this->addWhereFld( 'page_is_redirect', 0 );
160 }
161
162 $this->addOption( 'LIMIT', $this->params['limit'] + 1 );
163 $this->addOption( 'ORDER BY', $this->bl_from );
164 $this->addOption( 'STRAIGHT_JOIN' );
165 }
166
167 /**
168 * @param $resultPageSet ApiPageSet
169 * @return void
170 */
171 private function prepareSecondQuery( $resultPageSet = null ) {
172 /* SELECT page_id, page_title, page_namespace, page_is_redirect, pl_title, pl_namespace
173 FROM pagelinks, page WHERE pl_from=page_id
174 AND (pl_title='Foo' AND pl_namespace=0) OR (pl_title='Bar' AND pl_namespace=1)
175 ORDER BY pl_namespace, pl_title, pl_from LIMIT 11
176 */
177 $db = $this->getDB();
178 $this->addTables( array( 'page', $this->bl_table ) );
179 $this->addWhere( "{$this->bl_from}=page_id" );
180
181 if ( is_null( $resultPageSet ) ) {
182 $this->addFields( array( 'page_id', 'page_title', 'page_namespace', 'page_is_redirect' ) );
183 } else {
184 $this->addFields( $resultPageSet->getPageTableFields() );
185 }
186
187 $this->addFields( $this->bl_title );
188 if ( $this->hasNS ) {
189 $this->addFields( $this->bl_ns );
190 }
191
192 // We can't use LinkBatch here because $this->hasNS may be false
193 $titleWhere = array();
194 foreach ( $this->redirTitles as $t ) {
195 $titleWhere[] = "{$this->bl_title} = " . $db->addQuotes( $t->getDBkey() ) .
196 ( $this->hasNS ? " AND {$this->bl_ns} = '{$t->getNamespace()}'" : '' );
197 }
198 $this->addWhere( $db->makeList( $titleWhere, LIST_OR ) );
199 $this->addWhereFld( 'page_namespace', $this->params['namespace'] );
200
201 if ( !is_null( $this->redirID ) ) {
202 $first = $this->redirTitles[0];
203 $title = $db->strencode( $first->getDBkey() );
204 $ns = $first->getNamespace();
205 $from = $this->redirID;
206 if ( $this->hasNS ) {
207 $this->addWhere( "{$this->bl_ns} > $ns OR " .
208 "({$this->bl_ns} = $ns AND " .
209 "({$this->bl_title} > '$title' OR " .
210 "({$this->bl_title} = '$title' AND " .
211 "{$this->bl_from} >= $from)))" );
212 } else {
213 $this->addWhere( "{$this->bl_title} > '$title' OR " .
214 "({$this->bl_title} = '$title' AND " .
215 "{$this->bl_from} >= $from)" );
216 }
217 }
218 if ( $this->params['filterredir'] == 'redirects' ) {
219 $this->addWhereFld( 'page_is_redirect', 1 );
220 } elseif ( $this->params['filterredir'] == 'nonredirects' ) {
221 $this->addWhereFld( 'page_is_redirect', 0 );
222 }
223
224 $this->addOption( 'LIMIT', $this->params['limit'] + 1 );
225 $this->addOption( 'ORDER BY', $this->bl_sort );
226 $this->addOption( 'USE INDEX', array( 'page' => 'PRIMARY' ) );
227 }
228
229 /**
230 * @param $resultPageSet ApiPageSet
231 * @return void
232 */
233 private function run( $resultPageSet = null ) {
234 $this->params = $this->extractRequestParams( false );
235 $this->redirect = isset( $this->params['redirect'] ) && $this->params['redirect'];
236 $userMax = ( $this->redirect ? ApiBase::LIMIT_BIG1 / 2 : ApiBase::LIMIT_BIG1 );
237 $botMax = ( $this->redirect ? ApiBase::LIMIT_BIG2 / 2 : ApiBase::LIMIT_BIG2 );
238
239 $result = $this->getResult();
240
241 if ( $this->params['limit'] == 'max' ) {
242 $this->params['limit'] = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
243 $result->setParsedLimit( $this->getModuleName(), $this->params['limit'] );
244 }
245
246 $this->processContinue();
247 $this->prepareFirstQuery( $resultPageSet );
248
249 $res = $this->select( __METHOD__ . '::firstQuery' );
250
251 $count = 0;
252
253 foreach ( $res as $row ) {
254 if ( ++ $count > $this->params['limit'] ) {
255 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
256 // Continue string preserved in case the redirect query doesn't pass the limit
257 $this->continueStr = $this->getContinueStr( $row->page_id );
258 break;
259 }
260
261 if ( is_null( $resultPageSet ) ) {
262 $this->extractRowInfo( $row );
263 } else {
264 $this->pageMap[$row->page_namespace][$row->page_title] = $row->page_id;
265 if ( $row->page_is_redirect ) {
266 $this->redirTitles[] = Title::makeTitle( $row->page_namespace, $row->page_title );
267 }
268
269 $resultPageSet->processDbRow( $row );
270 }
271 }
272
273 if ( $this->redirect && count( $this->redirTitles ) ) {
274 $this->resetQueryParams();
275 $this->prepareSecondQuery( $resultPageSet );
276 $res = $this->select( __METHOD__ . '::secondQuery' );
277 $count = 0;
278 foreach ( $res as $row ) {
279 if ( ++$count > $this->params['limit'] ) {
280 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
281 // We need to keep the parent page of this redir in
282 if ( $this->hasNS ) {
283 $parentID = $this->pageMap[$row-> { $this->bl_ns } ][$row-> { $this->bl_title } ];
284 } else {
285 $parentID = $this->pageMap[NS_IMAGE][$row-> { $this->bl_title } ];
286 }
287 $this->continueStr = $this->getContinueRedirStr( $parentID, $row->page_id );
288 break;
289 }
290
291 if ( is_null( $resultPageSet ) ) {
292 $this->extractRedirRowInfo( $row );
293 } else {
294 $resultPageSet->processDbRow( $row );
295 }
296 }
297 }
298 if ( is_null( $resultPageSet ) ) {
299 // Try to add the result data in one go and pray that it fits
300 $fit = $result->addValue( 'query', $this->getModuleName(), array_values( $this->resultArr ) );
301 if ( !$fit ) {
302 // It didn't fit. Add elements one by one until the
303 // result is full.
304 foreach ( $this->resultArr as $pageID => $arr ) {
305 // Add the basic entry without redirlinks first
306 $fit = $result->addValue(
307 array( 'query', $this->getModuleName() ),
308 null, array_diff_key( $arr, array( 'redirlinks' => '' ) ) );
309 if ( !$fit ) {
310 $this->continueStr = $this->getContinueStr( $pageID );
311 break;
312 }
313
314 $hasRedirs = false;
315 $redirLinks = isset( $arr['redirlinks'] ) ? $arr['redirlinks'] : array();
316 foreach ( (array)$redirLinks as $key => $redir ) {
317 $fit = $result->addValue(
318 array( 'query', $this->getModuleName(), $pageID, 'redirlinks' ),
319 $key, $redir );
320 if ( !$fit ) {
321 $this->continueStr = $this->getContinueRedirStr( $pageID, $redir['pageid'] );
322 break;
323 }
324 $hasRedirs = true;
325 }
326 if ( $hasRedirs ) {
327 $result->setIndexedTagName_internal(
328 array( 'query', $this->getModuleName(), $pageID, 'redirlinks' ),
329 $this->bl_code );
330 }
331 if ( !$fit ) {
332 break;
333 }
334 }
335 }
336
337 $result->setIndexedTagName_internal(
338 array( 'query', $this->getModuleName() ),
339 $this->bl_code
340 );
341 }
342 if ( !is_null( $this->continueStr ) ) {
343 $this->setContinueEnumParameter( 'continue', $this->continueStr );
344 }
345 }
346
347 private function extractRowInfo( $row ) {
348 $this->pageMap[$row->page_namespace][$row->page_title] = $row->page_id;
349 $t = Title::makeTitle( $row->page_namespace, $row->page_title );
350 $a = array( 'pageid' => intval( $row->page_id ) );
351 ApiQueryBase::addTitleInfo( $a, $t );
352 if ( $row->page_is_redirect ) {
353 $a['redirect'] = '';
354 $this->redirTitles[] = $t;
355 }
356 // Put all the results in an array first
357 $this->resultArr[$a['pageid']] = $a;
358 }
359
360 private function extractRedirRowInfo( $row ) {
361 $a['pageid'] = intval( $row->page_id );
362 ApiQueryBase::addTitleInfo( $a, Title::makeTitle( $row->page_namespace, $row->page_title ) );
363 if ( $row->page_is_redirect ) {
364 $a['redirect'] = '';
365 }
366 $ns = $this->hasNS ? $row-> { $this->bl_ns } : NS_FILE;
367 $parentID = $this->pageMap[$ns][$row-> { $this->bl_title } ];
368 // Put all the results in an array first
369 $this->resultArr[$parentID]['redirlinks'][] = $a;
370 $this->getResult()->setIndexedTagName( $this->resultArr[$parentID]['redirlinks'], $this->bl_code );
371 }
372
373 protected function processContinue() {
374 if ( !is_null( $this->params['continue'] ) ) {
375 $this->parseContinueParam();
376 } else {
377 if ( $this->params['title'] !== '' ) {
378 $title = Title::newFromText( $this->params['title'] );
379 if ( !$title ) {
380 $this->dieUsageMsg( array( 'invalidtitle', $this->params['title'] ) );
381 } else {
382 $this->rootTitle = $title;
383 }
384 }
385 }
386
387 // only image titles are allowed for the root in imageinfo mode
388 if ( !$this->hasNS && $this->rootTitle->getNamespace() !== NS_FILE ) {
389 $this->dieUsage( "The title for {$this->getModuleName()} query must be an image", 'bad_image_title' );
390 }
391 }
392
393 protected function parseContinueParam() {
394 $continueList = explode( '|', $this->params['continue'] );
395 // expected format:
396 // ns | key | id1 [| id2]
397 // ns+key: root title
398 // id1: first-level page ID to continue from
399 // id2: second-level page ID to continue from
400
401 // null stuff out now so we know what's set and what isn't
402 $this->rootTitle = $this->contID = $this->redirID = null;
403 $rootNs = intval( $continueList[0] );
404 if ( $rootNs === 0 && $continueList[0] !== '0' ) {
405 // Illegal continue parameter
406 $this->dieUsage( 'Invalid continue param. You should pass the original value returned by the previous query', '_badcontinue' );
407 }
408 $this->rootTitle = Title::makeTitleSafe( $rootNs, $continueList[1] );
409
410 if ( !$this->rootTitle ) {
411 $this->dieUsage( 'Invalid continue param. You should pass the original value returned by the previous query', '_badcontinue' );
412 }
413 $contID = intval( $continueList[2] );
414
415 if ( $contID === 0 && $continueList[2] !== '0' ) {
416 $this->dieUsage( 'Invalid continue param. You should pass the original value returned by the previous query', '_badcontinue' );
417 }
418 $this->contID = $contID;
419 $id2 = isset( $continueList[3] ) ? $continueList[3] : null;
420 $redirID = intval( $id2 );
421
422 if ( $redirID === 0 && $id2 !== '0' ) {
423 // This one isn't required
424 return;
425 }
426 $this->redirID = $redirID;
427
428 }
429
430 protected function getContinueStr( $lastPageID ) {
431 return $this->rootTitle->getNamespace() .
432 '|' . $this->rootTitle->getDBkey() .
433 '|' . $lastPageID;
434 }
435
436 protected function getContinueRedirStr( $lastPageID, $lastRedirID ) {
437 return $this->getContinueStr( $lastPageID ) . '|' . $lastRedirID;
438 }
439
440 public function getAllowedParams() {
441 $retval = array(
442 'title' => array(
443 ApiBase::PARAM_TYPE => 'string',
444 ApiBase::PARAM_REQUIRED => true
445 ),
446 'continue' => null,
447 'namespace' => array(
448 ApiBase::PARAM_ISMULTI => true,
449 ApiBase::PARAM_TYPE => 'namespace'
450 ),
451 'filterredir' => array(
452 ApiBase::PARAM_DFLT => 'all',
453 ApiBase::PARAM_TYPE => array(
454 'all',
455 'redirects',
456 'nonredirects'
457 )
458 ),
459 'limit' => array(
460 ApiBase::PARAM_DFLT => 10,
461 ApiBase::PARAM_TYPE => 'limit',
462 ApiBase::PARAM_MIN => 1,
463 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
464 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
465 )
466 );
467 if ( $this->getModuleName() == 'embeddedin' ) {
468 return $retval;
469 }
470 $retval['redirect'] = false;
471 return $retval;
472 }
473
474 public function getParamDescription() {
475 $retval = array(
476 'title' => 'Title to search',
477 'continue' => 'When more results are available, use this to continue',
478 'namespace' => 'The namespace to enumerate',
479 );
480 if ( $this->getModuleName() != 'embeddedin' ) {
481 return array_merge( $retval, array(
482 'redirect' => 'If linking page is a redirect, find all pages that link to that redirect as well. Maximum limit is halved.',
483 '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",
484 '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)."
485 ) );
486 }
487 return array_merge( $retval, array(
488 'filterredir' => 'How to filter for redirects',
489 'limit' => 'How many total pages to return'
490 ) );
491 }
492
493 public function getDescription() {
494 switch ( $this->getModuleName() ) {
495 case 'backlinks':
496 return 'Find all pages that link to the given page';
497 case 'embeddedin':
498 return 'Find all pages that embed (transclude) the given title';
499 case 'imageusage':
500 return 'Find all pages that use the given image title.';
501 default:
502 ApiBase::dieDebug( __METHOD__, 'Unknown module name' );
503 }
504 }
505
506 public function getPossibleErrors() {
507 return array_merge( parent::getPossibleErrors(), array(
508 array( 'invalidtitle', 'title' ),
509 array( 'code' => 'bad_image_title', 'info' => "The title for {$this->getModuleName()} query must be an image" ),
510 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
511 ) );
512 }
513
514 public function getExamples() {
515 static $examples = array(
516 'backlinks' => array(
517 'api.php?action=query&list=backlinks&bltitle=Main%20Page',
518 'api.php?action=query&generator=backlinks&gbltitle=Main%20Page&prop=info'
519 ),
520 'embeddedin' => array(
521 'api.php?action=query&list=embeddedin&eititle=Template:Stub',
522 'api.php?action=query&generator=embeddedin&geititle=Template:Stub&prop=info'
523 ),
524 'imageusage' => array(
525 'api.php?action=query&list=imageusage&iutitle=File:Albert%20Einstein%20Head.jpg',
526 'api.php?action=query&generator=imageusage&giutitle=File:Albert%20Einstein%20Head.jpg&prop=info'
527 )
528 );
529
530 return $examples[$this->getModuleName()];
531 }
532
533 public function getHelpUrls() {
534 return $this->helpUrl;
535 }
536
537 public function getVersion() {
538 return __CLASS__ . ': $Id$';
539 }
540 }