(bug 19441) Updated date formatting for Lithuanian
[mediawiki.git] / includes / DatabaseFunctions.php
blob52e9a8c8dd06b4e34f049f8233bddc1d86cdb89d
1 <?php
2 /**
3 * Legacy database functions, for compatibility with pre-1.3 code
4 * NOTE: this file is no longer loaded by default.
5 * @file
6 * @ingroup Database
7 */
9 /**
10 * Usually aborts on failure
11 * If errors are explicitly ignored, returns success
12 * @param $sql String: SQL query
13 * @param $db Mixed: database handler
14 * @param $fname String: name of the php function calling
16 function wfQuery( $sql, $db, $fname = '' ) {
17 if ( !is_numeric( $db ) ) {
18 # Someone has tried to call this the old way
19 throw new FatalError( wfMsgNoDB( 'wrong_wfQuery_params', $db, $sql ) );
21 $c = wfGetDB( $db );
22 if ( $c !== false ) {
23 return $c->query( $sql, $fname );
24 } else {
25 return false;
29 /**
31 * @param $sql String: SQL query
32 * @param $dbi
33 * @param $fname String: name of the php function calling
34 * @return Array: first row from the database
36 function wfSingleQuery( $sql, $dbi, $fname = '' ) {
37 $db = wfGetDB( $dbi );
38 $res = $db->query($sql, $fname );
39 $row = $db->fetchRow( $res );
40 $ret = $row[0];
41 $db->freeResult( $res );
42 return $ret;
45 /**
46 * Turns on (false) or off (true) the automatic generation and sending
47 * of a "we're sorry, but there has been a database error" page on
48 * database errors. Default is on (false). When turned off, the
49 * code should use wfLastErrno() and wfLastError() to handle the
50 * situation as appropriate.
52 * @param $newstate
53 * @param $dbi
54 * @return Returns the previous state.
56 function wfIgnoreSQLErrors( $newstate, $dbi = DB_LAST ) {
57 $db = wfGetDB( $dbi );
58 if ( $db !== false ) {
59 return $db->ignoreErrors( $newstate );
60 } else {
61 return NULL;
65 /**#@+
66 * @param $res Database result handler
67 * @param $dbi
70 /**
71 * Free a database result
72 * @return Bool: whether result is sucessful or not.
74 function wfFreeResult( $res, $dbi = DB_LAST )
76 $db = wfGetDB( $dbi );
77 if ( $db !== false ) {
78 $db->freeResult( $res );
79 return true;
80 } else {
81 return false;
85 /**
86 * Get an object from a database result
87 * @return object|false object we requested
89 function wfFetchObject( $res, $dbi = DB_LAST ) {
90 $db = wfGetDB( $dbi );
91 if ( $db !== false ) {
92 return $db->fetchObject( $res, $dbi = DB_LAST );
93 } else {
94 return false;
98 /**
99 * Get a row from a database result
100 * @return object|false row we requested
102 function wfFetchRow( $res, $dbi = DB_LAST ) {
103 $db = wfGetDB( $dbi );
104 if ( $db !== false ) {
105 return $db->fetchRow ( $res, $dbi = DB_LAST );
106 } else {
107 return false;
112 * Get a number of rows from a database result
113 * @return integer|false number of rows
115 function wfNumRows( $res, $dbi = DB_LAST ) {
116 $db = wfGetDB( $dbi );
117 if ( $db !== false ) {
118 return $db->numRows( $res, $dbi = DB_LAST );
119 } else {
120 return false;
125 * Get the number of fields from a database result
126 * @return integer|false number of fields
128 function wfNumFields( $res, $dbi = DB_LAST ) {
129 $db = wfGetDB( $dbi );
130 if ( $db !== false ) {
131 return $db->numFields( $res );
132 } else {
133 return false;
138 * Return name of a field in a result
139 * @param $res Mixed: Ressource link see Database::fieldName()
140 * @param $n Integer: id of the field
141 * @param $dbi Default DB_LAST
142 * @return string|false name of field
144 function wfFieldName( $res, $n, $dbi = DB_LAST )
146 $db = wfGetDB( $dbi );
147 if ( $db !== false ) {
148 return $db->fieldName( $res, $n, $dbi = DB_LAST );
149 } else {
150 return false;
153 /**#@-*/
156 * @todo document function
157 * @see Database::insertId()
159 function wfInsertId( $dbi = DB_LAST ) {
160 $db = wfGetDB( $dbi );
161 if ( $db !== false ) {
162 return $db->insertId();
163 } else {
164 return false;
169 * @todo document function
170 * @see Database::dataSeek()
172 function wfDataSeek( $res, $row, $dbi = DB_LAST ) {
173 $db = wfGetDB( $dbi );
174 if ( $db !== false ) {
175 return $db->dataSeek( $res, $row );
176 } else {
177 return false;
182 * Get the last error number
183 * @see Database::lastErrno()
185 function wfLastErrno( $dbi = DB_LAST ) {
186 $db = wfGetDB( $dbi );
187 if ( $db !== false ) {
188 return $db->lastErrno();
189 } else {
190 return false;
195 * Get the last error
196 * @see Database::lastError()
198 function wfLastError( $dbi = DB_LAST ) {
199 $db = wfGetDB( $dbi );
200 if ( $db !== false ) {
201 return $db->lastError();
202 } else {
203 return false;
208 * Get the number of affected rows
209 * @see Database::affectedRows()
211 function wfAffectedRows( $dbi = DB_LAST ) {
212 $db = wfGetDB( $dbi );
213 if ( $db !== false ) {
214 return $db->affectedRows();
215 } else {
216 return false;
221 * Get the last query ran
222 * @see Database::lastQuery
224 function wfLastDBquery( $dbi = DB_LAST ) {
225 $db = wfGetDB( $dbi );
226 if ( $db !== false ) {
227 return $db->lastQuery();
228 } else {
229 return false;
234 * @see Database::Set()
235 * @todo document function
236 * @param $table
237 * @param $var
238 * @param $value
239 * @param $cond
240 * @param $dbi Default DB_MASTER
242 function wfSetSQL( $table, $var, $value, $cond, $dbi = DB_MASTER )
244 $db = wfGetDB( $dbi );
245 if ( $db !== false ) {
246 return $db->set( $table, $var, $value, $cond );
247 } else {
248 return false;
254 * Simple select wrapper, return one field
255 * @see Database::selectField()
256 * @param $table
257 * @param $var
258 * @param $cond Default ''
259 * @param $dbi Default DB_LAST
261 function wfGetSQL( $table, $var, $cond='', $dbi = DB_LAST )
263 $db = wfGetDB( $dbi );
264 if ( $db !== false ) {
265 return $db->selectField( $table, $var, $cond );
266 } else {
267 return false;
272 * Does a given field exist on the specified table?
273 * @see Database::fieldExists()
274 * @param $table
275 * @param $field
276 * @param $dbi Default DB_LAST
277 * @return Result of Database::fieldExists() or false.
279 function wfFieldExists( $table, $field, $dbi = DB_LAST ) {
280 $db = wfGetDB( $dbi );
281 if ( $db !== false ) {
282 return $db->fieldExists( $table, $field );
283 } else {
284 return false;
289 * Does the requested index exist on the specified table?
290 * @see Database::indexExists()
291 * @param $table String
292 * @param $index
293 * @param $dbi Default DB_LAST
294 * @return Result of Database::indexExists() or false.
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;
306 * @see Database::insert()
307 * @todo document function
308 * @param $table String
309 * @param $array Array
310 * @param $fname String, default 'wfInsertArray'.
311 * @param $dbi Default DB_MASTER
312 * @return result of Database::insert() or false.
314 function wfInsertArray( $table, $array, $fname = 'wfInsertArray', $dbi = DB_MASTER ) {
315 $db = wfGetDB( $dbi );
316 if ( $db !== false ) {
317 return $db->insert( $table, $array, $fname );
318 } else {
319 return false;
324 * @see Database::getArray()
325 * @todo document function
326 * @param $table String
327 * @param $vars
328 * @param $conds
329 * @param $fname String, default 'wfGetArray'.
330 * @param $dbi Default DB_LAST
331 * @return result of Database::getArray() or false.
333 function wfGetArray( $table, $vars, $conds, $fname = 'wfGetArray', $dbi = DB_LAST ) {
334 $db = wfGetDB( $dbi );
335 if ( $db !== false ) {
336 return $db->getArray( $table, $vars, $conds, $fname );
337 } else {
338 return false;
343 * @see Database::update()
344 * @param $table String
345 * @param $values
346 * @param $conds
347 * @param $fname String, default 'wfUpdateArray'
348 * @param $dbi Default DB_MASTER
349 * @return Result of Database::update()) or false;
350 * @todo document function
352 function wfUpdateArray( $table, $values, $conds, $fname = 'wfUpdateArray', $dbi = DB_MASTER ) {
353 $db = wfGetDB( $dbi );
354 if ( $db !== false ) {
355 $db->update( $table, $values, $conds, $fname );
356 return true;
357 } else {
358 return false;
363 * Get fully usable table name
364 * @see Database::tableName()
366 function wfTableName( $name, $dbi = DB_LAST ) {
367 $db = wfGetDB( $dbi );
368 if ( $db !== false ) {
369 return $db->tableName( $name );
370 } else {
371 return false;
376 * @todo document function
377 * @see Database::strencode()
379 function wfStrencode( $s, $dbi = DB_LAST ) {
380 $db = wfGetDB( $dbi );
381 if ( $db !== false ) {
382 return $db->strencode( $s );
383 } else {
384 return false;
389 * @todo document function
390 * @see Database::nextSequenceValue()
392 function wfNextSequenceValue( $seqName, $dbi = DB_MASTER ) {
393 $db = wfGetDB( $dbi );
394 if ( $db !== false ) {
395 return $db->nextSequenceValue( $seqName );
396 } else {
397 return false;
402 * @todo document function
403 * @see Database::useIndexClause()
405 function wfUseIndexClause( $index, $dbi = DB_SLAVE ) {
406 $db = wfGetDB( $dbi );
407 if ( $db !== false ) {
408 return $db->useIndexClause( $index );
409 } else {
410 return false;