MediaWiki  REL1_23
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(
00110                     'continue',
00111                     "{$row->iwl_from}|{$row->iwl_prefix}|{$row->iwl_title}"
00112                 );
00113                 break;
00114             }
00115             $entry = array( 'prefix' => $row->iwl_prefix );
00116 
00117             if ( $params['url'] ) {
00118                 $title = Title::newFromText( "{$row->iwl_prefix}:{$row->iwl_title}" );
00119                 if ( $title ) {
00120                     $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
00121                 }
00122             }
00123 
00124             ApiResult::setContent( $entry, $row->iwl_title );
00125             $fit = $this->addPageSubItem( $row->iwl_from, $entry );
00126             if ( !$fit ) {
00127                 $this->setContinueEnumParameter(
00128                     'continue',
00129                     "{$row->iwl_from}|{$row->iwl_prefix}|{$row->iwl_title}"
00130                 );
00131                 break;
00132             }
00133         }
00134     }
00135 
00136     public function getCacheMode( $params ) {
00137         return 'public';
00138     }
00139 
00140     public function getAllowedParams() {
00141         return array(
00142             'url' => false,
00143             'limit' => array(
00144                 ApiBase::PARAM_DFLT => 10,
00145                 ApiBase::PARAM_TYPE => 'limit',
00146                 ApiBase::PARAM_MIN => 1,
00147                 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
00148                 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
00149             ),
00150             'continue' => null,
00151             'prefix' => null,
00152             'title' => null,
00153             'dir' => array(
00154                 ApiBase::PARAM_DFLT => 'ascending',
00155                 ApiBase::PARAM_TYPE => array(
00156                     'ascending',
00157                     'descending'
00158                 )
00159             ),
00160         );
00161     }
00162 
00163     public function getParamDescription() {
00164         return array(
00165             'url' => 'Whether to get the full URL',
00166             'limit' => 'How many interwiki links to return',
00167             'continue' => 'When more results are available, use this to continue',
00168             'prefix' => 'Prefix for the interwiki',
00169             'title' => "Interwiki link to search for. Must be used with {$this->getModulePrefix()}prefix",
00170             'dir' => 'The direction in which to list',
00171         );
00172     }
00173 
00174     public function getResultProperties() {
00175         return array(
00176             '' => array(
00177                 'prefix' => 'string',
00178                 'url' => array(
00179                     ApiBase::PROP_TYPE => 'string',
00180                     ApiBase::PROP_NULLABLE => true
00181                 ),
00182                 '*' => 'string'
00183             )
00184         );
00185     }
00186 
00187     public function getDescription() {
00188         return 'Returns all interwiki links from the given page(s).';
00189     }
00190 
00191     public function getPossibleErrors() {
00192         return array_merge( parent::getPossibleErrors(), array(
00193             array( 'missingparam', 'prefix' ),
00194         ) );
00195     }
00196 
00197     public function getExamples() {
00198         return array(
00199             'api.php?action=query&prop=iwlinks&titles=Main%20Page'
00200                 => 'Get interwiki links from the [[Main Page]]',
00201         );
00202     }
00203 
00204     public function getHelpUrls() {
00205         return 'https://www.mediawiki.org/wiki/API:Iwlinks';
00206     }
00207 }