MediaWiki  REL1_22
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             $this->dieContinueUsageIf( count( $cont ) != 3 );
00062             $op = $params['dir'] == 'descending' ? '<' : '>';
00063             $db = $this->getDB();
00064             $iwlfrom = intval( $cont[0] );
00065             $iwlprefix = $db->addQuotes( $cont[1] );
00066             $iwltitle = $db->addQuotes( $cont[2] );
00067             $this->addWhere(
00068                 "iwl_from $op $iwlfrom OR " .
00069                 "(iwl_from = $iwlfrom AND " .
00070                 "(iwl_prefix $op $iwlprefix OR " .
00071                 "(iwl_prefix = $iwlprefix AND " .
00072                 "iwl_title $op= $iwltitle)))"
00073             );
00074         }
00075 
00076         $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
00077         if ( isset( $params['prefix'] ) ) {
00078             $this->addWhereFld( 'iwl_prefix', $params['prefix'] );
00079             if ( isset( $params['title'] ) ) {
00080                 $this->addWhereFld( 'iwl_title', $params['title'] );
00081                 $this->addOption( 'ORDER BY', 'iwl_from' . $sort );
00082             } else {
00083                 $this->addOption( 'ORDER BY', array(
00084                         'iwl_from' . $sort,
00085                         'iwl_title' . $sort
00086                 ));
00087             }
00088         } else {
00089             // Don't order by iwl_from if it's constant in the WHERE clause
00090             if ( count( $this->getPageSet()->getGoodTitles() ) == 1 ) {
00091                 $this->addOption( 'ORDER BY', 'iwl_prefix' . $sort );
00092             } else {
00093                 $this->addOption( 'ORDER BY', array(
00094                         'iwl_from' . $sort,
00095                         'iwl_prefix' . $sort,
00096                         'iwl_title' . $sort
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 getResultProperties() {
00169         return array(
00170             '' => array(
00171                 'prefix' => 'string',
00172                 'url' => array(
00173                     ApiBase::PROP_TYPE => 'string',
00174                     ApiBase::PROP_NULLABLE => true
00175                 ),
00176                 '*' => 'string'
00177             )
00178         );
00179     }
00180 
00181     public function getDescription() {
00182         return 'Returns all interwiki links from the given page(s)';
00183     }
00184 
00185     public function getPossibleErrors() {
00186         return array_merge( parent::getPossibleErrors(), array(
00187             array( 'missingparam', 'prefix' ),
00188         ) );
00189     }
00190 
00191     public function getExamples() {
00192         return array(
00193             'api.php?action=query&prop=iwlinks&titles=Main%20Page' => 'Get interwiki links from the [[Main Page]]',
00194         );
00195     }
00196 
00197     public function getHelpUrls() {
00198         return 'https://www.mediawiki.org/wiki/API:Iwlinks';
00199     }
00200 }