MediaWiki  REL1_19
ApiQueryLangLinks.php
Go to the documentation of this file.
00001 <?php
00032 class ApiQueryLangLinks extends ApiQueryBase {
00033 
00034         public function __construct( $query, $moduleName ) {
00035                 parent::__construct( $query, $moduleName, 'll' );
00036         }
00037 
00038         public function execute() {
00039                 if ( $this->getPageSet()->getGoodTitleCount() == 0 ) {
00040                         return;
00041                 }
00042 
00043                 $params = $this->extractRequestParams();
00044 
00045                 if ( isset( $params['title'] ) && !isset( $params['lang'] ) ) {
00046                         $this->dieUsageMsg( array( 'missingparam', 'lang' ) );
00047                 }
00048 
00049                 $this->addFields( array(
00050                         'll_from',
00051                         'll_lang',
00052                         'll_title'
00053                 ) );
00054 
00055                 $this->addTables( 'langlinks' );
00056                 $this->addWhereFld( 'll_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
00057                 if ( !is_null( $params['continue'] ) ) {
00058                         $cont = explode( '|', $params['continue'] );
00059                         if ( count( $cont ) != 2 ) {
00060                                 $this->dieUsage( 'Invalid continue param. You should pass the ' .
00061                                         'original value returned by the previous query', '_badcontinue' );
00062                         }
00063                         $llfrom = intval( $cont[0] );
00064                         $lllang = $this->getDB()->strencode( $cont[1] );
00065                         $this->addWhere(
00066                                 "ll_from > $llfrom OR " .
00067                                 "(ll_from = $llfrom AND " .
00068                                 "ll_lang >= '$lllang')"
00069                         );
00070                 }
00071 
00072             $dir = ( $params['dir'] == 'descending' ? ' DESC' : '' );
00073             if ( isset( $params['lang'] ) ) {
00074                         $this->addWhereFld( 'll_lang', $params['lang'] );
00075                         if ( isset( $params['title'] ) ) {
00076                                 $this->addWhereFld( 'll_title', $params['title'] );
00077                                 $this->addOption( 'ORDER BY', 'll_from' . $dir );
00078                         } else {
00079                                 $this->addOption( 'ORDER BY', array(
00080                                                         'll_title' . $dir,
00081                                                         'll_from' . $dir
00082                                 ));
00083                         }
00084                 } else {
00085                         // Don't order by ll_from if it's constant in the WHERE clause
00086                         if ( count( $this->getPageSet()->getGoodTitles() ) == 1 ) {
00087                                 $this->addOption( 'ORDER BY', 'll_lang' . $dir );
00088                         } else {
00089                                 $this->addOption( 'ORDER BY', array(
00090                                                         'll_from' . $dir,
00091                                                         'll_lang' . $dir
00092                                 ));
00093                         }
00094                 }
00095 
00096                 $this->addOption( 'LIMIT', $params['limit'] + 1 );
00097                 $res = $this->select( __METHOD__ );
00098 
00099                 $count = 0;
00100                 foreach ( $res as $row ) {
00101                         if ( ++$count > $params['limit'] ) {
00102                                 // We've reached the one extra which shows that
00103                                 // there are additional pages to be had. Stop here...
00104                                 $this->setContinueEnumParameter( 'continue', "{$row->ll_from}|{$row->ll_lang}" );
00105                                 break;
00106                         }
00107                         $entry = array( 'lang' => $row->ll_lang );
00108                         if ( $params['url'] ) {
00109                                 $title = Title::newFromText( "{$row->ll_lang}:{$row->ll_title}" );
00110                                 if ( $title ) {
00111                                         $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
00112                                 }
00113                         }
00114                         ApiResult::setContent( $entry, $row->ll_title );
00115                         $fit = $this->addPageSubItem( $row->ll_from, $entry );
00116                         if ( !$fit ) {
00117                                 $this->setContinueEnumParameter( 'continue', "{$row->ll_from}|{$row->ll_lang}" );
00118                                 break;
00119                         }
00120                 }
00121         }
00122 
00123         public function getCacheMode( $params ) {
00124                 return 'public';
00125         }
00126 
00127         public function getAllowedParams() {
00128                 return array(
00129                         'limit' => array(
00130                                 ApiBase::PARAM_DFLT => 10,
00131                                 ApiBase::PARAM_TYPE => 'limit',
00132                                 ApiBase::PARAM_MIN => 1,
00133                                 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
00134                                 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
00135                         ),
00136                         'continue' => null,
00137                         'url' => false,
00138                         'lang' => null,
00139                         'title' => null,
00140                         'dir' => array(
00141                                 ApiBase::PARAM_DFLT => 'ascending',
00142                                 ApiBase::PARAM_TYPE => array(
00143                                         'ascending',
00144                                         'descending'
00145                                 )
00146                         ),
00147                 );
00148         }
00149 
00150         public function getParamDescription() {
00151                 return array(
00152                         'limit' => 'How many langlinks to return',
00153                         'continue' => 'When more results are available, use this to continue',
00154                         'url' => 'Whether to get the full URL',
00155                         'lang' => 'Language code',
00156                         'title' => "Link to search for. Must be used with {$this->getModulePrefix()}lang",
00157                         'dir' => 'The direction in which to list',
00158                 );
00159         }
00160 
00161         public function getDescription() {
00162                 return 'Returns all interlanguage links from the given page(s)';
00163         }
00164 
00165         public function getPossibleErrors() {
00166                 return array_merge( parent::getPossibleErrors(), array(
00167                         array( 'missingparam', 'lang' ),
00168                         array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
00169                 ) );
00170         }
00171 
00172         public function getExamples() {
00173                 return array(
00174                         'api.php?action=query&prop=langlinks&titles=Main%20Page&redirects=' => 'Get interlanguage links from the [[Main Page]]',
00175                 );
00176         }
00177 
00178         public function getHelpUrls() {
00179                 return 'https://www.mediawiki.org/wiki/API:Properties#langlinks_.2F_ll';
00180         }
00181 
00182         public function getVersion() {
00183                 return __CLASS__ . ': $Id$';
00184         }
00185 }