Fixed spacing in db/debug/diff/externalstore/objectcache folder
[mediawiki.git] / includes / api / ApiQueryQueryPage.php
blob9d0fd0ab1ce82841fb9a0ea0ce18c6e5f2510978
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 /**
36 * Some query pages are useless because they're available elsewhere in the API
38 private $uselessQueryPages = array(
39 'MIMEsearch', // aiprop=mime
40 'LinkSearch', // list=exturlusage
41 'FileDuplicateSearch', // prop=duplicatefiles
44 public function __construct( $query, $moduleName ) {
45 parent::__construct( $query, $moduleName, 'qp' );
46 // We need to do this to make sure $wgQueryPages is set up
47 // This SUCKS
48 global $IP;
49 require_once( "$IP/includes/QueryPage.php" );
51 // Build mapping from special page names to QueryPage classes
52 global $wgQueryPages;
53 $this->qpMap = array();
54 foreach ( $wgQueryPages as $page ) {
55 if ( !in_array( $page[1], $this->uselessQueryPages ) ) {
56 $this->qpMap[$page[1]] = $page[0];
61 public function execute() {
62 $this->run();
65 public function executeGenerator( $resultPageSet ) {
66 $this->run( $resultPageSet );
69 /**
70 * @param $resultPageSet ApiPageSet
72 public function run( $resultPageSet = null ) {
73 global $wgQueryCacheLimit;
75 $params = $this->extractRequestParams();
76 $result = $this->getResult();
78 /** @var $qp QueryPage */
79 $qp = new $this->qpMap[$params['page']]();
80 if ( !$qp->userCanExecute( $this->getUser() ) ) {
81 $this->dieUsageMsg( 'specialpage-cantexecute' );
84 $r = array( 'name' => $params['page'] );
85 if ( $qp->isCached() ) {
86 if ( !$qp->isCacheable() ) {
87 $r['disabled'] = '';
88 } else {
89 $r['cached'] = '';
90 $ts = $qp->getCachedTimestamp();
91 if ( $ts ) {
92 $r['cachedtimestamp'] = wfTimestamp( TS_ISO_8601, $ts );
94 $r['maxresults'] = $wgQueryCacheLimit;
97 $result->addValue( array( 'query' ), $this->getModuleName(), $r );
99 if ( $qp->isCached() && !$qp->isCacheable() ) {
100 // Disabled query page, don't run the query
101 return;
104 $res = $qp->doQuery( $params['offset'], $params['limit'] + 1 );
105 $count = 0;
106 $titles = array();
107 foreach ( $res as $row ) {
108 if ( ++$count > $params['limit'] ) {
109 // We've had enough
110 $this->setContinueEnumParameter( 'offset', $params['offset'] + $params['limit'] );
111 break;
114 $title = Title::makeTitle( $row->namespace, $row->title );
115 if ( is_null( $resultPageSet ) ) {
116 $data = array( 'value' => $row->value );
117 if ( $qp->usesTimestamps() ) {
118 $data['timestamp'] = wfTimestamp( TS_ISO_8601, $row->value );
120 self::addTitleInfo( $data, $title );
122 foreach ( $row as $field => $value ) {
123 if ( !in_array( $field, array( 'namespace', 'title', 'value', 'qc_type' ) ) ) {
124 $data['databaseResult'][$field] = $value;
128 $fit = $result->addValue( array( 'query', $this->getModuleName(), 'results' ), null, $data );
129 if ( !$fit ) {
130 $this->setContinueEnumParameter( 'offset', $params['offset'] + $count - 1 );
131 break;
133 } else {
134 $titles[] = $title;
137 if ( is_null( $resultPageSet ) ) {
138 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName(), 'results' ), 'page' );
139 } else {
140 $resultPageSet->populateFromTitles( $titles );
144 public function getCacheMode( $params ) {
145 /** @var $qp QueryPage */
146 $qp = new $this->qpMap[$params['page']]();
147 if ( $qp->getRestriction() != '' ) {
148 return 'private';
150 return 'public';
153 public function getAllowedParams() {
154 return array(
155 'page' => array(
156 ApiBase::PARAM_TYPE => array_keys( $this->qpMap ),
157 ApiBase::PARAM_REQUIRED => true
159 'offset' => 0,
160 'limit' => array(
161 ApiBase::PARAM_DFLT => 10,
162 ApiBase::PARAM_TYPE => 'limit',
163 ApiBase::PARAM_MIN => 1,
164 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
165 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
170 public function getParamDescription() {
171 return array(
172 'page' => 'The name of the special page. Note, this is case sensitive',
173 'offset' => 'When more results are available, use this to continue',
174 'limit' => 'Number of results to return',
178 public function getResultProperties() {
179 return array(
180 ApiBase::PROP_ROOT => array(
181 'name' => array(
182 ApiBase::PROP_TYPE => 'string',
183 ApiBase::PROP_NULLABLE => false
185 'disabled' => array(
186 ApiBase::PROP_TYPE => 'boolean',
187 ApiBase::PROP_NULLABLE => false
189 'cached' => array(
190 ApiBase::PROP_TYPE => 'boolean',
191 ApiBase::PROP_NULLABLE => false
193 'cachedtimestamp' => array(
194 ApiBase::PROP_TYPE => 'timestamp',
195 ApiBase::PROP_NULLABLE => true
198 '' => array(
199 'value' => 'string',
200 'timestamp' => array(
201 ApiBase::PROP_TYPE => 'timestamp',
202 ApiBase::PROP_NULLABLE => true
204 'ns' => 'namespace',
205 'title' => 'string'
210 public function getDescription() {
211 return 'Get a list provided by a QueryPage-based special page';
214 public function getPossibleErrors() {
215 return array_merge( parent::getPossibleErrors(), array(
216 array( 'specialpage-cantexecute' )
217 ) );
220 public function getExamples() {
221 return array(
222 'api.php?action=query&list=querypage&qppage=Ancientpages'