API * Added rudimentary RC list
[lhc/web/wiklou.git] / includes / api / ApiQueryBase.php
1 <?php
2
3
4 /*
5 * Created on Sep 7, 2006
6 *
7 * API for MediaWiki 1.8+
8 *
9 * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@gmail.com>
10 *
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.
15 *
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.
20 *
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
25 */
26
27 if (!defined('MEDIAWIKI')) {
28 // Eclipse helper - will be ignored in production
29 require_once ('ApiBase.php');
30 }
31
32 abstract class ApiQueryBase extends ApiBase {
33
34 private $mQueryModule, $tables, $where, $fields, $options;
35
36 public function __construct($query, $moduleName, $paramPrefix = '') {
37 parent :: __construct($query->getMain(), $moduleName, $paramPrefix);
38 $this->mQueryModule = $query;
39
40 $this->tables = array ();
41 $this->where = array ();
42 $this->fields = array();
43 $this->options = array ();
44 }
45
46 protected function addTables($value) {
47 if(!is_array($this->tables))
48 $this->dieDebug(__METHOD__, 'Must not call setTablesAsExpression() before this method');
49 if(is_array($value))
50 $this->tables = array_merge($this->tables, $value);
51 else
52 $this->tables[] = $value;
53 }
54
55 protected function setTablesAsExpression($value) {
56 if(!empty($this->tables))
57 $this->dieDebug(__METHOD__, 'Must not call addTables() before this method');
58 $this->tables = $value;
59 }
60
61 protected function addFields($value) {
62 if(is_array($value))
63 $this->fields = array_merge($this->fields, $value);
64 else
65 $this->fields[] = $value;
66 }
67
68 protected function addFieldsIf($value, $condition) {
69 if ($condition)
70 $this->addFields($value);
71 }
72
73 protected function addWhere($value) {
74 if(is_array($value))
75 $this->where = array_merge($this->where, $value);
76 else
77 $this->where[] = $value;
78 }
79
80 protected function addWhereIf($value, $condition) {
81 if ($condition)
82 $this->addWhere($value);
83 }
84
85 protected function addWhereFld($field, $value) {
86 if(!is_null($value))
87 $this->where[$field] = $value;
88 }
89
90 protected function addWhereRange($field, $dir, $start, $end) {
91 $isDirNewer = ($dir === 'newer');
92 $after = ($isDirNewer ? '<=' : '>=');
93 $before = ($isDirNewer ? '>=' : '<=');
94 $db = $this->getDB();
95
96 if (!is_null($start))
97 $this->addWhere($field . $after . $db->addQuotes($start));
98
99 if (!is_null($end))
100 $this->addWhere($field . $before . $db->addQuotes($end));
101
102 $this->addOption('ORDER BY', $field . ($isDirNewer ? '' : ' DESC'));
103 }
104
105 protected function select($method) {
106 $this->profileDBIn();
107 $res = $this->getDB()->select($this->tables, $this->fields, $this->where, $method, $this->options);
108 $this->profileDBOut();
109 return $res;
110 }
111
112 protected function addOption($name, $value) {
113 $this->options[$name] = $value;
114 }
115
116 /**
117 * Override this method to request extra fields from the pageSet
118 * using $this->getPageSet()->requestField('fieldName')
119 */
120 public function requestExtraData() {
121 }
122
123 /**
124 * Get the main Query module
125 */
126 public function getQuery() {
127 return $this->mQueryModule;
128 }
129
130 protected function setContinueEnumParameter($paramName, $paramValue) {
131 $msg = array (
132 $this->encodeParamName($paramName
133 ) => $paramValue);
134 $this->getResult()->addValue('query-continue', $this->getModuleName(), $msg);
135 }
136
137 /**
138 * Get the Query database connection (readonly)
139 */
140 protected function getDB() {
141 return $this->getQuery()->getDB();
142 }
143
144 /**
145 * Get the PageSet object to work on
146 * @return ApiPageSet data
147 */
148 protected function getPageSet() {
149 return $this->mQueryModule->getPageSet();
150 }
151
152 /**
153 * This is a very simplistic utility function
154 * to convert a non-namespaced title string to a db key.
155 * It will replace all ' ' with '_'
156 */
157 public static function titleToKey($title) {
158 return str_replace(' ', '_', $title);
159 }
160
161 public static function keyToTitle($key) {
162 return str_replace('_', ' ', $key);
163 }
164
165 public static function getBaseVersion() {
166 return __CLASS__ . ': $Id$';
167 }
168 }
169
170 abstract class ApiQueryGeneratorBase extends ApiQueryBase {
171
172 private $mIsGenerator;
173
174 public function __construct($query, $moduleName, $paramPrefix = '') {
175 parent :: __construct($query, $moduleName, $paramPrefix);
176 $this->mIsGenerator = false;
177 }
178
179 public function setGeneratorMode() {
180 $this->mIsGenerator = true;
181 }
182
183 /**
184 * Overrides base class to prepend 'g' to every generator parameter
185 */
186 public function encodeParamName($paramName) {
187 if ($this->mIsGenerator)
188 return 'g' . parent :: encodeParamName($paramName);
189 else
190 return parent :: encodeParamName($paramName);
191 }
192
193 /**
194 * Execute this module as a generator
195 * @param $resultPageSet PageSet: All output should be appended to this object
196 */
197 public abstract function executeGenerator($resultPageSet);
198 }
199 ?>