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