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($tables, $alias = null) {
56 if (is_array($tables)) {
58 ApiBase
:: dieDebug(__METHOD__
, 'Multiple table aliases not supported');
59 $this->tables
= array_merge($this->tables
, $tables);
62 $tables = $this->getDB()->tableName($tables) . ' ' . $alias;
63 $this->tables
[] = $tables;
67 protected function addFields($value) {
69 $this->fields
= array_merge($this->fields
, $value);
71 $this->fields
[] = $value;
74 protected function addFieldsIf($value, $condition) {
76 $this->addFields($value);
82 protected function addWhere($value) {
84 $this->where
= array_merge($this->where
, $value);
86 $this->where
[] = $value;
89 protected function addWhereIf($value, $condition) {
91 $this->addWhere($value);
97 protected function addWhereFld($field, $value) {
99 $this->where
[$field] = $value;
102 protected function addWhereRange($field, $dir, $start, $end) {
103 $isDirNewer = ($dir === 'newer');
104 $after = ($isDirNewer ?
'>=' : '<=');
105 $before = ($isDirNewer ?
'<=' : '>=');
106 $db = $this->getDB();
108 if (!is_null($start))
109 $this->addWhere($field . $after . $db->addQuotes($start));
112 $this->addWhere($field . $before . $db->addQuotes($end));
114 if (!isset($this->options
['ORDER BY']))
115 $this->addOption('ORDER BY', $field . ($isDirNewer ?
'' : ' DESC'));
118 protected function addOption($name, $value = null) {
120 $this->options
[] = $name;
122 $this->options
[$name] = $value;
125 protected function select($method) {
127 // getDB has its own profileDBIn/Out calls
128 $db = $this->getDB();
130 $this->profileDBIn();
131 $res = $db->select($this->tables
, $this->fields
, $this->where
, $method, $this->options
);
132 $this->profileDBOut();
137 public static function addTitleInfo(&$arr, $title, $prefix='') {
138 $arr[$prefix . 'ns'] = intval($title->getNamespace());
139 $arr[$prefix . 'title'] = $title->getPrefixedText();
143 * Override this method to request extra fields from the pageSet
144 * using $pageSet->requestField('fieldName')
146 public function requestExtraData($pageSet) {
150 * Get the main Query module
152 public function getQuery() {
153 return $this->mQueryModule
;
157 * Add sub-element under the page element with the given pageId.
159 protected function addPageSubItems($pageId, $data) {
160 $result = $this->getResult();
161 $result->setIndexedTagName($data, $this->getModulePrefix());
162 $result->addValue(array ('query', 'pages', intval($pageId)),
163 $this->getModuleName(),
167 protected function setContinueEnumParameter($paramName, $paramValue) {
169 $paramName = $this->encodeParamName($paramName);
170 $msg = array( $paramName => $paramValue );
172 // This is an alternative continue format as a part of the URL string
173 // ApiResult :: setContent($msg, $paramName . '=' . urlencode($paramValue));
175 $this->getResult()->addValue('query-continue', $this->getModuleName(), $msg);
179 * Get the Query database connection (readonly)
181 protected function getDB() {
182 if (is_null($this->mDb
))
183 $this->mDb
= $this->getQuery()->getDB();
188 * Selects the query database connection with the given name.
189 * If no such connection has been requested before, it will be created.
190 * Subsequent calls with the same $name will return the same connection
191 * as the first, regardless of $db or $groups new values.
193 public function selectNamedDB($name, $db, $groups) {
194 $this->mDb
= $this->getQuery()->getNamedDB($name, $db, $groups);
198 * Get the PageSet object to work on
199 * @return ApiPageSet data
201 protected function getPageSet() {
202 return $this->getQuery()->getPageSet();
206 * This is a very simplistic utility function
207 * to convert a non-namespaced title string to a db key.
208 * It will replace all ' ' with '_'
210 public static function titleToKey($title) {
211 return str_replace(' ', '_', $title);
214 public static function keyToTitle($key) {
215 return str_replace('_', ' ', $key);
218 public function getTokenFlag($tokenArr, $action) {
219 if (in_array($action, $tokenArr)) {
221 if ($wgUser->isAllowed($action))
224 $this->dieUsage("Action '$action' is not allowed for the current user", 'permissiondenied');
229 public static function getBaseVersion() {
230 return __CLASS__
. ': $Id$';
237 abstract class ApiQueryGeneratorBase
extends ApiQueryBase
{
239 private $mIsGenerator;
241 public function __construct($query, $moduleName, $paramPrefix = '') {
242 parent
:: __construct($query, $moduleName, $paramPrefix);
243 $this->mIsGenerator
= false;
246 public function setGeneratorMode() {
247 $this->mIsGenerator
= true;
251 * Overrides base class to prepend 'g' to every generator parameter
253 public function encodeParamName($paramName) {
254 if ($this->mIsGenerator
)
255 return 'g' . parent
:: encodeParamName($paramName);
257 return parent
:: encodeParamName($paramName);
261 * Execute this module as a generator
262 * @param $resultPageSet PageSet: All output should be appended to this object
264 public abstract function executeGenerator($resultPageSet);