MediaWiki  REL1_24
ApiQueryIWBacklinks.php
Go to the documentation of this file.
00001 <?php
00032 class ApiQueryIWBacklinks extends ApiQueryGeneratorBase {
00033 
00034     public function __construct( ApiQuery $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             $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                 "iwl_prefix $op $prefix OR " .
00068                 "(iwl_prefix = $prefix AND " .
00069                 "(iwl_title $op $title OR " .
00070                 "(iwl_title = $title AND " .
00071                 "iwl_from $op= $from)))"
00072             );
00073         }
00074 
00075         $prop = array_flip( $params['prop'] );
00076         $iwprefix = isset( $prop['iwprefix'] );
00077         $iwtitle = isset( $prop['iwtitle'] );
00078 
00079         $this->addTables( array( 'iwlinks', 'page' ) );
00080         $this->addWhere( 'iwl_from = page_id' );
00081 
00082         $this->addFields( array( 'page_id', 'page_title', 'page_namespace', 'page_is_redirect',
00083             'iwl_from', 'iwl_prefix', 'iwl_title' ) );
00084 
00085         $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
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' . $sort );
00091             } else {
00092                 $this->addOption( 'ORDER BY', array(
00093                     'iwl_title' . $sort,
00094                     'iwl_from' . $sort
00095                 ) );
00096             }
00097         } else {
00098             $this->addOption( 'ORDER BY', array(
00099                 'iwl_prefix' . $sort,
00100                 'iwl_title' . $sort,
00101                 'iwl_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...
00117                 // Continue string preserved in case the redirect query doesn't
00118                 // pass the limit
00119                 $this->setContinueEnumParameter(
00120                     'continue',
00121                     "{$row->iwl_prefix}|{$row->iwl_title}|{$row->iwl_from}"
00122                 );
00123                 break;
00124             }
00125 
00126             if ( !is_null( $resultPageSet ) ) {
00127                 $pages[] = Title::newFromRow( $row );
00128             } else {
00129                 $entry = array( 'pageid' => $row->page_id );
00130 
00131                 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
00132                 ApiQueryBase::addTitleInfo( $entry, $title );
00133 
00134                 if ( $row->page_is_redirect ) {
00135                     $entry['redirect'] = '';
00136                 }
00137 
00138                 if ( $iwprefix ) {
00139                     $entry['iwprefix'] = $row->iwl_prefix;
00140                 }
00141 
00142                 if ( $iwtitle ) {
00143                     $entry['iwtitle'] = $row->iwl_title;
00144                 }
00145 
00146                 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $entry );
00147                 if ( !$fit ) {
00148                     $this->setContinueEnumParameter(
00149                         'continue',
00150                         "{$row->iwl_prefix}|{$row->iwl_title}|{$row->iwl_from}"
00151                     );
00152                     break;
00153                 }
00154             }
00155         }
00156 
00157         if ( is_null( $resultPageSet ) ) {
00158             $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'iw' );
00159         } else {
00160             $resultPageSet->populateFromTitles( $pages );
00161         }
00162     }
00163 
00164     public function getCacheMode( $params ) {
00165         return 'public';
00166     }
00167 
00168     public function getAllowedParams() {
00169         return array(
00170             'prefix' => null,
00171             'title' => null,
00172             'continue' => null,
00173             'limit' => array(
00174                 ApiBase::PARAM_DFLT => 10,
00175                 ApiBase::PARAM_TYPE => 'limit',
00176                 ApiBase::PARAM_MIN => 1,
00177                 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
00178                 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
00179             ),
00180             'prop' => array(
00181                 ApiBase::PARAM_ISMULTI => true,
00182                 ApiBase::PARAM_DFLT => '',
00183                 ApiBase::PARAM_TYPE => array(
00184                     'iwprefix',
00185                     'iwtitle',
00186                 ),
00187             ),
00188             'dir' => array(
00189                 ApiBase::PARAM_DFLT => 'ascending',
00190                 ApiBase::PARAM_TYPE => array(
00191                     'ascending',
00192                     'descending'
00193                 )
00194             ),
00195         );
00196     }
00197 
00198     public function getParamDescription() {
00199         return array(
00200             'prefix' => 'Prefix for the interwiki',
00201             'title' => "Interwiki link to search for. Must be used with {$this->getModulePrefix()}prefix",
00202             'continue' => 'When more results are available, use this to continue',
00203             'prop' => array(
00204                 'Which properties to get',
00205                 ' iwprefix       - Adds the prefix of the interwiki',
00206                 ' iwtitle        - Adds the title of the interwiki',
00207             ),
00208             'limit' => 'How many total pages to return',
00209             'dir' => 'The direction in which to list',
00210         );
00211     }
00212 
00213     public function getDescription() {
00214         return array( 'Find all pages that link to the given interwiki link.',
00215             'Can be used to find all links with a prefix, or',
00216             'all links to a title (with a given prefix).',
00217             'Using neither parameter is effectively "All IW Links".',
00218         );
00219     }
00220 
00221     public function getExamples() {
00222         return array(
00223             'api.php?action=query&list=iwbacklinks&iwbltitle=Test&iwblprefix=wikibooks',
00224             'api.php?action=query&generator=iwbacklinks&giwbltitle=Test&giwblprefix=wikibooks&prop=info'
00225         );
00226     }
00227 
00228     public function getHelpUrls() {
00229         return 'https://www.mediawiki.org/wiki/API:Iwbacklinks';
00230     }
00231 }