MediaWiki  REL1_20
ApiQueryLinks.php
Go to the documentation of this file.
00001 <?php
00032 class ApiQueryLinks extends ApiQueryGeneratorBase {
00033 
00034         const LINKS = 'links';
00035         const TEMPLATES = 'templates';
00036 
00037         private $table, $prefix, $description, $helpUrl;
00038 
00039         public function __construct( $query, $moduleName ) {
00040                 switch ( $moduleName ) {
00041                         case self::LINKS:
00042                                 $this->table = 'pagelinks';
00043                                 $this->prefix = 'pl';
00044                                 $this->description = 'link';
00045                                 $this->titlesParam = 'titles';
00046                                 $this->titlesParamDescription = 'Only list links to these titles. Useful for checking whether a certain page links to a certain title.';
00047                                 $this->helpUrl = 'https://www.mediawiki.org/wiki/API:Properties#links_.2F_pl';
00048                                 break;
00049                         case self::TEMPLATES:
00050                                 $this->table = 'templatelinks';
00051                                 $this->prefix = 'tl';
00052                                 $this->description = 'template';
00053                                 $this->titlesParam = 'templates';
00054                                 $this->titlesParamDescription = 'Only list these templates. Useful for checking whether a certain page uses a certain template.';
00055                                 $this->helpUrl = 'https://www.mediawiki.org/wiki/API:Properties#templates_.2F_tl';
00056                                 break;
00057                         default:
00058                                 ApiBase::dieDebug( __METHOD__, 'Unknown module name' );
00059                 }
00060 
00061                 parent::__construct( $query, $moduleName, $this->prefix );
00062         }
00063 
00064         public function execute() {
00065                 $this->run();
00066         }
00067 
00068         public function getCacheMode( $params ) {
00069                 return 'public';
00070         }
00071 
00072         public function executeGenerator( $resultPageSet ) {
00073                 $this->run( $resultPageSet );
00074         }
00075 
00080         private function run( $resultPageSet = null ) {
00081                 if ( $this->getPageSet()->getGoodTitleCount() == 0 ) {
00082                         return; // nothing to do
00083                 }
00084 
00085                 $params = $this->extractRequestParams();
00086 
00087                 $this->addFields( array(
00088                         'pl_from' => $this->prefix . '_from',
00089                         'pl_namespace' => $this->prefix . '_namespace',
00090                         'pl_title' => $this->prefix . '_title'
00091                 ) );
00092 
00093                 $this->addTables( $this->table );
00094                 $this->addWhereFld( $this->prefix . '_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
00095                 $this->addWhereFld( $this->prefix . '_namespace', $params['namespace'] );
00096 
00097                 if ( !is_null( $params[$this->titlesParam] ) ) {
00098                         $lb = new LinkBatch;
00099                         foreach ( $params[$this->titlesParam] as $t ) {
00100                                 $title = Title::newFromText( $t );
00101                                 if ( !$title ) {
00102                                         $this->setWarning( "\"$t\" is not a valid title" );
00103                                 } else {
00104                                         $lb->addObj( $title );
00105                                 }
00106                         }
00107                         $cond = $lb->constructSet( $this->prefix, $this->getDB() );
00108                         if ( $cond ) {
00109                                 $this->addWhere( $cond );
00110                         }
00111                 }
00112 
00113                 if ( !is_null( $params['continue'] ) ) {
00114                         $cont = explode( '|', $params['continue'] );
00115                         if ( count( $cont ) != 3 ) {
00116                                 $this->dieUsage( 'Invalid continue param. You should pass the ' .
00117                                         'original value returned by the previous query', '_badcontinue' );
00118                         }
00119                         $op = $params['dir'] == 'descending' ? '<' : '>';
00120                         $plfrom = intval( $cont[0] );
00121                         $plns = intval( $cont[1] );
00122                         $pltitle = $this->getDB()->addQuotes( $cont[2] );
00123                         $this->addWhere(
00124                                 "{$this->prefix}_from $op $plfrom OR " .
00125                                 "({$this->prefix}_from = $plfrom AND " .
00126                                 "({$this->prefix}_namespace $op $plns OR " .
00127                                 "({$this->prefix}_namespace = $plns AND " .
00128                                 "{$this->prefix}_title $op= $pltitle)))"
00129                         );
00130                 }
00131 
00132                 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
00133                 // Here's some MySQL craziness going on: if you use WHERE foo='bar'
00134                 // and later ORDER BY foo MySQL doesn't notice the ORDER BY is pointless
00135                 // but instead goes and filesorts, because the index for foo was used
00136                 // already. To work around this, we drop constant fields in the WHERE
00137                 // clause from the ORDER BY clause
00138                 $order = array();
00139                 if ( count( $this->getPageSet()->getGoodTitles() ) != 1 ) {
00140                         $order[] = $this->prefix . '_from' . $sort;
00141                 }
00142                 if ( count( $params['namespace'] ) != 1 ) {
00143                         $order[] = $this->prefix . '_namespace' . $sort;
00144                 }
00145 
00146                 $order[] = $this->prefix . '_title' . $sort;
00147                 $this->addOption( 'ORDER BY', $order );
00148                 $this->addOption( 'USE INDEX', $this->prefix . '_from' );
00149                 $this->addOption( 'LIMIT', $params['limit'] + 1 );
00150 
00151                 $res = $this->select( __METHOD__ );
00152 
00153                 if ( is_null( $resultPageSet ) ) {
00154                         $count = 0;
00155                         foreach ( $res as $row ) {
00156                                 if ( ++$count > $params['limit'] ) {
00157                                         // We've reached the one extra which shows that
00158                                         // there are additional pages to be had. Stop here...
00159                                         $this->setContinueEnumParameter( 'continue',
00160                                                 "{$row->pl_from}|{$row->pl_namespace}|{$row->pl_title}" );
00161                                         break;
00162                                 }
00163                                 $vals = array();
00164                                 ApiQueryBase::addTitleInfo( $vals, Title::makeTitle( $row->pl_namespace, $row->pl_title ) );
00165                                 $fit = $this->addPageSubItem( $row->pl_from, $vals );
00166                                 if ( !$fit ) {
00167                                         $this->setContinueEnumParameter( 'continue',
00168                                                 "{$row->pl_from}|{$row->pl_namespace}|{$row->pl_title}" );
00169                                         break;
00170                                 }
00171                         }
00172                 } else {
00173                         $titles = array();
00174                         $count = 0;
00175                         foreach ( $res as $row ) {
00176                                 if ( ++$count > $params['limit'] ) {
00177                                         // We've reached the one extra which shows that
00178                                         // there are additional pages to be had. Stop here...
00179                                         $this->setContinueEnumParameter( 'continue',
00180                                                 "{$row->pl_from}|{$row->pl_namespace}|{$row->pl_title}" );
00181                                         break;
00182                                 }
00183                                 $titles[] = Title::makeTitle( $row->pl_namespace, $row->pl_title );
00184                         }
00185                         $resultPageSet->populateFromTitles( $titles );
00186                 }
00187         }
00188 
00189         public function getAllowedParams() {
00190                 return array(
00191                         'namespace' => array(
00192                                 ApiBase::PARAM_TYPE => 'namespace',
00193                                 ApiBase::PARAM_ISMULTI => true
00194                         ),
00195                         'limit' => array(
00196                                 ApiBase::PARAM_DFLT => 10,
00197                                 ApiBase::PARAM_TYPE => 'limit',
00198                                 ApiBase::PARAM_MIN => 1,
00199                                 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
00200                                 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
00201                         ),
00202                         'continue' => null,
00203                         $this->titlesParam => array(
00204                                 ApiBase::PARAM_ISMULTI => true,
00205                         ),
00206                         'dir' => array(
00207                                 ApiBase::PARAM_DFLT => 'ascending',
00208                                 ApiBase::PARAM_TYPE => array(
00209                                         'ascending',
00210                                         'descending'
00211                                 )
00212                         ),
00213                 );
00214         }
00215 
00216         public function getParamDescription() {
00217                 $desc = $this->description;
00218                 return array(
00219                         'namespace' => "Show {$desc}s in this namespace(s) only",
00220                         'limit' => "How many {$desc}s to return",
00221                         'continue' => 'When more results are available, use this to continue',
00222                         $this->titlesParam => $this->titlesParamDescription,
00223                         'dir' => 'The direction in which to list',
00224                 );
00225         }
00226 
00227         public function getResultProperties() {
00228                 return array(
00229                         '' => array(
00230                                 'ns' => 'namespace',
00231                                 'title' => 'string'
00232                         )
00233                 );
00234         }
00235 
00236         public function getDescription() {
00237                 return "Returns all {$this->description}s from the given page(s)";
00238         }
00239 
00240         public function getExamples() {
00241                 $desc = $this->description;
00242                 $name = $this->getModuleName();
00243                 return array(
00244                         "api.php?action=query&prop={$name}&titles=Main%20Page" => "Get {$desc}s from the [[Main Page]]:",
00245                         "api.php?action=query&generator={$name}&titles=Main%20Page&prop=info" => "Get information about the {$desc} pages in the [[Main Page]]:",
00246                         "api.php?action=query&prop={$name}&titles=Main%20Page&{$this->prefix}namespace=2|10" => "Get {$desc}s from the Main Page in the User and Template namespaces:",
00247                 );
00248         }
00249 
00250         public function getHelpUrls() {
00251                 return $this->helpUrl;
00252         }
00253 
00254         public function getVersion() {
00255                 return __CLASS__ . ': $Id$';
00256         }
00257 }