MediaWiki  1.34.0
ApiQueryQueryPage.php
Go to the documentation of this file.
1 <?php
25 
32 
36  private $queryPages;
37 
42 
43  public function __construct( ApiQuery $query, $moduleName ) {
44  parent::__construct( $query, $moduleName, 'qp' );
45  $this->queryPages = array_values( array_diff(
46  array_column( QueryPage::getPages(), 1 ), // [ class, name ]
47  $this->getConfig()->get( 'APIUselessQueryPages' )
48  ) );
49  $this->specialPageFactory = MediaWikiServices::getInstance()->getSpecialPageFactory();
50  }
51 
52  public function execute() {
53  $this->run();
54  }
55 
56  public function executeGenerator( $resultPageSet ) {
57  $this->run( $resultPageSet );
58  }
59 
64  private function getSpecialPage( $name ) : QueryPage {
65  $qp = $this->specialPageFactory->getPage( $name );
66  if ( !$qp ) {
68  __METHOD__,
69  'SpecialPageFactory failed to create special page ' . $name
70  );
71  }
72  if ( !( $qp instanceof QueryPage ) ) {
74  __METHOD__,
75  'Special page ' . $name . ' is not a QueryPage'
76  );
77  }
78  return $qp;
79  }
80 
84  public function run( $resultPageSet = null ) {
85  $params = $this->extractRequestParams();
86  $result = $this->getResult();
87 
88  $qp = $this->getSpecialPage( $params['page'] );
89  if ( !$qp->userCanExecute( $this->getUser() ) ) {
90  $this->dieWithError( 'apierror-specialpage-cantexecute' );
91  }
92 
93  $r = [ 'name' => $params['page'] ];
94  if ( $qp->isCached() ) {
95  if ( !$qp->isCacheable() ) {
96  $r['disabled'] = true;
97  } else {
98  $r['cached'] = true;
99  $ts = $qp->getCachedTimestamp();
100  if ( $ts ) {
101  $r['cachedtimestamp'] = wfTimestamp( TS_ISO_8601, $ts );
102  }
103  $r['maxresults'] = $this->getConfig()->get( 'QueryCacheLimit' );
104  }
105  }
106  $result->addValue( [ 'query' ], $this->getModuleName(), $r );
107 
108  if ( $qp->isCached() && !$qp->isCacheable() ) {
109  // Disabled query page, don't run the query
110  return;
111  }
112 
113  $res = $qp->doQuery( $params['offset'], $params['limit'] + 1 );
114  $count = 0;
115  $titles = [];
116  foreach ( $res as $row ) {
117  if ( ++$count > $params['limit'] ) {
118  // We've had enough
119  $this->setContinueEnumParameter( 'offset', $params['offset'] + $params['limit'] );
120  break;
121  }
122 
123  $title = Title::makeTitle( $row->namespace, $row->title );
124  if ( is_null( $resultPageSet ) ) {
125  $data = [];
126  if ( isset( $row->value ) ) {
127  $data['value'] = $row->value;
128  if ( $qp->usesTimestamps() ) {
129  $data['timestamp'] = wfTimestamp( TS_ISO_8601, $row->value );
130  }
131  }
132  self::addTitleInfo( $data, $title );
133 
134  foreach ( $row as $field => $value ) {
135  if ( !in_array( $field, [ 'namespace', 'title', 'value', 'qc_type' ] ) ) {
136  $data['databaseResult'][$field] = $value;
137  }
138  }
139 
140  $fit = $result->addValue( [ 'query', $this->getModuleName(), 'results' ], null, $data );
141  if ( !$fit ) {
142  $this->setContinueEnumParameter( 'offset', $params['offset'] + $count - 1 );
143  break;
144  }
145  } else {
146  $titles[] = $title;
147  }
148  }
149  if ( is_null( $resultPageSet ) ) {
150  $result->addIndexedTagName(
151  [ 'query', $this->getModuleName(), 'results' ],
152  'page'
153  );
154  } else {
155  $resultPageSet->populateFromTitles( $titles );
156  }
157  }
158 
159  public function getCacheMode( $params ) {
160  $qp = $this->getSpecialPage( $params['page'] );
161  if ( $qp->getRestriction() != '' ) {
162  return 'private';
163  }
164 
165  return 'public';
166  }
167 
168  public function getAllowedParams() {
169  return [
170  'page' => [
173  ],
174  'offset' => [
175  ApiBase::PARAM_DFLT => 0,
176  ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
177  ],
178  'limit' => [
179  ApiBase::PARAM_DFLT => 10,
180  ApiBase::PARAM_TYPE => 'limit',
181  ApiBase::PARAM_MIN => 1,
184  ],
185  ];
186  }
187 
188  protected function getExamplesMessages() {
189  return [
190  'action=query&list=querypage&qppage=Ancientpages'
191  => 'apihelp-query+querypage-example-ancientpages',
192  ];
193  }
194 
195  public function getHelpUrls() {
196  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Querypage';
197  }
198 }
ContextSource\getConfig
getConfig()
Definition: ContextSource.php:63
ApiQueryQueryPage\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiQueryQueryPage.php:52
ApiQuery
This is the main query class.
Definition: ApiQuery.php:37
ApiBase\PARAM_REQUIRED
const PARAM_REQUIRED
(boolean) Is the parameter required?
Definition: ApiBase.php:118
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:117
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:2014
ApiBase\PARAM_HELP_MSG
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition: ApiBase.php:131
true
return true
Definition: router.php:92
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1869
ApiBase\PARAM_TYPE
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below.
Definition: ApiBase.php:94
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:640
$res
$res
Definition: testCompression.php:52
QueryPage
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:36
ApiQueryQueryPage\getCacheMode
getCacheMode( $params)
Get the cache mode for the data generated by this module.
Definition: ApiQueryQueryPage.php:159
ApiQueryGeneratorBase\setContinueEnumParameter
setContinueEnumParameter( $paramName, $paramValue)
Overridden to set the generator param if in generator mode.
Definition: ApiQueryGeneratorBase.php:84
ApiQueryQueryPage
Query module to get the results of a QueryPage-based special page.
Definition: ApiQueryQueryPage.php:31
ApiBase\PARAM_MIN
const PARAM_MIN
(integer) Lowest value allowed for the parameter, for PARAM_TYPE 'integer' and 'limit'.
Definition: ApiBase.php:106
ApiQueryQueryPage\$queryPages
string[] $queryPages
list of special page names
Definition: ApiQueryQueryPage.php:36
ApiQueryQueryPage\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiQueryQueryPage.php:195
ApiBase\LIMIT_BIG1
const LIMIT_BIG1
Fast query, standard limit.
Definition: ApiBase.php:259
ApiBase\PARAM_MAX
const PARAM_MAX
(integer) Max value allowed for the parameter, for PARAM_TYPE 'integer' and 'limit'.
Definition: ApiBase.php:97
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:761
$title
$title
Definition: testCompression.php:34
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:586
MediaWiki\Special\SpecialPageFactory
Factory for handling the special page list and generating SpecialPage objects.
Definition: SpecialPageFactory.php:64
ApiQueryQueryPage\executeGenerator
executeGenerator( $resultPageSet)
Execute this module as a generator.
Definition: ApiQueryQueryPage.php:56
ApiQueryQueryPage\getSpecialPage
getSpecialPage( $name)
Definition: ApiQueryQueryPage.php:64
ApiQueryQueryPage\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiQueryQueryPage.php:188
ApiQueryQueryPage\$specialPageFactory
SpecialPageFactory $specialPageFactory
Definition: ApiQueryQueryPage.php:41
ApiQueryQueryPage\run
run( $resultPageSet=null)
Definition: ApiQueryQueryPage.php:84
ApiQueryGeneratorBase
Definition: ApiQueryGeneratorBase.php:26
ApiBase\LIMIT_BIG2
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition: ApiBase.php:261
ApiQueryQueryPage\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiQueryQueryPage.php:168
ApiBase\PARAM_DFLT
const PARAM_DFLT
(null|boolean|integer|string) Default value of the parameter.
Definition: ApiBase.php:55
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:520
ApiBase\PARAM_MAX2
const PARAM_MAX2
(integer) Max value allowed for the parameter for users with the apihighlimits right,...
Definition: ApiBase.php:103
ApiQueryQueryPage\__construct
__construct(ApiQuery $query, $moduleName)
Definition: ApiQueryQueryPage.php:43
QueryPage\getPages
static getPages()
Get a list of query page classes and their associated special pages, for periodic updates.
Definition: QueryPage.php:74
ApiBase\dieDebug
static dieDebug( $method, $message)
Internal code errors should be reported with this method.
Definition: ApiBase.php:2220
ApiQueryBase\addTitleInfo
static addTitleInfo(&$arr, $title, $prefix='')
Add information (title and namespace) about a Title object to a result array.
Definition: ApiQueryBase.php:443