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