MediaWiki  REL1_19
ApiQueryLangBacklinks.php
Go to the documentation of this file.
00001 <?php
00032 class ApiQueryLangBacklinks extends ApiQueryGeneratorBase {
00033 
00034         public function __construct( $query, $moduleName ) {
00035                 parent::__construct( $query, $moduleName, 'lbl' );
00036         }
00037 
00038         public function execute() {
00039                 $this->run();
00040         }
00041 
00042         public function executeGenerator( $resultPageSet ) {
00043                 $this->run( $resultPageSet );
00044         }
00045 
00050         public function run( $resultPageSet = null ) {
00051                 $params = $this->extractRequestParams();
00052 
00053                 if ( isset( $params['title'] ) && !isset( $params['lang'] ) ) {
00054                         $this->dieUsageMsg( array( 'missingparam', 'lang' ) );
00055                 }
00056 
00057                 if ( !is_null( $params['continue'] ) ) {
00058                         $cont = explode( '|', $params['continue'] );
00059                         if ( count( $cont ) != 3 ) {
00060                                 $this->dieUsage( 'Invalid continue param. You should pass the ' .
00061                                         'original value returned by the previous query', '_badcontinue' );
00062                         }
00063 
00064                         $prefix = $this->getDB()->strencode( $cont[0] );
00065                         $title = $this->getDB()->strencode( $this->titleToKey( $cont[1] ) );
00066                         $from = intval( $cont[2] );
00067                         $this->addWhere(
00068                                 "ll_lang > '$prefix' OR " .
00069                                 "(ll_lang = '$prefix' AND " .
00070                                 "(ll_title > '$title' OR " .
00071                                 "(ll_title = '$title' AND " .
00072                                 "ll_from >= $from)))"
00073                         );
00074                 }
00075 
00076                 $prop = array_flip( $params['prop'] );
00077                 $lllang = isset( $prop['lllang'] );
00078                 $lltitle = isset( $prop['lltitle'] );
00079 
00080                 $this->addTables( array( 'langlinks', 'page' ) );
00081                 $this->addWhere( 'll_from = page_id' );
00082 
00083                 $this->addFields( array( 'page_id', 'page_title', 'page_namespace', 'page_is_redirect',
00084                         'll_from', 'll_lang', 'll_title' ) );
00085 
00086                 if ( isset( $params['lang'] ) ) {
00087                         $this->addWhereFld( 'll_lang', $params['lang'] );
00088                         if ( isset( $params['title'] ) ) {
00089                                 $this->addWhereFld( 'll_title', $params['title'] );
00090                                 $this->addOption( 'ORDER BY', 'll_from' );
00091                         } else {
00092                                 $this->addOption( 'ORDER BY', 'll_title, ll_from' );
00093                         }
00094                 } else {
00095                         $this->addOption( 'ORDER BY', 'll_lang, ll_title, ll_from' );
00096                 }
00097 
00098                 $this->addOption( 'LIMIT', $params['limit'] + 1 );
00099 
00100                 $res = $this->select( __METHOD__ );
00101 
00102                 $pages = array();
00103 
00104                 $count = 0;
00105                 $result = $this->getResult();
00106                 foreach ( $res as $row ) {
00107                         if ( ++ $count > $params['limit'] ) {
00108                                 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
00109                                 // Continue string preserved in case the redirect query doesn't pass the limit
00110                                 $this->setContinueEnumParameter( 'continue', "{$row->ll_lang}|{$row->ll_title}|{$row->ll_from}" );
00111                                 break;
00112                         }
00113 
00114                         if ( !is_null( $resultPageSet ) ) {
00115                                 $pages[] = Title::newFromRow( $row );
00116                         } else {
00117                                 $entry = array( 'pageid' => $row->page_id );
00118 
00119                                 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
00120                                 ApiQueryBase::addTitleInfo( $entry, $title );
00121 
00122                                 if ( $row->page_is_redirect ) {
00123                                         $entry['redirect'] = '';
00124                                 }
00125 
00126                                 if ( $lllang ) {
00127                                         $entry['lllang'] = $row->ll_lang;
00128                                 }
00129 
00130                                 if ( $lltitle ) {
00131                                         $entry['lltitle'] = $row->ll_title;
00132                                 }
00133 
00134                                 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $entry );
00135                                 if ( !$fit ) {
00136                                         $this->setContinueEnumParameter( 'continue', "{$row->ll_lang}|{$row->ll_title}|{$row->ll_from}" );
00137                                         break;
00138                                 }
00139                         }
00140                 }
00141 
00142                 if ( is_null( $resultPageSet ) ) {
00143                         $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'll' );
00144                 } else {
00145                         $resultPageSet->populateFromTitles( $pages );
00146                 }
00147         }
00148 
00149         public function getCacheMode( $params ) {
00150                 return 'public';
00151         }
00152 
00153         public function getAllowedParams() {
00154                 return array(
00155                         'lang' => null,
00156                         'title' => null,
00157                         'continue' => null,
00158                         'limit' => array(
00159                                 ApiBase::PARAM_DFLT => 10,
00160                                 ApiBase::PARAM_TYPE => 'limit',
00161                                 ApiBase::PARAM_MIN => 1,
00162                                 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
00163                                 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
00164                         ),
00165                         'prop' => array(
00166                                 ApiBase::PARAM_ISMULTI => true,
00167                                 ApiBase::PARAM_DFLT => '',
00168                                 ApiBase::PARAM_TYPE => array(
00169                                         'lllang',
00170                                         'lltitle',
00171                                 ),
00172                         ),
00173                 );
00174         }
00175 
00176         public function getParamDescription() {
00177                 return array(
00178                         'lang' => 'Language for the language link',
00179                         'title' => "Language link to search for. Must be used with {$this->getModulePrefix()}lang",
00180                         'continue' => 'When more results are available, use this to continue',
00181                         'prop' => array(
00182                                 'Which properties to get',
00183                                 ' lllang         - Adds the language code of the language link',
00184                                 ' lltitle        - Adds the title of the language ink',
00185                         ),
00186                         'limit' => 'How many total pages to return',
00187                 );
00188         }
00189 
00190         public function getDescription() {
00191                 return array( 'Find all pages that link to the given language link.',
00192                         'Can be used to find all links with a language code, or',
00193                         'all links to a title (with a given language).',
00194                         'Using neither parameter is effectively "All Language Links"',
00195                 );
00196         }
00197 
00198         public function getPossibleErrors() {
00199                 return array_merge( parent::getPossibleErrors(), array(
00200                         array( 'missingparam', 'lang' ),
00201                         array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
00202                 ) );
00203         }
00204 
00205         public function getExamples() {
00206                 return array(
00207                         'api.php?action=query&list=langbacklinks&lbltitle=Test&lbllang=fr',
00208                         'api.php?action=query&generator=langbacklinks&glbltitle=Test&lbllang=fr&prop=info'
00209                 );
00210         }
00211 
00212         public function getVersion() {
00213                 return __CLASS__ . ': $Id$';
00214         }
00215 }