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