Merge "Typo fix"
[mediawiki.git] / includes / api / ApiQueryQueryPage.php
blob79fe0498cfac167a1188eceb62b993370b0a130a
1 <?php
2 /**
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
24 * @file
27 /**
28 * Query module to get the results of a QueryPage-based special page
30 * @ingroup API
32 class ApiQueryQueryPage extends ApiQueryGeneratorBase {
33 private $qpMap;
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
38 // This SUCKS
39 global $IP;
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() {
53 $this->run();
56 public function executeGenerator( $resultPageSet ) {
57 $this->run( $resultPageSet );
60 /**
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() ) {
78 $r['disabled'] = '';
79 } else {
80 $r['cached'] = '';
81 $ts = $qp->getCachedTimestamp();
82 if ( $ts ) {
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
92 return;
95 $res = $qp->doQuery( $params['offset'], $params['limit'] + 1 );
96 $count = 0;
97 $titles = array();
98 foreach ( $res as $row ) {
99 if ( ++$count > $params['limit'] ) {
100 // We've had enough
101 $this->setContinueEnumParameter( 'offset', $params['offset'] + $params['limit'] );
102 break;
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 );
120 if ( !$fit ) {
121 $this->setContinueEnumParameter( 'offset', $params['offset'] + $count - 1 );
122 break;
124 } else {
125 $titles[] = $title;
128 if ( is_null( $resultPageSet ) ) {
129 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName(), 'results' ), 'page' );
130 } else {
131 $resultPageSet->populateFromTitles( $titles );
135 public function getCacheMode( $params ) {
136 /** @var $qp QueryPage */
137 $qp = new $this->qpMap[$params['page']]();
138 if ( $qp->getRestriction() != '' ) {
139 return 'private';
141 return 'public';
144 public function getAllowedParams() {
145 return array(
146 'page' => array(
147 ApiBase::PARAM_TYPE => array_keys( $this->qpMap ),
148 ApiBase::PARAM_REQUIRED => true
150 'offset' => 0,
151 'limit' => array(
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() {
162 return array(
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() {
170 return array(
171 ApiBase::PROP_ROOT => array(
172 'name' => array(
173 ApiBase::PROP_TYPE => 'string',
174 ApiBase::PROP_NULLABLE => false
176 'disabled' => array(
177 ApiBase::PROP_TYPE => 'boolean',
178 ApiBase::PROP_NULLABLE => false
180 'cached' => array(
181 ApiBase::PROP_TYPE => 'boolean',
182 ApiBase::PROP_NULLABLE => false
184 'cachedtimestamp' => array(
185 ApiBase::PROP_TYPE => 'timestamp',
186 ApiBase::PROP_NULLABLE => true
189 '' => array(
190 'value' => 'string',
191 'timestamp' => array(
192 ApiBase::PROP_TYPE => 'timestamp',
193 ApiBase::PROP_NULLABLE => true
195 'ns' => 'namespace',
196 'title' => 'string'
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' )
208 ) );
211 public function getExamples() {
212 return array(
213 'api.php?action=query&list=querypage&qppage=Ancientpages'
217 public function getHelpUrls() {
218 return 'https://www.mediawiki.org/wiki/API:Querypage';