MediaWiki  REL1_24
ApiQueryLangBacklinks.php
Go to the documentation of this file.
00001 <?php
00032 class ApiQueryLangBacklinks extends ApiQueryGeneratorBase {
00033 
00034     public function __construct( ApiQuery $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             $this->dieContinueUsageIf( count( $cont ) != 3 );
00060 
00061             $db = $this->getDB();
00062             $op = $params['dir'] == 'descending' ? '<' : '>';
00063             $prefix = $db->addQuotes( $cont[0] );
00064             $title = $db->addQuotes( $cont[1] );
00065             $from = intval( $cont[2] );
00066             $this->addWhere(
00067                 "ll_lang $op $prefix OR " .
00068                 "(ll_lang = $prefix AND " .
00069                 "(ll_title $op $title OR " .
00070                 "(ll_title = $title AND " .
00071                 "ll_from $op= $from)))"
00072             );
00073         }
00074 
00075         $prop = array_flip( $params['prop'] );
00076         $lllang = isset( $prop['lllang'] );
00077         $lltitle = isset( $prop['lltitle'] );
00078 
00079         $this->addTables( array( 'langlinks', 'page' ) );
00080         $this->addWhere( 'll_from = page_id' );
00081 
00082         $this->addFields( array( 'page_id', 'page_title', 'page_namespace', 'page_is_redirect',
00083             'll_from', 'll_lang', 'll_title' ) );
00084 
00085         $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
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' . $sort );
00091             } else {
00092                 $this->addOption( 'ORDER BY', array(
00093                     'll_title' . $sort,
00094                     'll_from' . $sort
00095                 ) );
00096             }
00097         } else {
00098             $this->addOption( 'ORDER BY', array(
00099                 'll_lang' . $sort,
00100                 'll_title' . $sort,
00101                 'll_from' . $sort
00102             ) );
00103         }
00104 
00105         $this->addOption( 'LIMIT', $params['limit'] + 1 );
00106 
00107         $res = $this->select( __METHOD__ );
00108 
00109         $pages = array();
00110 
00111         $count = 0;
00112         $result = $this->getResult();
00113         foreach ( $res as $row ) {
00114             if ( ++$count > $params['limit'] ) {
00115                 // We've reached the one extra which shows that there are
00116                 // additional pages to be had. Stop here... Continue string
00117                 // preserved in case the redirect query doesn't pass the limit.
00118                 $this->setContinueEnumParameter(
00119                     'continue',
00120                     "{$row->ll_lang}|{$row->ll_title}|{$row->ll_from}"
00121                 );
00122                 break;
00123             }
00124 
00125             if ( !is_null( $resultPageSet ) ) {
00126                 $pages[] = Title::newFromRow( $row );
00127             } else {
00128                 $entry = array( 'pageid' => $row->page_id );
00129 
00130                 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
00131                 ApiQueryBase::addTitleInfo( $entry, $title );
00132 
00133                 if ( $row->page_is_redirect ) {
00134                     $entry['redirect'] = '';
00135                 }
00136 
00137                 if ( $lllang ) {
00138                     $entry['lllang'] = $row->ll_lang;
00139                 }
00140 
00141                 if ( $lltitle ) {
00142                     $entry['lltitle'] = $row->ll_title;
00143                 }
00144 
00145                 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $entry );
00146                 if ( !$fit ) {
00147                     $this->setContinueEnumParameter(
00148                         'continue',
00149                         "{$row->ll_lang}|{$row->ll_title}|{$row->ll_from}"
00150                     );
00151                     break;
00152                 }
00153             }
00154         }
00155 
00156         if ( is_null( $resultPageSet ) ) {
00157             $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'll' );
00158         } else {
00159             $resultPageSet->populateFromTitles( $pages );
00160         }
00161     }
00162 
00163     public function getCacheMode( $params ) {
00164         return 'public';
00165     }
00166 
00167     public function getAllowedParams() {
00168         return array(
00169             'lang' => null,
00170             'title' => null,
00171             'continue' => null,
00172             'limit' => array(
00173                 ApiBase::PARAM_DFLT => 10,
00174                 ApiBase::PARAM_TYPE => 'limit',
00175                 ApiBase::PARAM_MIN => 1,
00176                 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
00177                 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
00178             ),
00179             'prop' => array(
00180                 ApiBase::PARAM_ISMULTI => true,
00181                 ApiBase::PARAM_DFLT => '',
00182                 ApiBase::PARAM_TYPE => array(
00183                     'lllang',
00184                     'lltitle',
00185                 ),
00186             ),
00187             'dir' => array(
00188                 ApiBase::PARAM_DFLT => 'ascending',
00189                 ApiBase::PARAM_TYPE => array(
00190                     'ascending',
00191                     'descending'
00192                 )
00193             ),
00194         );
00195     }
00196 
00197     public function getParamDescription() {
00198         return array(
00199             'lang' => 'Language for the language link',
00200             'title' => "Language link to search for. Must be used with {$this->getModulePrefix()}lang",
00201             'continue' => 'When more results are available, use this to continue',
00202             'prop' => array(
00203                 'Which properties to get',
00204                 ' lllang         - Adds the language code of the language link',
00205                 ' lltitle        - Adds the title of the language link',
00206             ),
00207             'limit' => 'How many total pages to return',
00208             'dir' => 'The direction in which to list',
00209         );
00210     }
00211 
00212     public function getDescription() {
00213         return array( 'Find all pages that link to the given language link.',
00214             'Can be used to find all links with a language code, or',
00215             'all links to a title (with a given language).',
00216             'Using neither parameter is effectively "All Language Links".',
00217             'Note that this may not consider language links added by extensions.',
00218         );
00219     }
00220 
00221     public function getExamples() {
00222         return array(
00223             'api.php?action=query&list=langbacklinks&lbltitle=Test&lbllang=fr',
00224             'api.php?action=query&generator=langbacklinks&glbltitle=Test&glbllang=fr&prop=info'
00225         );
00226     }
00227 
00228     public function getHelpUrls() {
00229         return 'https://www.mediawiki.org/wiki/API:Langbacklinks';
00230     }
00231 }