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 ApiQueryBase
:: addTitleInfo($vals, $title);
144 @ $tmp = $row->page_is_redirect
;
146 $vals['redirect'] = '';
152 @ $tmp = $row->rc_cur_id
;
154 $vals['pageid'] = intval($tmp);
156 @ $tmp = $row->rc_this_oldid
;
158 $vals['revid'] = intval($tmp);
160 if ( isset( $row->rc_last_oldid
) )
161 $vals['old_revid'] = intval( $row->rc_last_oldid
);
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 if ( isset( $row->rc_patrolled
) )
172 $vals['patrolled'] = '';
178 @ $tmp = $row->page_id
;
180 $vals['pageid'] = intval($tmp);
182 if ($row->log_params
!== '') {
183 $params = explode("\n", $row->log_params
);
184 if ($row->log_type
== 'move' && isset ($params[0])) {
185 $newTitle = Title
:: newFromText($params[0]);
187 $vals['new_ns'] = $newTitle->getNamespace();
188 $vals['new_title'] = $newTitle->getPrefixedText();
193 if (!empty ($params)) {
194 $this->getResult()->setIndexedTagName($params, 'param');
195 $vals = array_merge($vals, $params);
203 @ $tmp = $row->rev_page
;
205 $vals['pageid'] = intval($tmp);
212 $vals['type'] = $tmp;
216 $prefix . '_action' };
218 $vals['action'] = $tmp;
222 $prefix . '_text_id' };
224 $vals['oldid'] = intval($tmp);
226 // User Name / Anon IP
228 $prefix . '_user_text' };
230 @ $tmp = $row->user_name
;
231 if (!is_null($tmp)) {
232 $vals['user'] = $tmp;
235 if (!is_null($tmp) && $tmp)
242 if (!is_null($tmp) && $tmp)
250 $prefix . '_is_new' };
251 if (!is_null($tmp) && $tmp)
256 $prefix . '_minor_edit' };
259 $prefix . '_minor' };
260 if (!is_null($tmp) && $tmp)
265 $prefix . '_timestamp' };
267 $vals['timestamp'] = wfTimestamp(TS_ISO_8601
, $tmp);
271 $prefix . '_comment' };
272 if (!empty ($tmp)) // optimize bandwidth
273 $vals['comment'] = $tmp;
278 protected static function addTitleInfo(&$arr, $title) {
279 $arr['ns'] = $title->getNamespace();
280 $arr['title'] = $title->getPrefixedText();
283 private static function addRowInfo_title($row, $nsfld, $titlefld) {
284 if ( isset( $row-> $nsfld ) ) {
286 @ $title = $row-> $titlefld;
288 return Title
:: makeTitle($ns, $title);
294 * Override this method to request extra fields from the pageSet
295 * using $this->getPageSet()->requestField('fieldName')
297 public function requestExtraData() {
301 * Get the main Query module
303 public function getQuery() {
304 return $this->mQueryModule
;
307 protected function setContinueEnumParameter($paramName, $paramValue) {
309 $this->encodeParamName($paramName
311 $this->getResult()->addValue('query-continue', $this->getModuleName(), $msg);
315 * Get the Query database connection (readonly)
317 protected function getDB() {
318 return $this->getQuery()->getDB();
322 * Get the PageSet object to work on
323 * @return ApiPageSet data
325 protected function getPageSet() {
326 return $this->mQueryModule
->getPageSet();
330 * This is a very simplistic utility function
331 * to convert a non-namespaced title string to a db key.
332 * It will replace all ' ' with '_'
334 public static function titleToKey($title) {
335 return str_replace(' ', '_', $title);
338 public static function keyToTitle($key) {
339 return str_replace('_', ' ', $key);
342 public static function getBaseVersion() {
343 return __CLASS__
. ': $Id$';
350 abstract class ApiQueryGeneratorBase
extends ApiQueryBase
{
352 private $mIsGenerator;
354 public function __construct($query, $moduleName, $paramPrefix = '') {
355 parent
:: __construct($query, $moduleName, $paramPrefix);
356 $this->mIsGenerator
= false;
359 public function setGeneratorMode() {
360 $this->mIsGenerator
= true;
364 * Overrides base class to prepend 'g' to every generator parameter
366 public function encodeParamName($paramName) {
367 if ($this->mIsGenerator
)
368 return 'g' . parent
:: encodeParamName($paramName);
370 return parent
:: encodeParamName($paramName);
374 * Execute this module as a generator
375 * @param $resultPageSet PageSet: All output should be appended to this object
377 public abstract function executeGenerator($resultPageSet);