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