MediaWiki  REL1_19
ApiQueryIWLinks.php
Go to the documentation of this file.
00001 <?php
00033 class ApiQueryIWLinks extends ApiQueryBase {
00034 
00035         public function __construct( $query, $moduleName ) {
00036                 parent::__construct( $query, $moduleName, 'iw' );
00037         }
00038 
00039         public function execute() {
00040                 if ( $this->getPageSet()->getGoodTitleCount() == 0 ) {
00041                         return;
00042                 }
00043 
00044                 $params = $this->extractRequestParams();
00045 
00046                 if ( isset( $params['title'] ) && !isset( $params['prefix'] ) ) {
00047                         $this->dieUsageMsg( array( 'missingparam', 'prefix' ) );
00048                 }
00049 
00050                 $this->addFields( array(
00051                         'iwl_from',
00052                         'iwl_prefix',
00053                         'iwl_title'
00054                 ) );
00055 
00056                 $this->addTables( 'iwlinks' );
00057                 $this->addWhereFld( 'iwl_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
00058 
00059                 if ( !is_null( $params['continue'] ) ) {
00060                         $cont = explode( '|', $params['continue'] );
00061                         if ( count( $cont ) != 3 ) {
00062                                 $this->dieUsage( 'Invalid continue param. You should pass the ' .
00063                                         'original value returned by the previous query', '_badcontinue' );
00064                         }
00065                         $iwlfrom = intval( $cont[0] );
00066                         $iwlprefix = $this->getDB()->strencode( $cont[1] );
00067                         $iwltitle = $this->getDB()->strencode( $this->titleToKey( $cont[2] ) );
00068                         $this->addWhere(
00069                                 "iwl_from > $iwlfrom OR " .
00070                                 "(iwl_from = $iwlfrom AND " .
00071                                 "(iwl_prefix > '$iwlprefix' OR " .
00072                                 "(iwl_prefix = '$iwlprefix' AND " .
00073                                 "iwl_title >= '$iwltitle')))"
00074                         );
00075                 }
00076 
00077                 $dir = ( $params['dir'] == 'descending' ? ' DESC' : '' );
00078                 if ( isset( $params['prefix'] ) ) {
00079                         $this->addWhereFld( 'iwl_prefix', $params['prefix'] );
00080                         if ( isset( $params['title'] ) ) {
00081                                 $this->addWhereFld( 'iwl_title', $params['title'] );
00082                                 $this->addOption( 'ORDER BY', 'iwl_from' . $dir );
00083                         } else {
00084                                 $this->addOption( 'ORDER BY', array(
00085                                                 'iwl_title' . $dir,
00086                                                 'iwl_from' . $dir
00087                                 ));
00088                         }
00089                 } else {
00090                         // Don't order by iwl_from if it's constant in the WHERE clause
00091                         if ( count( $this->getPageSet()->getGoodTitles() ) == 1 ) {
00092                                 $this->addOption( 'ORDER BY', 'iwl_prefix' . $dir );
00093                         } else {
00094                                 $this->addOption( 'ORDER BY', array (
00095                                                 'iwl_from' . $dir,
00096                                                 'iwl_prefix' . $dir
00097                                 ));
00098                         }
00099                 }
00100 
00101                 $this->addOption( 'LIMIT', $params['limit'] + 1 );
00102                 $res = $this->select( __METHOD__ );
00103 
00104                 $count = 0;
00105                 foreach ( $res as $row ) {
00106                         if ( ++$count > $params['limit'] ) {
00107                                 // We've reached the one extra which shows that
00108                                 // there are additional pages to be had. Stop here...
00109                                 $this->setContinueEnumParameter( 'continue', "{$row->iwl_from}|{$row->iwl_prefix}|{$row->iwl_title}" );
00110                                 break;
00111                         }
00112                         $entry = array( 'prefix' => $row->iwl_prefix );
00113 
00114                         if ( $params['url'] ) {
00115                                 $title = Title::newFromText( "{$row->iwl_prefix}:{$row->iwl_title}" );
00116                                 if ( $title ) {
00117                                         $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
00118                                 }
00119                         }
00120 
00121                         ApiResult::setContent( $entry, $row->iwl_title );
00122                         $fit = $this->addPageSubItem( $row->iwl_from, $entry );
00123                         if ( !$fit ) {
00124                                 $this->setContinueEnumParameter( 'continue', "{$row->iwl_from}|{$row->iwl_prefix}|{$row->iwl_title}" );
00125                                 break;
00126                         }
00127                 }
00128         }
00129 
00130         public function getCacheMode( $params ) {
00131                 return 'public';
00132         }
00133 
00134         public function getAllowedParams() {
00135                 return array(
00136                         'url' => false,
00137                         'limit' => array(
00138                                 ApiBase::PARAM_DFLT => 10,
00139                                 ApiBase::PARAM_TYPE => 'limit',
00140                                 ApiBase::PARAM_MIN => 1,
00141                                 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
00142                                 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
00143                         ),
00144                         'continue' => null,
00145                         'prefix' => null,
00146                         'title' => null,
00147                         'dir' => array(
00148                                 ApiBase::PARAM_DFLT => 'ascending',
00149                                 ApiBase::PARAM_TYPE => array(
00150                                         'ascending',
00151                                         'descending'
00152                                 )
00153                         ),
00154                 );
00155         }
00156 
00157         public function getParamDescription() {
00158                 return array(
00159                         'url' => 'Whether to get the full URL',
00160                         'limit' => 'How many interwiki links to return',
00161                         'continue' => 'When more results are available, use this to continue',
00162                         'prefix' => 'Prefix for the interwiki',
00163                         'title' => "Interwiki link to search for. Must be used with {$this->getModulePrefix()}prefix",
00164                         'dir' => 'The direction in which to list',
00165                 );
00166         }
00167 
00168         public function getDescription() {
00169                 return 'Returns all interwiki links from the given page(s)';
00170         }
00171 
00172         public function getPossibleErrors() {
00173                 return array_merge( parent::getPossibleErrors(), array(
00174                         array( 'missingparam', 'prefix' ),
00175                         array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
00176                 ) );
00177         }
00178 
00179         public function getExamples() {
00180                 return array(
00181                         'api.php?action=query&prop=iwlinks&titles=Main%20Page' => 'Get interwiki links from the [[Main Page]]',
00182                 );
00183         }
00184 
00185         public function getVersion() {
00186                 return __CLASS__ . ': $Id$';
00187         }
00188 }