MediaWiki  REL1_24
ApiQueryLangLinks.php
Go to the documentation of this file.
00001 <?php
00032 class ApiQueryLangLinks extends ApiQueryBase {
00033 
00034     public function __construct( ApiQuery $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         $prop = array_flip( (array)$params['prop'] );
00045 
00046         if ( isset( $params['title'] ) && !isset( $params['lang'] ) ) {
00047             $this->dieUsageMsg( array( 'missingparam', 'lang' ) );
00048         }
00049 
00050         // Handle deprecated param
00051         $this->requireMaxOneParameter( $params, 'url', 'prop' );
00052         if ( $params['url'] ) {
00053             $this->logFeatureUsage( 'prop=langlinks&llurl' );
00054             $prop = array( 'url' => 1 );
00055         }
00056 
00057         $this->addFields( array(
00058             'll_from',
00059             'll_lang',
00060             'll_title'
00061         ) );
00062 
00063         $this->addTables( 'langlinks' );
00064         $this->addWhereFld( 'll_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
00065         if ( !is_null( $params['continue'] ) ) {
00066             $cont = explode( '|', $params['continue'] );
00067             $this->dieContinueUsageIf( count( $cont ) != 2 );
00068             $op = $params['dir'] == 'descending' ? '<' : '>';
00069             $llfrom = intval( $cont[0] );
00070             $lllang = $this->getDB()->addQuotes( $cont[1] );
00071             $this->addWhere(
00072                 "ll_from $op $llfrom OR " .
00073                 "(ll_from = $llfrom AND " .
00074                 "ll_lang $op= $lllang)"
00075             );
00076         }
00077 
00078         //FIXME: (follow-up) To allow extensions to add to the language links, we need
00079         //       to load them all, add the extra links, then apply paging.
00080         //       Should not be terrible, it's not going to be more than a few hundred links.
00081 
00082         // Note that, since (ll_from, ll_lang) is a unique key, we don't need
00083         // to sort by ll_title to ensure deterministic ordering.
00084         $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
00085         if ( isset( $params['lang'] ) ) {
00086             $this->addWhereFld( 'll_lang', $params['lang'] );
00087             if ( isset( $params['title'] ) ) {
00088                 $this->addWhereFld( 'll_title', $params['title'] );
00089             }
00090             $this->addOption( 'ORDER BY', 'll_from' . $sort );
00091         } else {
00092             // Don't order by ll_from if it's constant in the WHERE clause
00093             if ( count( $this->getPageSet()->getGoodTitles() ) == 1 ) {
00094                 $this->addOption( 'ORDER BY', 'll_lang' . $sort );
00095             } else {
00096                 $this->addOption( 'ORDER BY', array(
00097                     'll_from' . $sort,
00098                     'll_lang' . $sort
00099                 ) );
00100             }
00101         }
00102 
00103         $this->addOption( 'LIMIT', $params['limit'] + 1 );
00104         $res = $this->select( __METHOD__ );
00105 
00106         $count = 0;
00107         foreach ( $res as $row ) {
00108             if ( ++$count > $params['limit'] ) {
00109                 // We've reached the one extra which shows that
00110                 // there are additional pages to be had. Stop here...
00111                 $this->setContinueEnumParameter( 'continue', "{$row->ll_from}|{$row->ll_lang}" );
00112                 break;
00113             }
00114             $entry = array( 'lang' => $row->ll_lang );
00115             if ( isset( $prop['url'] ) ) {
00116                 $title = Title::newFromText( "{$row->ll_lang}:{$row->ll_title}" );
00117                 if ( $title ) {
00118                     $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
00119                 }
00120             }
00121             if ( isset( $prop['langname'] ) ) {
00122                 $entry['langname'] = Language::fetchLanguageName( $row->ll_lang, $params['inlanguagecode'] );
00123             }
00124             if ( isset( $prop['autonym'] ) ) {
00125                 $entry['autonym'] = Language::fetchLanguageName( $row->ll_lang );
00126             }
00127             ApiResult::setContent( $entry, $row->ll_title );
00128             $fit = $this->addPageSubItem( $row->ll_from, $entry );
00129             if ( !$fit ) {
00130                 $this->setContinueEnumParameter( 'continue', "{$row->ll_from}|{$row->ll_lang}" );
00131                 break;
00132             }
00133         }
00134     }
00135 
00136     public function getCacheMode( $params ) {
00137         return 'public';
00138     }
00139 
00140     public function getAllowedParams() {
00141         global $wgContLang;
00142         return array(
00143             'limit' => array(
00144                 ApiBase::PARAM_DFLT => 10,
00145                 ApiBase::PARAM_TYPE => 'limit',
00146                 ApiBase::PARAM_MIN => 1,
00147                 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
00148                 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
00149             ),
00150             'continue' => null,
00151             'url' => array(
00152                 ApiBase::PARAM_DFLT => false,
00153                 ApiBase::PARAM_DEPRECATED => true,
00154             ),
00155             'prop' => array(
00156                 ApiBase::PARAM_ISMULTI => true,
00157                 ApiBase::PARAM_TYPE => array(
00158                     'url',
00159                     'langname',
00160                     'autonym',
00161                 )
00162             ),
00163             'lang' => null,
00164             'title' => null,
00165             'dir' => array(
00166                 ApiBase::PARAM_DFLT => 'ascending',
00167                 ApiBase::PARAM_TYPE => array(
00168                     'ascending',
00169                     'descending'
00170                 )
00171             ),
00172             'inlanguagecode' => $wgContLang->getCode(),
00173         );
00174     }
00175 
00176     public function getParamDescription() {
00177         return array(
00178             'limit' => 'How many langlinks to return',
00179             'continue' => 'When more results are available, use this to continue',
00180             'url' => "Whether to get the full URL (Cannot be used with {$this->getModulePrefix()}prop)",
00181             'prop' => array(
00182                 'Which additional properties to get for each interlanguage link',
00183                 ' url      - Adds the full URL',
00184                 ' langname - Adds the localised language name (best effort, use CLDR extension)',
00185                 "            Use {$this->getModulePrefix()}inlanguagecode to control the language",
00186                 ' autonym  - Adds the native language name',
00187             ),
00188             'lang' => 'Language code',
00189             'title' => "Link to search for. Must be used with {$this->getModulePrefix()}lang",
00190             'dir' => 'The direction in which to list',
00191             'inlanguagecode' => 'Language code for localised language names',
00192         );
00193     }
00194 
00195     public function getDescription() {
00196         return 'Returns all interlanguage links from the given page(s).';
00197     }
00198 
00199     public function getExamples() {
00200         return array(
00201             'api.php?action=query&prop=langlinks&titles=Main%20Page&redirects='
00202                 => 'Get interlanguage links from the [[Main Page]]',
00203         );
00204     }
00205 
00206     public function getHelpUrls() {
00207         return 'https://www.mediawiki.org/wiki/API:Properties#langlinks_.2F_ll';
00208     }
00209 }