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