MediaWiki
REL1_21
|
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(); 00192 foreach ( $this->redirTitles as $t ) { 00193 $redirNs = $t->getNamespace(); 00194 $redirDBkey = $t->getDBkey(); 00195 $titleWhere[] = "{$this->bl_title} = " . $db->addQuotes( $redirDBkey ) . 00196 ( $this->hasNS ? " AND {$this->bl_ns} = {$redirNs}" : '' ); 00197 $allRedirNs[] = $redirNs; 00198 $allRedirDBkey[] = $redirDBkey; 00199 } 00200 $this->addWhere( $db->makeList( $titleWhere, LIST_OR ) ); 00201 $this->addWhereFld( 'page_namespace', $this->params['namespace'] ); 00202 00203 if ( !is_null( $this->redirID ) ) { 00204 $op = $this->params['dir'] == 'descending' ? '<' : '>'; 00206 $first = $this->redirTitles[0]; 00207 $title = $db->addQuotes( $first->getDBkey() ); 00208 $ns = $first->getNamespace(); 00209 $from = $this->redirID; 00210 if ( $this->hasNS ) { 00211 $this->addWhere( "{$this->bl_ns} $op $ns OR " . 00212 "({$this->bl_ns} = $ns AND " . 00213 "({$this->bl_title} $op $title OR " . 00214 "({$this->bl_title} = $title AND " . 00215 "{$this->bl_from} $op= $from)))" ); 00216 } else { 00217 $this->addWhere( "{$this->bl_title} $op $title OR " . 00218 "({$this->bl_title} = $title AND " . 00219 "{$this->bl_from} $op= $from)" ); 00220 } 00221 } 00222 if ( $this->params['filterredir'] == 'redirects' ) { 00223 $this->addWhereFld( 'page_is_redirect', 1 ); 00224 } elseif ( $this->params['filterredir'] == 'nonredirects' ) { 00225 $this->addWhereFld( 'page_is_redirect', 0 ); 00226 } 00227 00228 $this->addOption( 'LIMIT', $this->params['limit'] + 1 ); 00229 $orderBy = array(); 00230 $sort = ( $this->params['dir'] == 'descending' ? ' DESC' : '' ); 00231 // Don't order by namespace/title if it's constant in the WHERE clause 00232 if( $this->hasNS && count( array_unique( $allRedirNs ) ) != 1 ) { 00233 $orderBy[] = $this->bl_ns . $sort; 00234 } 00235 if( count( array_unique( $allRedirDBkey ) ) != 1 ) { 00236 $orderBy[] = $this->bl_title . $sort; 00237 } 00238 $orderBy[] = $this->bl_from . $sort; 00239 $this->addOption( 'ORDER BY', $orderBy ); 00240 $this->addOption( 'USE INDEX', array( 'page' => 'PRIMARY' ) ); 00241 } 00242 00247 private function run( $resultPageSet = null ) { 00248 $this->params = $this->extractRequestParams( false ); 00249 $this->redirect = isset( $this->params['redirect'] ) && $this->params['redirect']; 00250 $userMax = ( $this->redirect ? ApiBase::LIMIT_BIG1 / 2 : ApiBase::LIMIT_BIG1 ); 00251 $botMax = ( $this->redirect ? ApiBase::LIMIT_BIG2 / 2 : ApiBase::LIMIT_BIG2 ); 00252 00253 $result = $this->getResult(); 00254 00255 if ( $this->params['limit'] == 'max' ) { 00256 $this->params['limit'] = $this->getMain()->canApiHighLimits() ? $botMax : $userMax; 00257 $result->setParsedLimit( $this->getModuleName(), $this->params['limit'] ); 00258 } 00259 00260 $this->processContinue(); 00261 $this->prepareFirstQuery( $resultPageSet ); 00262 00263 $res = $this->select( __METHOD__ . '::firstQuery' ); 00264 00265 $count = 0; 00266 00267 foreach ( $res as $row ) { 00268 if ( ++ $count > $this->params['limit'] ) { 00269 // We've reached the one extra which shows that there are additional pages to be had. Stop here... 00270 // Continue string preserved in case the redirect query doesn't pass the limit 00271 $this->continueStr = $this->getContinueStr( $row->page_id ); 00272 break; 00273 } 00274 00275 if ( is_null( $resultPageSet ) ) { 00276 $this->extractRowInfo( $row ); 00277 } else { 00278 $this->pageMap[$row->page_namespace][$row->page_title] = $row->page_id; 00279 if ( $row->page_is_redirect ) { 00280 $this->redirTitles[] = Title::makeTitle( $row->page_namespace, $row->page_title ); 00281 } 00282 00283 $resultPageSet->processDbRow( $row ); 00284 } 00285 } 00286 00287 if ( $this->redirect && count( $this->redirTitles ) ) { 00288 $this->resetQueryParams(); 00289 $this->prepareSecondQuery( $resultPageSet ); 00290 $res = $this->select( __METHOD__ . '::secondQuery' ); 00291 $count = 0; 00292 foreach ( $res as $row ) { 00293 if ( ++$count > $this->params['limit'] ) { 00294 // We've reached the one extra which shows that there are additional pages to be had. Stop here... 00295 // We need to keep the parent page of this redir in 00296 if ( $this->hasNS ) { 00297 $parentID = $this->pageMap[$row-> { $this->bl_ns } ][$row-> { $this->bl_title } ]; 00298 } else { 00299 $parentID = $this->pageMap[NS_FILE][$row-> { $this->bl_title } ]; 00300 } 00301 $this->continueStr = $this->getContinueRedirStr( $parentID, $row->page_id ); 00302 break; 00303 } 00304 00305 if ( is_null( $resultPageSet ) ) { 00306 $this->extractRedirRowInfo( $row ); 00307 } else { 00308 $resultPageSet->processDbRow( $row ); 00309 } 00310 } 00311 } 00312 if ( is_null( $resultPageSet ) ) { 00313 // Try to add the result data in one go and pray that it fits 00314 $fit = $result->addValue( 'query', $this->getModuleName(), array_values( $this->resultArr ) ); 00315 if ( !$fit ) { 00316 // It didn't fit. Add elements one by one until the 00317 // result is full. 00318 foreach ( $this->resultArr as $pageID => $arr ) { 00319 // Add the basic entry without redirlinks first 00320 $fit = $result->addValue( 00321 array( 'query', $this->getModuleName() ), 00322 null, array_diff_key( $arr, array( 'redirlinks' => '' ) ) ); 00323 if ( !$fit ) { 00324 $this->continueStr = $this->getContinueStr( $pageID ); 00325 break; 00326 } 00327 00328 $hasRedirs = false; 00329 $redirLinks = isset( $arr['redirlinks'] ) ? $arr['redirlinks'] : array(); 00330 foreach ( (array)$redirLinks as $key => $redir ) { 00331 $fit = $result->addValue( 00332 array( 'query', $this->getModuleName(), $pageID, 'redirlinks' ), 00333 $key, $redir ); 00334 if ( !$fit ) { 00335 $this->continueStr = $this->getContinueRedirStr( $pageID, $redir['pageid'] ); 00336 break; 00337 } 00338 $hasRedirs = true; 00339 } 00340 if ( $hasRedirs ) { 00341 $result->setIndexedTagName_internal( 00342 array( 'query', $this->getModuleName(), $pageID, 'redirlinks' ), 00343 $this->bl_code ); 00344 } 00345 if ( !$fit ) { 00346 break; 00347 } 00348 } 00349 } 00350 00351 $result->setIndexedTagName_internal( 00352 array( 'query', $this->getModuleName() ), 00353 $this->bl_code 00354 ); 00355 } 00356 if ( !is_null( $this->continueStr ) ) { 00357 $this->setContinueEnumParameter( 'continue', $this->continueStr ); 00358 } 00359 } 00360 00361 private function extractRowInfo( $row ) { 00362 $this->pageMap[$row->page_namespace][$row->page_title] = $row->page_id; 00363 $t = Title::makeTitle( $row->page_namespace, $row->page_title ); 00364 $a = array( 'pageid' => intval( $row->page_id ) ); 00365 ApiQueryBase::addTitleInfo( $a, $t ); 00366 if ( $row->page_is_redirect ) { 00367 $a['redirect'] = ''; 00368 $this->redirTitles[] = $t; 00369 } 00370 // Put all the results in an array first 00371 $this->resultArr[$a['pageid']] = $a; 00372 } 00373 00374 private function extractRedirRowInfo( $row ) { 00375 $a['pageid'] = intval( $row->page_id ); 00376 ApiQueryBase::addTitleInfo( $a, Title::makeTitle( $row->page_namespace, $row->page_title ) ); 00377 if ( $row->page_is_redirect ) { 00378 $a['redirect'] = ''; 00379 } 00380 $ns = $this->hasNS ? $row-> { $this->bl_ns } : NS_FILE; 00381 $parentID = $this->pageMap[$ns][$row-> { $this->bl_title } ]; 00382 // Put all the results in an array first 00383 $this->resultArr[$parentID]['redirlinks'][] = $a; 00384 $this->getResult()->setIndexedTagName( $this->resultArr[$parentID]['redirlinks'], $this->bl_code ); 00385 } 00386 00387 protected function processContinue() { 00388 if ( !is_null( $this->params['continue'] ) ) { 00389 $this->parseContinueParam(); 00390 } else { 00391 $this->rootTitle = $this->getTitleOrPageId( $this->params )->getTitle(); 00392 } 00393 00394 // only image titles are allowed for the root in imageinfo mode 00395 if ( !$this->hasNS && $this->rootTitle->getNamespace() !== NS_FILE ) { 00396 $this->dieUsage( "The title for {$this->getModuleName()} query must be an image", 'bad_image_title' ); 00397 } 00398 } 00399 00400 protected function parseContinueParam() { 00401 $continueList = explode( '|', $this->params['continue'] ); 00402 // expected format: 00403 // ns | key | id1 [| id2] 00404 // ns+key: root title 00405 // id1: first-level page ID to continue from 00406 // id2: second-level page ID to continue from 00407 00408 // null stuff out now so we know what's set and what isn't 00409 $this->rootTitle = $this->contID = $this->redirID = null; 00410 $rootNs = intval( $continueList[0] ); 00411 $this->dieContinueUsageIf( $rootNs === 0 && $continueList[0] !== '0' ); 00412 00413 $this->rootTitle = Title::makeTitleSafe( $rootNs, $continueList[1] ); 00414 $this->dieContinueUsageIf( !$this->rootTitle ); 00415 00416 $contID = intval( $continueList[2] ); 00417 $this->dieContinueUsageIf( $contID === 0 && $continueList[2] !== '0' ); 00418 00419 $this->contID = $contID; 00420 $id2 = isset( $continueList[3] ) ? $continueList[3] : null; 00421 $redirID = intval( $id2 ); 00422 00423 if ( $redirID === 0 && $id2 !== '0' ) { 00424 // This one isn't required 00425 return; 00426 } 00427 $this->redirID = $redirID; 00428 00429 } 00430 00431 protected function getContinueStr( $lastPageID ) { 00432 return $this->rootTitle->getNamespace() . 00433 '|' . $this->rootTitle->getDBkey() . 00434 '|' . $lastPageID; 00435 } 00436 00437 protected function getContinueRedirStr( $lastPageID, $lastRedirID ) { 00438 return $this->getContinueStr( $lastPageID ) . '|' . $lastRedirID; 00439 } 00440 00441 public function getAllowedParams() { 00442 $retval = array( 00443 'title' => array( 00444 ApiBase::PARAM_TYPE => 'string', 00445 ), 00446 'pageid' => array( 00447 ApiBase::PARAM_TYPE => 'integer', 00448 ), 00449 'continue' => null, 00450 'namespace' => array( 00451 ApiBase::PARAM_ISMULTI => true, 00452 ApiBase::PARAM_TYPE => 'namespace' 00453 ), 00454 'dir' => array( 00455 ApiBase::PARAM_DFLT => 'ascending', 00456 ApiBase::PARAM_TYPE => array( 00457 'ascending', 00458 'descending' 00459 ) 00460 ), 00461 'filterredir' => array( 00462 ApiBase::PARAM_DFLT => 'all', 00463 ApiBase::PARAM_TYPE => array( 00464 'all', 00465 'redirects', 00466 'nonredirects' 00467 ) 00468 ), 00469 'limit' => array( 00470 ApiBase::PARAM_DFLT => 10, 00471 ApiBase::PARAM_TYPE => 'limit', 00472 ApiBase::PARAM_MIN => 1, 00473 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1, 00474 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2 00475 ) 00476 ); 00477 if ( $this->getModuleName() == 'embeddedin' ) { 00478 return $retval; 00479 } 00480 $retval['redirect'] = false; 00481 return $retval; 00482 } 00483 00484 public function getParamDescription() { 00485 $retval = array( 00486 'title' => "Title to search. Cannot be used together with {$this->bl_code}pageid", 00487 'pageid' => "Pageid to search. Cannot be used together with {$this->bl_code}title", 00488 'continue' => 'When more results are available, use this to continue', 00489 'namespace' => 'The namespace to enumerate', 00490 'dir' => 'The direction in which to list', 00491 ); 00492 if ( $this->getModuleName() != 'embeddedin' ) { 00493 return array_merge( $retval, array( 00494 'redirect' => 'If linking page is a redirect, find all pages that link to that redirect as well. Maximum limit is halved.', 00495 '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", 00496 '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)." 00497 ) ); 00498 } 00499 return array_merge( $retval, array( 00500 'filterredir' => 'How to filter for redirects', 00501 'limit' => 'How many total pages to return' 00502 ) ); 00503 } 00504 00505 public function getResultProperties() { 00506 return array( 00507 '' => array( 00508 'pageid' => 'integer', 00509 'ns' => 'namespace', 00510 'title' => 'string', 00511 'redirect' => 'boolean' 00512 ) 00513 ); 00514 } 00515 00516 public function getDescription() { 00517 switch ( $this->getModuleName() ) { 00518 case 'backlinks': 00519 return 'Find all pages that link to the given page'; 00520 case 'embeddedin': 00521 return 'Find all pages that embed (transclude) the given title'; 00522 case 'imageusage': 00523 return 'Find all pages that use the given image title.'; 00524 default: 00525 ApiBase::dieDebug( __METHOD__, 'Unknown module name' ); 00526 } 00527 } 00528 00529 public function getPossibleErrors() { 00530 return array_merge( parent::getPossibleErrors(), 00531 $this->getTitleOrPageIdErrorMessage(), 00532 array( 00533 array( 'code' => 'bad_image_title', 'info' => "The title for {$this->getModuleName()} query must be an image" ), 00534 ) 00535 ); 00536 } 00537 00538 public function getExamples() { 00539 static $examples = array( 00540 'backlinks' => array( 00541 'api.php?action=query&list=backlinks&bltitle=Main%20Page', 00542 'api.php?action=query&generator=backlinks&gbltitle=Main%20Page&prop=info' 00543 ), 00544 'embeddedin' => array( 00545 'api.php?action=query&list=embeddedin&eititle=Template:Stub', 00546 'api.php?action=query&generator=embeddedin&geititle=Template:Stub&prop=info' 00547 ), 00548 'imageusage' => array( 00549 'api.php?action=query&list=imageusage&iutitle=File:Albert%20Einstein%20Head.jpg', 00550 'api.php?action=query&generator=imageusage&giutitle=File:Albert%20Einstein%20Head.jpg&prop=info' 00551 ) 00552 ); 00553 00554 return $examples[$this->getModuleName()]; 00555 } 00556 00557 public function getHelpUrls() { 00558 return $this->helpUrl; 00559 } 00560 }