(bug 9575) Accept upload description from GET parameters
[lhc/web/wiklou.git] / includes / api / ApiQueryBase.php
1 <?php
2
3 /*
4 * Created on Sep 7, 2006
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
9 *
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.
14 *
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.
19 *
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
24 */
25
26 if (!defined('MEDIAWIKI')) {
27 // Eclipse helper - will be ignored in production
28 require_once ('ApiBase.php');
29 }
30
31 /**
32 * This is a base class for all Query modules.
33 * It provides some common functionality such as constructing various SQL queries.
34 *
35 * @addtogroup API
36 */
37 abstract class ApiQueryBase extends ApiBase {
38
39 private $mQueryModule, $mDb, $tables, $where, $fields, $options;
40
41 public function __construct($query, $moduleName, $paramPrefix = '') {
42 parent :: __construct($query->getMain(), $moduleName, $paramPrefix);
43 $this->mQueryModule = $query;
44 $this->mDb = null;
45 $this->resetQueryParams();
46 }
47
48 protected function resetQueryParams() {
49 $this->tables = array ();
50 $this->where = array ();
51 $this->fields = array ();
52 $this->options = array ();
53 }
54
55 protected function addTables($value) {
56 if (is_array($value))
57 $this->tables = array_merge($this->tables, $value);
58 else
59 $this->tables[] = $value;
60 }
61
62 protected function addFields($value) {
63 if (is_array($value))
64 $this->fields = array_merge($this->fields, $value);
65 else
66 $this->fields[] = $value;
67 }
68
69 protected function addFieldsIf($value, $condition) {
70 if ($condition) {
71 $this->addFields($value);
72 return true;
73 }
74 return false;
75 }
76
77 protected function addWhere($value) {
78 if (is_array($value))
79 $this->where = array_merge($this->where, $value);
80 else
81 $this->where[] = $value;
82 }
83
84 protected function addWhereIf($value, $condition) {
85 if ($condition) {
86 $this->addWhere($value);
87 return true;
88 }
89 return false;
90 }
91
92 protected function addWhereFld($field, $value) {
93 if (!is_null($value))
94 $this->where[$field] = $value;
95 }
96
97 protected function addWhereRange($field, $dir, $start, $end) {
98 $isDirNewer = ($dir === 'newer');
99 $after = ($isDirNewer ? '>=' : '<=');
100 $before = ($isDirNewer ? '<=' : '>=');
101 $db = $this->getDB();
102
103 if (!is_null($start))
104 $this->addWhere($field . $after . $db->addQuotes($start));
105
106 if (!is_null($end))
107 $this->addWhere($field . $before . $db->addQuotes($end));
108
109 $this->addOption('ORDER BY', $field . ($isDirNewer ? '' : ' DESC'));
110 }
111
112 protected function addOption($name, $value = null) {
113 if (is_null($value))
114 $this->options[] = $name;
115 else
116 $this->options[$name] = $value;
117 }
118
119 protected function select($method) {
120
121 // getDB has its own profileDBIn/Out calls
122 $db = $this->getDB();
123
124 $this->profileDBIn();
125 $res = $db->select($this->tables, $this->fields, $this->where, $method, $this->options);
126 $this->profileDBOut();
127
128 return $res;
129 }
130
131 public static function addTitleInfo(&$arr, $title, $prefix='') {
132 $arr[$prefix . 'ns'] = intval($title->getNamespace());
133 $arr[$prefix . 'title'] = $title->getPrefixedText();
134 }
135
136 /**
137 * Override this method to request extra fields from the pageSet
138 * using $pageSet->requestField('fieldName')
139 */
140 public function requestExtraData($pageSet) {
141 }
142
143 /**
144 * Get the main Query module
145 */
146 public function getQuery() {
147 return $this->mQueryModule;
148 }
149
150 /**
151 * Add sub-element under the page element with the given pageId.
152 */
153 protected function addPageSubItems($pageId, $data) {
154 $result = $this->getResult();
155 $result->setIndexedTagName($data, $this->getModulePrefix());
156 $result->addValue(array ('query', 'pages', intval($pageId)),
157 $this->getModuleName(),
158 $data);
159 }
160
161 protected function setContinueEnumParameter($paramName, $paramValue) {
162
163 $paramName = $this->encodeParamName($paramName);
164 $msg = array( $paramName => $paramValue );
165
166 // This is an alternative continue format as a part of the URL string
167 // ApiResult :: setContent($msg, $paramName . '=' . urlencode($paramValue));
168
169 $this->getResult()->addValue('query-continue', $this->getModuleName(), $msg);
170 }
171
172 /**
173 * Get the Query database connection (readonly)
174 */
175 protected function getDB() {
176 if (is_null($this->mDb))
177 $this->mDb = $this->getQuery()->getDB();
178 return $this->mDb;
179 }
180
181 /**
182 * Selects the query database connection with the given name.
183 * If no such connection has been requested before, it will be created.
184 * Subsequent calls with the same $name will return the same connection
185 * as the first, regardless of $db or $groups new values.
186 */
187 public function selectNamedDB($name, $db, $groups) {
188 $this->mDb = $this->getQuery()->getNamedDB($name, $db, $groups);
189 }
190
191 /**
192 * Get the PageSet object to work on
193 * @return ApiPageSet data
194 */
195 protected function getPageSet() {
196 return $this->getQuery()->getPageSet();
197 }
198
199 /**
200 * This is a very simplistic utility function
201 * to convert a non-namespaced title string to a db key.
202 * It will replace all ' ' with '_'
203 */
204 public static function titleToKey($title) {
205 return str_replace(' ', '_', $title);
206 }
207
208 public static function keyToTitle($key) {
209 return str_replace('_', ' ', $key);
210 }
211
212 public static function getBaseVersion() {
213 return __CLASS__ . ': $Id$';
214 }
215 }
216
217 /**
218 * @addtogroup API
219 */
220 abstract class ApiQueryGeneratorBase extends ApiQueryBase {
221
222 private $mIsGenerator;
223
224 public function __construct($query, $moduleName, $paramPrefix = '') {
225 parent :: __construct($query, $moduleName, $paramPrefix);
226 $this->mIsGenerator = false;
227 }
228
229 public function setGeneratorMode() {
230 $this->mIsGenerator = true;
231 }
232
233 /**
234 * Overrides base class to prepend 'g' to every generator parameter
235 */
236 public function encodeParamName($paramName) {
237 if ($this->mIsGenerator)
238 return 'g' . parent :: encodeParamName($paramName);
239 else
240 return parent :: encodeParamName($paramName);
241 }
242
243 /**
244 * Execute this module as a generator
245 * @param $resultPageSet PageSet: All output should be appended to this object
246 */
247 public abstract function executeGenerator($resultPageSet);
248 }
249