5 * Created on Dec 22, 2010
7 * Copyright © 2010 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
28 * Query module to get the results of a QueryPage-based special page
32 class ApiQueryQueryPage
extends ApiQueryGeneratorBase
{
35 public function __construct( $query, $moduleName ) {
36 parent
::__construct( $query, $moduleName, 'qp' );
37 // Build mapping from special page names to QueryPage classes
38 global $wgAPIUselessQueryPages;
39 $this->qpMap
= array();
40 foreach ( QueryPage
::getPages() as $page ) {
41 if ( !in_array( $page[1], $wgAPIUselessQueryPages ) ) {
42 $this->qpMap
[$page[1]] = $page[0];
47 public function execute() {
51 public function executeGenerator( $resultPageSet ) {
52 $this->run( $resultPageSet );
56 * @param ApiPageSet $resultPageSet
58 public function run( $resultPageSet = null ) {
59 global $wgQueryCacheLimit;
61 $params = $this->extractRequestParams();
62 $result = $this->getResult();
64 /** @var $qp QueryPage */
65 $qp = new $this->qpMap
[$params['page']]();
66 if ( !$qp->userCanExecute( $this->getUser() ) ) {
67 $this->dieUsageMsg( 'specialpage-cantexecute' );
70 $r = array( 'name' => $params['page'] );
71 if ( $qp->isCached() ) {
72 if ( !$qp->isCacheable() ) {
76 $ts = $qp->getCachedTimestamp();
78 $r['cachedtimestamp'] = wfTimestamp( TS_ISO_8601
, $ts );
80 $r['maxresults'] = $wgQueryCacheLimit;
83 $result->addValue( array( 'query' ), $this->getModuleName(), $r );
85 if ( $qp->isCached() && !$qp->isCacheable() ) {
86 // Disabled query page, don't run the query
90 $res = $qp->doQuery( $params['offset'], $params['limit'] +
1 );
93 foreach ( $res as $row ) {
94 if ( ++
$count > $params['limit'] ) {
96 $this->setContinueEnumParameter( 'offset', $params['offset'] +
$params['limit'] );
100 $title = Title
::makeTitle( $row->namespace, $row->title
);
101 if ( is_null( $resultPageSet ) ) {
102 $data = array( 'value' => $row->value
);
103 if ( $qp->usesTimestamps() ) {
104 $data['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->value
);
106 self
::addTitleInfo( $data, $title );
108 foreach ( $row as $field => $value ) {
109 if ( !in_array( $field, array( 'namespace', 'title', 'value', 'qc_type' ) ) ) {
110 $data['databaseResult'][$field] = $value;
114 $fit = $result->addValue( array( 'query', $this->getModuleName(), 'results' ), null, $data );
116 $this->setContinueEnumParameter( 'offset', $params['offset'] +
$count - 1 );
123 if ( is_null( $resultPageSet ) ) {
124 $result->setIndexedTagName_internal(
125 array( 'query', $this->getModuleName(), 'results' ),
129 $resultPageSet->populateFromTitles( $titles );
133 public function getCacheMode( $params ) {
134 /** @var $qp QueryPage */
135 $qp = new $this->qpMap
[$params['page']]();
136 if ( $qp->getRestriction() != '' ) {
143 public function getAllowedParams() {
146 ApiBase
::PARAM_TYPE
=> array_keys( $this->qpMap
),
147 ApiBase
::PARAM_REQUIRED
=> true
151 ApiBase
::PARAM_DFLT
=> 10,
152 ApiBase
::PARAM_TYPE
=> 'limit',
153 ApiBase
::PARAM_MIN
=> 1,
154 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
155 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
160 public function getParamDescription() {
162 'page' => 'The name of the special page. Note, this is case sensitive',
163 'offset' => 'When more results are available, use this to continue',
164 'limit' => 'Number of results to return',
168 public function getResultProperties() {
170 ApiBase
::PROP_ROOT
=> array(
172 ApiBase
::PROP_TYPE
=> 'string',
173 ApiBase
::PROP_NULLABLE
=> false
176 ApiBase
::PROP_TYPE
=> 'boolean',
177 ApiBase
::PROP_NULLABLE
=> false
180 ApiBase
::PROP_TYPE
=> 'boolean',
181 ApiBase
::PROP_NULLABLE
=> false
183 'cachedtimestamp' => array(
184 ApiBase
::PROP_TYPE
=> 'timestamp',
185 ApiBase
::PROP_NULLABLE
=> true
190 'timestamp' => array(
191 ApiBase
::PROP_TYPE
=> 'timestamp',
192 ApiBase
::PROP_NULLABLE
=> true
200 public function getDescription() {
201 return 'Get a list provided by a QueryPage-based special page.';
204 public function getPossibleErrors() {
205 return array_merge( parent
::getPossibleErrors(), array(
206 array( 'specialpage-cantexecute' )
210 public function getExamples() {
212 'api.php?action=query&list=querypage&qppage=Ancientpages'
216 public function getHelpUrls() {
217 return 'https://www.mediawiki.org/wiki/API:Querypage';