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;
40 $this->tables
= array ();
41 $this->where
= array ();
42 $this->fields
= array();
43 $this->options
= array ();
46 protected function addTables($value) {
48 $this->tables
= array_merge($this->tables
, $value);
50 $this->tables
[] = $value;
53 protected function addFields($value) {
55 $this->fields
= array_merge($this->fields
, $value);
57 $this->fields
[] = $value;
60 protected function addFieldsIf($value, $condition) {
62 $this->addFields($value);
68 protected function addWhere($value) {
70 $this->where
= array_merge($this->where
, $value);
72 $this->where
[] = $value;
75 protected function addWhereIf($value, $condition) {
77 $this->addWhere($value);
83 protected function addWhereFld($field, $value) {
85 $this->where
[$field] = $value;
88 protected function addWhereRange($field, $dir, $start, $end) {
89 $isDirNewer = ($dir === 'newer');
90 $after = ($isDirNewer ?
'<=' : '>=');
91 $before = ($isDirNewer ?
'>=' : '<=');
95 $this->addWhere($field . $after . $db->addQuotes($start));
98 $this->addWhere($field . $before . $db->addQuotes($end));
100 $this->addOption('ORDER BY', $field . ($isDirNewer ?
'' : ' DESC'));
103 protected function addOption($name, $value) {
104 $this->options
[$name] = $value;
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();
115 protected function addRowInfo($prefix, $row) {
120 @$tmp = $row->{$prefix . '_id'};
121 if(!is_null($tmp)) $vals[$prefix . 'id'] = intval($tmp);
124 $title = ApiQueryBase
::addRowInfo_title($row, $prefix . '_namespace', $prefix . '_title');
126 if (!$title->userCanRead())
128 $vals['ns'] = $title->getNamespace();
129 $vals['title'] = $title->getPrefixedText();
132 if ($prefix === 'rc') {
135 @$tmp = $row->rc_cur_id
;
136 if(!is_null($tmp)) $vals['pageid'] = intval($tmp);
138 @$tmp = $row->rc_this_oldid
;
139 if(!is_null($tmp)) $vals['revid'] = intval($tmp);
141 @$tmp = $row->rc_last_oldid
;
142 if(!is_null($tmp)) $vals['old_revid'] = intval($tmp);
144 $title = ApiQueryBase
::addRowInfo_title($row, 'rc_moved_to_ns', 'rc_moved_to_title');
146 if (!$title->userCanRead())
148 $vals['new_ns'] = $title->getNamespace();
149 $vals['new_title'] = $title->getPrefixedText();
152 @$tmp = $row->rc_patrolled
;
153 if(!is_null($tmp)) $vals['patrolled'] = '';
155 } elseif ($prefix === 'log') {
158 @$tmp = $row->page_id
;
159 if(!is_null($tmp)) $vals['pageid'] = intval($tmp);
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]);
166 $vals['new_ns'] = $newTitle->getNamespace();
167 $vals['new_title'] = $newTitle->getPrefixedText();
172 if (!empty ($params)) {
173 $this->getResult()->setIndexedTagName($params, 'param');
174 $vals = array_merge($vals, $params);
180 @$tmp = $row->{$prefix . '_type'};
181 if(!is_null($tmp)) $vals['type'] = $tmp;
184 @$tmp = $row->{$prefix . '_action'};
185 if(!is_null($tmp)) $vals['action'] = $tmp;
188 @$tmp = $row->{$prefix . '_text_id'};
189 if(!is_null($tmp)) $vals['oldid'] = intval($tmp);
191 // User Name / Anon IP
192 @$tmp = $row->{$prefix . '_user_text'};
193 if(is_null($tmp)) @$tmp = $row->user_name
;
195 $vals['user'] = $tmp;
196 @$tmp = !$row->{$prefix . '_user'};
197 if(!is_null($tmp) && $tmp)
202 @$tmp = $row->{$prefix . '_bot'};
203 if(!is_null($tmp) && $tmp) $vals['bot'] = '';
206 @$tmp = $row->{$prefix . '_new'};
207 if(is_null($tmp)) @$tmp = $row->{$prefix . '_is_new'};
208 if(!is_null($tmp) && $tmp) $vals['new'] = '';
211 @$tmp = $row->{$prefix . '_minor_edit'};
212 if(is_null($tmp)) @$tmp = $row->{$prefix . '_minor'};
213 if(!is_null($tmp) && $tmp) $vals['minor'] = '';
216 @$tmp = $row->{$prefix . '_timestamp'};
218 $vals['timestamp'] = wfTimestamp(TS_ISO_8601
, $tmp);
221 @$tmp = $row->{$prefix . '_comment'};
222 if(!empty($tmp)) // optimize bandwidth
223 $vals['comment'] = $tmp;
228 private static function addRowInfo_title($row, $nsfld, $titlefld) {
231 @$title = $row->$titlefld;
233 return Title
:: makeTitle($ns, $title);
239 * Override this method to request extra fields from the pageSet
240 * using $this->getPageSet()->requestField('fieldName')
242 public function requestExtraData() {
246 * Get the main Query module
248 public function getQuery() {
249 return $this->mQueryModule
;
252 protected function setContinueEnumParameter($paramName, $paramValue) {
254 $this->encodeParamName($paramName
256 $this->getResult()->addValue('query-continue', $this->getModuleName(), $msg);
260 * Get the Query database connection (readonly)
262 protected function getDB() {
263 return $this->getQuery()->getDB();
267 * Get the PageSet object to work on
268 * @return ApiPageSet data
270 protected function getPageSet() {
271 return $this->mQueryModule
->getPageSet();
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 '_'
279 public static function titleToKey($title) {
280 return str_replace(' ', '_', $title);
283 public static function keyToTitle($key) {
284 return str_replace('_', ' ', $key);
287 public static function getBaseVersion() {
288 return __CLASS__
. ': $Id$';
292 abstract class ApiQueryGeneratorBase
extends ApiQueryBase
{
294 private $mIsGenerator;
296 public function __construct($query, $moduleName, $paramPrefix = '') {
297 parent
:: __construct($query, $moduleName, $paramPrefix);
298 $this->mIsGenerator
= false;
301 public function setGeneratorMode() {
302 $this->mIsGenerator
= true;
306 * Overrides base class to prepend 'g' to every generator parameter
308 public function encodeParamName($paramName) {
309 if ($this->mIsGenerator
)
310 return 'g' . parent
:: encodeParamName($paramName);
312 return parent
:: encodeParamName($paramName);
316 * Execute this module as a generator
317 * @param $resultPageSet PageSet: All output should be appended to this object
319 public abstract function executeGenerator($resultPageSet);