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