API * touched field format
[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
40 $this->tables = array ();
41 $this->where = array ();
42 $this->fields = array();
43 $this->options = array ();
44 }
45
46 protected function addTables($value) {
47 if(is_array($value))
48 $this->tables = array_merge($this->tables, $value);
49 else
50 $this->tables[] = $value;
51 }
52
53 protected function addFields($value) {
54 if(is_array($value))
55 $this->fields = array_merge($this->fields, $value);
56 else
57 $this->fields[] = $value;
58 }
59
60 protected function addFieldsIf($value, $condition) {
61 if ($condition) {
62 $this->addFields($value);
63 return true;
64 }
65 return false;
66 }
67
68 protected function addWhere($value) {
69 if(is_array($value))
70 $this->where = array_merge($this->where, $value);
71 else
72 $this->where[] = $value;
73 }
74
75 protected function addWhereIf($value, $condition) {
76 if ($condition) {
77 $this->addWhere($value);
78 return true;
79 }
80 return false;
81 }
82
83 protected function addWhereFld($field, $value) {
84 if(!is_null($value))
85 $this->where[$field] = $value;
86 }
87
88 protected function addWhereRange($field, $dir, $start, $end) {
89 $isDirNewer = ($dir === 'newer');
90 $after = ($isDirNewer ? '<=' : '>=');
91 $before = ($isDirNewer ? '>=' : '<=');
92 $db = $this->getDB();
93
94 if (!is_null($start))
95 $this->addWhere($field . $after . $db->addQuotes($start));
96
97 if (!is_null($end))
98 $this->addWhere($field . $before . $db->addQuotes($end));
99
100 $this->addOption('ORDER BY', $field . ($isDirNewer ? '' : ' DESC'));
101 }
102
103 protected function addOption($name, $value) {
104 $this->options[$name] = $value;
105 }
106
107 protected function select($method) {
108 $this->profileDBIn();
109 $res = $this->getDB()->select($this->tables, $this->fields, $this->where, $method, $this->options);
110 $this->profileDBOut();
111 return $res;
112 }
113
114
115 protected function addRowInfo($prefix, $row) {
116
117 $vals = array();
118
119 // ID
120 @$tmp = $row->{$prefix . '_id'};
121 if(!is_null($tmp)) $vals[$prefix . 'id'] = intval($tmp);
122
123 // Title
124 $title = ApiQueryBase::addRowInfo_title($row, $prefix . '_namespace', $prefix . '_title');
125 if ($title) {
126 if (!$title->userCanRead())
127 return false;
128 $vals['ns'] = $title->getNamespace();
129 $vals['title'] = $title->getPrefixedText();
130 }
131
132 if ($prefix === 'rc') {
133
134 // PageId
135 @$tmp = $row->rc_cur_id;
136 if(!is_null($tmp)) $vals['pageid'] = intval($tmp);
137
138 @$tmp = $row->rc_this_oldid;
139 if(!is_null($tmp)) $vals['revid'] = intval($tmp);
140
141 @$tmp = $row->rc_last_oldid;
142 if(!is_null($tmp)) $vals['old_revid'] = intval($tmp);
143
144 $title = ApiQueryBase::addRowInfo_title($row, 'rc_moved_to_ns', 'rc_moved_to_title');
145 if ($title) {
146 if (!$title->userCanRead())
147 return false;
148 $vals['new_ns'] = $title->getNamespace();
149 $vals['new_title'] = $title->getPrefixedText();
150 }
151
152 @$tmp = $row->rc_patrolled;
153 if(!is_null($tmp)) $vals['patrolled'] = '';
154
155 } elseif ($prefix === 'log') {
156
157 // PageId
158 @$tmp = $row->page_id;
159 if(!is_null($tmp)) $vals['pageid'] = intval($tmp);
160
161 if ($row->log_params !== '') {
162 $params = explode("\n", $row->log_params);
163 if ($row->log_type == 'move' && isset ($params[0])) {
164 $newTitle = Title :: newFromText($params[0]);
165 if ($newTitle) {
166 $vals['new_ns'] = $newTitle->getNamespace();
167 $vals['new_title'] = $newTitle->getPrefixedText();
168 $params = null;
169 }
170 }
171
172 if (!empty ($params)) {
173 $this->getResult()->setIndexedTagName($params, 'param');
174 $vals = array_merge($vals, $params);
175 }
176 }
177 }
178
179 // Type
180 @$tmp = $row->{$prefix . '_type'};
181 if(!is_null($tmp)) $vals['type'] = $tmp;
182
183 // Action
184 @$tmp = $row->{$prefix . '_action'};
185 if(!is_null($tmp)) $vals['action'] = $tmp;
186
187 // Old ID
188 @$tmp = $row->{$prefix . '_text_id'};
189 if(!is_null($tmp)) $vals['oldid'] = intval($tmp);
190
191 // User Name / Anon IP
192 @$tmp = $row->{$prefix . '_user_text'};
193 if(is_null($tmp)) @$tmp = $row->user_name;
194 if(!is_null($tmp)) {
195 $vals['user'] = $tmp;
196 @$tmp = !$row->{$prefix . '_user'};
197 if(!is_null($tmp) && $tmp)
198 $vals['anon'] = '';
199 }
200
201 // Bot Edit
202 @$tmp = $row->{$prefix . '_bot'};
203 if(!is_null($tmp) && $tmp) $vals['bot'] = '';
204
205 // New Edit
206 @$tmp = $row->{$prefix . '_new'};
207 if(is_null($tmp)) @$tmp = $row->{$prefix . '_is_new'};
208 if(!is_null($tmp) && $tmp) $vals['new'] = '';
209
210 // Minor Edit
211 @$tmp = $row->{$prefix . '_minor_edit'};
212 if(is_null($tmp)) @$tmp = $row->{$prefix . '_minor'};
213 if(!is_null($tmp) && $tmp) $vals['minor'] = '';
214
215 // Timestamp
216 @$tmp = $row->{$prefix . '_timestamp'};
217 if(!is_null($tmp))
218 $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $tmp);
219
220 // Comment
221 @$tmp = $row->{$prefix . '_comment'};
222 if(!empty($tmp)) // optimize bandwidth
223 $vals['comment'] = $tmp;
224
225 return $vals;
226 }
227
228 private static function addRowInfo_title($row, $nsfld, $titlefld) {
229 @$ns = $row->$nsfld;
230 if(!is_null($ns)) {
231 @$title = $row->$titlefld;
232 if(!empty($title))
233 return Title :: makeTitle($ns, $title);
234 }
235 return false;
236 }
237
238 /**
239 * Override this method to request extra fields from the pageSet
240 * using $this->getPageSet()->requestField('fieldName')
241 */
242 public function requestExtraData() {
243 }
244
245 /**
246 * Get the main Query module
247 */
248 public function getQuery() {
249 return $this->mQueryModule;
250 }
251
252 protected function setContinueEnumParameter($paramName, $paramValue) {
253 $msg = array (
254 $this->encodeParamName($paramName
255 ) => $paramValue);
256 $this->getResult()->addValue('query-continue', $this->getModuleName(), $msg);
257 }
258
259 /**
260 * Get the Query database connection (readonly)
261 */
262 protected function getDB() {
263 return $this->getQuery()->getDB();
264 }
265
266 /**
267 * Get the PageSet object to work on
268 * @return ApiPageSet data
269 */
270 protected function getPageSet() {
271 return $this->mQueryModule->getPageSet();
272 }
273
274 /**
275 * This is a very simplistic utility function
276 * to convert a non-namespaced title string to a db key.
277 * It will replace all ' ' with '_'
278 */
279 public static function titleToKey($title) {
280 return str_replace(' ', '_', $title);
281 }
282
283 public static function keyToTitle($key) {
284 return str_replace('_', ' ', $key);
285 }
286
287 public static function getBaseVersion() {
288 return __CLASS__ . ': $Id$';
289 }
290 }
291
292 abstract class ApiQueryGeneratorBase extends ApiQueryBase {
293
294 private $mIsGenerator;
295
296 public function __construct($query, $moduleName, $paramPrefix = '') {
297 parent :: __construct($query, $moduleName, $paramPrefix);
298 $this->mIsGenerator = false;
299 }
300
301 public function setGeneratorMode() {
302 $this->mIsGenerator = true;
303 }
304
305 /**
306 * Overrides base class to prepend 'g' to every generator parameter
307 */
308 public function encodeParamName($paramName) {
309 if ($this->mIsGenerator)
310 return 'g' . parent :: encodeParamName($paramName);
311 else
312 return parent :: encodeParamName($paramName);
313 }
314
315 /**
316 * Execute this module as a generator
317 * @param $resultPageSet PageSet: All output should be appended to this object
318 */
319 public abstract function executeGenerator($resultPageSet);
320 }
321 ?>