MediaWiki  REL1_22
ApiQueryQueryPage.php
Go to the documentation of this file.
00001 <?php
00032 class ApiQueryQueryPage extends ApiQueryGeneratorBase {
00033     private $qpMap;
00034 
00035     public function __construct( $query, $moduleName ) {
00036         parent::__construct( $query, $moduleName, 'qp' );
00037         // We need to do this to make sure $wgQueryPages is set up
00038         // This SUCKS
00039         global $IP;
00040         require_once "$IP/includes/QueryPage.php";
00041 
00042         // Build mapping from special page names to QueryPage classes
00043         global $wgQueryPages, $wgAPIUselessQueryPages;
00044         $this->qpMap = array();
00045         foreach ( $wgQueryPages as $page ) {
00046             if ( !in_array( $page[1], $wgAPIUselessQueryPages ) ) {
00047                 $this->qpMap[$page[1]] = $page[0];
00048             }
00049         }
00050     }
00051 
00052     public function execute() {
00053         $this->run();
00054     }
00055 
00056     public function executeGenerator( $resultPageSet ) {
00057         $this->run( $resultPageSet );
00058     }
00059 
00063     public function run( $resultPageSet = null ) {
00064         global $wgQueryCacheLimit;
00065 
00066         $params = $this->extractRequestParams();
00067         $result = $this->getResult();
00068 
00070         $qp = new $this->qpMap[$params['page']]();
00071         if ( !$qp->userCanExecute( $this->getUser() ) ) {
00072             $this->dieUsageMsg( 'specialpage-cantexecute' );
00073         }
00074 
00075         $r = array( 'name' => $params['page'] );
00076         if ( $qp->isCached() ) {
00077             if ( !$qp->isCacheable() ) {
00078                 $r['disabled'] = '';
00079             } else {
00080                 $r['cached'] = '';
00081                 $ts = $qp->getCachedTimestamp();
00082                 if ( $ts ) {
00083                     $r['cachedtimestamp'] = wfTimestamp( TS_ISO_8601, $ts );
00084                 }
00085                 $r['maxresults'] = $wgQueryCacheLimit;
00086             }
00087         }
00088         $result->addValue( array( 'query' ), $this->getModuleName(), $r );
00089 
00090         if ( $qp->isCached() && !$qp->isCacheable() ) {
00091             // Disabled query page, don't run the query
00092             return;
00093         }
00094 
00095         $res = $qp->doQuery( $params['offset'], $params['limit'] + 1 );
00096         $count = 0;
00097         $titles = array();
00098         foreach ( $res as $row ) {
00099             if ( ++$count > $params['limit'] ) {
00100                 // We've had enough
00101                 $this->setContinueEnumParameter( 'offset', $params['offset'] + $params['limit'] );
00102                 break;
00103             }
00104 
00105             $title = Title::makeTitle( $row->namespace, $row->title );
00106             if ( is_null( $resultPageSet ) ) {
00107                 $data = array( 'value' => $row->value );
00108                 if ( $qp->usesTimestamps() ) {
00109                     $data['timestamp'] = wfTimestamp( TS_ISO_8601, $row->value );
00110                 }
00111                 self::addTitleInfo( $data, $title );
00112 
00113                 foreach ( $row as $field => $value ) {
00114                     if ( !in_array( $field, array( 'namespace', 'title', 'value', 'qc_type' ) ) ) {
00115                         $data['databaseResult'][$field] = $value;
00116                     }
00117                 }
00118 
00119                 $fit = $result->addValue( array( 'query', $this->getModuleName(), 'results' ), null, $data );
00120                 if ( !$fit ) {
00121                     $this->setContinueEnumParameter( 'offset', $params['offset'] + $count - 1 );
00122                     break;
00123                 }
00124             } else {
00125                 $titles[] = $title;
00126             }
00127         }
00128         if ( is_null( $resultPageSet ) ) {
00129             $result->setIndexedTagName_internal( array( 'query', $this->getModuleName(), 'results' ), 'page' );
00130         } else {
00131             $resultPageSet->populateFromTitles( $titles );
00132         }
00133     }
00134 
00135     public function getCacheMode( $params ) {
00137         $qp = new $this->qpMap[$params['page']]();
00138         if ( $qp->getRestriction() != '' ) {
00139             return 'private';
00140         }
00141         return 'public';
00142     }
00143 
00144     public function getAllowedParams() {
00145         return array(
00146             'page' => array(
00147                 ApiBase::PARAM_TYPE => array_keys( $this->qpMap ),
00148                 ApiBase::PARAM_REQUIRED => true
00149             ),
00150             'offset' => 0,
00151             'limit' => array(
00152                 ApiBase::PARAM_DFLT => 10,
00153                 ApiBase::PARAM_TYPE => 'limit',
00154                 ApiBase::PARAM_MIN => 1,
00155                 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
00156                 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
00157             ),
00158         );
00159     }
00160 
00161     public function getParamDescription() {
00162         return array(
00163             'page' => 'The name of the special page. Note, this is case sensitive',
00164             'offset' => 'When more results are available, use this to continue',
00165             'limit' => 'Number of results to return',
00166         );
00167     }
00168 
00169     public function getResultProperties() {
00170         return array(
00171             ApiBase::PROP_ROOT => array(
00172                 'name' => array(
00173                     ApiBase::PROP_TYPE => 'string',
00174                     ApiBase::PROP_NULLABLE => false
00175                 ),
00176                 'disabled' => array(
00177                     ApiBase::PROP_TYPE => 'boolean',
00178                     ApiBase::PROP_NULLABLE => false
00179                 ),
00180                 'cached' => array(
00181                     ApiBase::PROP_TYPE => 'boolean',
00182                     ApiBase::PROP_NULLABLE => false
00183                 ),
00184                 'cachedtimestamp' => array(
00185                     ApiBase::PROP_TYPE => 'timestamp',
00186                     ApiBase::PROP_NULLABLE => true
00187                 )
00188             ),
00189             '' => array(
00190                 'value' => 'string',
00191                 'timestamp' => array(
00192                     ApiBase::PROP_TYPE => 'timestamp',
00193                     ApiBase::PROP_NULLABLE => true
00194                 ),
00195                 'ns' => 'namespace',
00196                 'title' => 'string'
00197             )
00198         );
00199     }
00200 
00201     public function getDescription() {
00202         return 'Get a list provided by a QueryPage-based special page';
00203     }
00204 
00205     public function getPossibleErrors() {
00206         return array_merge( parent::getPossibleErrors(), array(
00207             array( 'specialpage-cantexecute' )
00208         ) );
00209     }
00210 
00211     public function getExamples() {
00212         return array(
00213             'api.php?action=query&list=querypage&qppage=Ancientpages'
00214         );
00215     }
00216 
00217     public function getHelpUrls() {
00218         return 'https://www.mediawiki.org/wiki/API:Querypage';
00219     }
00220 }