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