5 * Created on Sep 7, 2006
7 * API for MediaWiki 1.8+
9 * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@gmail.com>
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.
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.
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
27 if (!defined('MEDIAWIKI')) {
28 // Eclipse helper - will be ignored in production
29 require_once ('ApiBase.php');
32 abstract class ApiQueryBase
extends ApiBase
{
34 private $mQueryModule, $tables, $where, $fields, $options;
36 public function __construct($query, $moduleName, $paramPrefix = '') {
37 parent
:: __construct($query->getMain(), $moduleName, $paramPrefix);
38 $this->mQueryModule
= $query;
39 $this->resetQueryParams();
42 protected function resetQueryParams() {
43 $this->tables
= array ();
44 $this->where
= array ();
45 $this->fields
= array();
46 $this->options
= array ();
49 protected function addTables($value) {
51 $this->tables
= array_merge($this->tables
, $value);
53 $this->tables
[] = $value;
56 protected function addFields($value) {
58 $this->fields
= array_merge($this->fields
, $value);
60 $this->fields
[] = $value;
63 protected function addFieldsIf($value, $condition) {
65 $this->addFields($value);
71 protected function addWhere($value) {
73 $this->where
= array_merge($this->where
, $value);
75 $this->where
[] = $value;
78 protected function addWhereIf($value, $condition) {
80 $this->addWhere($value);
86 protected function addWhereFld($field, $value) {
88 $this->where
[$field] = $value;
91 protected function addWhereRange($field, $dir, $start, $end) {
92 $isDirNewer = ($dir === 'newer');
93 $after = ($isDirNewer ?
'<=' : '>=');
94 $before = ($isDirNewer ?
'>=' : '<=');
95 $db = & $this->getDB();
98 $this->addWhere($field . $after . $db->addQuotes($start));
101 $this->addWhere($field . $before . $db->addQuotes($end));
103 $this->addOption('ORDER BY', $field . ($isDirNewer ?
'' : ' DESC'));
106 protected function addOption($name, $value) {
107 $this->options
[$name] = $value;
110 protected function select($method) {
112 // getDB has its own profileDBIn/Out calls
113 $db = & $this->getDB();
115 $this->profileDBIn();
116 $res = $db->select($this->tables
, $this->fields
, $this->where
, $method, $this->options
);
117 $this->profileDBOut();
123 protected function addRowInfo($prefix, $row) {
128 @$tmp = $row->{$prefix . '_id'};
129 if(!is_null($tmp)) $vals[$prefix . 'id'] = intval($tmp);
132 $title = ApiQueryBase
::addRowInfo_title($row, $prefix . '_namespace', $prefix . '_title');
134 if (!$title->userCanRead())
136 $vals['ns'] = $title->getNamespace();
137 $vals['title'] = $title->getPrefixedText();
144 @$tmp = $row->page_is_redirect
;
145 if($tmp) $vals['redirect'] = '';
151 @$tmp = $row->rc_cur_id
;
152 if(!is_null($tmp)) $vals['pageid'] = intval($tmp);
154 @$tmp = $row->rc_this_oldid
;
155 if(!is_null($tmp)) $vals['revid'] = intval($tmp);
157 @$tmp = $row->rc_last_oldid
;
158 if(!is_null($tmp)) $vals['old_revid'] = intval($tmp);
160 $title = ApiQueryBase
::addRowInfo_title($row, 'rc_moved_to_ns', 'rc_moved_to_title');
162 if (!$title->userCanRead())
164 $vals['new_ns'] = $title->getNamespace();
165 $vals['new_title'] = $title->getPrefixedText();
168 @$tmp = $row->rc_patrolled
;
169 if(!is_null($tmp)) $vals['patrolled'] = '';
175 @$tmp = $row->page_id
;
176 if(!is_null($tmp)) $vals['pageid'] = intval($tmp);
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]);
183 $vals['new_ns'] = $newTitle->getNamespace();
184 $vals['new_title'] = $newTitle->getPrefixedText();
189 if (!empty ($params)) {
190 $this->getResult()->setIndexedTagName($params, 'param');
191 $vals = array_merge($vals, $params);
199 @$tmp = $row->{$prefix . '_type'};
200 if(!is_null($tmp)) $vals['type'] = $tmp;
203 @$tmp = $row->{$prefix . '_action'};
204 if(!is_null($tmp)) $vals['action'] = $tmp;
207 @$tmp = $row->{$prefix . '_text_id'};
208 if(!is_null($tmp)) $vals['oldid'] = intval($tmp);
210 // User Name / Anon IP
211 @$tmp = $row->{$prefix . '_user_text'};
212 if(is_null($tmp)) @$tmp = $row->user_name
;
214 $vals['user'] = $tmp;
215 @$tmp = !$row->{$prefix . '_user'};
216 if(!is_null($tmp) && $tmp)
221 @$tmp = $row->{$prefix . '_bot'};
222 if(!is_null($tmp) && $tmp) $vals['bot'] = '';
225 @$tmp = $row->{$prefix . '_new'};
226 if(is_null($tmp)) @$tmp = $row->{$prefix . '_is_new'};
227 if(!is_null($tmp) && $tmp) $vals['new'] = '';
230 @$tmp = $row->{$prefix . '_minor_edit'};
231 if(is_null($tmp)) @$tmp = $row->{$prefix . '_minor'};
232 if(!is_null($tmp) && $tmp) $vals['minor'] = '';
235 @$tmp = $row->{$prefix . '_timestamp'};
237 $vals['timestamp'] = wfTimestamp(TS_ISO_8601
, $tmp);
240 @$tmp = $row->{$prefix . '_comment'};
241 if(!empty($tmp)) // optimize bandwidth
242 $vals['comment'] = $tmp;
247 private static function addRowInfo_title($row, $nsfld, $titlefld) {
250 @$title = $row->$titlefld;
252 return Title
:: makeTitle($ns, $title);
258 * Override this method to request extra fields from the pageSet
259 * using $this->getPageSet()->requestField('fieldName')
261 public function requestExtraData() {
265 * Get the main Query module
267 public function getQuery() {
268 return $this->mQueryModule
;
271 protected function setContinueEnumParameter($paramName, $paramValue) {
273 $this->encodeParamName($paramName
275 $this->getResult()->addValue('query-continue', $this->getModuleName(), $msg);
279 * Get the Query database connection (readonly)
281 protected function getDB() {
282 return $this->getQuery()->getDB();
286 * Get the PageSet object to work on
287 * @return ApiPageSet data
289 protected function getPageSet() {
290 return $this->mQueryModule
->getPageSet();
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 '_'
298 public static function titleToKey($title) {
299 return str_replace(' ', '_', $title);
302 public static function keyToTitle($key) {
303 return str_replace('_', ' ', $key);
306 public static function getBaseVersion() {
307 return __CLASS__
. ': $Id$';
311 abstract class ApiQueryGeneratorBase
extends ApiQueryBase
{
313 private $mIsGenerator;
315 public function __construct($query, $moduleName, $paramPrefix = '') {
316 parent
:: __construct($query, $moduleName, $paramPrefix);
317 $this->mIsGenerator
= false;
320 public function setGeneratorMode() {
321 $this->mIsGenerator
= true;
325 * Overrides base class to prepend 'g' to every generator parameter
327 public function encodeParamName($paramName) {
328 if ($this->mIsGenerator
)
329 return 'g' . parent
:: encodeParamName($paramName);
331 return parent
:: encodeParamName($paramName);
335 * Execute this module as a generator
336 * @param $resultPageSet PageSet: All output should be appended to this object
338 public abstract function executeGenerator($resultPageSet);