MediaWiki  REL1_21
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_title' . $sort,
00085                                                 'iwl_from' . $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                                 ));
00097                         }
00098                 }
00099 
00100                 $this->addOption( 'LIMIT', $params['limit'] + 1 );
00101                 $res = $this->select( __METHOD__ );
00102 
00103                 $count = 0;
00104                 foreach ( $res as $row ) {
00105                         if ( ++$count > $params['limit'] ) {
00106                                 // We've reached the one extra which shows that
00107                                 // there are additional pages to be had. Stop here...
00108                                 $this->setContinueEnumParameter( 'continue', "{$row->iwl_from}|{$row->iwl_prefix}|{$row->iwl_title}" );
00109                                 break;
00110                         }
00111                         $entry = array( 'prefix' => $row->iwl_prefix );
00112 
00113                         if ( $params['url'] ) {
00114                                 $title = Title::newFromText( "{$row->iwl_prefix}:{$row->iwl_title}" );
00115                                 if ( $title ) {
00116                                         $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
00117                                 }
00118                         }
00119 
00120                         ApiResult::setContent( $entry, $row->iwl_title );
00121                         $fit = $this->addPageSubItem( $row->iwl_from, $entry );
00122                         if ( !$fit ) {
00123                                 $this->setContinueEnumParameter( 'continue', "{$row->iwl_from}|{$row->iwl_prefix}|{$row->iwl_title}" );
00124                                 break;
00125                         }
00126                 }
00127         }
00128 
00129         public function getCacheMode( $params ) {
00130                 return 'public';
00131         }
00132 
00133         public function getAllowedParams() {
00134                 return array(
00135                         'url' => false,
00136                         'limit' => array(
00137                                 ApiBase::PARAM_DFLT => 10,
00138                                 ApiBase::PARAM_TYPE => 'limit',
00139                                 ApiBase::PARAM_MIN => 1,
00140                                 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
00141                                 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
00142                         ),
00143                         'continue' => null,
00144                         'prefix' => null,
00145                         'title' => null,
00146                         'dir' => array(
00147                                 ApiBase::PARAM_DFLT => 'ascending',
00148                                 ApiBase::PARAM_TYPE => array(
00149                                         'ascending',
00150                                         'descending'
00151                                 )
00152                         ),
00153                 );
00154         }
00155 
00156         public function getParamDescription() {
00157                 return array(
00158                         'url' => 'Whether to get the full URL',
00159                         'limit' => 'How many interwiki links to return',
00160                         'continue' => 'When more results are available, use this to continue',
00161                         'prefix' => 'Prefix for the interwiki',
00162                         'title' => "Interwiki link to search for. Must be used with {$this->getModulePrefix()}prefix",
00163                         'dir' => 'The direction in which to list',
00164                 );
00165         }
00166 
00167         public function getResultProperties() {
00168                 return array(
00169                         '' => array(
00170                                 'prefix' => 'string',
00171                                 'url' => array(
00172                                         ApiBase::PROP_TYPE => 'string',
00173                                         ApiBase::PROP_NULLABLE => true
00174                                 ),
00175                                 '*' => 'string'
00176                         )
00177                 );
00178         }
00179 
00180         public function getDescription() {
00181                 return 'Returns all interwiki links from the given page(s)';
00182         }
00183 
00184         public function getPossibleErrors() {
00185                 return array_merge( parent::getPossibleErrors(), array(
00186                         array( 'missingparam', 'prefix' ),
00187                 ) );
00188         }
00189 
00190         public function getExamples() {
00191                 return array(
00192                         'api.php?action=query&prop=iwlinks&titles=Main%20Page' => 'Get interwiki links from the [[Main Page]]',
00193                 );
00194         }
00195 }