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 ?
'>=' : '<=');
92 $db = & $this->getDB();
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) {
109 // getDB has its own profileDBIn/Out calls
110 $db = & $this->getDB();
112 $this->profileDBIn();
113 $res = $db->select($this->tables
, $this->fields
, $this->where
, $method, $this->options
);
114 $this->profileDBOut();
120 protected function addRowInfo($prefix, $row) {
125 @$tmp = $row->{$prefix . '_id'};
126 if(!is_null($tmp)) $vals[$prefix . 'id'] = intval($tmp);
129 $title = ApiQueryBase
::addRowInfo_title($row, $prefix . '_namespace', $prefix . '_title');
131 if (!$title->userCanRead())
133 $vals['ns'] = $title->getNamespace();
134 $vals['title'] = $title->getPrefixedText();
141 @$tmp = $row->page_is_redirect
;
142 if($tmp) $vals['redirect'] = '';
148 @$tmp = $row->rc_cur_id
;
149 if(!is_null($tmp)) $vals['pageid'] = intval($tmp);
151 @$tmp = $row->rc_this_oldid
;
152 if(!is_null($tmp)) $vals['revid'] = intval($tmp);
154 @$tmp = $row->rc_last_oldid
;
155 if(!is_null($tmp)) $vals['old_revid'] = intval($tmp);
157 $title = ApiQueryBase
::addRowInfo_title($row, 'rc_moved_to_ns', 'rc_moved_to_title');
159 if (!$title->userCanRead())
161 $vals['new_ns'] = $title->getNamespace();
162 $vals['new_title'] = $title->getPrefixedText();
165 @$tmp = $row->rc_patrolled
;
166 if(!is_null($tmp)) $vals['patrolled'] = '';
172 @$tmp = $row->page_id
;
173 if(!is_null($tmp)) $vals['pageid'] = intval($tmp);
175 if ($row->log_params
!== '') {
176 $params = explode("\n", $row->log_params
);
177 if ($row->log_type
== 'move' && isset ($params[0])) {
178 $newTitle = Title
:: newFromText($params[0]);
180 $vals['new_ns'] = $newTitle->getNamespace();
181 $vals['new_title'] = $newTitle->getPrefixedText();
186 if (!empty ($params)) {
187 $this->getResult()->setIndexedTagName($params, 'param');
188 $vals = array_merge($vals, $params);
196 @$tmp = $row->{$prefix . '_type'};
197 if(!is_null($tmp)) $vals['type'] = $tmp;
200 @$tmp = $row->{$prefix . '_action'};
201 if(!is_null($tmp)) $vals['action'] = $tmp;
204 @$tmp = $row->{$prefix . '_text_id'};
205 if(!is_null($tmp)) $vals['oldid'] = intval($tmp);
207 // User Name / Anon IP
208 @$tmp = $row->{$prefix . '_user_text'};
209 if(is_null($tmp)) @$tmp = $row->user_name
;
211 $vals['user'] = $tmp;
212 @$tmp = !$row->{$prefix . '_user'};
213 if(!is_null($tmp) && $tmp)
218 @$tmp = $row->{$prefix . '_bot'};
219 if(!is_null($tmp) && $tmp) $vals['bot'] = '';
222 @$tmp = $row->{$prefix . '_new'};
223 if(is_null($tmp)) @$tmp = $row->{$prefix . '_is_new'};
224 if(!is_null($tmp) && $tmp) $vals['new'] = '';
227 @$tmp = $row->{$prefix . '_minor_edit'};
228 if(is_null($tmp)) @$tmp = $row->{$prefix . '_minor'};
229 if(!is_null($tmp) && $tmp) $vals['minor'] = '';
232 @$tmp = $row->{$prefix . '_timestamp'};
234 $vals['timestamp'] = wfTimestamp(TS_ISO_8601
, $tmp);
237 @$tmp = $row->{$prefix . '_comment'};
238 if(!empty($tmp)) // optimize bandwidth
239 $vals['comment'] = $tmp;
244 private static function addRowInfo_title($row, $nsfld, $titlefld) {
247 @$title = $row->$titlefld;
249 return Title
:: makeTitle($ns, $title);
255 * Override this method to request extra fields from the pageSet
256 * using $this->getPageSet()->requestField('fieldName')
258 public function requestExtraData() {
262 * Get the main Query module
264 public function getQuery() {
265 return $this->mQueryModule
;
268 protected function setContinueEnumParameter($paramName, $paramValue) {
270 $this->encodeParamName($paramName
272 $this->getResult()->addValue('query-continue', $this->getModuleName(), $msg);
276 * Get the Query database connection (readonly)
278 protected function getDB() {
279 return $this->getQuery()->getDB();
283 * Get the PageSet object to work on
284 * @return ApiPageSet data
286 protected function getPageSet() {
287 return $this->mQueryModule
->getPageSet();
291 * This is a very simplistic utility function
292 * to convert a non-namespaced title string to a db key.
293 * It will replace all ' ' with '_'
295 public static function titleToKey($title) {
296 return str_replace(' ', '_', $title);
299 public static function keyToTitle($key) {
300 return str_replace('_', ' ', $key);
303 public static function getBaseVersion() {
304 return __CLASS__
. ': $Id$';
308 abstract class ApiQueryGeneratorBase
extends ApiQueryBase
{
310 private $mIsGenerator;
312 public function __construct($query, $moduleName, $paramPrefix = '') {
313 parent
:: __construct($query, $moduleName, $paramPrefix);
314 $this->mIsGenerator
= false;
317 public function setGeneratorMode() {
318 $this->mIsGenerator
= true;
322 * Overrides base class to prepend 'g' to every generator parameter
324 public function encodeParamName($paramName) {
325 if ($this->mIsGenerator
)
326 return 'g' . parent
:: encodeParamName($paramName);
328 return parent
:: encodeParamName($paramName);
332 * Execute this module as a generator
333 * @param $resultPageSet PageSet: All output should be appended to this object
335 public abstract function executeGenerator($resultPageSet);