MediaWiki  REL1_24
ApiQueryQueryPage.php
Go to the documentation of this file.
00001 <?php
00032 class ApiQueryQueryPage extends ApiQueryGeneratorBase {
00033     private $qpMap;
00034 
00035     public function __construct( ApiQuery $query, $moduleName ) {
00036         parent::__construct( $query, $moduleName, 'qp' );
00037         // Build mapping from special page names to QueryPage classes
00038         $uselessQueryPages = $this->getConfig()->get( 'APIUselessQueryPages' );
00039         $this->qpMap = array();
00040         foreach ( QueryPage::getPages() as $page ) {
00041             if ( !in_array( $page[1], $uselessQueryPages ) ) {
00042                 $this->qpMap[$page[1]] = $page[0];
00043             }
00044         }
00045     }
00046 
00047     public function execute() {
00048         $this->run();
00049     }
00050 
00051     public function executeGenerator( $resultPageSet ) {
00052         $this->run( $resultPageSet );
00053     }
00054 
00058     public function run( $resultPageSet = null ) {
00059         $params = $this->extractRequestParams();
00060         $result = $this->getResult();
00061 
00063         $qp = new $this->qpMap[$params['page']]();
00064         if ( !$qp->userCanExecute( $this->getUser() ) ) {
00065             $this->dieUsageMsg( 'specialpage-cantexecute' );
00066         }
00067 
00068         $r = array( 'name' => $params['page'] );
00069         if ( $qp->isCached() ) {
00070             if ( !$qp->isCacheable() ) {
00071                 $r['disabled'] = '';
00072             } else {
00073                 $r['cached'] = '';
00074                 $ts = $qp->getCachedTimestamp();
00075                 if ( $ts ) {
00076                     $r['cachedtimestamp'] = wfTimestamp( TS_ISO_8601, $ts );
00077                 }
00078                 $r['maxresults'] = $this->getConfig()->get( 'QueryCacheLimit' );
00079             }
00080         }
00081         $result->addValue( array( 'query' ), $this->getModuleName(), $r );
00082 
00083         if ( $qp->isCached() && !$qp->isCacheable() ) {
00084             // Disabled query page, don't run the query
00085             return;
00086         }
00087 
00088         $res = $qp->doQuery( $params['offset'], $params['limit'] + 1 );
00089         $count = 0;
00090         $titles = array();
00091         foreach ( $res as $row ) {
00092             if ( ++$count > $params['limit'] ) {
00093                 // We've had enough
00094                 $this->setContinueEnumParameter( 'offset', $params['offset'] + $params['limit'] );
00095                 break;
00096             }
00097 
00098             $title = Title::makeTitle( $row->namespace, $row->title );
00099             if ( is_null( $resultPageSet ) ) {
00100                 $data = array( 'value' => $row->value );
00101                 if ( $qp->usesTimestamps() ) {
00102                     $data['timestamp'] = wfTimestamp( TS_ISO_8601, $row->value );
00103                 }
00104                 self::addTitleInfo( $data, $title );
00105 
00106                 foreach ( $row as $field => $value ) {
00107                     if ( !in_array( $field, array( 'namespace', 'title', 'value', 'qc_type' ) ) ) {
00108                         $data['databaseResult'][$field] = $value;
00109                     }
00110                 }
00111 
00112                 $fit = $result->addValue( array( 'query', $this->getModuleName(), 'results' ), null, $data );
00113                 if ( !$fit ) {
00114                     $this->setContinueEnumParameter( 'offset', $params['offset'] + $count - 1 );
00115                     break;
00116                 }
00117             } else {
00118                 $titles[] = $title;
00119             }
00120         }
00121         if ( is_null( $resultPageSet ) ) {
00122             $result->setIndexedTagName_internal(
00123                 array( 'query', $this->getModuleName(), 'results' ),
00124                 'page'
00125             );
00126         } else {
00127             $resultPageSet->populateFromTitles( $titles );
00128         }
00129     }
00130 
00131     public function getCacheMode( $params ) {
00133         $qp = new $this->qpMap[$params['page']]();
00134         if ( $qp->getRestriction() != '' ) {
00135             return 'private';
00136         }
00137 
00138         return 'public';
00139     }
00140 
00141     public function getAllowedParams() {
00142         return array(
00143             'page' => array(
00144                 ApiBase::PARAM_TYPE => array_keys( $this->qpMap ),
00145                 ApiBase::PARAM_REQUIRED => true
00146             ),
00147             'offset' => 0,
00148             'limit' => array(
00149                 ApiBase::PARAM_DFLT => 10,
00150                 ApiBase::PARAM_TYPE => 'limit',
00151                 ApiBase::PARAM_MIN => 1,
00152                 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
00153                 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
00154             ),
00155         );
00156     }
00157 
00158     public function getParamDescription() {
00159         return array(
00160             'page' => 'The name of the special page. Note, this is case sensitive',
00161             'offset' => 'When more results are available, use this to continue',
00162             'limit' => 'Number of results to return',
00163         );
00164     }
00165 
00166     public function getDescription() {
00167         return 'Get a list provided by a QueryPage-based special page.';
00168     }
00169 
00170     public function getExamples() {
00171         return array(
00172             'api.php?action=query&list=querypage&qppage=Ancientpages'
00173         );
00174     }
00175 
00176     public function getHelpUrls() {
00177         return 'https://www.mediawiki.org/wiki/API:Querypage';
00178     }
00179 }