bc8c483f74323caca357e6138558efc7fce1b558
4 * Created on Sep 7, 2006
6 * API for MediaWiki 1.8+
8 * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@gmail.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * http://www.gnu.org/copyleft/gpl.html
26 if (!defined('MEDIAWIKI')) {
27 // Eclipse helper - will be ignored in production
28 require_once ('ApiBase.php');
34 abstract class ApiQueryBase
extends ApiBase
{
36 private $mQueryModule, $tables, $where, $fields, $options;
38 public function __construct($query, $moduleName, $paramPrefix = '') {
39 parent
:: __construct($query->getMain(), $moduleName, $paramPrefix);
40 $this->mQueryModule
= $query;
41 $this->resetQueryParams();
44 protected function resetQueryParams() {
45 $this->tables
= array ();
46 $this->where
= array ();
47 $this->fields
= array ();
48 $this->options
= array ();
51 protected function addTables($value) {
53 $this->tables
= array_merge($this->tables
, $value);
55 $this->tables
[] = $value;
58 protected function addFields($value) {
60 $this->fields
= array_merge($this->fields
, $value);
62 $this->fields
[] = $value;
65 protected function addFieldsIf($value, $condition) {
67 $this->addFields($value);
73 protected function addWhere($value) {
75 $this->where
= array_merge($this->where
, $value);
77 $this->where
[] = $value;
80 protected function addWhereIf($value, $condition) {
82 $this->addWhere($value);
88 protected function addWhereFld($field, $value) {
90 $this->where
[$field] = $value;
93 protected function addWhereRange($field, $dir, $start, $end) {
94 $isDirNewer = ($dir === 'newer');
95 $after = ($isDirNewer ?
'>=' : '<=');
96 $before = ($isDirNewer ?
'<=' : '>=');
100 $this->addWhere($field . $after . $db->addQuotes($start));
103 $this->addWhere($field . $before . $db->addQuotes($end));
105 $this->addOption('ORDER BY', $field . ($isDirNewer ?
'' : ' DESC'));
108 protected function addOption($name, $value = null) {
110 $this->options
[] = $name;
112 $this->options
[$name] = $value;
115 protected function select($method) {
117 // getDB has its own profileDBIn/Out calls
118 $db = $this->getDB();
120 $this->profileDBIn();
121 $res = $db->select($this->tables
, $this->fields
, $this->where
, $method, $this->options
);
122 $this->profileDBOut();
127 protected function addRowInfo($prefix, $row) {
132 if ( isset( $row-> { $prefix . '_id' } ) )
133 $vals[$prefix . 'id'] = intval( $row-> { $prefix . '_id' } );
136 $title = ApiQueryBase
:: addRowInfo_title($row, $prefix . '_namespace', $prefix . '_title');
138 if (!$title->userCanRead())
140 $vals['ns'] = $title->getNamespace();
141 $vals['title'] = $title->getPrefixedText();
148 @ $tmp = $row->page_is_redirect
;
150 $vals['redirect'] = '';
156 @ $tmp = $row->rc_cur_id
;
158 $vals['pageid'] = intval($tmp);
160 @ $tmp = $row->rc_this_oldid
;
162 $vals['revid'] = intval($tmp);
164 if ( isset( $row->rc_last_oldid
) )
165 $vals['old_revid'] = intval( $row->rc_last_oldid
);
167 $title = ApiQueryBase
:: addRowInfo_title($row, 'rc_moved_to_ns', 'rc_moved_to_title');
169 if (!$title->userCanRead())
171 $vals['new_ns'] = $title->getNamespace();
172 $vals['new_title'] = $title->getPrefixedText();
175 if ( isset( $row->rc_patrolled
) )
176 $vals['patrolled'] = '';
182 @ $tmp = $row->page_id
;
184 $vals['pageid'] = intval($tmp);
186 if ($row->log_params
!== '') {
187 $params = explode("\n", $row->log_params
);
188 if ($row->log_type
== 'move' && isset ($params[0])) {
189 $newTitle = Title
:: newFromText($params[0]);
191 $vals['new_ns'] = $newTitle->getNamespace();
192 $vals['new_title'] = $newTitle->getPrefixedText();
197 if (!empty ($params)) {
198 $this->getResult()->setIndexedTagName($params, 'param');
199 $vals = array_merge($vals, $params);
207 @ $tmp = $row->rev_page
;
209 $vals['pageid'] = intval($tmp);
216 $vals['type'] = $tmp;
220 $prefix . '_action' };
222 $vals['action'] = $tmp;
226 $prefix . '_text_id' };
228 $vals['oldid'] = intval($tmp);
230 // User Name / Anon IP
232 $prefix . '_user_text' };
234 @ $tmp = $row->user_name
;
235 if (!is_null($tmp)) {
236 $vals['user'] = $tmp;
239 if (!is_null($tmp) && $tmp)
246 if (!is_null($tmp) && $tmp)
254 $prefix . '_is_new' };
255 if (!is_null($tmp) && $tmp)
260 $prefix . '_minor_edit' };
263 $prefix . '_minor' };
264 if (!is_null($tmp) && $tmp)
269 $prefix . '_timestamp' };
271 $vals['timestamp'] = wfTimestamp(TS_ISO_8601
, $tmp);
275 $prefix . '_comment' };
276 if (!empty ($tmp)) // optimize bandwidth
277 $vals['comment'] = $tmp;
282 private static function addRowInfo_title($row, $nsfld, $titlefld) {
283 if ( isset( $row-> $nsfld ) ) {
285 @ $title = $row-> $titlefld;
287 return Title
:: makeTitle($ns, $title);
293 * Override this method to request extra fields from the pageSet
294 * using $this->getPageSet()->requestField('fieldName')
296 public function requestExtraData() {
300 * Get the main Query module
302 public function getQuery() {
303 return $this->mQueryModule
;
306 protected function setContinueEnumParameter($paramName, $paramValue) {
308 $this->encodeParamName($paramName
310 $this->getResult()->addValue('query-continue', $this->getModuleName(), $msg);
314 * Get the Query database connection (readonly)
316 protected function getDB() {
317 return $this->getQuery()->getDB();
321 * Get the PageSet object to work on
322 * @return ApiPageSet data
324 protected function getPageSet() {
325 return $this->mQueryModule
->getPageSet();
329 * This is a very simplistic utility function
330 * to convert a non-namespaced title string to a db key.
331 * It will replace all ' ' with '_'
333 public static function titleToKey($title) {
334 return str_replace(' ', '_', $title);
337 public static function keyToTitle($key) {
338 return str_replace('_', ' ', $key);
341 public static function getBaseVersion() {
342 return __CLASS__
. ': $Id$';
349 abstract class ApiQueryGeneratorBase
extends ApiQueryBase
{
351 private $mIsGenerator;
353 public function __construct($query, $moduleName, $paramPrefix = '') {
354 parent
:: __construct($query, $moduleName, $paramPrefix);
355 $this->mIsGenerator
= false;
358 public function setGeneratorMode() {
359 $this->mIsGenerator
= true;
363 * Overrides base class to prepend 'g' to every generator parameter
365 public function encodeParamName($paramName) {
366 if ($this->mIsGenerator
)
367 return 'g' . parent
:: encodeParamName($paramName);
369 return parent
:: encodeParamName($paramName);
373 * Execute this module as a generator
374 * @param $resultPageSet PageSet: All output should be appended to this object
376 public abstract function executeGenerator($resultPageSet);