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 // We need to do this to make sure $wgQueryPages is set up
40 require_once "$IP/includes/QueryPage.php";
42 // Build mapping from special page names to QueryPage classes
43 global $wgQueryPages, $wgAPIUselessQueryPages;
44 $this->qpMap
= array();
45 foreach ( $wgQueryPages as $page ) {
46 if ( !in_array( $page[1], $wgAPIUselessQueryPages ) ) {
47 $this->qpMap
[$page[1]] = $page[0];
52 public function execute() {
56 public function executeGenerator( $resultPageSet ) {
57 $this->run( $resultPageSet );
61 * @param $resultPageSet ApiPageSet
63 public function run( $resultPageSet = null ) {
64 global $wgQueryCacheLimit;
66 $params = $this->extractRequestParams();
67 $result = $this->getResult();
69 /** @var $qp QueryPage */
70 $qp = new $this->qpMap
[$params['page']]();
71 if ( !$qp->userCanExecute( $this->getUser() ) ) {
72 $this->dieUsageMsg( 'specialpage-cantexecute' );
75 $r = array( 'name' => $params['page'] );
76 if ( $qp->isCached() ) {
77 if ( !$qp->isCacheable() ) {
81 $ts = $qp->getCachedTimestamp();
83 $r['cachedtimestamp'] = wfTimestamp( TS_ISO_8601
, $ts );
85 $r['maxresults'] = $wgQueryCacheLimit;
88 $result->addValue( array( 'query' ), $this->getModuleName(), $r );
90 if ( $qp->isCached() && !$qp->isCacheable() ) {
91 // Disabled query page, don't run the query
95 $res = $qp->doQuery( $params['offset'], $params['limit'] +
1 );
98 foreach ( $res as $row ) {
99 if ( ++
$count > $params['limit'] ) {
101 $this->setContinueEnumParameter( 'offset', $params['offset'] +
$params['limit'] );
105 $title = Title
::makeTitle( $row->namespace, $row->title
);
106 if ( is_null( $resultPageSet ) ) {
107 $data = array( 'value' => $row->value
);
108 if ( $qp->usesTimestamps() ) {
109 $data['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->value
);
111 self
::addTitleInfo( $data, $title );
113 foreach ( $row as $field => $value ) {
114 if ( !in_array( $field, array( 'namespace', 'title', 'value', 'qc_type' ) ) ) {
115 $data['databaseResult'][$field] = $value;
119 $fit = $result->addValue( array( 'query', $this->getModuleName(), 'results' ), null, $data );
121 $this->setContinueEnumParameter( 'offset', $params['offset'] +
$count - 1 );
128 if ( is_null( $resultPageSet ) ) {
129 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName(), 'results' ), 'page' );
131 $resultPageSet->populateFromTitles( $titles );
135 public function getCacheMode( $params ) {
136 /** @var $qp QueryPage */
137 $qp = new $this->qpMap
[$params['page']]();
138 if ( $qp->getRestriction() != '' ) {
144 public function getAllowedParams() {
147 ApiBase
::PARAM_TYPE
=> array_keys( $this->qpMap
),
148 ApiBase
::PARAM_REQUIRED
=> true
152 ApiBase
::PARAM_DFLT
=> 10,
153 ApiBase
::PARAM_TYPE
=> 'limit',
154 ApiBase
::PARAM_MIN
=> 1,
155 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
156 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
161 public function getParamDescription() {
163 'page' => 'The name of the special page. Note, this is case sensitive',
164 'offset' => 'When more results are available, use this to continue',
165 'limit' => 'Number of results to return',
169 public function getResultProperties() {
171 ApiBase
::PROP_ROOT
=> array(
173 ApiBase
::PROP_TYPE
=> 'string',
174 ApiBase
::PROP_NULLABLE
=> false
177 ApiBase
::PROP_TYPE
=> 'boolean',
178 ApiBase
::PROP_NULLABLE
=> false
181 ApiBase
::PROP_TYPE
=> 'boolean',
182 ApiBase
::PROP_NULLABLE
=> false
184 'cachedtimestamp' => array(
185 ApiBase
::PROP_TYPE
=> 'timestamp',
186 ApiBase
::PROP_NULLABLE
=> true
191 'timestamp' => array(
192 ApiBase
::PROP_TYPE
=> 'timestamp',
193 ApiBase
::PROP_NULLABLE
=> true
201 public function getDescription() {
202 return 'Get a list provided by a QueryPage-based special page';
205 public function getPossibleErrors() {
206 return array_merge( parent
::getPossibleErrors(), array(
207 array( 'specialpage-cantexecute' )
211 public function getExamples() {
213 'api.php?action=query&list=querypage&qppage=Ancientpages'
217 public function getHelpUrls() {
218 return 'https://www.mediawiki.org/wiki/API:Querypage';