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