* New properties: links, templates, images, langlinks
[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 ApiQueryBase :: addTitleInfo($vals, $title);
139
140 switch ($prefix) {
141
142 case 'page' :
143 // page_is_redirect
144 @ $tmp = $row->page_is_redirect;
145 if ($tmp)
146 $vals['redirect'] = '';
147
148 break;
149
150 case 'rc' :
151 // PageId
152 @ $tmp = $row->rc_cur_id;
153 if (!is_null($tmp))
154 $vals['pageid'] = intval($tmp);
155
156 @ $tmp = $row->rc_this_oldid;
157 if (!is_null($tmp))
158 $vals['revid'] = intval($tmp);
159
160 if ( isset( $row->rc_last_oldid ) )
161 $vals['old_revid'] = intval( $row->rc_last_oldid );
162
163 $title = ApiQueryBase :: addRowInfo_title($row, 'rc_moved_to_ns', 'rc_moved_to_title');
164 if ($title) {
165 if (!$title->userCanRead())
166 return false;
167 $vals['new_ns'] = $title->getNamespace();
168 $vals['new_title'] = $title->getPrefixedText();
169 }
170
171 if ( isset( $row->rc_patrolled ) )
172 $vals['patrolled'] = '';
173
174 break;
175
176 case 'log' :
177 // PageId
178 @ $tmp = $row->page_id;
179 if (!is_null($tmp))
180 $vals['pageid'] = intval($tmp);
181
182 if ($row->log_params !== '') {
183 $params = explode("\n", $row->log_params);
184 if ($row->log_type == 'move' && isset ($params[0])) {
185 $newTitle = Title :: newFromText($params[0]);
186 if ($newTitle) {
187 $vals['new_ns'] = $newTitle->getNamespace();
188 $vals['new_title'] = $newTitle->getPrefixedText();
189 $params = null;
190 }
191 }
192
193 if (!empty ($params)) {
194 $this->getResult()->setIndexedTagName($params, 'param');
195 $vals = array_merge($vals, $params);
196 }
197 }
198
199 break;
200
201 case 'rev' :
202 // PageID
203 @ $tmp = $row->rev_page;
204 if (!is_null($tmp))
205 $vals['pageid'] = intval($tmp);
206 }
207
208 // Type
209 @ $tmp = $row-> {
210 $prefix . '_type' };
211 if (!is_null($tmp))
212 $vals['type'] = $tmp;
213
214 // Action
215 @ $tmp = $row-> {
216 $prefix . '_action' };
217 if (!is_null($tmp))
218 $vals['action'] = $tmp;
219
220 // Old ID
221 @ $tmp = $row-> {
222 $prefix . '_text_id' };
223 if (!is_null($tmp))
224 $vals['oldid'] = intval($tmp);
225
226 // User Name / Anon IP
227 @ $tmp = $row-> {
228 $prefix . '_user_text' };
229 if (is_null($tmp))
230 @ $tmp = $row->user_name;
231 if (!is_null($tmp)) {
232 $vals['user'] = $tmp;
233 @ $tmp = !$row-> {
234 $prefix . '_user' };
235 if (!is_null($tmp) && $tmp)
236 $vals['anon'] = '';
237 }
238
239 // Bot Edit
240 @ $tmp = $row-> {
241 $prefix . '_bot' };
242 if (!is_null($tmp) && $tmp)
243 $vals['bot'] = '';
244
245 // New Edit
246 @ $tmp = $row-> {
247 $prefix . '_new' };
248 if (is_null($tmp))
249 @ $tmp = $row-> {
250 $prefix . '_is_new' };
251 if (!is_null($tmp) && $tmp)
252 $vals['new'] = '';
253
254 // Minor Edit
255 @ $tmp = $row-> {
256 $prefix . '_minor_edit' };
257 if (is_null($tmp))
258 @ $tmp = $row-> {
259 $prefix . '_minor' };
260 if (!is_null($tmp) && $tmp)
261 $vals['minor'] = '';
262
263 // Timestamp
264 @ $tmp = $row-> {
265 $prefix . '_timestamp' };
266 if (!is_null($tmp))
267 $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $tmp);
268
269 // Comment
270 @ $tmp = $row-> {
271 $prefix . '_comment' };
272 if (!empty ($tmp)) // optimize bandwidth
273 $vals['comment'] = $tmp;
274
275 return $vals;
276 }
277
278 protected static function addTitleInfo(&$arr, $title) {
279 $arr['ns'] = $title->getNamespace();
280 $arr['title'] = $title->getPrefixedText();
281 }
282
283 private static function addRowInfo_title($row, $nsfld, $titlefld) {
284 if ( isset( $row-> $nsfld ) ) {
285 $ns = $row-> $nsfld;
286 @ $title = $row-> $titlefld;
287 if (!empty ($title))
288 return Title :: makeTitle($ns, $title);
289 }
290 return false;
291 }
292
293 /**
294 * Override this method to request extra fields from the pageSet
295 * using $this->getPageSet()->requestField('fieldName')
296 */
297 public function requestExtraData() {
298 }
299
300 /**
301 * Get the main Query module
302 */
303 public function getQuery() {
304 return $this->mQueryModule;
305 }
306
307 protected function setContinueEnumParameter($paramName, $paramValue) {
308 $msg = array (
309 $this->encodeParamName($paramName
310 ) => $paramValue);
311 $this->getResult()->addValue('query-continue', $this->getModuleName(), $msg);
312 }
313
314 /**
315 * Get the Query database connection (readonly)
316 */
317 protected function getDB() {
318 return $this->getQuery()->getDB();
319 }
320
321 /**
322 * Get the PageSet object to work on
323 * @return ApiPageSet data
324 */
325 protected function getPageSet() {
326 return $this->mQueryModule->getPageSet();
327 }
328
329 /**
330 * This is a very simplistic utility function
331 * to convert a non-namespaced title string to a db key.
332 * It will replace all ' ' with '_'
333 */
334 public static function titleToKey($title) {
335 return str_replace(' ', '_', $title);
336 }
337
338 public static function keyToTitle($key) {
339 return str_replace('_', ' ', $key);
340 }
341
342 public static function getBaseVersion() {
343 return __CLASS__ . ': $Id$';
344 }
345 }
346
347 /**
348 * @addtogroup API
349 */
350 abstract class ApiQueryGeneratorBase extends ApiQueryBase {
351
352 private $mIsGenerator;
353
354 public function __construct($query, $moduleName, $paramPrefix = '') {
355 parent :: __construct($query, $moduleName, $paramPrefix);
356 $this->mIsGenerator = false;
357 }
358
359 public function setGeneratorMode() {
360 $this->mIsGenerator = true;
361 }
362
363 /**
364 * Overrides base class to prepend 'g' to every generator parameter
365 */
366 public function encodeParamName($paramName) {
367 if ($this->mIsGenerator)
368 return 'g' . parent :: encodeParamName($paramName);
369 else
370 return parent :: encodeParamName($paramName);
371 }
372
373 /**
374 * Execute this module as a generator
375 * @param $resultPageSet PageSet: All output should be appended to this object
376 */
377 public abstract function executeGenerator($resultPageSet);
378 }
379 ?>