MediaWiki REL1_40
ApiQueryQueryPage.php
Go to the documentation of this file.
1<?php
28
35
39 private $queryPages;
40
44 private $specialPageFactory;
45
51 public function __construct(
52 ApiQuery $query,
53 $moduleName,
54 SpecialPageFactory $specialPageFactory
55 ) {
56 parent::__construct( $query, $moduleName, 'qp' );
57 $this->queryPages = array_values( array_diff(
58 array_column( QueryPage::getPages(), 1 ), // [ class, name ]
59 $this->getConfig()->get( MainConfigNames::APIUselessQueryPages )
60 ) );
61 $this->specialPageFactory = $specialPageFactory;
62 }
63
64 public function execute() {
65 $this->run();
66 }
67
68 public function executeGenerator( $resultPageSet ) {
69 $this->run( $resultPageSet );
70 }
71
76 private function getSpecialPage( $name ): QueryPage {
77 $qp = $this->specialPageFactory->getPage( $name );
78 if ( !$qp ) {
80 __METHOD__,
81 'SpecialPageFactory failed to create special page ' . $name
82 );
83 }
84 if ( !( $qp instanceof QueryPage ) ) {
86 __METHOD__,
87 'Special page ' . $name . ' is not a QueryPage'
88 );
89 }
90 // @phan-suppress-next-line PhanTypeMismatchReturnNullable T240141
91 return $qp;
92 }
93
97 public function run( $resultPageSet = null ) {
98 $params = $this->extractRequestParams();
99 $result = $this->getResult();
100
101 $qp = $this->getSpecialPage( $params['page'] );
102 if ( !$qp->userCanExecute( $this->getUser() ) ) {
103 $this->dieWithError( 'apierror-specialpage-cantexecute' );
104 }
105
106 if ( $resultPageSet === null ) {
107 $r = [ 'name' => $params['page'] ];
108 if ( $qp->isCached() ) {
109 if ( !$qp->isCacheable() ) {
110 $r['disabled'] = true;
111 } else {
112 $r['cached'] = true;
113 $ts = $qp->getCachedTimestamp();
114 if ( $ts ) {
115 $r['cachedtimestamp'] = wfTimestamp( TS_ISO_8601, $ts );
116 }
117 $r['maxresults'] = $this->getConfig()->get( MainConfigNames::QueryCacheLimit );
118 }
119 }
120 $result->addValue( [ 'query' ], $this->getModuleName(), $r );
121 }
122
123 if ( $qp->isCached() && !$qp->isCacheable() ) {
124 // Disabled query page, don't run the query
125 return;
126 }
127
128 $res = $qp->doQuery( $params['offset'], $params['limit'] + 1 );
129 $count = 0;
130 $titles = [];
131 foreach ( $res as $row ) {
132 if ( ++$count > $params['limit'] ) {
133 // We've had enough
134 $this->setContinueEnumParameter( 'offset', $params['offset'] + $params['limit'] );
135 break;
136 }
137
138 $title = Title::makeTitle( $row->namespace, $row->title );
139 if ( $resultPageSet === null ) {
140 $data = [];
141 if ( isset( $row->value ) ) {
142 $data['value'] = $row->value;
143 if ( $qp->usesTimestamps() ) {
144 $data['timestamp'] = wfTimestamp( TS_ISO_8601, $row->value );
145 }
146 }
147 self::addTitleInfo( $data, $title );
148
149 foreach ( $row as $field => $value ) {
150 if ( !in_array( $field, [ 'namespace', 'title', 'value', 'qc_type' ] ) ) {
151 $data['databaseResult'][$field] = $value;
152 }
153 }
154
155 $fit = $result->addValue( [ 'query', $this->getModuleName(), 'results' ], null, $data );
156 if ( !$fit ) {
157 $this->setContinueEnumParameter( 'offset', $params['offset'] + $count - 1 );
158 break;
159 }
160 } else {
161 $titles[] = $title;
162 }
163 }
164 if ( $resultPageSet === null ) {
165 $result->addIndexedTagName(
166 [ 'query', $this->getModuleName(), 'results' ],
167 'page'
168 );
169 } else {
170 $resultPageSet->populateFromTitles( $titles );
171 }
172 }
173
174 public function getCacheMode( $params ) {
175 $qp = $this->getSpecialPage( $params['page'] );
176 if ( $qp->getRestriction() != '' ) {
177 return 'private';
178 }
179
180 return 'public';
181 }
182
183 public function getAllowedParams() {
184 return [
185 'page' => [
186 ParamValidator::PARAM_TYPE => $this->queryPages,
187 ParamValidator::PARAM_REQUIRED => true
188 ],
189 'offset' => [
190 ParamValidator::PARAM_DEFAULT => 0,
191 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
192 ],
193 'limit' => [
194 ParamValidator::PARAM_DEFAULT => 10,
195 ParamValidator::PARAM_TYPE => 'limit',
196 IntegerDef::PARAM_MIN => 1,
197 IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
198 IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
199 ],
200 ];
201 }
202
203 protected function getExamplesMessages() {
204 return [
205 'action=query&list=querypage&qppage=Ancientpages'
206 => 'apihelp-query+querypage-example-ancientpages',
207 ];
208 }
209
210 public function getHelpUrls() {
211 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Querypage';
212 }
213}
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
static dieDebug( $method, $message)
Internal code errors should be reported with this method.
Definition ApiBase.php:1701
const LIMIT_BIG1
Fast query, standard limit.
Definition ApiBase.php:229
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:166
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition ApiBase.php:231
Query module to get the results of a QueryPage-based special page.
__construct(ApiQuery $query, $moduleName, SpecialPageFactory $specialPageFactory)
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
getHelpUrls()
Return links to more detailed help pages about the module.
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
executeGenerator( $resultPageSet)
Execute this module as a generator.
getCacheMode( $params)
Get the cache mode for the data generated by this module.
getExamplesMessages()
Returns usage examples for this module.
run( $resultPageSet=null)
This is the main query class.
Definition ApiQuery.php:42
A class containing constants representing the names of configuration variables.
Factory for handling the special page list and generating SpecialPage objects.
Represents a title within MediaWiki.
Definition Title.php:82
This is a class for doing query pages; since they're almost all the same, we factor out some of the f...
Definition QueryPage.php:45
static getPages()
Get a list of query page classes and their associated special pages, for periodic updates.
Definition QueryPage.php:90
Service for formatting and validating API parameters.
Type definition for integer types.
return true
Definition router.php:92