MediaWiki  REL1_20
ApiQueryAllLinks.php
Go to the documentation of this file.
00001 <?php
00032 class ApiQueryAllLinks extends ApiQueryGeneratorBase {
00033 
00034         public function __construct( $query, $moduleName ) {
00035                 parent::__construct( $query, $moduleName, 'al' );
00036         }
00037 
00038         public function execute() {
00039                 $this->run();
00040         }
00041 
00042         public function getCacheMode( $params ) {
00043                 return 'public';
00044         }
00045 
00046         public function executeGenerator( $resultPageSet ) {
00047                 $this->run( $resultPageSet );
00048         }
00049 
00054         private function run( $resultPageSet = null ) {
00055                 $db = $this->getDB();
00056                 $params = $this->extractRequestParams();
00057 
00058                 $prop = array_flip( $params['prop'] );
00059                 $fld_ids = isset( $prop['ids'] );
00060                 $fld_title = isset( $prop['title'] );
00061 
00062                 if ( $params['unique'] ) {
00063                         if ( !is_null( $resultPageSet ) ) {
00064                                 $this->dieUsage( $this->getModuleName() . ' cannot be used as a generator in unique links mode', 'params' );
00065                         }
00066                         if ( $fld_ids ) {
00067                                 $this->dieUsage( $this->getModuleName() . ' cannot return corresponding page ids in unique links mode', 'params' );
00068                         }
00069                         $this->addOption( 'DISTINCT' );
00070                 }
00071 
00072                 $this->addTables( 'pagelinks' );
00073                 $this->addWhereFld( 'pl_namespace', $params['namespace'] );
00074 
00075                 if ( !is_null( $params['from'] ) && !is_null( $params['continue'] ) ) {
00076                         $this->dieUsage( 'alcontinue and alfrom cannot be used together', 'params' );
00077                 }
00078                 if ( !is_null( $params['continue'] ) ) {
00079                         $continueArr = explode( '|', $params['continue'] );
00080                         $op = $params['dir'] == 'descending' ? '<' : '>';
00081                         if ( $params['unique'] ) {
00082                                 if ( count( $continueArr ) != 1 ) {
00083                                         $this->dieUsage( 'Invalid continue parameter', 'badcontinue' );
00084                                 }
00085                                 $continueTitle = $db->addQuotes( $continueArr[0] );
00086                                 $this->addWhere( "pl_title $op= $continueTitle" );
00087                         } else {
00088                                 if ( count( $continueArr ) != 2 ) {
00089                                         $this->dieUsage( 'Invalid continue parameter', 'badcontinue' );
00090                                 }
00091                                 $continueTitle = $db->addQuotes( $continueArr[0] );
00092                                 $continueFrom = intval( $continueArr[1] );
00093                                 $this->addWhere(
00094                                         "pl_title $op $continueTitle OR " .
00095                                         "(pl_title = $continueTitle AND " .
00096                                         "pl_from $op= $continueFrom)"
00097                                 );
00098                         }
00099                 }
00100 
00101                 $from = ( is_null( $params['from'] ) ? null : $this->titlePartToKey( $params['from'] ) );
00102                 $to = ( is_null( $params['to'] ) ? null : $this->titlePartToKey( $params['to'] ) );
00103                 $this->addWhereRange( 'pl_title', 'newer', $from, $to );
00104 
00105                 if ( isset( $params['prefix'] ) ) {
00106                         $this->addWhere( 'pl_title' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
00107                 }
00108 
00109                 $this->addFields( 'pl_title' );
00110                 $this->addFieldsIf( 'pl_from', !$params['unique'] );
00111 
00112                 $this->addOption( 'USE INDEX', 'pl_namespace' );
00113                 $limit = $params['limit'];
00114                 $this->addOption( 'LIMIT', $limit + 1 );
00115 
00116                 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
00117                 $orderBy = array();
00118                 $orderBy[] = 'pl_title' . $sort;
00119                 if ( !$params['unique'] ) {
00120                         $orderBy[] = 'pl_from' . $sort;
00121                 }
00122                 $this->addOption( 'ORDER BY', $orderBy );
00123 
00124                 $res = $this->select( __METHOD__ );
00125 
00126                 $pageids = array();
00127                 $count = 0;
00128                 $result = $this->getResult();
00129                 foreach ( $res as $row ) {
00130                         if ( ++ $count > $limit ) {
00131                                 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
00132                                 if ( $params['unique'] ) {
00133                                         $this->setContinueEnumParameter( 'continue', $row->pl_title );
00134                                 } else {
00135                                         $this->setContinueEnumParameter( 'continue', $row->pl_title . "|" . $row->pl_from );
00136                                 }
00137                                 break;
00138                         }
00139 
00140                         if ( is_null( $resultPageSet ) ) {
00141                                 $vals = array();
00142                                 if ( $fld_ids ) {
00143                                         $vals['fromid'] = intval( $row->pl_from );
00144                                 }
00145                                 if ( $fld_title ) {
00146                                         $title = Title::makeTitle( $params['namespace'], $row->pl_title );
00147                                         ApiQueryBase::addTitleInfo( $vals, $title );
00148                                 }
00149                                 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
00150                                 if ( !$fit ) {
00151                                         if ( $params['unique'] ) {
00152                                                 $this->setContinueEnumParameter( 'continue', $row->pl_title );
00153                                         } else {
00154                                                 $this->setContinueEnumParameter( 'continue', $row->pl_title . "|" . $row->pl_from );
00155                                         }
00156                                         break;
00157                                 }
00158                         } else {
00159                                 $pageids[] = $row->pl_from;
00160                         }
00161                 }
00162 
00163                 if ( is_null( $resultPageSet ) ) {
00164                         $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'l' );
00165                 } else {
00166                         $resultPageSet->populateFromPageIDs( $pageids );
00167                 }
00168         }
00169 
00170         public function getAllowedParams() {
00171                 return array(
00172                         'continue' => null,
00173                         'from' => null,
00174                         'to' => null,
00175                         'prefix' => null,
00176                         'unique' => false,
00177                         'prop' => array(
00178                                 ApiBase::PARAM_ISMULTI => true,
00179                                 ApiBase::PARAM_DFLT => 'title',
00180                                 ApiBase::PARAM_TYPE => array(
00181                                         'ids',
00182                                         'title'
00183                                 )
00184                         ),
00185                         'namespace' => array(
00186                                 ApiBase::PARAM_DFLT => 0,
00187                                 ApiBase::PARAM_TYPE => 'namespace'
00188                         ),
00189                         'limit' => array(
00190                                 ApiBase::PARAM_DFLT => 10,
00191                                 ApiBase::PARAM_TYPE => 'limit',
00192                                 ApiBase::PARAM_MIN => 1,
00193                                 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
00194                                 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
00195                         ),
00196                         'dir' => array(
00197                                 ApiBase::PARAM_DFLT => 'ascending',
00198                                 ApiBase::PARAM_TYPE => array(
00199                                         'ascending',
00200                                         'descending'
00201                                 )
00202                         ),
00203                 );
00204         }
00205 
00206         public function getParamDescription() {
00207                 $p = $this->getModulePrefix();
00208                 return array(
00209                         'from' => 'The page title to start enumerating from',
00210                         'to' => 'The page title to stop enumerating at',
00211                         'prefix' => 'Search for all page titles that begin with this value',
00212                         'unique' => "Only show unique links. Cannot be used with generator or {$p}prop=ids",
00213                         'prop' => array(
00214                                 'What pieces of information to include',
00215                                 " ids    - Adds pageid of where the link is from (Cannot be used with {$p}unique)",
00216                                 ' title  - Adds the title of the link',
00217                         ),
00218                         'namespace' => 'The namespace to enumerate',
00219                         'limit' => 'How many total links to return',
00220                         'continue' => 'When more results are available, use this to continue',
00221                         'dir' => 'The direction in which to list',
00222                 );
00223         }
00224 
00225         public function getResultProperties() {
00226                 return array(
00227                         'ids' => array(
00228                                 'fromid' => 'integer'
00229                         ),
00230                         'title' => array(
00231                                 'ns' => 'namespace',
00232                                 'title' => 'string'
00233                         )
00234                 );
00235         }
00236 
00237         public function getDescription() {
00238                 return 'Enumerate all links that point to a given namespace';
00239         }
00240 
00241         public function getPossibleErrors() {
00242                 $m = $this->getModuleName();
00243                 return array_merge( parent::getPossibleErrors(), array(
00244                         array( 'code' => 'params', 'info' => "{$m} cannot be used as a generator in unique links mode" ),
00245                         array( 'code' => 'params', 'info' => "{$m} cannot return corresponding page ids in unique links mode" ),
00246                         array( 'code' => 'params', 'info' => 'alcontinue and alfrom cannot be used together' ),
00247                         array( 'code' => 'badcontinue', 'info' => 'Invalid continue parameter' ),
00248                 ) );
00249         }
00250 
00251         public function getExamples() {
00252                 return array(
00253                         'api.php?action=query&list=alllinks&alunique=&alfrom=B',
00254                 );
00255         }
00256 
00257         public function getHelpUrls() {
00258                 return 'https://www.mediawiki.org/wiki/API:Alllinks';
00259         }
00260 
00261         public function getVersion() {
00262                 return __CLASS__ . ': $Id$';
00263         }
00264 }