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