API * better self-description for various modules
[lhc/web/wiklou.git] / includes / api / ApiQueryBase.php
1 <?php
2
3
4 /*
5 * Created on Sep 7, 2006
6 *
7 * API for MediaWiki 1.8+
8 *
9 * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@gmail.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 * http://www.gnu.org/copyleft/gpl.html
25 */
26
27 if (!defined('MEDIAWIKI')) {
28 // Eclipse helper - will be ignored in production
29 require_once ('ApiBase.php');
30 }
31
32 abstract class ApiQueryBase extends ApiBase {
33
34 private $mQueryModule, $tables, $where, $fields, $options;
35
36 public function __construct($query, $moduleName, $paramPrefix = '') {
37 parent :: __construct($query->getMain(), $moduleName, $paramPrefix);
38 $this->mQueryModule = $query;
39 $this->resetQueryParams();
40 }
41
42 protected function resetQueryParams() {
43 $this->tables = array ();
44 $this->where = array ();
45 $this->fields = array ();
46 $this->options = array ();
47 }
48
49 protected function addTables($value) {
50 if (is_array($value))
51 $this->tables = array_merge($this->tables, $value);
52 else
53 $this->tables[] = $value;
54 }
55
56 protected function addFields($value) {
57 if (is_array($value))
58 $this->fields = array_merge($this->fields, $value);
59 else
60 $this->fields[] = $value;
61 }
62
63 protected function addFieldsIf($value, $condition) {
64 if ($condition) {
65 $this->addFields($value);
66 return true;
67 }
68 return false;
69 }
70
71 protected function addWhere($value) {
72 if (is_array($value))
73 $this->where = array_merge($this->where, $value);
74 else
75 $this->where[] = $value;
76 }
77
78 protected function addWhereIf($value, $condition) {
79 if ($condition) {
80 $this->addWhere($value);
81 return true;
82 }
83 return false;
84 }
85
86 protected function addWhereFld($field, $value) {
87 if (!is_null($value))
88 $this->where[$field] = $value;
89 }
90
91 protected function addWhereRange($field, $dir, $start, $end) {
92 $isDirNewer = ($dir === 'newer');
93 $after = ($isDirNewer ? '>=' : '<=');
94 $before = ($isDirNewer ? '<=' : '>=');
95 $db = & $this->getDB();
96
97 if (!is_null($start))
98 $this->addWhere($field . $after . $db->addQuotes($start));
99
100 if (!is_null($end))
101 $this->addWhere($field . $before . $db->addQuotes($end));
102
103 $this->addOption('ORDER BY', $field . ($isDirNewer ? '' : ' DESC'));
104 }
105
106 protected function addOption($name, $value = null) {
107 if (is_null($value))
108 $this->options[] = $name;
109 else
110 $this->options[$name] = $value;
111 }
112
113 protected function select($method) {
114
115 // getDB has its own profileDBIn/Out calls
116 $db = & $this->getDB();
117
118 $this->profileDBIn();
119 $res = $db->select($this->tables, $this->fields, $this->where, $method, $this->options);
120 $this->profileDBOut();
121
122 return $res;
123 }
124
125 protected function addRowInfo($prefix, $row) {
126
127 $vals = array ();
128
129 // ID
130 @ $tmp = $row-> {
131 $prefix . '_id' };
132 if (!is_null($tmp))
133 $vals[$prefix . 'id'] = intval($tmp);
134
135 // Title
136 $title = ApiQueryBase :: addRowInfo_title($row, $prefix . '_namespace', $prefix . '_title');
137 if ($title) {
138 if (!$title->userCanRead())
139 return false;
140 $vals['ns'] = $title->getNamespace();
141 $vals['title'] = $title->getPrefixedText();
142 }
143
144 switch ($prefix) {
145
146 case 'page' :
147 // page_is_redirect
148 @ $tmp = $row->page_is_redirect;
149 if ($tmp)
150 $vals['redirect'] = '';
151
152 break;
153
154 case 'rc' :
155 // PageId
156 @ $tmp = $row->rc_cur_id;
157 if (!is_null($tmp))
158 $vals['pageid'] = intval($tmp);
159
160 @ $tmp = $row->rc_this_oldid;
161 if (!is_null($tmp))
162 $vals['revid'] = intval($tmp);
163
164 @ $tmp = $row->rc_last_oldid;
165 if (!is_null($tmp))
166 $vals['old_revid'] = intval($tmp);
167
168 $title = ApiQueryBase :: addRowInfo_title($row, 'rc_moved_to_ns', 'rc_moved_to_title');
169 if ($title) {
170 if (!$title->userCanRead())
171 return false;
172 $vals['new_ns'] = $title->getNamespace();
173 $vals['new_title'] = $title->getPrefixedText();
174 }
175
176 @ $tmp = $row->rc_patrolled;
177 if (!is_null($tmp))
178 $vals['patrolled'] = '';
179
180 break;
181
182 case 'log' :
183 // PageId
184 @ $tmp = $row->page_id;
185 if (!is_null($tmp))
186 $vals['pageid'] = intval($tmp);
187
188 if ($row->log_params !== '') {
189 $params = explode("\n", $row->log_params);
190 if ($row->log_type == 'move' && isset ($params[0])) {
191 $newTitle = Title :: newFromText($params[0]);
192 if ($newTitle) {
193 $vals['new_ns'] = $newTitle->getNamespace();
194 $vals['new_title'] = $newTitle->getPrefixedText();
195 $params = null;
196 }
197 }
198
199 if (!empty ($params)) {
200 $this->getResult()->setIndexedTagName($params, 'param');
201 $vals = array_merge($vals, $params);
202 }
203 }
204
205 break;
206
207 case 'rev' :
208 // PageID
209 @ $tmp = $row->rev_page;
210 if (!is_null($tmp))
211 $vals['pageid'] = intval($tmp);
212 }
213
214 // Type
215 @ $tmp = $row-> {
216 $prefix . '_type' };
217 if (!is_null($tmp))
218 $vals['type'] = $tmp;
219
220 // Action
221 @ $tmp = $row-> {
222 $prefix . '_action' };
223 if (!is_null($tmp))
224 $vals['action'] = $tmp;
225
226 // Old ID
227 @ $tmp = $row-> {
228 $prefix . '_text_id' };
229 if (!is_null($tmp))
230 $vals['oldid'] = intval($tmp);
231
232 // User Name / Anon IP
233 @ $tmp = $row-> {
234 $prefix . '_user_text' };
235 if (is_null($tmp))
236 @ $tmp = $row->user_name;
237 if (!is_null($tmp)) {
238 $vals['user'] = $tmp;
239 @ $tmp = !$row-> {
240 $prefix . '_user' };
241 if (!is_null($tmp) && $tmp)
242 $vals['anon'] = '';
243 }
244
245 // Bot Edit
246 @ $tmp = $row-> {
247 $prefix . '_bot' };
248 if (!is_null($tmp) && $tmp)
249 $vals['bot'] = '';
250
251 // New Edit
252 @ $tmp = $row-> {
253 $prefix . '_new' };
254 if (is_null($tmp))
255 @ $tmp = $row-> {
256 $prefix . '_is_new' };
257 if (!is_null($tmp) && $tmp)
258 $vals['new'] = '';
259
260 // Minor Edit
261 @ $tmp = $row-> {
262 $prefix . '_minor_edit' };
263 if (is_null($tmp))
264 @ $tmp = $row-> {
265 $prefix . '_minor' };
266 if (!is_null($tmp) && $tmp)
267 $vals['minor'] = '';
268
269 // Timestamp
270 @ $tmp = $row-> {
271 $prefix . '_timestamp' };
272 if (!is_null($tmp))
273 $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $tmp);
274
275 // Comment
276 @ $tmp = $row-> {
277 $prefix . '_comment' };
278 if (!empty ($tmp)) // optimize bandwidth
279 $vals['comment'] = $tmp;
280
281 return $vals;
282 }
283
284 private static function addRowInfo_title($row, $nsfld, $titlefld) {
285 @ $ns = $row-> $nsfld;
286 if (!is_null($ns)) {
287 @ $title = $row-> $titlefld;
288 if (!empty ($title))
289 return Title :: makeTitle($ns, $title);
290 }
291 return false;
292 }
293
294 /**
295 * Override this method to request extra fields from the pageSet
296 * using $this->getPageSet()->requestField('fieldName')
297 */
298 public function requestExtraData() {
299 }
300
301 /**
302 * Get the main Query module
303 */
304 public function getQuery() {
305 return $this->mQueryModule;
306 }
307
308 protected function setContinueEnumParameter($paramName, $paramValue) {
309 $msg = array (
310 $this->encodeParamName($paramName
311 ) => $paramValue);
312 $this->getResult()->addValue('query-continue', $this->getModuleName(), $msg);
313 }
314
315 /**
316 * Get the Query database connection (readonly)
317 */
318 protected function getDB() {
319 return $this->getQuery()->getDB();
320 }
321
322 /**
323 * Get the PageSet object to work on
324 * @return ApiPageSet data
325 */
326 protected function getPageSet() {
327 return $this->mQueryModule->getPageSet();
328 }
329
330 /**
331 * This is a very simplistic utility function
332 * to convert a non-namespaced title string to a db key.
333 * It will replace all ' ' with '_'
334 */
335 public static function titleToKey($title) {
336 return str_replace(' ', '_', $title);
337 }
338
339 public static function keyToTitle($key) {
340 return str_replace('_', ' ', $key);
341 }
342
343 public static function getBaseVersion() {
344 return __CLASS__ . ': $Id$';
345 }
346 }
347
348 abstract class ApiQueryGeneratorBase extends ApiQueryBase {
349
350 private $mIsGenerator;
351
352 public function __construct($query, $moduleName, $paramPrefix = '') {
353 parent :: __construct($query, $moduleName, $paramPrefix);
354 $this->mIsGenerator = false;
355 }
356
357 public function setGeneratorMode() {
358 $this->mIsGenerator = true;
359 }
360
361 /**
362 * Overrides base class to prepend 'g' to every generator parameter
363 */
364 public function encodeParamName($paramName) {
365 if ($this->mIsGenerator)
366 return 'g' . parent :: encodeParamName($paramName);
367 else
368 return parent :: encodeParamName($paramName);
369 }
370
371 /**
372 * Execute this module as a generator
373 * @param $resultPageSet PageSet: All output should be appended to this object
374 */
375 public abstract function executeGenerator($resultPageSet);
376 }
377 ?>