MediaWiki  REL1_19
ApiQueryIWBacklinks.php
Go to the documentation of this file.
00001 <?php
00032 class ApiQueryIWBacklinks extends ApiQueryGeneratorBase {
00033 
00034         public function __construct( $query, $moduleName ) {
00035                 parent::__construct( $query, $moduleName, 'iwbl' );
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['prefix'] ) ) {
00054                         $this->dieUsageMsg( array( 'missingparam', 'prefix' ) );
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                                 "iwl_prefix > '$prefix' OR " .
00069                                 "(iwl_prefix = '$prefix' AND " .
00070                                 "(iwl_title > '$title' OR " .
00071                                 "(iwl_title = '$title' AND " .
00072                                 "iwl_from >= $from)))"
00073                         );
00074                 }
00075 
00076                 $prop = array_flip( $params['prop'] );
00077                 $iwprefix = isset( $prop['iwprefix'] );
00078                 $iwtitle = isset( $prop['iwtitle'] );
00079 
00080                 $this->addTables( array( 'iwlinks', 'page' ) );
00081                 $this->addWhere( 'iwl_from = page_id' );
00082 
00083                 $this->addFields( array( 'page_id', 'page_title', 'page_namespace', 'page_is_redirect',
00084                         'iwl_from', 'iwl_prefix', 'iwl_title' ) );
00085 
00086                 if ( isset( $params['prefix'] ) ) {
00087                         $this->addWhereFld( 'iwl_prefix', $params['prefix'] );
00088                         if ( isset( $params['title'] ) ) {
00089                                 $this->addWhereFld( 'iwl_title', $params['title'] );
00090                                 $this->addOption( 'ORDER BY', 'iwl_from' );
00091                         } else {
00092                                 $this->addOption( 'ORDER BY', 'iwl_title, iwl_from' );
00093                         }
00094                 } else {
00095                         $this->addOption( 'ORDER BY', 'iwl_prefix, iwl_title, iwl_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->iwl_prefix}|{$row->iwl_title}|{$row->iwl_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 ( $iwprefix ) {
00127                                         $entry['iwprefix'] = $row->iwl_prefix;
00128                                 }
00129 
00130                                 if ( $iwtitle ) {
00131                                         $entry['iwtitle'] = $row->iwl_title;
00132                                 }
00133 
00134                                 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $entry );
00135                                 if ( !$fit ) {
00136                                         $this->setContinueEnumParameter( 'continue', "{$row->iwl_prefix}|{$row->iwl_title}|{$row->iwl_from}" );
00137                                         break;
00138                                 }
00139                         }
00140                 }
00141 
00142                 if ( is_null( $resultPageSet ) ) {
00143                         $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'iw' );
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                         'prefix' => 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                                         'iwprefix',
00170                                         'iwtitle',
00171                                 ),
00172                         ),
00173                 );
00174         }
00175 
00176         public function getParamDescription() {
00177                 return array(
00178                         'prefix' => 'Prefix for the interwiki',
00179                         'title' => "Interwiki link to search for. Must be used with {$this->getModulePrefix()}prefix",
00180                         'continue' => 'When more results are available, use this to continue',
00181                         'prop' => array(
00182                                 'Which properties to get',
00183                                 ' iwprefix       - Adds the prefix of the interwiki',
00184                                 ' iwtitle        - Adds the title of the interwiki',
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 interwiki link.',
00192                         'Can be used to find all links with a prefix, or',
00193                         'all links to a title (with a given prefix).',
00194                         'Using neither parameter is effectively "All IW Links"',
00195                 );
00196         }
00197 
00198         public function getPossibleErrors() {
00199                 return array_merge( parent::getPossibleErrors(), array(
00200                         array( 'missingparam', 'prefix' ),
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=iwbacklinks&iwbltitle=Test&iwblprefix=wikibooks',
00208                         'api.php?action=query&generator=iwbacklinks&giwbltitle=Test&iwblprefix=wikibooks&prop=info'
00209                 );
00210         }
00211 
00212         public function getVersion() {
00213                 return __CLASS__ . ': $Id$';
00214         }
00215 }