4 * Created on Sep 7, 2006
6 * API for MediaWiki 1.8+
8 * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@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');
32 * This is a base class for all Query modules.
33 * It provides some common functionality such as constructing various SQL queries.
37 abstract class ApiQueryBase
extends ApiBase
{
39 private $mQueryModule, $mDb, $tables, $where, $fields, $options;
41 public function __construct($query, $moduleName, $paramPrefix = '') {
42 parent
:: __construct($query->getMain(), $moduleName, $paramPrefix);
43 $this->mQueryModule
= $query;
45 $this->resetQueryParams();
48 protected function resetQueryParams() {
49 $this->tables
= array ();
50 $this->where
= array ();
51 $this->fields
= array ();
52 $this->options
= array ();
55 protected function addTables($value) {
57 $this->tables
= array_merge($this->tables
, $value);
59 $this->tables
[] = $value;
62 protected function addFields($value) {
64 $this->fields
= array_merge($this->fields
, $value);
66 $this->fields
[] = $value;
69 protected function addFieldsIf($value, $condition) {
71 $this->addFields($value);
77 protected function addWhere($value) {
79 $this->where
= array_merge($this->where
, $value);
81 $this->where
[] = $value;
84 protected function addWhereIf($value, $condition) {
86 $this->addWhere($value);
92 protected function addWhereFld($field, $value) {
94 $this->where
[$field] = $value;
97 protected function addWhereRange($field, $dir, $start, $end) {
98 $isDirNewer = ($dir === 'newer');
99 $after = ($isDirNewer ?
'>=' : '<=');
100 $before = ($isDirNewer ?
'<=' : '>=');
101 $db = $this->getDB();
103 if (!is_null($start))
104 $this->addWhere($field . $after . $db->addQuotes($start));
107 $this->addWhere($field . $before . $db->addQuotes($end));
109 $this->addOption('ORDER BY', $field . ($isDirNewer ?
'' : ' DESC'));
112 protected function addOption($name, $value = null) {
114 $this->options
[] = $name;
116 $this->options
[$name] = $value;
119 protected function select($method) {
121 // getDB has its own profileDBIn/Out calls
122 $db = $this->getDB();
124 $this->profileDBIn();
125 $res = $db->select($this->tables
, $this->fields
, $this->where
, $method, $this->options
);
126 $this->profileDBOut();
131 public static function addTitleInfo(&$arr, $title, $includeRestricted=false, $prefix='') {
132 if ($includeRestricted ||
$title->userCanRead()) {
133 $arr[$prefix . 'ns'] = intval($title->getNamespace());
134 $arr[$prefix . 'title'] = $title->getPrefixedText();
136 if (!$title->userCanRead())
137 $arr[$prefix . 'inaccessible'] = "";
141 * Override this method to request extra fields from the pageSet
142 * using $this->getPageSet()->requestField('fieldName')
144 public function requestExtraData() {
148 * Get the main Query module
150 public function getQuery() {
151 return $this->mQueryModule
;
154 protected function setContinueEnumParameter($paramName, $paramValue) {
156 $this->encodeParamName($paramName
158 $this->getResult()->addValue('query-continue', $this->getModuleName(), $msg);
162 * Get the Query database connection (readonly)
164 protected function getDB() {
165 if (is_null($this->mDb
))
166 $this->mDb
= $this->getQuery()->getDB();
171 * Selects the query database connection with the given name.
172 * If no such connection has been requested before, it will be created.
173 * Subsequent calls with the same $name will return the same connection
174 * as the first, regardless of $db or $groups new values.
176 public function selectNamedDB($name, $db, $groups) {
177 $this->mDb
= $this->getQuery()->getNamedDB($name, $db, $groups);
181 * Get the PageSet object to work on
182 * @return ApiPageSet data
184 protected function getPageSet() {
185 return $this->getQuery()->getPageSet();
189 * This is a very simplistic utility function
190 * to convert a non-namespaced title string to a db key.
191 * It will replace all ' ' with '_'
193 public static function titleToKey($title) {
194 return str_replace(' ', '_', $title);
197 public static function keyToTitle($key) {
198 return str_replace('_', ' ', $key);
201 public static function getBaseVersion() {
202 return __CLASS__
. ': $Id$';
209 abstract class ApiQueryGeneratorBase
extends ApiQueryBase
{
211 private $mIsGenerator;
213 public function __construct($query, $moduleName, $paramPrefix = '') {
214 parent
:: __construct($query, $moduleName, $paramPrefix);
215 $this->mIsGenerator
= false;
218 public function setGeneratorMode() {
219 $this->mIsGenerator
= true;
223 * Overrides base class to prepend 'g' to every generator parameter
225 public function encodeParamName($paramName) {
226 if ($this->mIsGenerator
)
227 return 'g' . parent
:: encodeParamName($paramName);
229 return parent
:: encodeParamName($paramName);
233 * Execute this module as a generator
234 * @param $resultPageSet PageSet: All output should be appended to this object
236 public abstract function executeGenerator($resultPageSet);