MediaWiki
REL1_20
|
00001 <?php 00035 class ApiQueryBacklinks extends ApiQueryGeneratorBase { 00036 00040 private $rootTitle; 00041 00042 private $params, $contID, $redirID, $redirect; 00043 private $bl_ns, $bl_from, $bl_table, $bl_code, $bl_title, $bl_fields, $hasNS; 00044 00050 private $pageMap = array(); 00051 private $resultArr; 00052 00053 private $redirTitles = array(); 00054 private $continueStr = null; 00055 00056 // output element name, database column field prefix, database table 00057 private $backlinksSettings = array( 00058 'backlinks' => array( 00059 'code' => 'bl', 00060 'prefix' => 'pl', 00061 'linktbl' => 'pagelinks', 00062 'helpurl' => 'https://www.mediawiki.org/wiki/API:Backlinks', 00063 ), 00064 'embeddedin' => array( 00065 'code' => 'ei', 00066 'prefix' => 'tl', 00067 'linktbl' => 'templatelinks', 00068 'helpurl' => 'https://www.mediawiki.org/wiki/API:Embeddedin', 00069 ), 00070 'imageusage' => array( 00071 'code' => 'iu', 00072 'prefix' => 'il', 00073 'linktbl' => 'imagelinks', 00074 'helpurl' => 'https://www.mediawiki.org/wiki/API:Imageusage', 00075 ) 00076 ); 00077 00078 public function __construct( $query, $moduleName ) { 00079 $settings = $this->backlinksSettings[$moduleName]; 00080 $prefix = $settings['prefix']; 00081 $code = $settings['code']; 00082 $this->resultArr = array(); 00083 00084 parent::__construct( $query, $moduleName, $code ); 00085 $this->bl_ns = $prefix . '_namespace'; 00086 $this->bl_from = $prefix . '_from'; 00087 $this->bl_table = $settings['linktbl']; 00088 $this->bl_code = $code; 00089 $this->helpUrl = $settings['helpurl']; 00090 00091 $this->hasNS = $moduleName !== 'imageusage'; 00092 if ( $this->hasNS ) { 00093 $this->bl_title = $prefix . '_title'; 00094 $this->bl_fields = array( 00095 $this->bl_ns, 00096 $this->bl_title 00097 ); 00098 } else { 00099 $this->bl_title = $prefix . '_to'; 00100 $this->bl_fields = array( 00101 $this->bl_title 00102 ); 00103 } 00104 } 00105 00106 public function execute() { 00107 $this->run(); 00108 } 00109 00110 public function getCacheMode( $params ) { 00111 return 'public'; 00112 } 00113 00114 public function executeGenerator( $resultPageSet ) { 00115 $this->run( $resultPageSet ); 00116 } 00117 00122 private function prepareFirstQuery( $resultPageSet = null ) { 00123 /* SELECT page_id, page_title, page_namespace, page_is_redirect 00124 * FROM pagelinks, page WHERE pl_from=page_id 00125 * AND pl_title='Foo' AND pl_namespace=0 00126 * LIMIT 11 ORDER BY pl_from 00127 */ 00128 $this->addTables( array( $this->bl_table, 'page' ) ); 00129 $this->addWhere( "{$this->bl_from}=page_id" ); 00130 if ( is_null( $resultPageSet ) ) { 00131 $this->addFields( array( 'page_id', 'page_title', 'page_namespace' ) ); 00132 } else { 00133 $this->addFields( $resultPageSet->getPageTableFields() ); 00134 } 00135 00136 $this->addFields( 'page_is_redirect' ); 00137 $this->addWhereFld( $this->bl_title, $this->rootTitle->getDBkey() ); 00138 00139 if ( $this->hasNS ) { 00140 $this->addWhereFld( $this->bl_ns, $this->rootTitle->getNamespace() ); 00141 } 00142 $this->addWhereFld( 'page_namespace', $this->params['namespace'] ); 00143 00144 if ( !is_null( $this->contID ) ) { 00145 $op = $this->params['dir'] == 'descending' ? '<' : '>'; 00146 $this->addWhere( "{$this->bl_from}$op={$this->contID}" ); 00147 } 00148 00149 if ( $this->params['filterredir'] == 'redirects' ) { 00150 $this->addWhereFld( 'page_is_redirect', 1 ); 00151 } elseif ( $this->params['filterredir'] == 'nonredirects' && !$this->redirect ) { 00152 // bug 22245 - Check for !redirect, as filtering nonredirects, when getting what links to them is contradictory 00153 $this->addWhereFld( 'page_is_redirect', 0 ); 00154 } 00155 00156 $this->addOption( 'LIMIT', $this->params['limit'] + 1 ); 00157 $sort = ( $this->params['dir'] == 'descending' ? ' DESC' : '' ); 00158 $this->addOption( 'ORDER BY', $this->bl_from . $sort ); 00159 $this->addOption( 'STRAIGHT_JOIN' ); 00160 } 00161 00166 private function prepareSecondQuery( $resultPageSet = null ) { 00167 /* SELECT page_id, page_title, page_namespace, page_is_redirect, pl_title, pl_namespace 00168 FROM pagelinks, page WHERE pl_from=page_id 00169 AND (pl_title='Foo' AND pl_namespace=0) OR (pl_title='Bar' AND pl_namespace=1) 00170 ORDER BY pl_namespace, pl_title, pl_from LIMIT 11 00171 */ 00172 $db = $this->getDB(); 00173 $this->addTables( array( 'page', $this->bl_table ) ); 00174 $this->addWhere( "{$this->bl_from}=page_id" ); 00175 00176 if ( is_null( $resultPageSet ) ) { 00177 $this->addFields( array( 'page_id', 'page_title', 'page_namespace', 'page_is_redirect' ) ); 00178 } else { 00179 $this->addFields( $resultPageSet->getPageTableFields() ); 00180 } 00181 00182 $this->addFields( $this->bl_title ); 00183 if ( $this->hasNS ) { 00184 $this->addFields( $this->bl_ns ); 00185 } 00186 00187 // We can't use LinkBatch here because $this->hasNS may be false 00188 $titleWhere = array(); 00189 $allRedirNs = array(); 00190 $allRedirDBkey = array(); 00191 foreach ( $this->redirTitles as $t ) { 00192 $redirNs = $t->getNamespace(); 00193 $redirDBkey = $t->getDBkey(); 00194 $titleWhere[] = "{$this->bl_title} = " . $db->addQuotes( $redirDBkey ) . 00195 ( $this->hasNS ? " AND {$this->bl_ns} = {$redirNs}" : '' ); 00196 $allRedirNs[] = $redirNs; 00197 $allRedirDBkey[] = $redirDBkey; 00198 } 00199 $this->addWhere( $db->makeList( $titleWhere, LIST_OR ) ); 00200 $this->addWhereFld( 'page_namespace', $this->params['namespace'] ); 00201 00202 if ( !is_null( $this->redirID ) ) { 00203 $op = $this->params['dir'] == 'descending' ? '<' : '>'; 00204 $first = $this->redirTitles[0]; 00205 $title = $db->addQuotes( $first->getDBkey() ); 00206 $ns = $first->getNamespace(); 00207 $from = $this->redirID; 00208 if ( $this->hasNS ) { 00209 $this->addWhere( "{$this->bl_ns} $op $ns OR " . 00210 "({$this->bl_ns} = $ns AND " . 00211 "({$this->bl_title} $op $title OR " . 00212 "({$this->bl_title} = $title AND " . 00213 "{$this->bl_from} $op= $from)))" ); 00214 } else { 00215 $this->addWhere( "{$this->bl_title} $op $title OR " . 00216 "({$this->bl_title} = $title AND " . 00217 "{$this->bl_from} $op= $from)" ); 00218 } 00219 } 00220 if ( $this->params['filterredir'] == 'redirects' ) { 00221 $this->addWhereFld( 'page_is_redirect', 1 ); 00222 } elseif ( $this->params['filterredir'] == 'nonredirects' ) { 00223 $this->addWhereFld( 'page_is_redirect', 0 ); 00224 } 00225 00226 $this->addOption( 'LIMIT', $this->params['limit'] + 1 ); 00227 $orderBy = array(); 00228 $sort = ( $this->params['dir'] == 'descending' ? ' DESC' : '' ); 00229 // Don't order by namespace/title if it's constant in the WHERE clause 00230 if( $this->hasNS && count( array_unique( $allRedirNs ) ) != 1 ) { 00231 $orderBy[] = $this->bl_ns . $sort; 00232 } 00233 if( count( array_unique( $allRedirDBkey ) ) != 1 ) { 00234 $orderBy[] = $this->bl_title . $sort; 00235 } 00236 $orderBy[] = $this->bl_from . $sort; 00237 $this->addOption( 'ORDER BY', $orderBy ); 00238 $this->addOption( 'USE INDEX', array( 'page' => 'PRIMARY' ) ); 00239 } 00240 00245 private function run( $resultPageSet = null ) { 00246 $this->params = $this->extractRequestParams( false ); 00247 $this->redirect = isset( $this->params['redirect'] ) && $this->params['redirect']; 00248 $userMax = ( $this->redirect ? ApiBase::LIMIT_BIG1 / 2 : ApiBase::LIMIT_BIG1 ); 00249 $botMax = ( $this->redirect ? ApiBase::LIMIT_BIG2 / 2 : ApiBase::LIMIT_BIG2 ); 00250 00251 $result = $this->getResult(); 00252 00253 if ( $this->params['limit'] == 'max' ) { 00254 $this->params['limit'] = $this->getMain()->canApiHighLimits() ? $botMax : $userMax; 00255 $result->setParsedLimit( $this->getModuleName(), $this->params['limit'] ); 00256 } 00257 00258 $this->processContinue(); 00259 $this->prepareFirstQuery( $resultPageSet ); 00260 00261 $res = $this->select( __METHOD__ . '::firstQuery' ); 00262 00263 $count = 0; 00264 00265 foreach ( $res as $row ) { 00266 if ( ++ $count > $this->params['limit'] ) { 00267 // We've reached the one extra which shows that there are additional pages to be had. Stop here... 00268 // Continue string preserved in case the redirect query doesn't pass the limit 00269 $this->continueStr = $this->getContinueStr( $row->page_id ); 00270 break; 00271 } 00272 00273 if ( is_null( $resultPageSet ) ) { 00274 $this->extractRowInfo( $row ); 00275 } else { 00276 $this->pageMap[$row->page_namespace][$row->page_title] = $row->page_id; 00277 if ( $row->page_is_redirect ) { 00278 $this->redirTitles[] = Title::makeTitle( $row->page_namespace, $row->page_title ); 00279 } 00280 00281 $resultPageSet->processDbRow( $row ); 00282 } 00283 } 00284 00285 if ( $this->redirect && count( $this->redirTitles ) ) { 00286 $this->resetQueryParams(); 00287 $this->prepareSecondQuery( $resultPageSet ); 00288 $res = $this->select( __METHOD__ . '::secondQuery' ); 00289 $count = 0; 00290 foreach ( $res as $row ) { 00291 if ( ++$count > $this->params['limit'] ) { 00292 // We've reached the one extra which shows that there are additional pages to be had. Stop here... 00293 // We need to keep the parent page of this redir in 00294 if ( $this->hasNS ) { 00295 $parentID = $this->pageMap[$row-> { $this->bl_ns } ][$row-> { $this->bl_title } ]; 00296 } else { 00297 $parentID = $this->pageMap[NS_FILE][$row-> { $this->bl_title } ]; 00298 } 00299 $this->continueStr = $this->getContinueRedirStr( $parentID, $row->page_id ); 00300 break; 00301 } 00302 00303 if ( is_null( $resultPageSet ) ) { 00304 $this->extractRedirRowInfo( $row ); 00305 } else { 00306 $resultPageSet->processDbRow( $row ); 00307 } 00308 } 00309 } 00310 if ( is_null( $resultPageSet ) ) { 00311 // Try to add the result data in one go and pray that it fits 00312 $fit = $result->addValue( 'query', $this->getModuleName(), array_values( $this->resultArr ) ); 00313 if ( !$fit ) { 00314 // It didn't fit. Add elements one by one until the 00315 // result is full. 00316 foreach ( $this->resultArr as $pageID => $arr ) { 00317 // Add the basic entry without redirlinks first 00318 $fit = $result->addValue( 00319 array( 'query', $this->getModuleName() ), 00320 null, array_diff_key( $arr, array( 'redirlinks' => '' ) ) ); 00321 if ( !$fit ) { 00322 $this->continueStr = $this->getContinueStr( $pageID ); 00323 break; 00324 } 00325 00326 $hasRedirs = false; 00327 $redirLinks = isset( $arr['redirlinks'] ) ? $arr['redirlinks'] : array(); 00328 foreach ( (array)$redirLinks as $key => $redir ) { 00329 $fit = $result->addValue( 00330 array( 'query', $this->getModuleName(), $pageID, 'redirlinks' ), 00331 $key, $redir ); 00332 if ( !$fit ) { 00333 $this->continueStr = $this->getContinueRedirStr( $pageID, $redir['pageid'] ); 00334 break; 00335 } 00336 $hasRedirs = true; 00337 } 00338 if ( $hasRedirs ) { 00339 $result->setIndexedTagName_internal( 00340 array( 'query', $this->getModuleName(), $pageID, 'redirlinks' ), 00341 $this->bl_code ); 00342 } 00343 if ( !$fit ) { 00344 break; 00345 } 00346 } 00347 } 00348 00349 $result->setIndexedTagName_internal( 00350 array( 'query', $this->getModuleName() ), 00351 $this->bl_code 00352 ); 00353 } 00354 if ( !is_null( $this->continueStr ) ) { 00355 $this->setContinueEnumParameter( 'continue', $this->continueStr ); 00356 } 00357 } 00358 00359 private function extractRowInfo( $row ) { 00360 $this->pageMap[$row->page_namespace][$row->page_title] = $row->page_id; 00361 $t = Title::makeTitle( $row->page_namespace, $row->page_title ); 00362 $a = array( 'pageid' => intval( $row->page_id ) ); 00363 ApiQueryBase::addTitleInfo( $a, $t ); 00364 if ( $row->page_is_redirect ) { 00365 $a['redirect'] = ''; 00366 $this->redirTitles[] = $t; 00367 } 00368 // Put all the results in an array first 00369 $this->resultArr[$a['pageid']] = $a; 00370 } 00371 00372 private function extractRedirRowInfo( $row ) { 00373 $a['pageid'] = intval( $row->page_id ); 00374 ApiQueryBase::addTitleInfo( $a, Title::makeTitle( $row->page_namespace, $row->page_title ) ); 00375 if ( $row->page_is_redirect ) { 00376 $a['redirect'] = ''; 00377 } 00378 $ns = $this->hasNS ? $row-> { $this->bl_ns } : NS_FILE; 00379 $parentID = $this->pageMap[$ns][$row-> { $this->bl_title } ]; 00380 // Put all the results in an array first 00381 $this->resultArr[$parentID]['redirlinks'][] = $a; 00382 $this->getResult()->setIndexedTagName( $this->resultArr[$parentID]['redirlinks'], $this->bl_code ); 00383 } 00384 00385 protected function processContinue() { 00386 if ( !is_null( $this->params['continue'] ) ) { 00387 $this->parseContinueParam(); 00388 } else { 00389 $this->rootTitle = $this->getTitleOrPageId( $this->params )->getTitle(); 00390 } 00391 00392 // only image titles are allowed for the root in imageinfo mode 00393 if ( !$this->hasNS && $this->rootTitle->getNamespace() !== NS_FILE ) { 00394 $this->dieUsage( "The title for {$this->getModuleName()} query must be an image", 'bad_image_title' ); 00395 } 00396 } 00397 00398 protected function parseContinueParam() { 00399 $continueList = explode( '|', $this->params['continue'] ); 00400 // expected format: 00401 // ns | key | id1 [| id2] 00402 // ns+key: root title 00403 // id1: first-level page ID to continue from 00404 // id2: second-level page ID to continue from 00405 00406 // null stuff out now so we know what's set and what isn't 00407 $this->rootTitle = $this->contID = $this->redirID = null; 00408 $rootNs = intval( $continueList[0] ); 00409 if ( $rootNs === 0 && $continueList[0] !== '0' ) { 00410 // Illegal continue parameter 00411 $this->dieUsage( 'Invalid continue param. You should pass the original value returned by the previous query', '_badcontinue' ); 00412 } 00413 $this->rootTitle = Title::makeTitleSafe( $rootNs, $continueList[1] ); 00414 00415 if ( !$this->rootTitle ) { 00416 $this->dieUsage( 'Invalid continue param. You should pass the original value returned by the previous query', '_badcontinue' ); 00417 } 00418 $contID = intval( $continueList[2] ); 00419 00420 if ( $contID === 0 && $continueList[2] !== '0' ) { 00421 $this->dieUsage( 'Invalid continue param. You should pass the original value returned by the previous query', '_badcontinue' ); 00422 } 00423 $this->contID = $contID; 00424 $id2 = isset( $continueList[3] ) ? $continueList[3] : null; 00425 $redirID = intval( $id2 ); 00426 00427 if ( $redirID === 0 && $id2 !== '0' ) { 00428 // This one isn't required 00429 return; 00430 } 00431 $this->redirID = $redirID; 00432 00433 } 00434 00435 protected function getContinueStr( $lastPageID ) { 00436 return $this->rootTitle->getNamespace() . 00437 '|' . $this->rootTitle->getDBkey() . 00438 '|' . $lastPageID; 00439 } 00440 00441 protected function getContinueRedirStr( $lastPageID, $lastRedirID ) { 00442 return $this->getContinueStr( $lastPageID ) . '|' . $lastRedirID; 00443 } 00444 00445 public function getAllowedParams() { 00446 $retval = array( 00447 'title' => array( 00448 ApiBase::PARAM_TYPE => 'string', 00449 ), 00450 'pageid' => array( 00451 ApiBase::PARAM_TYPE => 'integer', 00452 ), 00453 'continue' => null, 00454 'namespace' => array( 00455 ApiBase::PARAM_ISMULTI => true, 00456 ApiBase::PARAM_TYPE => 'namespace' 00457 ), 00458 'dir' => array( 00459 ApiBase::PARAM_DFLT => 'ascending', 00460 ApiBase::PARAM_TYPE => array( 00461 'ascending', 00462 'descending' 00463 ) 00464 ), 00465 'filterredir' => array( 00466 ApiBase::PARAM_DFLT => 'all', 00467 ApiBase::PARAM_TYPE => array( 00468 'all', 00469 'redirects', 00470 'nonredirects' 00471 ) 00472 ), 00473 'limit' => array( 00474 ApiBase::PARAM_DFLT => 10, 00475 ApiBase::PARAM_TYPE => 'limit', 00476 ApiBase::PARAM_MIN => 1, 00477 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1, 00478 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2 00479 ) 00480 ); 00481 if ( $this->getModuleName() == 'embeddedin' ) { 00482 return $retval; 00483 } 00484 $retval['redirect'] = false; 00485 return $retval; 00486 } 00487 00488 public function getParamDescription() { 00489 $retval = array( 00490 'title' => "Title to search. Cannot be used together with {$this->bl_code}pageid", 00491 'pageid' => "Pageid to search. Cannot be used together with {$this->bl_code}title", 00492 'continue' => 'When more results are available, use this to continue', 00493 'namespace' => 'The namespace to enumerate', 00494 'dir' => 'The direction in which to list', 00495 ); 00496 if ( $this->getModuleName() != 'embeddedin' ) { 00497 return array_merge( $retval, array( 00498 'redirect' => 'If linking page is a redirect, find all pages that link to that redirect as well. Maximum limit is halved.', 00499 '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", 00500 '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)." 00501 ) ); 00502 } 00503 return array_merge( $retval, array( 00504 'filterredir' => 'How to filter for redirects', 00505 'limit' => 'How many total pages to return' 00506 ) ); 00507 } 00508 00509 public function getResultProperties() { 00510 return array( 00511 '' => array( 00512 'pageid' => 'integer', 00513 'ns' => 'namespace', 00514 'title' => 'string', 00515 'redirect' => 'boolean' 00516 ) 00517 ); 00518 } 00519 00520 public function getDescription() { 00521 switch ( $this->getModuleName() ) { 00522 case 'backlinks': 00523 return 'Find all pages that link to the given page'; 00524 case 'embeddedin': 00525 return 'Find all pages that embed (transclude) the given title'; 00526 case 'imageusage': 00527 return 'Find all pages that use the given image title.'; 00528 default: 00529 ApiBase::dieDebug( __METHOD__, 'Unknown module name' ); 00530 } 00531 } 00532 00533 public function getPossibleErrors() { 00534 return array_merge( parent::getPossibleErrors(), 00535 $this->getTitleOrPageIdErrorMessage(), 00536 array( 00537 array( 'code' => 'bad_image_title', 'info' => "The title for {$this->getModuleName()} query must be an image" ), 00538 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ), 00539 ) 00540 ); 00541 } 00542 00543 public function getExamples() { 00544 static $examples = array( 00545 'backlinks' => array( 00546 'api.php?action=query&list=backlinks&bltitle=Main%20Page', 00547 'api.php?action=query&generator=backlinks&gbltitle=Main%20Page&prop=info' 00548 ), 00549 'embeddedin' => array( 00550 'api.php?action=query&list=embeddedin&eititle=Template:Stub', 00551 'api.php?action=query&generator=embeddedin&geititle=Template:Stub&prop=info' 00552 ), 00553 'imageusage' => array( 00554 'api.php?action=query&list=imageusage&iutitle=File:Albert%20Einstein%20Head.jpg', 00555 'api.php?action=query&generator=imageusage&giutitle=File:Albert%20Einstein%20Head.jpg&prop=info' 00556 ) 00557 ); 00558 00559 return $examples[$this->getModuleName()]; 00560 } 00561 00562 public function getHelpUrls() { 00563 return $this->helpUrl; 00564 } 00565 00566 public function getVersion() { 00567 return __CLASS__ . ': $Id$'; 00568 } 00569 }