MediaWiki  REL1_19
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                         $this->prefix . '_from AS pl_from',
00089                         $this->prefix . '_namespace AS pl_namespace',
00090                         $this->prefix . '_title AS pl_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                         $plfrom = intval( $cont[0] );
00120                         $plns = intval( $cont[1] );
00121                         $pltitle = $this->getDB()->strencode( $this->titleToKey( $cont[2] ) );
00122                         $this->addWhere(
00123                                 "{$this->prefix}_from > $plfrom OR " .
00124                                 "({$this->prefix}_from = $plfrom AND " .
00125                                 "({$this->prefix}_namespace > $plns OR " .
00126                                 "({$this->prefix}_namespace = $plns AND " .
00127                                 "{$this->prefix}_title >= '$pltitle')))"
00128                         );
00129                 }
00130 
00131                 $dir = ( $params['dir'] == 'descending' ? ' DESC' : '' );
00132                 // Here's some MySQL craziness going on: if you use WHERE foo='bar'
00133                 // and later ORDER BY foo MySQL doesn't notice the ORDER BY is pointless
00134                 // but instead goes and filesorts, because the index for foo was used
00135                 // already. To work around this, we drop constant fields in the WHERE
00136                 // clause from the ORDER BY clause
00137                 $order = array();
00138                 if ( count( $this->getPageSet()->getGoodTitles() ) != 1 ) {
00139                         $order[] = $this->prefix . '_from' . $dir;
00140                 }
00141                 if ( count( $params['namespace'] ) != 1 ) {
00142                         $order[] = $this->prefix . '_namespace' . $dir;
00143                 }
00144 
00145                 $order[] = $this->prefix . "_title" . $dir;
00146                 $this->addOption( 'ORDER BY', $order );
00147                 $this->addOption( 'USE INDEX', $this->prefix . '_from' );
00148                 $this->addOption( 'LIMIT', $params['limit'] + 1 );
00149 
00150                 $res = $this->select( __METHOD__ );
00151 
00152                 if ( is_null( $resultPageSet ) ) {
00153                         $count = 0;
00154                         foreach ( $res as $row ) {
00155                                 if ( ++$count > $params['limit'] ) {
00156                                         // We've reached the one extra which shows that
00157                                         // there are additional pages to be had. Stop here...
00158                                         $this->setContinueEnumParameter( 'continue',
00159                                                 "{$row->pl_from}|{$row->pl_namespace}|" .
00160                                                 $this->keyToTitle( $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}|" .
00169                                                 $this->keyToTitle( $row->pl_title ) );
00170                                         break;
00171                                 }
00172                         }
00173                 } else {
00174                         $titles = array();
00175                         $count = 0;
00176                         foreach ( $res as $row ) {
00177                                 if ( ++$count > $params['limit'] ) {
00178                                         // We've reached the one extra which shows that
00179                                         // there are additional pages to be had. Stop here...
00180                                         $this->setContinueEnumParameter( 'continue',
00181                                                 "{$row->pl_from}|{$row->pl_namespace}|" .
00182                                                 $this->keyToTitle( $row->pl_title ) );
00183                                         break;
00184                                 }
00185                                 $titles[] = Title::makeTitle( $row->pl_namespace, $row->pl_title );
00186                         }
00187                         $resultPageSet->populateFromTitles( $titles );
00188                 }
00189         }
00190 
00191         public function getAllowedParams() {
00192                 return array(
00193                         'namespace' => array(
00194                                 ApiBase::PARAM_TYPE => 'namespace',
00195                                 ApiBase::PARAM_ISMULTI => true
00196                         ),
00197                         'limit' => array(
00198                                 ApiBase::PARAM_DFLT => 10,
00199                                 ApiBase::PARAM_TYPE => 'limit',
00200                                 ApiBase::PARAM_MIN => 1,
00201                                 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
00202                                 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
00203                         ),
00204                         'continue' => null,
00205                         $this->titlesParam => array(
00206                                 ApiBase::PARAM_ISMULTI => true,
00207                         ),
00208                         'dir' => array(
00209                                 ApiBase::PARAM_DFLT => 'ascending',
00210                                 ApiBase::PARAM_TYPE => array(
00211                                         'ascending',
00212                                         'descending'
00213                                 )
00214                         ),
00215                 );
00216         }
00217 
00218         public function getParamDescription() {
00219                 $desc = $this->description;
00220                 return array(
00221                         'namespace' => "Show {$desc}s in this namespace(s) only",
00222                         'limit' => "How many {$desc}s to return",
00223                         'continue' => 'When more results are available, use this to continue',
00224                         $this->titlesParam => $this->titlesParamDescription,
00225                         'dir' => 'The direction in which to list',
00226                 );
00227         }
00228 
00229         public function getDescription() {
00230                 return "Returns all {$this->description}s from the given page(s)";
00231         }
00232 
00233         public function getExamples() {
00234                 $desc = $this->description;
00235                 $name = $this->getModuleName();
00236                 return array(
00237                         "api.php?action=query&prop={$name}&titles=Main%20Page" => "Get {$desc}s from the [[Main Page]]:",
00238                         "api.php?action=query&generator={$name}&titles=Main%20Page&prop=info" => "Get information about the {$desc} pages in the [[Main Page]]:",
00239                         "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:",
00240                 );
00241         }
00242 
00243         public function getHelpUrls() {
00244                 return $this->helpUrl;
00245         }
00246 
00247         public function getVersion() {
00248                 return __CLASS__ . ': $Id$';
00249         }
00250 }