MediaWiki  REL1_24
ApiQueryIWLinks.php
Go to the documentation of this file.
00001 <?php
00033 class ApiQueryIWLinks extends ApiQueryBase {
00034 
00035     public function __construct( ApiQuery $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         $prop = array_flip( (array)$params['prop'] );
00046 
00047         if ( isset( $params['title'] ) && !isset( $params['prefix'] ) ) {
00048             $this->dieUsageMsg( array( 'missingparam', 'prefix' ) );
00049         }
00050 
00051         // Handle deprecated param
00052         $this->requireMaxOneParameter( $params, 'url', 'prop' );
00053         if ( $params['url'] ) {
00054             $this->logFeatureUsage( 'prop=iwlinks&iwurl' );
00055             $prop = array( 'url' => 1 );
00056         }
00057 
00058         $this->addFields( array(
00059             'iwl_from',
00060             'iwl_prefix',
00061             'iwl_title'
00062         ) );
00063 
00064         $this->addTables( 'iwlinks' );
00065         $this->addWhereFld( 'iwl_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
00066 
00067         if ( !is_null( $params['continue'] ) ) {
00068             $cont = explode( '|', $params['continue'] );
00069             $this->dieContinueUsageIf( count( $cont ) != 3 );
00070             $op = $params['dir'] == 'descending' ? '<' : '>';
00071             $db = $this->getDB();
00072             $iwlfrom = intval( $cont[0] );
00073             $iwlprefix = $db->addQuotes( $cont[1] );
00074             $iwltitle = $db->addQuotes( $cont[2] );
00075             $this->addWhere(
00076                 "iwl_from $op $iwlfrom OR " .
00077                 "(iwl_from = $iwlfrom AND " .
00078                 "(iwl_prefix $op $iwlprefix OR " .
00079                 "(iwl_prefix = $iwlprefix AND " .
00080                 "iwl_title $op= $iwltitle)))"
00081             );
00082         }
00083 
00084         $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
00085         if ( isset( $params['prefix'] ) ) {
00086             $this->addWhereFld( 'iwl_prefix', $params['prefix'] );
00087             if ( isset( $params['title'] ) ) {
00088                 $this->addWhereFld( 'iwl_title', $params['title'] );
00089                 $this->addOption( 'ORDER BY', 'iwl_from' . $sort );
00090             } else {
00091                 $this->addOption( 'ORDER BY', array(
00092                     'iwl_from' . $sort,
00093                     'iwl_title' . $sort
00094                 ) );
00095             }
00096         } else {
00097             // Don't order by iwl_from if it's constant in the WHERE clause
00098             if ( count( $this->getPageSet()->getGoodTitles() ) == 1 ) {
00099                 $this->addOption( 'ORDER BY', 'iwl_prefix' . $sort );
00100             } else {
00101                 $this->addOption( 'ORDER BY', array(
00102                     'iwl_from' . $sort,
00103                     'iwl_prefix' . $sort,
00104                     'iwl_title' . $sort
00105                 ) );
00106             }
00107         }
00108 
00109         $this->addOption( 'LIMIT', $params['limit'] + 1 );
00110         $res = $this->select( __METHOD__ );
00111 
00112         $count = 0;
00113         foreach ( $res as $row ) {
00114             if ( ++$count > $params['limit'] ) {
00115                 // We've reached the one extra which shows that
00116                 // there are additional pages to be had. Stop here...
00117                 $this->setContinueEnumParameter(
00118                     'continue',
00119                     "{$row->iwl_from}|{$row->iwl_prefix}|{$row->iwl_title}"
00120                 );
00121                 break;
00122             }
00123             $entry = array( 'prefix' => $row->iwl_prefix );
00124 
00125             if ( isset( $prop['url'] ) ) {
00126                 $title = Title::newFromText( "{$row->iwl_prefix}:{$row->iwl_title}" );
00127                 if ( $title ) {
00128                     $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
00129                 }
00130             }
00131 
00132             ApiResult::setContent( $entry, $row->iwl_title );
00133             $fit = $this->addPageSubItem( $row->iwl_from, $entry );
00134             if ( !$fit ) {
00135                 $this->setContinueEnumParameter(
00136                     'continue',
00137                     "{$row->iwl_from}|{$row->iwl_prefix}|{$row->iwl_title}"
00138                 );
00139                 break;
00140             }
00141         }
00142     }
00143 
00144     public function getCacheMode( $params ) {
00145         return 'public';
00146     }
00147 
00148     public function getAllowedParams() {
00149         return array(
00150             'url' => array(
00151                 ApiBase::PARAM_DFLT => false,
00152                 ApiBase::PARAM_DEPRECATED => true,
00153             ),
00154             'prop' => array(
00155                 ApiBase::PARAM_ISMULTI => true,
00156                 ApiBase::PARAM_TYPE => array(
00157                     'url',
00158                 )
00159             ),
00160             'limit' => array(
00161                 ApiBase::PARAM_DFLT => 10,
00162                 ApiBase::PARAM_TYPE => 'limit',
00163                 ApiBase::PARAM_MIN => 1,
00164                 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
00165                 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
00166             ),
00167             'continue' => null,
00168             'prefix' => null,
00169             'title' => null,
00170             'dir' => array(
00171                 ApiBase::PARAM_DFLT => 'ascending',
00172                 ApiBase::PARAM_TYPE => array(
00173                     'ascending',
00174                     'descending'
00175                 )
00176             ),
00177         );
00178     }
00179 
00180     public function getParamDescription() {
00181         return array(
00182             'prop' => array(
00183                 'Which additional properties to get for each interlanguage link',
00184                 ' url      - Adds the full URL',
00185             ),
00186             'url' => "Whether to get the full URL (Cannot be used with {$this->getModulePrefix()}prop)",
00187             'limit' => 'How many interwiki links to return',
00188             'continue' => 'When more results are available, use this to continue',
00189             'prefix' => 'Prefix for the interwiki',
00190             'title' => "Interwiki link to search for. Must be used with {$this->getModulePrefix()}prefix",
00191             'dir' => 'The direction in which to list',
00192         );
00193     }
00194 
00195     public function getDescription() {
00196         return 'Returns all interwiki links from the given page(s).';
00197     }
00198 
00199     public function getExamples() {
00200         return array(
00201             'api.php?action=query&prop=iwlinks&titles=Main%20Page'
00202                 => 'Get interwiki links from the [[Main Page]]',
00203         );
00204     }
00205 
00206     public function getHelpUrls() {
00207         return 'https://www.mediawiki.org/wiki/API:Iwlinks';
00208     }
00209 }