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