Single messages can be passed as strings to Api::dieUsageMsg()
[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 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiQueryBase.php' );
30 }
31
32 /**
33 * Query module to get the results of a QueryPage-based special page
34 *
35 * @ingroup API
36 */
37 class ApiQueryQueryPage extends ApiQueryGeneratorBase {
38 private $qpMap;
39
40 /**
41 * Some query pages are useless because they're available elsewhere in the API
42 */
43 private $uselessQueryPages = array(
44 'MIMEsearch', // aiprop=mime
45 'LinkSearch', // list=exturlusage
46 'FileDuplicateSearch', // prop=duplicatefiles
47 );
48
49 public function __construct( $query, $moduleName ) {
50 parent::__construct( $query, $moduleName, 'qp' );
51 // We need to do this to make sure $wgQueryPages is set up
52 // This SUCKS
53 global $IP;
54 require_once( "$IP/includes/QueryPage.php" );
55
56 // Build mapping from special page names to QueryPage classes
57 global $wgQueryPages;
58 $this->qpMap = array();
59 foreach ( $wgQueryPages as $page ) {
60 if( !in_array( $page[1], $this->uselessQueryPages ) ) {
61 $this->qpMap[$page[1]] = $page[0];
62 }
63 }
64 }
65
66 public function execute() {
67 $this->run();
68 }
69
70 public function executeGenerator( $resultPageSet ) {
71 $this->run( $resultPageSet );
72 }
73
74 /**
75 * @param $resultPageSet ApiPageSet
76 * @return void
77 */
78 public function run( $resultPageSet = null ) {
79 global $wgUser;
80 $params = $this->extractRequestParams();
81 $result = $this->getResult();
82
83 $qp = new $this->qpMap[$params['page']]();
84 if ( !$qp->userCanExecute( $wgUser ) ) {
85 $this->dieUsageMsg( 'specialpage-cantexecute' );
86 }
87
88 $r = array( 'name' => $params['page'] );
89 if ( $qp->isCached() ) {
90 if ( !$qp->isCacheable() ) {
91 $r['disabled'] = '';
92 } else {
93 $r['cached'] = '';
94 $ts = $qp->getCachedTimestamp();
95 if ( $ts ) {
96 $r['cachedtimestamp'] = wfTimestamp( TS_ISO_8601, $ts );
97 }
98 }
99 }
100 $result->addValue( array( 'query' ), $this->getModuleName(), $r );
101
102 $res = $qp->doQuery( $params['limit'] + 1, $params['offset'] );
103 $count = 0;
104 $titles = array();
105 foreach ( $res as $row ) {
106 if ( ++$count > $params['limit'] ) {
107 // We've had enough
108 $this->setContinueEnumParameter( 'offset', $params['offset'] + $params['limit'] );
109 break;
110 }
111
112 $title = Title::makeTitle( $row->namespace, $row->title );
113 if ( is_null( $resultPageSet ) ) {
114 $data = array( 'value' => $row->value );
115 if ( $qp->usesTimestamps() ) {
116 $data['timestamp'] = wfTimestamp( TS_ISO_8601, $row->value );
117 }
118 self::addTitleInfo( $data, $title );
119
120 foreach ( $row as $field => $value ) {
121 if ( !in_array( $field, array( 'namespace', 'title', 'value', 'qc_type' ) ) ) {
122 $data['databaseResult'][$field] = $value;
123 }
124 }
125
126 $fit = $result->addValue( array( 'query', $this->getModuleName(), 'results' ), null, $data );
127 if ( !$fit ) {
128 $this->setContinueEnumParameter( 'offset', $params['offset'] + $count - 1 );
129 break;
130 }
131 } else {
132 $titles[] = $title;
133 }
134 }
135 if ( is_null( $resultPageSet ) ) {
136 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName(), 'results' ), 'page' );
137 } else {
138 $resultPageSet->populateFromTitles( $titles );
139 }
140 }
141
142 public function getCacheMode( $params ) {
143 $qp = new $this->qpMap[$params['page']]();
144 if ( $qp->getRestriction() != '' ) {
145 return 'private';
146 }
147 return 'public';
148 }
149
150 public function getAllowedParams() {
151 return array(
152 'page' => array(
153 ApiBase::PARAM_TYPE => array_keys( $this->qpMap ),
154 ApiBase::PARAM_REQUIRED => true
155 ),
156 'offset' => 0,
157 'limit' => array(
158 ApiBase::PARAM_DFLT => 10,
159 ApiBase::PARAM_TYPE => 'limit',
160 ApiBase::PARAM_MIN => 1,
161 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
162 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
163 ),
164 );
165 }
166
167 public function getParamDescription() {
168 return array(
169 'page' => 'The name of the special page. Note, this is case sensitive',
170 'offset' => 'When more results are available, use this to continue',
171 'limit' => 'Number of results to return',
172 );
173 }
174
175 public function getDescription() {
176 return 'Get a list provided by a QueryPage-based special page';
177 }
178
179 public function getPossibleErrors() {
180 return array_merge( parent::getPossibleErrors(), array(
181 ) );
182 }
183
184 protected function getExamples() {
185 return array(
186 'api.php?action=query&list=querypage&qppage=Ancientpages'
187 );
188 }
189
190 public function getVersion() {
191 return __CLASS__ . ': $Id$';
192 }
193 }