* (bug 8042) Make miser mode caching limits settable via $wgQueryCacheLimit
[mediawiki.git] / includes / api / ApiQueryBase.php
blobcc9a4fed7a855da2bd8e48522bc856feece58db5
1 <?php
4 /*
5 * Created on Sep 7, 2006
7 * API for MediaWiki 1.8+
9 * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@gmail.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 * http://www.gnu.org/copyleft/gpl.html
27 if (!defined('MEDIAWIKI')) {
28 // Eclipse helper - will be ignored in production
29 require_once ('ApiBase.php');
32 abstract class ApiQueryBase extends ApiBase {
34 private $mQueryModule, $tables, $where, $fields, $options;
36 public function __construct($query, $moduleName, $paramPrefix = '') {
37 parent :: __construct($query->getMain(), $moduleName, $paramPrefix);
38 $this->mQueryModule = $query;
39 $this->resetQueryParams();
42 protected function resetQueryParams() {
43 $this->tables = array ();
44 $this->where = array ();
45 $this->fields = array ();
46 $this->options = array ();
49 protected function addTables($value) {
50 if (is_array($value))
51 $this->tables = array_merge($this->tables, $value);
52 else
53 $this->tables[] = $value;
56 protected function addFields($value) {
57 if (is_array($value))
58 $this->fields = array_merge($this->fields, $value);
59 else
60 $this->fields[] = $value;
63 protected function addFieldsIf($value, $condition) {
64 if ($condition) {
65 $this->addFields($value);
66 return true;
68 return false;
71 protected function addWhere($value) {
72 if (is_array($value))
73 $this->where = array_merge($this->where, $value);
74 else
75 $this->where[] = $value;
78 protected function addWhereIf($value, $condition) {
79 if ($condition) {
80 $this->addWhere($value);
81 return true;
83 return false;
86 protected function addWhereFld($field, $value) {
87 if (!is_null($value))
88 $this->where[$field] = $value;
91 protected function addWhereRange($field, $dir, $start, $end) {
92 $isDirNewer = ($dir === 'newer');
93 $after = ($isDirNewer ? '>=' : '<=');
94 $before = ($isDirNewer ? '<=' : '>=');
95 $db = $this->getDB();
97 if (!is_null($start))
98 $this->addWhere($field . $after . $db->addQuotes($start));
100 if (!is_null($end))
101 $this->addWhere($field . $before . $db->addQuotes($end));
103 $this->addOption('ORDER BY', $field . ($isDirNewer ? '' : ' DESC'));
106 protected function addOption($name, $value = null) {
107 if (is_null($value))
108 $this->options[] = $name;
109 else
110 $this->options[$name] = $value;
113 protected function select($method) {
115 // getDB has its own profileDBIn/Out calls
116 $db = $this->getDB();
118 $this->profileDBIn();
119 $res = $db->select($this->tables, $this->fields, $this->where, $method, $this->options);
120 $this->profileDBOut();
122 return $res;
125 protected function addRowInfo($prefix, $row) {
127 $vals = array ();
129 // ID
130 if ( isset( $row-> { $prefix . '_id' } ) )
131 $vals[$prefix . 'id'] = intval( $row-> { $prefix . '_id' } );
133 // Title
134 $title = ApiQueryBase :: addRowInfo_title($row, $prefix . '_namespace', $prefix . '_title');
135 if ($title) {
136 if (!$title->userCanRead())
137 return false;
138 $vals['ns'] = $title->getNamespace();
139 $vals['title'] = $title->getPrefixedText();
142 switch ($prefix) {
144 case 'page' :
145 // page_is_redirect
146 @ $tmp = $row->page_is_redirect;
147 if ($tmp)
148 $vals['redirect'] = '';
150 break;
152 case 'rc' :
153 // PageId
154 @ $tmp = $row->rc_cur_id;
155 if (!is_null($tmp))
156 $vals['pageid'] = intval($tmp);
158 @ $tmp = $row->rc_this_oldid;
159 if (!is_null($tmp))
160 $vals['revid'] = intval($tmp);
162 if ( isset( $row->rc_last_oldid ) )
163 $vals['old_revid'] = intval( $row->rc_last_oldid );
165 $title = ApiQueryBase :: addRowInfo_title($row, 'rc_moved_to_ns', 'rc_moved_to_title');
166 if ($title) {
167 if (!$title->userCanRead())
168 return false;
169 $vals['new_ns'] = $title->getNamespace();
170 $vals['new_title'] = $title->getPrefixedText();
173 if ( isset( $row->rc_patrolled ) )
174 $vals['patrolled'] = '';
176 break;
178 case 'log' :
179 // PageId
180 @ $tmp = $row->page_id;
181 if (!is_null($tmp))
182 $vals['pageid'] = intval($tmp);
184 if ($row->log_params !== '') {
185 $params = explode("\n", $row->log_params);
186 if ($row->log_type == 'move' && isset ($params[0])) {
187 $newTitle = Title :: newFromText($params[0]);
188 if ($newTitle) {
189 $vals['new_ns'] = $newTitle->getNamespace();
190 $vals['new_title'] = $newTitle->getPrefixedText();
191 $params = null;
195 if (!empty ($params)) {
196 $this->getResult()->setIndexedTagName($params, 'param');
197 $vals = array_merge($vals, $params);
201 break;
203 case 'rev' :
204 // PageID
205 @ $tmp = $row->rev_page;
206 if (!is_null($tmp))
207 $vals['pageid'] = intval($tmp);
210 // Type
211 @ $tmp = $row-> {
212 $prefix . '_type' };
213 if (!is_null($tmp))
214 $vals['type'] = $tmp;
216 // Action
217 @ $tmp = $row-> {
218 $prefix . '_action' };
219 if (!is_null($tmp))
220 $vals['action'] = $tmp;
222 // Old ID
223 @ $tmp = $row-> {
224 $prefix . '_text_id' };
225 if (!is_null($tmp))
226 $vals['oldid'] = intval($tmp);
228 // User Name / Anon IP
229 @ $tmp = $row-> {
230 $prefix . '_user_text' };
231 if (is_null($tmp))
232 @ $tmp = $row->user_name;
233 if (!is_null($tmp)) {
234 $vals['user'] = $tmp;
235 @ $tmp = !$row-> {
236 $prefix . '_user' };
237 if (!is_null($tmp) && $tmp)
238 $vals['anon'] = '';
241 // Bot Edit
242 @ $tmp = $row-> {
243 $prefix . '_bot' };
244 if (!is_null($tmp) && $tmp)
245 $vals['bot'] = '';
247 // New Edit
248 @ $tmp = $row-> {
249 $prefix . '_new' };
250 if (is_null($tmp))
251 @ $tmp = $row-> {
252 $prefix . '_is_new' };
253 if (!is_null($tmp) && $tmp)
254 $vals['new'] = '';
256 // Minor Edit
257 @ $tmp = $row-> {
258 $prefix . '_minor_edit' };
259 if (is_null($tmp))
260 @ $tmp = $row-> {
261 $prefix . '_minor' };
262 if (!is_null($tmp) && $tmp)
263 $vals['minor'] = '';
265 // Timestamp
266 @ $tmp = $row-> {
267 $prefix . '_timestamp' };
268 if (!is_null($tmp))
269 $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $tmp);
271 // Comment
272 @ $tmp = $row-> {
273 $prefix . '_comment' };
274 if (!empty ($tmp)) // optimize bandwidth
275 $vals['comment'] = $tmp;
277 return $vals;
280 private static function addRowInfo_title($row, $nsfld, $titlefld) {
281 if ( isset( $row-> $nsfld ) ) {
282 $ns = $row-> $nsfld;
283 @ $title = $row-> $titlefld;
284 if (!empty ($title))
285 return Title :: makeTitle($ns, $title);
287 return false;
291 * Override this method to request extra fields from the pageSet
292 * using $this->getPageSet()->requestField('fieldName')
294 public function requestExtraData() {
298 * Get the main Query module
300 public function getQuery() {
301 return $this->mQueryModule;
304 protected function setContinueEnumParameter($paramName, $paramValue) {
305 $msg = array (
306 $this->encodeParamName($paramName
307 ) => $paramValue);
308 $this->getResult()->addValue('query-continue', $this->getModuleName(), $msg);
312 * Get the Query database connection (readonly)
314 protected function getDB() {
315 return $this->getQuery()->getDB();
319 * Get the PageSet object to work on
320 * @return ApiPageSet data
322 protected function getPageSet() {
323 return $this->mQueryModule->getPageSet();
327 * This is a very simplistic utility function
328 * to convert a non-namespaced title string to a db key.
329 * It will replace all ' ' with '_'
331 public static function titleToKey($title) {
332 return str_replace(' ', '_', $title);
335 public static function keyToTitle($key) {
336 return str_replace('_', ' ', $key);
339 public static function getBaseVersion() {
340 return __CLASS__ . ': $Id$';
344 abstract class ApiQueryGeneratorBase extends ApiQueryBase {
346 private $mIsGenerator;
348 public function __construct($query, $moduleName, $paramPrefix = '') {
349 parent :: __construct($query, $moduleName, $paramPrefix);
350 $this->mIsGenerator = false;
353 public function setGeneratorMode() {
354 $this->mIsGenerator = true;
358 * Overrides base class to prepend 'g' to every generator parameter
360 public function encodeParamName($paramName) {
361 if ($this->mIsGenerator)
362 return 'g' . parent :: encodeParamName($paramName);
363 else
364 return parent :: encodeParamName($paramName);
368 * Execute this module as a generator
369 * @param $resultPageSet PageSet: All output should be appended to this object
371 public abstract function executeGenerator($resultPageSet);