MediaWiki  REL1_19
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                 $params = $this->extractRequestParams();
00074                 $result = $this->getResult();
00075 
00076                 $qp = new $this->qpMap[$params['page']]();
00077                 if ( !$qp->userCanExecute( $this->getUser() ) ) {
00078                         $this->dieUsageMsg( 'specialpage-cantexecute' );
00079                 }
00080 
00081                 $r = array( 'name' => $params['page'] );
00082                 if ( $qp->isCached() ) {
00083                         if ( !$qp->isCacheable() ) {
00084                                 $r['disabled'] = '';
00085                         } else {
00086                                 $r['cached'] = '';
00087                                 $ts = $qp->getCachedTimestamp();
00088                                 if ( $ts ) {
00089                                         $r['cachedtimestamp'] = wfTimestamp( TS_ISO_8601, $ts );
00090                                 }
00091                         }
00092                 }
00093                 $result->addValue( array( 'query' ), $this->getModuleName(), $r );
00094 
00095                 if ( $qp->isCached() && !$qp->isCacheable() ) {
00096                         // Disabled query page, don't run the query
00097                         return;
00098                 }
00099 
00100                 $res = $qp->doQuery( $params['offset'], $params['limit'] + 1 );
00101                 $count = 0;
00102                 $titles = array();
00103                 foreach ( $res as $row ) {
00104                         if ( ++$count > $params['limit'] ) {
00105                                 // We've had enough
00106                                 $this->setContinueEnumParameter( 'offset', $params['offset'] + $params['limit'] );
00107                                 break;
00108                         }
00109 
00110                         $title = Title::makeTitle( $row->namespace, $row->title );
00111                         if ( is_null( $resultPageSet ) ) {
00112                                 $data = array( 'value' => $row->value );
00113                                 if ( $qp->usesTimestamps() ) {
00114                                         $data['timestamp'] = wfTimestamp( TS_ISO_8601, $row->value );
00115                                 }
00116                                 self::addTitleInfo( $data, $title );
00117 
00118                                 foreach ( $row as $field => $value ) {
00119                                         if ( !in_array( $field, array( 'namespace', 'title', 'value', 'qc_type' ) ) ) {
00120                                                 $data['databaseResult'][$field] = $value;
00121                                         }
00122                                 }
00123 
00124                                 $fit = $result->addValue( array( 'query', $this->getModuleName(), 'results' ), null, $data );
00125                                 if ( !$fit ) {
00126                                         $this->setContinueEnumParameter( 'offset', $params['offset'] + $count - 1 );
00127                                         break;
00128                                 }
00129                         } else {
00130                                 $titles[] = $title;
00131                         }
00132                 }
00133                 if ( is_null( $resultPageSet ) ) {
00134                         $result->setIndexedTagName_internal( array( 'query', $this->getModuleName(), 'results' ), 'page' );
00135                 } else {
00136                         $resultPageSet->populateFromTitles( $titles );
00137                 }
00138         }
00139 
00140         public function getCacheMode( $params ) {
00141                 $qp = new $this->qpMap[$params['page']]();
00142                 if ( $qp->getRestriction() != '' ) {
00143                         return 'private';
00144                 }
00145                 return 'public';
00146         }
00147 
00148         public function getAllowedParams() {
00149                 return array(
00150                         'page' => array(
00151                                 ApiBase::PARAM_TYPE => array_keys( $this->qpMap ),
00152                                 ApiBase::PARAM_REQUIRED => true
00153                         ),
00154                         'offset' => 0,
00155                         'limit' => array(
00156                                 ApiBase::PARAM_DFLT => 10,
00157                                 ApiBase::PARAM_TYPE => 'limit',
00158                                 ApiBase::PARAM_MIN => 1,
00159                                 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
00160                                 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
00161                         ),
00162                 );
00163         }
00164 
00165         public function getParamDescription() {
00166                 return array(
00167                         'page' => 'The name of the special page. Note, this is case sensitive',
00168                         'offset' => 'When more results are available, use this to continue',
00169                         'limit' => 'Number of results to return',
00170                 );
00171         }
00172 
00173         public function getDescription() {
00174                 return 'Get a list provided by a QueryPage-based special page';
00175         }
00176 
00177         public function getPossibleErrors() {
00178                 return array_merge( parent::getPossibleErrors(), array(
00179                          array( 'specialpage-cantexecute' )
00180                 ) );
00181         }
00182 
00183         public function getExamples() {
00184                 return array(
00185                         'api.php?action=query&list=querypage&qppage=Ancientpages'
00186                 );
00187         }
00188 
00189         public function getVersion() {
00190                 return __CLASS__ . ': $Id$';
00191         }
00192 }