3 * Copyright © 2010 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
24 * Query module to get the results of a QueryPage-based special page
28 class ApiQueryQueryPage
extends ApiQueryGeneratorBase
{
31 public function __construct( ApiQuery
$query, $moduleName ) {
32 parent
::__construct( $query, $moduleName, 'qp' );
33 // Build mapping from special page names to QueryPage classes
34 $uselessQueryPages = $this->getConfig()->get( 'APIUselessQueryPages' );
36 foreach ( QueryPage
::getPages() as $page ) {
37 if ( !in_array( $page[1], $uselessQueryPages ) ) {
38 $this->qpMap
[$page[1]] = $page[0];
43 public function execute() {
47 public function executeGenerator( $resultPageSet ) {
48 $this->run( $resultPageSet );
52 * @param ApiPageSet $resultPageSet
54 public function run( $resultPageSet = null ) {
55 $params = $this->extractRequestParams();
56 $result = $this->getResult();
58 /** @var QueryPage $qp */
59 $qp = new $this->qpMap
[$params['page']]();
60 if ( !$qp->userCanExecute( $this->getUser() ) ) {
61 $this->dieWithError( 'apierror-specialpage-cantexecute' );
64 $r = [ 'name' => $params['page'] ];
65 if ( $qp->isCached() ) {
66 if ( !$qp->isCacheable() ) {
67 $r['disabled'] = true;
70 $ts = $qp->getCachedTimestamp();
72 $r['cachedtimestamp'] = wfTimestamp( TS_ISO_8601
, $ts );
74 $r['maxresults'] = $this->getConfig()->get( 'QueryCacheLimit' );
77 $result->addValue( [ 'query' ], $this->getModuleName(), $r );
79 if ( $qp->isCached() && !$qp->isCacheable() ) {
80 // Disabled query page, don't run the query
84 $res = $qp->doQuery( $params['offset'], $params['limit'] +
1 );
87 foreach ( $res as $row ) {
88 if ( ++
$count > $params['limit'] ) {
90 $this->setContinueEnumParameter( 'offset', $params['offset'] +
$params['limit'] );
94 $title = Title
::makeTitle( $row->namespace, $row->title
);
95 if ( is_null( $resultPageSet ) ) {
96 $data = [ 'value' => $row->value
];
97 if ( $qp->usesTimestamps() ) {
98 $data['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->value
);
100 self
::addTitleInfo( $data, $title );
102 foreach ( $row as $field => $value ) {
103 if ( !in_array( $field, [ 'namespace', 'title', 'value', 'qc_type' ] ) ) {
104 $data['databaseResult'][$field] = $value;
108 $fit = $result->addValue( [ 'query', $this->getModuleName(), 'results' ], null, $data );
110 $this->setContinueEnumParameter( 'offset', $params['offset'] +
$count - 1 );
117 if ( is_null( $resultPageSet ) ) {
118 $result->addIndexedTagName(
119 [ 'query', $this->getModuleName(), 'results' ],
123 $resultPageSet->populateFromTitles( $titles );
127 public function getCacheMode( $params ) {
128 /** @var QueryPage $qp */
129 $qp = new $this->qpMap
[$params['page']]();
130 if ( $qp->getRestriction() != '' ) {
137 public function getAllowedParams() {
140 ApiBase
::PARAM_TYPE
=> array_keys( $this->qpMap
),
141 ApiBase
::PARAM_REQUIRED
=> true
144 ApiBase
::PARAM_DFLT
=> 0,
145 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
148 ApiBase
::PARAM_DFLT
=> 10,
149 ApiBase
::PARAM_TYPE
=> 'limit',
150 ApiBase
::PARAM_MIN
=> 1,
151 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
152 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
157 protected function getExamplesMessages() {
159 'action=query&list=querypage&qppage=Ancientpages'
160 => 'apihelp-query+querypage-example-ancientpages',
164 public function getHelpUrls() {
165 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Querypage';