Merge "Allow additional interwiki prefixes on local interwiki links"
[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 // 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];
43 }
44 }
45 }
46
47 public function execute() {
48 $this->run();
49 }
50
51 public function executeGenerator( $resultPageSet ) {
52 $this->run( $resultPageSet );
53 }
54
55 /**
56 * @param ApiPageSet $resultPageSet
57 */
58 public function run( $resultPageSet = null ) {
59 global $wgQueryCacheLimit;
60
61 $params = $this->extractRequestParams();
62 $result = $this->getResult();
63
64 /** @var $qp QueryPage */
65 $qp = new $this->qpMap[$params['page']]();
66 if ( !$qp->userCanExecute( $this->getUser() ) ) {
67 $this->dieUsageMsg( 'specialpage-cantexecute' );
68 }
69
70 $r = array( 'name' => $params['page'] );
71 if ( $qp->isCached() ) {
72 if ( !$qp->isCacheable() ) {
73 $r['disabled'] = '';
74 } else {
75 $r['cached'] = '';
76 $ts = $qp->getCachedTimestamp();
77 if ( $ts ) {
78 $r['cachedtimestamp'] = wfTimestamp( TS_ISO_8601, $ts );
79 }
80 $r['maxresults'] = $wgQueryCacheLimit;
81 }
82 }
83 $result->addValue( array( 'query' ), $this->getModuleName(), $r );
84
85 if ( $qp->isCached() && !$qp->isCacheable() ) {
86 // Disabled query page, don't run the query
87 return;
88 }
89
90 $res = $qp->doQuery( $params['offset'], $params['limit'] + 1 );
91 $count = 0;
92 $titles = array();
93 foreach ( $res as $row ) {
94 if ( ++$count > $params['limit'] ) {
95 // We've had enough
96 $this->setContinueEnumParameter( 'offset', $params['offset'] + $params['limit'] );
97 break;
98 }
99
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 );
105 }
106 self::addTitleInfo( $data, $title );
107
108 foreach ( $row as $field => $value ) {
109 if ( !in_array( $field, array( 'namespace', 'title', 'value', 'qc_type' ) ) ) {
110 $data['databaseResult'][$field] = $value;
111 }
112 }
113
114 $fit = $result->addValue( array( 'query', $this->getModuleName(), 'results' ), null, $data );
115 if ( !$fit ) {
116 $this->setContinueEnumParameter( 'offset', $params['offset'] + $count - 1 );
117 break;
118 }
119 } else {
120 $titles[] = $title;
121 }
122 }
123 if ( is_null( $resultPageSet ) ) {
124 $result->setIndexedTagName_internal(
125 array( 'query', $this->getModuleName(), 'results' ),
126 'page'
127 );
128 } else {
129 $resultPageSet->populateFromTitles( $titles );
130 }
131 }
132
133 public function getCacheMode( $params ) {
134 /** @var $qp QueryPage */
135 $qp = new $this->qpMap[$params['page']]();
136 if ( $qp->getRestriction() != '' ) {
137 return 'private';
138 }
139
140 return 'public';
141 }
142
143 public function getAllowedParams() {
144 return array(
145 'page' => array(
146 ApiBase::PARAM_TYPE => array_keys( $this->qpMap ),
147 ApiBase::PARAM_REQUIRED => true
148 ),
149 'offset' => 0,
150 'limit' => array(
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
156 ),
157 );
158 }
159
160 public function getParamDescription() {
161 return array(
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',
165 );
166 }
167
168 public function getResultProperties() {
169 return array(
170 ApiBase::PROP_ROOT => array(
171 'name' => array(
172 ApiBase::PROP_TYPE => 'string',
173 ApiBase::PROP_NULLABLE => false
174 ),
175 'disabled' => array(
176 ApiBase::PROP_TYPE => 'boolean',
177 ApiBase::PROP_NULLABLE => false
178 ),
179 'cached' => array(
180 ApiBase::PROP_TYPE => 'boolean',
181 ApiBase::PROP_NULLABLE => false
182 ),
183 'cachedtimestamp' => array(
184 ApiBase::PROP_TYPE => 'timestamp',
185 ApiBase::PROP_NULLABLE => true
186 )
187 ),
188 '' => array(
189 'value' => 'string',
190 'timestamp' => array(
191 ApiBase::PROP_TYPE => 'timestamp',
192 ApiBase::PROP_NULLABLE => true
193 ),
194 'ns' => 'namespace',
195 'title' => 'string'
196 )
197 );
198 }
199
200 public function getDescription() {
201 return 'Get a list provided by a QueryPage-based special page.';
202 }
203
204 public function getPossibleErrors() {
205 return array_merge( parent::getPossibleErrors(), array(
206 array( 'specialpage-cantexecute' )
207 ) );
208 }
209
210 public function getExamples() {
211 return array(
212 'api.php?action=query&list=querypage&qppage=Ancientpages'
213 );
214 }
215
216 public function getHelpUrls() {
217 return 'https://www.mediawiki.org/wiki/API:Querypage';
218 }
219 }