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 = null) {
108 $this->options
[] = $name;
110 $this->options
[$name] = $value;
113 protected function select($method) {
115 // getDB has its own profileDBIn/Out calls
116 $db = & $this->getDB();
118 $this->profileDBIn();
119 $res = $db->select($this->tables
, $this->fields
, $this->where
, $method, $this->options
);
120 $this->profileDBOut();
126 protected function addRowInfo($prefix, $row) {
131 @$tmp = $row->{$prefix . '_id'};
132 if(!is_null($tmp)) $vals[$prefix . 'id'] = intval($tmp);
135 $title = ApiQueryBase
::addRowInfo_title($row, $prefix . '_namespace', $prefix . '_title');
137 if (!$title->userCanRead())
139 $vals['ns'] = $title->getNamespace();
140 $vals['title'] = $title->getPrefixedText();
147 @$tmp = $row->page_is_redirect
;
148 if($tmp) $vals['redirect'] = '';
154 @$tmp = $row->rc_cur_id
;
155 if(!is_null($tmp)) $vals['pageid'] = intval($tmp);
157 @$tmp = $row->rc_this_oldid
;
158 if(!is_null($tmp)) $vals['revid'] = intval($tmp);
160 @$tmp = $row->rc_last_oldid
;
161 if(!is_null($tmp)) $vals['old_revid'] = intval($tmp);
163 $title = ApiQueryBase
::addRowInfo_title($row, 'rc_moved_to_ns', 'rc_moved_to_title');
165 if (!$title->userCanRead())
167 $vals['new_ns'] = $title->getNamespace();
168 $vals['new_title'] = $title->getPrefixedText();
171 @$tmp = $row->rc_patrolled
;
172 if(!is_null($tmp)) $vals['patrolled'] = '';
178 @$tmp = $row->page_id
;
179 if(!is_null($tmp)) $vals['pageid'] = intval($tmp);
181 if ($row->log_params
!== '') {
182 $params = explode("\n", $row->log_params
);
183 if ($row->log_type
== 'move' && isset ($params[0])) {
184 $newTitle = Title
:: newFromText($params[0]);
186 $vals['new_ns'] = $newTitle->getNamespace();
187 $vals['new_title'] = $newTitle->getPrefixedText();
192 if (!empty ($params)) {
193 $this->getResult()->setIndexedTagName($params, 'param');
194 $vals = array_merge($vals, $params);
202 @$tmp = $row->{$prefix . '_type'};
203 if(!is_null($tmp)) $vals['type'] = $tmp;
206 @$tmp = $row->{$prefix . '_action'};
207 if(!is_null($tmp)) $vals['action'] = $tmp;
210 @$tmp = $row->{$prefix . '_text_id'};
211 if(!is_null($tmp)) $vals['oldid'] = intval($tmp);
213 // User Name / Anon IP
214 @$tmp = $row->{$prefix . '_user_text'};
215 if(is_null($tmp)) @$tmp = $row->user_name
;
217 $vals['user'] = $tmp;
218 @$tmp = !$row->{$prefix . '_user'};
219 if(!is_null($tmp) && $tmp)
224 @$tmp = $row->{$prefix . '_bot'};
225 if(!is_null($tmp) && $tmp) $vals['bot'] = '';
228 @$tmp = $row->{$prefix . '_new'};
229 if(is_null($tmp)) @$tmp = $row->{$prefix . '_is_new'};
230 if(!is_null($tmp) && $tmp) $vals['new'] = '';
233 @$tmp = $row->{$prefix . '_minor_edit'};
234 if(is_null($tmp)) @$tmp = $row->{$prefix . '_minor'};
235 if(!is_null($tmp) && $tmp) $vals['minor'] = '';
238 @$tmp = $row->{$prefix . '_timestamp'};
240 $vals['timestamp'] = wfTimestamp(TS_ISO_8601
, $tmp);
243 @$tmp = $row->{$prefix . '_comment'};
244 if(!empty($tmp)) // optimize bandwidth
245 $vals['comment'] = $tmp;
250 private static function addRowInfo_title($row, $nsfld, $titlefld) {
253 @$title = $row->$titlefld;
255 return Title
:: makeTitle($ns, $title);
261 * Override this method to request extra fields from the pageSet
262 * using $this->getPageSet()->requestField('fieldName')
264 public function requestExtraData() {
268 * Get the main Query module
270 public function getQuery() {
271 return $this->mQueryModule
;
274 protected function setContinueEnumParameter($paramName, $paramValue) {
276 $this->encodeParamName($paramName
278 $this->getResult()->addValue('query-continue', $this->getModuleName(), $msg);
282 * Get the Query database connection (readonly)
284 protected function getDB() {
285 return $this->getQuery()->getDB();
289 * Get the PageSet object to work on
290 * @return ApiPageSet data
292 protected function getPageSet() {
293 return $this->mQueryModule
->getPageSet();
297 * This is a very simplistic utility function
298 * to convert a non-namespaced title string to a db key.
299 * It will replace all ' ' with '_'
301 public static function titleToKey($title) {
302 return str_replace(' ', '_', $title);
305 public static function keyToTitle($key) {
306 return str_replace('_', ' ', $key);
309 public static function getBaseVersion() {
310 return __CLASS__
. ': $Id$';
314 abstract class ApiQueryGeneratorBase
extends ApiQueryBase
{
316 private $mIsGenerator;
318 public function __construct($query, $moduleName, $paramPrefix = '') {
319 parent
:: __construct($query, $moduleName, $paramPrefix);
320 $this->mIsGenerator
= false;
323 public function setGeneratorMode() {
324 $this->mIsGenerator
= true;
328 * Overrides base class to prepend 'g' to every generator parameter
330 public function encodeParamName($paramName) {
331 if ($this->mIsGenerator
)
332 return 'g' . parent
:: encodeParamName($paramName);
334 return parent
:: encodeParamName($paramName);
338 * Execute this module as a generator
339 * @param $resultPageSet PageSet: All output should be appended to this object
341 public abstract function executeGenerator($resultPageSet);