5a6ca8be750c61679c789ecc8182606c281ea6e3
[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 } elseif ($prefix === 'rev') {
179
180 // PageId
181 @$tmp = $row->rev_page;
182 if(!is_null($tmp)) $vals['pageid'] = intval($tmp);
183 }
184
185 // Type
186 @$tmp = $row->{$prefix . '_type'};
187 if(!is_null($tmp)) $vals['type'] = $tmp;
188
189 // Action
190 @$tmp = $row->{$prefix . '_action'};
191 if(!is_null($tmp)) $vals['action'] = $tmp;
192
193 // Old ID
194 @$tmp = $row->{$prefix . '_text_id'};
195 if(!is_null($tmp)) $vals['oldid'] = intval($tmp);
196
197 // User Name / Anon IP
198 @$tmp = $row->{$prefix . '_user_text'};
199 if(is_null($tmp)) @$tmp = $row->user_name;
200 if(!is_null($tmp)) {
201 $vals['user'] = $tmp;
202 @$tmp = !$row->{$prefix . '_user'};
203 if(!is_null($tmp) && $tmp)
204 $vals['anon'] = '';
205 }
206
207 // Bot Edit
208 @$tmp = $row->{$prefix . '_bot'};
209 if(!is_null($tmp) && $tmp) $vals['bot'] = '';
210
211 // New Edit
212 @$tmp = $row->{$prefix . '_new'};
213 if(is_null($tmp)) @$tmp = $row->{$prefix . '_is_new'};
214 if(!is_null($tmp) && $tmp) $vals['new'] = '';
215
216 // Minor Edit
217 @$tmp = $row->{$prefix . '_minor_edit'};
218 if(is_null($tmp)) @$tmp = $row->{$prefix . '_minor'};
219 if(!is_null($tmp) && $tmp) $vals['minor'] = '';
220
221 // Timestamp
222 @$tmp = $row->{$prefix . '_timestamp'};
223 if(!is_null($tmp))
224 $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $tmp);
225
226 // Comment
227 @$tmp = $row->{$prefix . '_comment'};
228 if(!empty($tmp)) // optimize bandwidth
229 $vals['comment'] = $tmp;
230
231 return $vals;
232 }
233
234 private static function addRowInfo_title($row, $nsfld, $titlefld) {
235 @$ns = $row->$nsfld;
236 if(!is_null($ns)) {
237 @$title = $row->$titlefld;
238 if(!empty($title))
239 return Title :: makeTitle($ns, $title);
240 }
241 return false;
242 }
243
244 /**
245 * Override this method to request extra fields from the pageSet
246 * using $this->getPageSet()->requestField('fieldName')
247 */
248 public function requestExtraData() {
249 }
250
251 /**
252 * Get the main Query module
253 */
254 public function getQuery() {
255 return $this->mQueryModule;
256 }
257
258 protected function setContinueEnumParameter($paramName, $paramValue) {
259 $msg = array (
260 $this->encodeParamName($paramName
261 ) => $paramValue);
262 $this->getResult()->addValue('query-continue', $this->getModuleName(), $msg);
263 }
264
265 /**
266 * Get the Query database connection (readonly)
267 */
268 protected function getDB() {
269 return $this->getQuery()->getDB();
270 }
271
272 /**
273 * Get the PageSet object to work on
274 * @return ApiPageSet data
275 */
276 protected function getPageSet() {
277 return $this->mQueryModule->getPageSet();
278 }
279
280 /**
281 * This is a very simplistic utility function
282 * to convert a non-namespaced title string to a db key.
283 * It will replace all ' ' with '_'
284 */
285 public static function titleToKey($title) {
286 return str_replace(' ', '_', $title);
287 }
288
289 public static function keyToTitle($key) {
290 return str_replace('_', ' ', $key);
291 }
292
293 public static function getBaseVersion() {
294 return __CLASS__ . ': $Id$';
295 }
296 }
297
298 abstract class ApiQueryGeneratorBase extends ApiQueryBase {
299
300 private $mIsGenerator;
301
302 public function __construct($query, $moduleName, $paramPrefix = '') {
303 parent :: __construct($query, $moduleName, $paramPrefix);
304 $this->mIsGenerator = false;
305 }
306
307 public function setGeneratorMode() {
308 $this->mIsGenerator = true;
309 }
310
311 /**
312 * Overrides base class to prepend 'g' to every generator parameter
313 */
314 public function encodeParamName($paramName) {
315 if ($this->mIsGenerator)
316 return 'g' . parent :: encodeParamName($paramName);
317 else
318 return parent :: encodeParamName($paramName);
319 }
320
321 /**
322 * Execute this module as a generator
323 * @param $resultPageSet PageSet: All output should be appended to this object
324 */
325 public abstract function executeGenerator($resultPageSet);
326 }
327 ?>