simplify r107833 per CR, just use wfExpandUrl()
[lhc/web/wiklou.git] / includes / db / DatabaseUtility.php
1 <?php
2 /**
3 * Utility class.
4 * @ingroup Database
5 */
6 class DBObject {
7 public $mData;
8
9 function __construct( $data ) {
10 $this->mData = $data;
11 }
12
13 /**
14 * @return bool
15 */
16 function isLOB() {
17 return false;
18 }
19
20 function data() {
21 return $this->mData;
22 }
23 }
24
25 /**
26 * Utility class
27 * @ingroup Database
28 *
29 * This allows us to distinguish a blob from a normal string and an array of strings
30 */
31 class Blob {
32 private $mData;
33
34 function __construct( $data ) {
35 $this->mData = $data;
36 }
37
38 function fetch() {
39 return $this->mData;
40 }
41 }
42
43 /**
44 * Base for all database-specific classes representing information about database fields
45 * @ingroup Database
46 */
47 interface Field {
48 /**
49 * Field name
50 * @return string
51 */
52 function name();
53
54 /**
55 * Name of table this field belongs to
56 * @return string
57 */
58 function tableName();
59
60 /**
61 * Database type
62 * @return string
63 */
64 function type();
65
66 /**
67 * Whether this field can store NULL values
68 * @return bool
69 */
70 function isNullable();
71 }
72
73 /**
74 * Result wrapper for grabbing data queried by someone else
75 * @ingroup Database
76 */
77 class ResultWrapper implements Iterator {
78 var $db, $result, $pos = 0, $currentRow = null;
79
80 /**
81 * Create a new result object from a result resource and a Database object
82 *
83 * @param DatabaseBase $database
84 * @param resource $result
85 */
86 function __construct( $database, $result ) {
87 $this->db = $database;
88
89 if ( $result instanceof ResultWrapper ) {
90 $this->result = $result->result;
91 } else {
92 $this->result = $result;
93 }
94 }
95
96 /**
97 * Get the number of rows in a result object
98 *
99 * @return integer
100 */
101 function numRows() {
102 return $this->db->numRows( $this );
103 }
104
105 /**
106 * Fetch the next row from the given result object, in object form.
107 * Fields can be retrieved with $row->fieldname, with fields acting like
108 * member variables.
109 *
110 * @return object
111 * @throws DBUnexpectedError Thrown if the database returns an error
112 */
113 function fetchObject() {
114 return $this->db->fetchObject( $this );
115 }
116
117 /**
118 * Fetch the next row from the given result object, in associative array
119 * form. Fields are retrieved with $row['fieldname'].
120 *
121 * @return Array
122 * @throws DBUnexpectedError Thrown if the database returns an error
123 */
124 function fetchRow() {
125 return $this->db->fetchRow( $this );
126 }
127
128 /**
129 * Free a result object
130 */
131 function free() {
132 $this->db->freeResult( $this );
133 unset( $this->result );
134 unset( $this->db );
135 }
136
137 /**
138 * Change the position of the cursor in a result object.
139 * See mysql_data_seek()
140 *
141 * @param $row integer
142 */
143 function seek( $row ) {
144 $this->db->dataSeek( $this, $row );
145 }
146
147 /*********************
148 * Iterator functions
149 * Note that using these in combination with the non-iterator functions
150 * above may cause rows to be skipped or repeated.
151 */
152
153 function rewind() {
154 if ( $this->numRows() ) {
155 $this->db->dataSeek( $this, 0 );
156 }
157 $this->pos = 0;
158 $this->currentRow = null;
159 }
160
161 /**
162 * @return int
163 */
164 function current() {
165 if ( is_null( $this->currentRow ) ) {
166 $this->next();
167 }
168 return $this->currentRow;
169 }
170
171 /**
172 * @return int
173 */
174 function key() {
175 return $this->pos;
176 }
177
178 /**
179 * @return int
180 */
181 function next() {
182 $this->pos++;
183 $this->currentRow = $this->fetchObject();
184 return $this->currentRow;
185 }
186
187 /**
188 * @return bool
189 */
190 function valid() {
191 return $this->current() !== false;
192 }
193 }
194
195 /**
196 * Overloads the relevant methods of the real ResultsWrapper so it
197 * doesn't go anywhere near an actual database.
198 */
199 class FakeResultWrapper extends ResultWrapper {
200 var $result = array();
201 var $db = null; // And it's going to stay that way :D
202 var $pos = 0;
203 var $currentRow = null;
204
205 function __construct( $array ) {
206 $this->result = $array;
207 }
208
209 /**
210 * @return int
211 */
212 function numRows() {
213 return count( $this->result );
214 }
215
216 function fetchRow() {
217 if ( $this->pos < count( $this->result ) ) {
218 $this->currentRow = $this->result[$this->pos];
219 } else {
220 $this->currentRow = false;
221 }
222 $this->pos++;
223 return $this->currentRow;
224 }
225
226 function seek( $row ) {
227 $this->pos = $row;
228 }
229
230 function free() {}
231
232 // Callers want to be able to access fields with $this->fieldName
233 function fetchObject() {
234 $this->fetchRow();
235 if ( $this->currentRow ) {
236 return (object)$this->currentRow;
237 } else {
238 return false;
239 }
240 }
241
242 function rewind() {
243 $this->pos = 0;
244 $this->currentRow = null;
245 }
246
247 function next() {
248 return $this->fetchObject();
249 }
250 }
251
252 /**
253 * Used by DatabaseBase::buildLike() to represent characters that have special meaning in SQL LIKE clauses
254 * and thus need no escaping. Don't instantiate it manually, use DatabaseBase::anyChar() and anyString() instead.
255 */
256 class LikeMatch {
257 private $str;
258
259 /**
260 * Store a string into a LikeMatch marker object.
261 *
262 * @param String $s
263 */
264 public function __construct( $s ) {
265 $this->str = $s;
266 }
267
268 /**
269 * Return the original stored string.
270 *
271 * @return String
272 */
273 public function toString() {
274 return $this->str;
275 }
276 }
277
278 /**
279 * An object representing a master or slave position in a replicated setup.
280 */
281 interface DBMasterPos {
282 }
283