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