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