Removed hardcodes for Zh-conversion
[lhc/web/wiklou.git] / includes / DatabaseFunctions.php
1 <?php
2 /**
3 * Backwards compatibility wrapper for Database.php
4 *
5 * Note: $wgDatabase has ceased to exist. Destroy all references.
6 *
7 * @version # $Id$
8 * @package MediaWiki
9 */
10
11 /**
12 * Usually aborts on failure
13 * If errors are explicitly ignored, returns success
14 * @param string $sql SQL query
15 * @param mixed $db database handler
16 * @param string $fname name of the php function calling
17 */
18 function wfQuery( $sql, $db, $fname = '' ) {
19 global $wgOut;
20 if ( !is_numeric( $db ) ) {
21 # Someone has tried to call this the old way
22 $wgOut->fatalError( wfMsgNoDB( 'wrong_wfQuery_params', $db, $sql ) );
23 }
24 $c =& wfGetDB( $db );
25 if ( $c !== false ) {
26 return $c->query( $sql, $fname );
27 } else {
28 return false;
29 }
30 }
31
32 /**
33 *
34 * @param string $sql SQL query
35 * @param $dbi
36 * @param string $fname name of the php function calling
37 * @return array first row from the database
38 */
39 function wfSingleQuery( $sql, $dbi, $fname = '' ) {
40 $db =& wfGetDB( $dbi );
41 $res = $db->query($sql, $fname );
42 $row = $db->fetchRow( $res );
43 $ret = $row[0];
44 $db->freeResult( $res );
45 return $ret;
46 }
47
48 /*
49 * @todo document function
50 */
51 function &wfGetDB( $db = DB_LAST ) {
52 global $wgLoadBalancer;
53 return $wgLoadBalancer->getConnection( $db );
54 }
55
56 /**
57 * Turns buffering of SQL result
58 * Sets on (true) or off (false). Default is "on" and it should not be changed
59 * without good reasons.
60 *
61 * @param $newstate
62 * @param $dbi
63 * @return mixed|NULL Returns the previous state.
64 */
65 function wfBufferSQLResults( $newstate, $dbi = DB_LAST ) {
66 $db =& wfGetDB( $dbi );
67 if ( $db !== false ) {
68 return $db->setBufferResults( $newstate );
69 } else {
70 return NULL;
71 }
72 }
73
74 /**
75 * Turns on (false) or off (true) the automatic generation and sending
76 * of a "we're sorry, but there has been a database error" page on
77 * database errors. Default is on (false). When turned off, the
78 * code should use wfLastErrno() and wfLastError() to handle the
79 * situation as appropriate.
80 *
81 * @param $newstate
82 * @param $dbi
83 * @return Returns the previous state.
84 */
85 function wfIgnoreSQLErrors( $newstate, $dbi = DB_LAST ) {
86 $db =& wfGetDB( $dbi );
87 if ( $db !== false ) {
88 return $db->ignoreErrors( $newstate );
89 } else {
90 return NULL;
91 }
92 }
93
94 /**#@+
95 * @param $res database result handler
96 * @param $dbi
97 */
98
99 /**
100 * Free a database result
101 * @return bool whether result is sucessful or not
102 */
103 function wfFreeResult( $res, $dbi = DB_LAST )
104 {
105 $db =& wfGetDB( $dbi );
106 if ( $db !== false ) {
107 $db->freeResult( $res );
108 return true;
109 } else {
110 return false;
111 }
112 }
113
114 /**
115 * Get an object from a database result
116 * @return object|false object we requested
117 */
118 function wfFetchObject( $res, $dbi = DB_LAST ) {
119 $db =& wfGetDB( $dbi );
120 if ( $db !== false ) {
121 return $db->fetchObject( $res, $dbi = DB_LAST );
122 } else {
123 return false;
124 }
125 }
126
127 /**
128 * Get a row from a database result
129 * @return object|false row we requested
130 */
131 function wfFetchRow( $res, $dbi = DB_LAST ) {
132 $db =& wfGetDB( $dbi );
133 if ( $db !== false ) {
134 return $db->fetchRow ( $res, $dbi = DB_LAST );
135 } else {
136 return false;
137 }
138 }
139
140 /**
141 * Get a number of rows from a database result
142 * @return integer|false number of rows
143 */
144 function wfNumRows( $res, $dbi = DB_LAST ) {
145 $db =& wfGetDB( $dbi );
146 if ( $db !== false ) {
147 return $db->numRows( $res, $dbi = DB_LAST );
148 } else {
149 return false;
150 }
151 }
152
153 /**
154 * Get the number of fields from a database result
155 * @return integer|false number of fields
156 */
157 function wfNumFields( $res, $dbi = DB_LAST ) {
158 $db =& wfGetDB( $dbi );
159 if ( $db !== false ) {
160 return $db->numFields( $res );
161 } else {
162 return false;
163 }
164 }
165
166 /**
167 * Return name of a field in a result
168 * @param integer $n id of the field
169 * @return string|false name of field
170 */
171 function wfFieldName( $res, $n, $dbi = DB_LAST )
172 {
173 $db =& wfGetDB( $dbi );
174 if ( $db !== false ) {
175 return $db->fieldName( $res, $n, $dbi = DB_LAST );
176 } else {
177 return false;
178 }
179 }
180 /**#@-*/
181
182 /**
183 * @todo document function
184 */
185 function wfInsertId( $dbi = DB_LAST ) {
186 $db =& wfGetDB( $dbi );
187 if ( $db !== false ) {
188 return $db->insertId();
189 } else {
190 return false;
191 }
192 }
193
194 /**
195 * @todo document function
196 */
197 function wfDataSeek( $res, $row, $dbi = DB_LAST ) {
198 $db =& wfGetDB( $dbi );
199 if ( $db !== false ) {
200 return $db->dataSeek( $res, $row );
201 } else {
202 return false;
203 }
204 }
205
206 /**
207 * @todo document function
208 */
209 function wfLastErrno( $dbi = DB_LAST ) {
210 $db =& wfGetDB( $dbi );
211 if ( $db !== false ) {
212 return $db->lastErrno();
213 } else {
214 return false;
215 }
216 }
217
218 /**
219 * @todo document function
220 */
221 function wfLastError( $dbi = DB_LAST ) {
222 $db =& wfGetDB( $dbi );
223 if ( $db !== false ) {
224 return $db->lastError();
225 } else {
226 return false;
227 }
228 }
229
230 /**
231 * @todo document function
232 */
233 function wfAffectedRows( $dbi = DB_LAST ) {
234 $db =& wfGetDB( $dbi );
235 if ( $db !== false ) {
236 return $db->affectedRows();
237 } else {
238 return false;
239 }
240 }
241
242 /**
243 * @todo document function
244 */
245 function wfLastDBquery( $dbi = DB_LAST ) {
246 $db =& wfGetDB( $dbi );
247 if ( $db !== false ) {
248 return $db->lastQuery();
249 } else {
250 return false;
251 }
252 }
253
254 /**
255 * @todo document function
256 */
257 function wfSetSQL( $table, $var, $value, $cond, $dbi = DB_MASTER )
258 {
259 $db =& wfGetDB( $dbi );
260 if ( $db !== false ) {
261 return $db->set( $table, $var, $value, $cond );
262 } else {
263 return false;
264 }
265 }
266
267
268 /**
269 * @todo document function
270 */
271 function wfGetSQL( $table, $var, $cond='', $dbi = DB_LAST )
272 {
273 $db =& wfGetDB( $dbi );
274 if ( $db !== false ) {
275 return $db->getField( $table, $var, $cond );
276 } else {
277 return false;
278 }
279 }
280
281 /**
282 * @todo document function
283 */
284 function wfFieldExists( $table, $field, $dbi = DB_LAST ) {
285 $db =& wfGetDB( $dbi );
286 if ( $db !== false ) {
287 return $db->fieldExists( $table, $field );
288 } else {
289 return false;
290 }
291 }
292
293 /**
294 * @todo document function
295 */
296 function wfIndexExists( $table, $index, $dbi = DB_LAST ) {
297 $db =& wfGetDB( $dbi );
298 if ( $db !== false ) {
299 return $db->indexExists( $table, $index );
300 } else {
301 return false;
302 }
303 }
304
305 /**
306 * @todo document function
307 */
308 function wfInsertArray( $table, $array, $fname = 'wfInsertArray', $dbi = DB_MASTER ) {
309 $db =& wfGetDB( $dbi );
310 if ( $db !== false ) {
311 return $db->insertArray( $table, $array, $fname );
312 } else {
313 return false;
314 }
315 }
316
317 /**
318 * @todo document function
319 */
320 function wfGetArray( $table, $vars, $conds, $fname = 'wfGetArray', $dbi = DB_LAST ) {
321 $db =& wfGetDB( $dbi );
322 if ( $db !== false ) {
323 return $db->getArray( $table, $vars, $conds, $fname );
324 } else {
325 return false;
326 }
327 }
328
329 /**
330 * @todo document function
331 */
332 function wfUpdateArray( $table, $values, $conds, $fname = 'wfUpdateArray', $dbi = DB_MASTER ) {
333 $db =& wfGetDB( $dbi );
334 if ( $db !== false ) {
335 $db->updateArray( $table, $values, $conds, $fname );
336 return true;
337 } else {
338 return false;
339 }
340 }
341
342 /**
343 * @todo document function
344 */
345 function wfTableName( $name, $dbi = DB_LAST ) {
346 $db =& wfGetDB( $dbi );
347 if ( $db !== false ) {
348 return $db->tableName( $name );
349 } else {
350 return false;
351 }
352 }
353
354 /**
355 * @todo document function
356 */
357 function wfStrencode( $s, $dbi = DB_LAST ) {
358 $db =& wfGetDB( $dbi );
359 if ( $db !== false ) {
360 return $db->strencode( $s );
361 } else {
362 return false;
363 }
364 }
365
366 /**
367 * @todo document function
368 */
369 function wfNextSequenceValue( $seqName, $dbi = DB_MASTER ) {
370 $db =& wfGetDB( $dbi );
371 if ( $db !== false ) {
372 return $db->nextSequenceValue( $seqName );
373 } else {
374 return false;
375 }
376 }
377
378 /**
379 * @todo document function
380 */
381 function wfUseIndexClause( $index, $dbi = DB_SLAVE ) {
382 $db =& wfGetDB( $dbi );
383 if ( $db !== false ) {
384 return $db->useIndexClause( $index );
385 } else {
386 return false;
387 }
388 }
389 ?>