MediaWiki  REL1_20
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                         $op = $params['dir'] == 'descending' ? '<' : '>';
00066                         $db = $this->getDB();
00067                         $iwlfrom = intval( $cont[0] );
00068                         $iwlprefix = $db->addQuotes( $cont[1] );
00069                         $iwltitle = $db->addQuotes( $cont[2] );
00070                         $this->addWhere(
00071                                 "iwl_from $op $iwlfrom OR " .
00072                                 "(iwl_from = $iwlfrom AND " .
00073                                 "(iwl_prefix $op $iwlprefix OR " .
00074                                 "(iwl_prefix = $iwlprefix AND " .
00075                                 "iwl_title $op= $iwltitle)))"
00076                         );
00077                 }
00078 
00079                 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
00080                 if ( isset( $params['prefix'] ) ) {
00081                         $this->addWhereFld( 'iwl_prefix', $params['prefix'] );
00082                         if ( isset( $params['title'] ) ) {
00083                                 $this->addWhereFld( 'iwl_title', $params['title'] );
00084                                 $this->addOption( 'ORDER BY', 'iwl_from' . $sort );
00085                         } else {
00086                                 $this->addOption( 'ORDER BY', array(
00087                                                 'iwl_title' . $sort,
00088                                                 'iwl_from' . $sort
00089                                 ));
00090                         }
00091                 } else {
00092                         // Don't order by iwl_from if it's constant in the WHERE clause
00093                         if ( count( $this->getPageSet()->getGoodTitles() ) == 1 ) {
00094                                 $this->addOption( 'ORDER BY', 'iwl_prefix' . $sort );
00095                         } else {
00096                                 $this->addOption( 'ORDER BY', array (
00097                                                 'iwl_from' . $sort,
00098                                                 'iwl_prefix' . $sort
00099                                 ));
00100                         }
00101                 }
00102 
00103                 $this->addOption( 'LIMIT', $params['limit'] + 1 );
00104                 $res = $this->select( __METHOD__ );
00105 
00106                 $count = 0;
00107                 foreach ( $res as $row ) {
00108                         if ( ++$count > $params['limit'] ) {
00109                                 // We've reached the one extra which shows that
00110                                 // there are additional pages to be had. Stop here...
00111                                 $this->setContinueEnumParameter( 'continue', "{$row->iwl_from}|{$row->iwl_prefix}|{$row->iwl_title}" );
00112                                 break;
00113                         }
00114                         $entry = array( 'prefix' => $row->iwl_prefix );
00115 
00116                         if ( $params['url'] ) {
00117                                 $title = Title::newFromText( "{$row->iwl_prefix}:{$row->iwl_title}" );
00118                                 if ( $title ) {
00119                                         $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
00120                                 }
00121                         }
00122 
00123                         ApiResult::setContent( $entry, $row->iwl_title );
00124                         $fit = $this->addPageSubItem( $row->iwl_from, $entry );
00125                         if ( !$fit ) {
00126                                 $this->setContinueEnumParameter( 'continue', "{$row->iwl_from}|{$row->iwl_prefix}|{$row->iwl_title}" );
00127                                 break;
00128                         }
00129                 }
00130         }
00131 
00132         public function getCacheMode( $params ) {
00133                 return 'public';
00134         }
00135 
00136         public function getAllowedParams() {
00137                 return array(
00138                         'url' => false,
00139                         'limit' => array(
00140                                 ApiBase::PARAM_DFLT => 10,
00141                                 ApiBase::PARAM_TYPE => 'limit',
00142                                 ApiBase::PARAM_MIN => 1,
00143                                 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
00144                                 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
00145                         ),
00146                         'continue' => null,
00147                         'prefix' => null,
00148                         'title' => null,
00149                         'dir' => array(
00150                                 ApiBase::PARAM_DFLT => 'ascending',
00151                                 ApiBase::PARAM_TYPE => array(
00152                                         'ascending',
00153                                         'descending'
00154                                 )
00155                         ),
00156                 );
00157         }
00158 
00159         public function getParamDescription() {
00160                 return array(
00161                         'url' => 'Whether to get the full URL',
00162                         'limit' => 'How many interwiki links to return',
00163                         'continue' => 'When more results are available, use this to continue',
00164                         'prefix' => 'Prefix for the interwiki',
00165                         'title' => "Interwiki link to search for. Must be used with {$this->getModulePrefix()}prefix",
00166                         'dir' => 'The direction in which to list',
00167                 );
00168         }
00169 
00170         public function getResultProperties() {
00171                 return array(
00172                         '' => array(
00173                                 'prefix' => 'string',
00174                                 'url' => array(
00175                                         ApiBase::PROP_TYPE => 'string',
00176                                         ApiBase::PROP_NULLABLE => true
00177                                 ),
00178                                 '*' => 'string'
00179                         )
00180                 );
00181         }
00182 
00183         public function getDescription() {
00184                 return 'Returns all interwiki links from the given page(s)';
00185         }
00186 
00187         public function getPossibleErrors() {
00188                 return array_merge( parent::getPossibleErrors(), array(
00189                         array( 'missingparam', 'prefix' ),
00190                         array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
00191                 ) );
00192         }
00193 
00194         public function getExamples() {
00195                 return array(
00196                         'api.php?action=query&prop=iwlinks&titles=Main%20Page' => 'Get interwiki links from the [[Main Page]]',
00197                 );
00198         }
00199 
00200         public function getVersion() {
00201                 return __CLASS__ . ': $Id$';
00202         }
00203 }