MediaWiki  master
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  if ( $resultPageSet === null ) {
106  $r = [ 'name' => $params['page'] ];
107  if ( $qp->isCached() ) {
108  if ( !$qp->isCacheable() ) {
109  $r['disabled'] = true;
110  } else {
111  $r['cached'] = true;
112  $ts = $qp->getCachedTimestamp();
113  if ( $ts ) {
114  $r['cachedtimestamp'] = wfTimestamp( TS_ISO_8601, $ts );
115  }
116  $r['maxresults'] = $this->getConfig()->get( MainConfigNames::QueryCacheLimit );
117  }
118  }
119  $result->addValue( [ 'query' ], $this->getModuleName(), $r );
120  }
121 
122  if ( $qp->isCached() && !$qp->isCacheable() ) {
123  // Disabled query page, don't run the query
124  return;
125  }
126 
127  $res = $qp->doQuery( $params['offset'], $params['limit'] + 1 );
128  $count = 0;
129  $titles = [];
130  foreach ( $res as $row ) {
131  if ( ++$count > $params['limit'] ) {
132  // We've had enough
133  $this->setContinueEnumParameter( 'offset', $params['offset'] + $params['limit'] );
134  break;
135  }
136 
137  $title = Title::makeTitle( $row->namespace, $row->title );
138  if ( $resultPageSet === null ) {
139  $data = [];
140  if ( isset( $row->value ) ) {
141  $data['value'] = $row->value;
142  if ( $qp->usesTimestamps() ) {
143  $data['timestamp'] = wfTimestamp( TS_ISO_8601, $row->value );
144  }
145  }
146  self::addTitleInfo( $data, $title );
147 
148  foreach ( $row as $field => $value ) {
149  if ( !in_array( $field, [ 'namespace', 'title', 'value', 'qc_type' ] ) ) {
150  $data['databaseResult'][$field] = $value;
151  }
152  }
153 
154  $fit = $result->addValue( [ 'query', $this->getModuleName(), 'results' ], null, $data );
155  if ( !$fit ) {
156  $this->setContinueEnumParameter( 'offset', $params['offset'] + $count - 1 );
157  break;
158  }
159  } else {
160  $titles[] = $title;
161  }
162  }
163  if ( $resultPageSet === null ) {
164  $result->addIndexedTagName(
165  [ 'query', $this->getModuleName(), 'results' ],
166  'page'
167  );
168  } else {
169  $resultPageSet->populateFromTitles( $titles );
170  }
171  }
172 
173  public function getCacheMode( $params ) {
174  $qp = $this->getSpecialPage( $params['page'] );
175  if ( $qp->getRestriction() != '' ) {
176  return 'private';
177  }
178 
179  return 'public';
180  }
181 
182  public function getAllowedParams() {
183  return [
184  'page' => [
185  ParamValidator::PARAM_TYPE => $this->queryPages,
186  ParamValidator::PARAM_REQUIRED => true
187  ],
188  'offset' => [
189  ParamValidator::PARAM_DEFAULT => 0,
190  ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
191  ],
192  'limit' => [
193  ParamValidator::PARAM_DEFAULT => 10,
194  ParamValidator::PARAM_TYPE => 'limit',
195  IntegerDef::PARAM_MIN => 1,
196  IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
197  IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
198  ],
199  ];
200  }
201 
202  protected function getExamplesMessages() {
203  return [
204  'action=query&list=querypage&qppage=Ancientpages'
205  => 'apihelp-query+querypage-example-ancientpages',
206  ];
207  }
208 
209  public function getHelpUrls() {
210  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Querypage';
211  }
212 }
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:1696
const LIMIT_BIG1
Fast query, standard limit.
Definition: ApiBase.php:222
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition: ApiBase.php:164
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition: ApiBase.php:224
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
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:641
Service for formatting and validating API parameters.
Type definition for integer types.
Definition: IntegerDef.php:23
return true
Definition: router.php:90