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