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 public function __construct(
32 ApiQuery $query,
33 string $moduleName,
34 private readonly SpecialPageFactory $specialPageFactory,
35 ) {
36 parent::__construct( $query, $moduleName, 'qp' );
37 $this->queryPages = array_values( array_diff(
38 array_column( QueryPage::getPages(), 1 ), // [ class, name ]
40 ) );
41 }
42
43 public function execute() {
44 $this->run();
45 }
46
48 public function executeGenerator( $resultPageSet ) {
49 $this->run( $resultPageSet );
50 }
51
56 private function getSpecialPage( $name ): QueryPage {
57 $qp = $this->specialPageFactory->getPage( $name );
58 if ( !$qp ) {
60 __METHOD__,
61 'SpecialPageFactory failed to create special page ' . $name
62 );
63 }
64 if ( !( $qp instanceof QueryPage ) ) {
66 __METHOD__,
67 'Special page ' . $name . ' is not a QueryPage'
68 );
69 }
70 return $qp;
71 }
72
76 public function run( $resultPageSet = null ) {
77 $params = $this->extractRequestParams();
78 $result = $this->getResult();
79
80 $qp = $this->getSpecialPage( $params['page'] );
81 if ( !$qp->userCanExecute( $this->getUser() ) ) {
82 $this->dieWithError( 'apierror-specialpage-cantexecute' );
83 }
84
85 if ( $resultPageSet === null ) {
86 $r = [ 'name' => $params['page'] ];
87 if ( $qp->isCached() ) {
88 if ( !$qp->isCacheable() ) {
89 $r['disabled'] = true;
90 } else {
91 $r['cached'] = true;
92 $ts = $qp->getCachedTimestamp();
93 if ( $ts ) {
94 $r['cachedtimestamp'] = wfTimestamp( TS::ISO_8601, $ts );
95 }
96 $r['maxresults'] = $this->getConfig()->get( MainConfigNames::QueryCacheLimit );
97 }
98 }
99 $result->addValue( [ 'query' ], $this->getModuleName(), $r );
100 }
101
102 if ( $qp->isCached() && !$qp->isCacheable() ) {
103 // Disabled query page, don't run the query
104 return;
105 }
106
107 $res = $qp->doQuery( $params['offset'], $params['limit'] + 1 );
108 $count = 0;
109 $titles = [];
110 foreach ( $res as $row ) {
111 if ( ++$count > $params['limit'] ) {
112 // We've had enough
113 $this->setContinueEnumParameter( 'offset', $params['offset'] + $params['limit'] );
114 break;
115 }
116
117 $title = Title::makeTitle( $row->namespace, $row->title );
118 if ( $resultPageSet === null ) {
119 $data = [];
120 if ( isset( $row->value ) ) {
121 $data['value'] = $row->value;
122 if ( $qp->usesTimestamps() ) {
123 $data['timestamp'] = wfTimestamp( TS::ISO_8601, $row->value );
124 }
125 }
126 self::addTitleInfo( $data, $title );
127
128 foreach ( $row as $field => $value ) {
129 if ( !in_array( $field, [ 'namespace', 'title', 'value', 'qc_type' ] ) ) {
130 $data['databaseResult'][$field] = $value;
131 }
132 }
133
134 $fit = $result->addValue( [ 'query', $this->getModuleName(), 'results' ], null, $data );
135 if ( !$fit ) {
136 $this->setContinueEnumParameter( 'offset', $params['offset'] + $count - 1 );
137 break;
138 }
139 } else {
140 $titles[] = $title;
141 }
142 }
143 if ( $resultPageSet === null ) {
144 $result->addIndexedTagName(
145 [ 'query', $this->getModuleName(), 'results' ],
146 'page'
147 );
148 } else {
149 $resultPageSet->populateFromTitles( $titles );
150 }
151 }
152
154 public function getCacheMode( $params ) {
155 $qp = $this->getSpecialPage( $params['page'] );
156 if ( $qp->getRestriction() != '' ) {
157 return 'private';
158 }
159
160 return 'public';
161 }
162
164 public function getAllowedParams() {
165 return [
166 'page' => [
167 ParamValidator::PARAM_TYPE => $this->queryPages,
168 ParamValidator::PARAM_REQUIRED => true
169 ],
170 'offset' => [
171 ParamValidator::PARAM_DEFAULT => 0,
172 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
173 ],
174 'limit' => [
175 ParamValidator::PARAM_DEFAULT => 10,
176 ParamValidator::PARAM_TYPE => 'limit',
177 IntegerDef::PARAM_MIN => 1,
178 IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
179 IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
180 ],
181 ];
182 }
183
185 protected function getExamplesMessages() {
186 return [
187 'action=query&list=querypage&qppage=Ancientpages'
188 => 'apihelp-query+querypage-example-ancientpages',
189 ];
190 }
191
193 public function getHelpUrls() {
194 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Querypage';
195 }
196}
197
199class_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:1759
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:233
const LIMIT_BIG1
Fast query, standard limit.
Definition ApiBase.php:231
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...
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.
__construct(ApiQuery $query, string $moduleName, private readonly SpecialPageFactory $specialPageFactory,)
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.