5 * Created on Oct 16, 2006
7 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
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.
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.
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
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( "ApiQueryBase.php" );
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
40 class ApiQueryBacklinks
extends ApiQueryGeneratorBase
{
47 private $params, $contID, $redirID, $redirect;
48 private $bl_ns, $bl_from, $bl_table, $bl_code, $bl_title, $bl_sort, $bl_fields, $hasNS;
51 * Maps ns and title to pageid
55 private $pageMap = array();
58 private $redirTitles = array();
59 private $continueStr = null;
61 // output element name, database column field prefix, database table
62 private $backlinksSettings = array(
66 'linktbl' => 'pagelinks',
67 'helpurl' => 'http://www.mediawiki.org/wiki/API:Backlinks',
69 'embeddedin' => array(
72 'linktbl' => 'templatelinks',
73 'helpurl' => 'http://www.mediawiki.org/wiki/API:Embeddedin',
75 'imageusage' => array(
78 'linktbl' => 'imagelinks',
79 'helpurl' => 'http://www.mediawiki.org/wiki/API:Imageusage',
83 public function __construct( $query, $moduleName ) {
84 $settings = $this->backlinksSettings
[$moduleName];
85 $prefix = $settings['prefix'];
86 $code = $settings['code'];
87 $this->resultArr
= array();
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'];
96 $this->hasNS
= $moduleName !== 'imageusage';
98 $this->bl_title
= $prefix . '_title';
99 $this->bl_sort
= "{$this->bl_ns}, {$this->bl_title}, {$this->bl_from}";
100 $this->bl_fields
= array(
105 $this->bl_title
= $prefix . '_to';
106 $this->bl_sort
= "{$this->bl_title}, {$this->bl_from}";
107 $this->bl_fields
= array(
113 public function execute() {
117 public function getCacheMode( $params ) {
121 public function executeGenerator( $resultPageSet ) {
122 $this->run( $resultPageSet );
126 * @param $resultPageSet ApiPageSet
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
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' ) );
140 $this->addFields( $resultPageSet->getPageTableFields() );
143 $this->addFields( 'page_is_redirect' );
144 $this->addWhereFld( $this->bl_title
, $this->rootTitle
->getDBkey() );
146 if ( $this->hasNS
) {
147 $this->addWhereFld( $this->bl_ns
, $this->rootTitle
->getNamespace() );
149 $this->addWhereFld( 'page_namespace', $this->params
['namespace'] );
151 if ( !is_null( $this->contID
) ) {
152 $this->addWhere( "{$this->bl_from}>={$this->contID}" );
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 );
162 $this->addOption( 'LIMIT', $this->params
['limit'] +
1 );
163 $this->addOption( 'ORDER BY', $this->bl_from
);
164 $this->addOption( 'STRAIGHT_JOIN' );
168 * @param $resultPageSet ApiPageSet
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
177 $db = $this->getDB();
178 $this->addTables( array( 'page', $this->bl_table
) );
179 $this->addWhere( "{$this->bl_from}=page_id" );
181 if ( is_null( $resultPageSet ) ) {
182 $this->addFields( array( 'page_id', 'page_title', 'page_namespace', 'page_is_redirect' ) );
184 $this->addFields( $resultPageSet->getPageTableFields() );
187 $this->addFields( $this->bl_title
);
188 if ( $this->hasNS
) {
189 $this->addFields( $this->bl_ns
);
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()}'" : '' );
198 $this->addWhere( $db->makeList( $titleWhere, LIST_OR
) );
199 $this->addWhereFld( 'page_namespace', $this->params
['namespace'] );
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)))" );
213 $this->addWhere( "{$this->bl_title} > '$title' OR " .
214 "({$this->bl_title} = '$title' AND " .
215 "{$this->bl_from} >= $from)" );
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 );
224 $this->addOption( 'LIMIT', $this->params
['limit'] +
1 );
225 $this->addOption( 'ORDER BY', $this->bl_sort
);
226 $this->addOption( 'USE INDEX', array( 'page' => 'PRIMARY' ) );
230 * @param $resultPageSet ApiPageSet
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
);
239 $result = $this->getResult();
241 if ( $this->params
['limit'] == 'max' ) {
242 $this->params
['limit'] = $this->getMain()->canApiHighLimits() ?
$botMax : $userMax;
243 $result->setParsedLimit( $this->getModuleName(), $this->params
['limit'] );
246 $this->processContinue();
247 $this->prepareFirstQuery( $resultPageSet );
249 $res = $this->select( __METHOD__
. '::firstQuery' );
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
);
261 if ( is_null( $resultPageSet ) ) {
262 $this->extractRowInfo( $row );
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
);
269 $resultPageSet->processDbRow( $row );
273 if ( $this->redirect
&& count( $this->redirTitles
) ) {
274 $this->resetQueryParams();
275 $this->prepareSecondQuery( $resultPageSet );
276 $res = $this->select( __METHOD__
. '::secondQuery' );
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
} ];
285 $parentID = $this->pageMap
[NS_IMAGE
][$row-> { $this->bl_title
} ];
287 $this->continueStr
= $this->getContinueRedirStr( $parentID, $row->page_id
);
291 if ( is_null( $resultPageSet ) ) {
292 $this->extractRedirRowInfo( $row );
294 $resultPageSet->processDbRow( $row );
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
) );
302 // It didn't fit. Add elements one by one until the
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' => '' ) ) );
310 $this->continueStr
= $this->getContinueStr( $pageID );
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' ),
321 $this->continueStr
= $this->getContinueRedirStr( $pageID, $redir['pageid'] );
327 $result->setIndexedTagName_internal(
328 array( 'query', $this->getModuleName(), $pageID, 'redirlinks' ),
337 $result->setIndexedTagName_internal(
338 array( 'query', $this->getModuleName() ),
342 if ( !is_null( $this->continueStr
) ) {
343 $this->setContinueEnumParameter( 'continue', $this->continueStr
);
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
) {
354 $this->redirTitles
[] = $t;
356 // Put all the results in an array first
357 $this->resultArr
[$a['pageid']] = $a;
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
) {
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
);
373 protected function processContinue() {
374 if ( !is_null( $this->params
['continue'] ) ) {
375 $this->parseContinueParam();
377 if ( $this->params
['title'] !== '' ) {
378 $title = Title
::newFromText( $this->params
['title'] );
380 $this->dieUsageMsg( array( 'invalidtitle', $this->params
['title'] ) );
382 $this->rootTitle
= $title;
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' );
393 protected function parseContinueParam() {
394 $continueList = explode( '|', $this->params
['continue'] );
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
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' );
408 $this->rootTitle
= Title
::makeTitleSafe( $rootNs, $continueList[1] );
410 if ( !$this->rootTitle
) {
411 $this->dieUsage( 'Invalid continue param. You should pass the original value returned by the previous query', '_badcontinue' );
413 $contID = intval( $continueList[2] );
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' );
418 $this->contID
= $contID;
419 $id2 = isset( $continueList[3] ) ?
$continueList[3] : null;
420 $redirID = intval( $id2 );
422 if ( $redirID === 0 && $id2 !== '0' ) {
423 // This one isn't required
426 $this->redirID
= $redirID;
430 protected function getContinueStr( $lastPageID ) {
431 return $this->rootTitle
->getNamespace() .
432 '|' . $this->rootTitle
->getDBkey() .
436 protected function getContinueRedirStr( $lastPageID, $lastRedirID ) {
437 return $this->getContinueStr( $lastPageID ) . '|' . $lastRedirID;
440 public function getAllowedParams() {
443 ApiBase
::PARAM_TYPE
=> 'string',
444 ApiBase
::PARAM_REQUIRED
=> true
447 'namespace' => array(
448 ApiBase
::PARAM_ISMULTI
=> true,
449 ApiBase
::PARAM_TYPE
=> 'namespace'
451 'filterredir' => array(
452 ApiBase
::PARAM_DFLT
=> 'all',
453 ApiBase
::PARAM_TYPE
=> 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
467 if ( $this->getModuleName() == 'embeddedin' ) {
470 $retval['redirect'] = false;
474 public function getParamDescription() {
476 'title' => 'Title to search',
477 'continue' => 'When more results are available, use this to continue',
478 'namespace' => 'The namespace to enumerate',
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)."
487 return array_merge( $retval, array(
488 'filterredir' => 'How to filter for redirects',
489 'limit' => 'How many total pages to return'
493 public function getDescription() {
494 switch ( $this->getModuleName() ) {
496 return 'Find all pages that link to the given page';
498 return 'Find all pages that embed (transclude) the given title';
500 return 'Find all pages that use the given image title.';
502 ApiBase
::dieDebug( __METHOD__
, 'Unknown module name' );
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' ),
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'
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'
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'
530 return $examples[$this->getModuleName()];
533 public function getHelpUrls() {
534 return $this->helpUrl
;
537 public function getVersion() {
538 return __CLASS__
. ': $Id$';