MediaWiki  REL1_21
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                         $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 additional pages to be had. Stop here...
00116                                 // Continue string preserved in case the redirect query doesn't pass the limit
00117                                 $this->setContinueEnumParameter( 'continue', "{$row->ll_lang}|{$row->ll_title}|{$row->ll_from}" );
00118                                 break;
00119                         }
00120 
00121                         if ( !is_null( $resultPageSet ) ) {
00122                                 $pages[] = Title::newFromRow( $row );
00123                         } else {
00124                                 $entry = array( 'pageid' => $row->page_id );
00125 
00126                                 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
00127                                 ApiQueryBase::addTitleInfo( $entry, $title );
00128 
00129                                 if ( $row->page_is_redirect ) {
00130                                         $entry['redirect'] = '';
00131                                 }
00132 
00133                                 if ( $lllang ) {
00134                                         $entry['lllang'] = $row->ll_lang;
00135                                 }
00136 
00137                                 if ( $lltitle ) {
00138                                         $entry['lltitle'] = $row->ll_title;
00139                                 }
00140 
00141                                 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $entry );
00142                                 if ( !$fit ) {
00143                                         $this->setContinueEnumParameter( 'continue', "{$row->ll_lang}|{$row->ll_title}|{$row->ll_from}" );
00144                                         break;
00145                                 }
00146                         }
00147                 }
00148 
00149                 if ( is_null( $resultPageSet ) ) {
00150                         $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'll' );
00151                 } else {
00152                         $resultPageSet->populateFromTitles( $pages );
00153                 }
00154         }
00155 
00156         public function getCacheMode( $params ) {
00157                 return 'public';
00158         }
00159 
00160         public function getAllowedParams() {
00161                 return array(
00162                         'lang' => null,
00163                         'title' => null,
00164                         'continue' => null,
00165                         'limit' => array(
00166                                 ApiBase::PARAM_DFLT => 10,
00167                                 ApiBase::PARAM_TYPE => 'limit',
00168                                 ApiBase::PARAM_MIN => 1,
00169                                 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
00170                                 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
00171                         ),
00172                         'prop' => array(
00173                                 ApiBase::PARAM_ISMULTI => true,
00174                                 ApiBase::PARAM_DFLT => '',
00175                                 ApiBase::PARAM_TYPE => array(
00176                                         'lllang',
00177                                         'lltitle',
00178                                 ),
00179                         ),
00180                         'dir' => array(
00181                                 ApiBase::PARAM_DFLT => 'ascending',
00182                                 ApiBase::PARAM_TYPE => array(
00183                                         'ascending',
00184                                         'descending'
00185                                 )
00186                         ),
00187                 );
00188         }
00189 
00190         public function getParamDescription() {
00191                 return array(
00192                         'lang' => 'Language for the language link',
00193                         'title' => "Language link to search for. Must be used with {$this->getModulePrefix()}lang",
00194                         'continue' => 'When more results are available, use this to continue',
00195                         'prop' => array(
00196                                 'Which properties to get',
00197                                 ' lllang         - Adds the language code of the language link',
00198                                 ' lltitle        - Adds the title of the language ink',
00199                         ),
00200                         'limit' => 'How many total pages to return',
00201                         'dir' => 'The direction in which to list',
00202                 );
00203         }
00204 
00205         public function getResultProperties() {
00206                 return array(
00207                         '' => array(
00208                                 'pageid' => 'integer',
00209                                 'ns' => 'namespace',
00210                                 'title' => 'string',
00211                                 'redirect' => 'boolean'
00212                         ),
00213                         'lllang' => array(
00214                                 'lllang' => 'string'
00215                         ),
00216                         'lltitle' => array(
00217                                 'lltitle' => 'string'
00218                         )
00219                 );
00220         }
00221 
00222         public function getDescription() {
00223                 return array( 'Find all pages that link to the given language link.',
00224                         'Can be used to find all links with a language code, or',
00225                         'all links to a title (with a given language).',
00226                         'Using neither parameter is effectively "All Language Links"',
00227                 );
00228         }
00229 
00230         public function getPossibleErrors() {
00231                 return array_merge( parent::getPossibleErrors(), array(
00232                         array( 'missingparam', 'lang' ),
00233                 ) );
00234         }
00235 
00236         public function getExamples() {
00237                 return array(
00238                         'api.php?action=query&list=langbacklinks&lbltitle=Test&lbllang=fr',
00239                         'api.php?action=query&generator=langbacklinks&glbltitle=Test&glbllang=fr&prop=info'
00240                 );
00241         }
00242 }