MediaWiki master
ApiQueryQueryPage.php
Go to the documentation of this file.
1<?php
9namespace MediaWiki\Api;
10
17use Wikimedia\Timestamp\TimestampFormat as TS;
18
25
29 private $queryPages;
30
31 private SpecialPageFactory $specialPageFactory;
32
33 public function __construct(
34 ApiQuery $query,
35 string $moduleName,
36 SpecialPageFactory $specialPageFactory
37 ) {
38 parent::__construct( $query, $moduleName, 'qp' );
39 $this->queryPages = array_values( array_diff(
40 array_column( QueryPage::getPages(), 1 ), // [ class, name ]
42 ) );
43 $this->specialPageFactory = $specialPageFactory;
44 }
45
46 public function execute() {
47 $this->run();
48 }
49
51 public function executeGenerator( $resultPageSet ) {
52 $this->run( $resultPageSet );
53 }
54
59 private function getSpecialPage( $name ): QueryPage {
60 $qp = $this->specialPageFactory->getPage( $name );
61 if ( !$qp ) {
63 __METHOD__,
64 'SpecialPageFactory failed to create special page ' . $name
65 );
66 }
67 if ( !( $qp instanceof QueryPage ) ) {
69 __METHOD__,
70 'Special page ' . $name . ' is not a QueryPage'
71 );
72 }
73 // @phan-suppress-next-line PhanTypeMismatchReturnNullable T240141
74 return $qp;
75 }
76
80 public function run( $resultPageSet = null ) {
81 $params = $this->extractRequestParams();
82 $result = $this->getResult();
83
84 $qp = $this->getSpecialPage( $params['page'] );
85 if ( !$qp->userCanExecute( $this->getUser() ) ) {
86 $this->dieWithError( 'apierror-specialpage-cantexecute' );
87 }
88
89 if ( $resultPageSet === null ) {
90 $r = [ 'name' => $params['page'] ];
91 if ( $qp->isCached() ) {
92 if ( !$qp->isCacheable() ) {
93 $r['disabled'] = true;
94 } else {
95 $r['cached'] = true;
96 $ts = $qp->getCachedTimestamp();
97 if ( $ts ) {
98 $r['cachedtimestamp'] = wfTimestamp( TS::ISO_8601, $ts );
99 }
100 $r['maxresults'] = $this->getConfig()->get( MainConfigNames::QueryCacheLimit );
101 }
102 }
103 $result->addValue( [ 'query' ], $this->getModuleName(), $r );
104 }
105
106 if ( $qp->isCached() && !$qp->isCacheable() ) {
107 // Disabled query page, don't run the query
108 return;
109 }
110
111 $res = $qp->doQuery( $params['offset'], $params['limit'] + 1 );
112 $count = 0;
113 $titles = [];
114 foreach ( $res as $row ) {
115 if ( ++$count > $params['limit'] ) {
116 // We've had enough
117 $this->setContinueEnumParameter( 'offset', $params['offset'] + $params['limit'] );
118 break;
119 }
120
121 $title = Title::makeTitle( $row->namespace, $row->title );
122 if ( $resultPageSet === null ) {
123 $data = [];
124 if ( isset( $row->value ) ) {
125 $data['value'] = $row->value;
126 if ( $qp->usesTimestamps() ) {
127 $data['timestamp'] = wfTimestamp( TS::ISO_8601, $row->value );
128 }
129 }
130 self::addTitleInfo( $data, $title );
131
132 foreach ( $row as $field => $value ) {
133 if ( !in_array( $field, [ 'namespace', 'title', 'value', 'qc_type' ] ) ) {
134 $data['databaseResult'][$field] = $value;
135 }
136 }
137
138 $fit = $result->addValue( [ 'query', $this->getModuleName(), 'results' ], null, $data );
139 if ( !$fit ) {
140 $this->setContinueEnumParameter( 'offset', $params['offset'] + $count - 1 );
141 break;
142 }
143 } else {
144 $titles[] = $title;
145 }
146 }
147 if ( $resultPageSet === null ) {
148 $result->addIndexedTagName(
149 [ 'query', $this->getModuleName(), 'results' ],
150 'page'
151 );
152 } else {
153 $resultPageSet->populateFromTitles( $titles );
154 }
155 }
156
158 public function getCacheMode( $params ) {
159 $qp = $this->getSpecialPage( $params['page'] );
160 if ( $qp->getRestriction() != '' ) {
161 return 'private';
162 }
163
164 return 'public';
165 }
166
168 public function getAllowedParams() {
169 return [
170 'page' => [
171 ParamValidator::PARAM_TYPE => $this->queryPages,
172 ParamValidator::PARAM_REQUIRED => true
173 ],
174 'offset' => [
175 ParamValidator::PARAM_DEFAULT => 0,
176 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
177 ],
178 'limit' => [
179 ParamValidator::PARAM_DEFAULT => 10,
180 ParamValidator::PARAM_TYPE => 'limit',
181 IntegerDef::PARAM_MIN => 1,
182 IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
183 IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
184 ],
185 ];
186 }
187
189 protected function getExamplesMessages() {
190 return [
191 'action=query&list=querypage&qppage=Ancientpages'
192 => 'apihelp-query+querypage-example-ancientpages',
193 ];
194 }
195
197 public function getHelpUrls() {
198 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Querypage';
199 }
200}
201
203class_alias( ApiQueryQueryPage::class, 'ApiQueryQueryPage' );
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:1748
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:167
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition ApiBase.php:234
const LIMIT_BIG1
Fast query, standard limit.
Definition ApiBase.php:232
Query module to get the results of a QueryPage-based special page.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
__construct(ApiQuery $query, string $moduleName, SpecialPageFactory $specialPageFactory)
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
getExamplesMessages()
Returns usage examples for this module.Return value has query strings as keys, with values being eith...
getCacheMode( $params)
Get the cache mode for the data generated by this module.Override this in the module subclass....
getHelpUrls()
Return links to more detailed help pages about the module.1.25, returning boolean false is deprecated...
executeGenerator( $resultPageSet)
Execute this module as a generator.
This is the main query class.
Definition ApiQuery.php:36
makeTitle( $linkId)
Convert a link ID to a Title.to override Title
A class containing constants representing the names of configuration variables.
const QueryCacheLimit
Name constant for the QueryCacheLimit setting, for use with Config::get()
const APIUselessQueryPages
Name constant for the APIUselessQueryPages setting, for use with Config::get()
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:77
static getPages()
Get a list of query page classes and their associated special pages, for periodic updates.
Factory for handling the special page list and generating SpecialPage objects.
Represents a title within MediaWiki.
Definition Title.php:69
Service for formatting and validating API parameters.
Type definition for integer types.