MediaWiki  REL1_22
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             $this->dieContinueUsageIf( count( $cont ) != 2 );
00060             $op = $params['dir'] == 'descending' ? '<' : '>';
00061             $llfrom = intval( $cont[0] );
00062             $lllang = $this->getDB()->addQuotes( $cont[1] );
00063             $this->addWhere(
00064                 "ll_from $op $llfrom OR " .
00065                 "(ll_from = $llfrom AND " .
00066                 "ll_lang $op= $lllang)"
00067             );
00068         }
00069 
00070         //FIXME: (follow-up) To allow extensions to add to the language links, we need
00071         //       to load them all, add the extra links, then apply paging.
00072         //       Should not be terrible, it's not going to be more than a few hundred links.
00073 
00074         // Note that, since (ll_from, ll_lang) is a unique key, we don't need
00075         // to sort by ll_title to ensure deterministic ordering.
00076         $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
00077         if ( isset( $params['lang'] ) ) {
00078             $this->addWhereFld( 'll_lang', $params['lang'] );
00079             if ( isset( $params['title'] ) ) {
00080                 $this->addWhereFld( 'll_title', $params['title'] );
00081             }
00082             $this->addOption( 'ORDER BY', 'll_from' . $sort );
00083         } else {
00084             // Don't order by ll_from if it's constant in the WHERE clause
00085             if ( count( $this->getPageSet()->getGoodTitles() ) == 1 ) {
00086                 $this->addOption( 'ORDER BY', 'll_lang' . $sort );
00087             } else {
00088                 $this->addOption( 'ORDER BY', array(
00089                             'll_from' . $sort,
00090                             'll_lang' . $sort
00091                 ));
00092             }
00093         }
00094 
00095         $this->addOption( 'LIMIT', $params['limit'] + 1 );
00096         $res = $this->select( __METHOD__ );
00097 
00098         $count = 0;
00099         foreach ( $res as $row ) {
00100             if ( ++$count > $params['limit'] ) {
00101                 // We've reached the one extra which shows that
00102                 // there are additional pages to be had. Stop here...
00103                 $this->setContinueEnumParameter( 'continue', "{$row->ll_from}|{$row->ll_lang}" );
00104                 break;
00105             }
00106             $entry = array( 'lang' => $row->ll_lang );
00107             if ( $params['url'] ) {
00108                 $title = Title::newFromText( "{$row->ll_lang}:{$row->ll_title}" );
00109                 if ( $title ) {
00110                     $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
00111                 }
00112             }
00113             ApiResult::setContent( $entry, $row->ll_title );
00114             $fit = $this->addPageSubItem( $row->ll_from, $entry );
00115             if ( !$fit ) {
00116                 $this->setContinueEnumParameter( 'continue', "{$row->ll_from}|{$row->ll_lang}" );
00117                 break;
00118             }
00119         }
00120     }
00121 
00122     public function getCacheMode( $params ) {
00123         return 'public';
00124     }
00125 
00126     public function getAllowedParams() {
00127         return array(
00128             'limit' => array(
00129                 ApiBase::PARAM_DFLT => 10,
00130                 ApiBase::PARAM_TYPE => 'limit',
00131                 ApiBase::PARAM_MIN => 1,
00132                 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
00133                 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
00134             ),
00135             'continue' => null,
00136             'url' => false,
00137             'lang' => null,
00138             'title' => null,
00139             'dir' => array(
00140                 ApiBase::PARAM_DFLT => 'ascending',
00141                 ApiBase::PARAM_TYPE => array(
00142                     'ascending',
00143                     'descending'
00144                 )
00145             ),
00146         );
00147     }
00148 
00149     public function getParamDescription() {
00150         return array(
00151             'limit' => 'How many langlinks to return',
00152             'continue' => 'When more results are available, use this to continue',
00153             'url' => 'Whether to get the full URL',
00154             'lang' => 'Language code',
00155             'title' => "Link to search for. Must be used with {$this->getModulePrefix()}lang",
00156             'dir' => 'The direction in which to list',
00157         );
00158     }
00159 
00160     public function getResultProperties() {
00161         return array(
00162             '' => array(
00163                 'lang' => 'string',
00164                 'url' => array(
00165                     ApiBase::PROP_TYPE => 'string',
00166                     ApiBase::PROP_NULLABLE => true
00167                 ),
00168                 '*' => 'string'
00169             )
00170         );
00171     }
00172 
00173     public function getDescription() {
00174         return 'Returns all interlanguage links from the given page(s)';
00175     }
00176 
00177     public function getPossibleErrors() {
00178         return array_merge( parent::getPossibleErrors(), array(
00179             array( 'missingparam', 'lang' ),
00180         ) );
00181     }
00182 
00183     public function getExamples() {
00184         return array(
00185             'api.php?action=query&prop=langlinks&titles=Main%20Page&redirects=' => 'Get interlanguage links from the [[Main Page]]',
00186         );
00187     }
00188 
00189     public function getHelpUrls() {
00190         return 'https://www.mediawiki.org/wiki/API:Properties#langlinks_.2F_ll';
00191     }
00192 }