88af62b6d379e9f85ed0fbeb546685db26eb43a9
[lhc/web/wiklou.git] / includes / api / ApiQueryQueryPage.php
1 <?php
2 /**
3 *
4 *
5 * Created on Dec 22, 2010
6 *
7 * Copyright © 2010 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
8 *
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.
13 *
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.
18 *
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
23 *
24 * @file
25 */
26
27 /**
28 * Query module to get the results of a QueryPage-based special page
29 *
30 * @ingroup API
31 */
32 class ApiQueryQueryPage extends ApiQueryGeneratorBase {
33 private $qpMap;
34
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";
41
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];
48 }
49 }
50 }
51
52 public function execute() {
53 $this->run();
54 }
55
56 public function executeGenerator( $resultPageSet ) {
57 $this->run( $resultPageSet );
58 }
59
60 /**
61 * @param $resultPageSet ApiPageSet
62 */
63 public function run( $resultPageSet = null ) {
64 global $wgQueryCacheLimit;
65
66 $params = $this->extractRequestParams();
67 $result = $this->getResult();
68
69 /** @var $qp QueryPage */
70 $qp = new $this->qpMap[$params['page']]();
71 if ( !$qp->userCanExecute( $this->getUser() ) ) {
72 $this->dieUsageMsg( 'specialpage-cantexecute' );
73 }
74
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 );
84 }
85 $r['maxresults'] = $wgQueryCacheLimit;
86 }
87 }
88 $result->addValue( array( 'query' ), $this->getModuleName(), $r );
89
90 if ( $qp->isCached() && !$qp->isCacheable() ) {
91 // Disabled query page, don't run the query
92 return;
93 }
94
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;
103 }
104
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 );
110 }
111 self::addTitleInfo( $data, $title );
112
113 foreach ( $row as $field => $value ) {
114 if ( !in_array( $field, array( 'namespace', 'title', 'value', 'qc_type' ) ) ) {
115 $data['databaseResult'][$field] = $value;
116 }
117 }
118
119 $fit = $result->addValue( array( 'query', $this->getModuleName(), 'results' ), null, $data );
120 if ( !$fit ) {
121 $this->setContinueEnumParameter( 'offset', $params['offset'] + $count - 1 );
122 break;
123 }
124 } else {
125 $titles[] = $title;
126 }
127 }
128 if ( is_null( $resultPageSet ) ) {
129 $result->setIndexedTagName_internal(
130 array( 'query', $this->getModuleName(), 'results' ),
131 'page'
132 );
133 } else {
134 $resultPageSet->populateFromTitles( $titles );
135 }
136 }
137
138 public function getCacheMode( $params ) {
139 /** @var $qp QueryPage */
140 $qp = new $this->qpMap[$params['page']]();
141 if ( $qp->getRestriction() != '' ) {
142 return 'private';
143 }
144
145 return 'public';
146 }
147
148 public function getAllowedParams() {
149 return array(
150 'page' => array(
151 ApiBase::PARAM_TYPE => array_keys( $this->qpMap ),
152 ApiBase::PARAM_REQUIRED => true
153 ),
154 'offset' => 0,
155 'limit' => array(
156 ApiBase::PARAM_DFLT => 10,
157 ApiBase::PARAM_TYPE => 'limit',
158 ApiBase::PARAM_MIN => 1,
159 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
160 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
161 ),
162 );
163 }
164
165 public function getParamDescription() {
166 return array(
167 'page' => 'The name of the special page. Note, this is case sensitive',
168 'offset' => 'When more results are available, use this to continue',
169 'limit' => 'Number of results to return',
170 );
171 }
172
173 public function getResultProperties() {
174 return array(
175 ApiBase::PROP_ROOT => array(
176 'name' => array(
177 ApiBase::PROP_TYPE => 'string',
178 ApiBase::PROP_NULLABLE => false
179 ),
180 'disabled' => array(
181 ApiBase::PROP_TYPE => 'boolean',
182 ApiBase::PROP_NULLABLE => false
183 ),
184 'cached' => array(
185 ApiBase::PROP_TYPE => 'boolean',
186 ApiBase::PROP_NULLABLE => false
187 ),
188 'cachedtimestamp' => array(
189 ApiBase::PROP_TYPE => 'timestamp',
190 ApiBase::PROP_NULLABLE => true
191 )
192 ),
193 '' => array(
194 'value' => 'string',
195 'timestamp' => array(
196 ApiBase::PROP_TYPE => 'timestamp',
197 ApiBase::PROP_NULLABLE => true
198 ),
199 'ns' => 'namespace',
200 'title' => 'string'
201 )
202 );
203 }
204
205 public function getDescription() {
206 return 'Get a list provided by a QueryPage-based special page';
207 }
208
209 public function getPossibleErrors() {
210 return array_merge( parent::getPossibleErrors(), array(
211 array( 'specialpage-cantexecute' )
212 ) );
213 }
214
215 public function getExamples() {
216 return array(
217 'api.php?action=query&list=querypage&qppage=Ancientpages'
218 );
219 }
220
221 public function getHelpUrls() {
222 return 'https://www.mediawiki.org/wiki/API:Querypage';
223 }
224 }