-<?
-global $IP;
-include_once( "$IP/FulltextStoplist.php" );
+<?php
+/**
+ * Legacy database functions, for compatibility with pre-1.3 code
+ * NOTE: this file is no longer loaded by default.
+ * @file
+ * @ingroup Database
+ */
-$wgLastDatabaseQuery = "";
+/**
+ * Usually aborts on failure
+ * If errors are explicitly ignored, returns success
+ * @param $sql String: SQL query
+ * @param $db Mixed: database handler
+ * @param $fname String: name of the php function calling
+ */
+function wfQuery( $sql, $db, $fname = '' ) {
+ if ( !is_numeric( $db ) ) {
+ # Someone has tried to call this the old way
+ throw new FatalError( wfMsgNoDB( 'wrong_wfQuery_params', $db, $sql ) );
+ }
+ $c = wfGetDB( $db );
+ if ( $c !== false ) {
+ return $c->query( $sql, $fname );
+ } else {
+ return false;
+ }
+}
-function wfGetDB( $altuser = "", $altpassword = "" )
-{
- global $wgDBserver, $wgDBuser, $wgDBpassword;
- global $wgDBname, $wgDBconnection, $wgEmergencyContact;
-
- $noconn = str_replace( "$1", $wgDBserver, wfMsg( "noconnect" ) );
- $nodb = str_replace( "$1", $wgDBname, wfMsg( "nodb" ) );
-
- $helpme = "\n<p>If this error persists after reloading and clearing " .
- "your browser cache, please notify the <a href=\"mailto:" .
- $wgEmergencyContact . "\">Wikipedia developers</a>.</p>";
-
- if ( $altuser != "" ) {
- $wgDBconnection = mysql_connect( $wgDBserver, $altuser, $altpassword )
- or die( "bad sql user" );
- mysql_select_db( $wgDBname, $wgDBconnection ) or die(
- htmlspecialchars(mysql_error()) );
- }
-
- if ( ! $wgDBconnection ) {
- $wgDBconnection = mysql_pconnect( $wgDBserver, $wgDBuser,
- $wgDBpassword ) or die( $noconn .
- "\n<p><b>" . htmlspecialchars(mysql_error()) . "</b></p>\n" . $helpme );
- if( !mysql_select_db( $wgDBname, $wgDBconnection ) ) {
- wfDebug( "Persistent connection is broken?\n", true );
-
- $wgDBconnection = mysql_connect( $wgDBserver, $wgDBuser,
- $wgDBpassword ) or die( $noconn .
- "\n<p><b>" . htmlspecialchars(mysql_error()) . "</b> (tried non-p connect)</p>\n" . $helpme );
- mysql_select_db( $wgDBname, $wgDBconnection ) or die( $nodb .
- "\n<p><b>" . htmlspecialchars(mysql_error()) . "</b> (tried non-p connect)</p>\n" . $helpme );
- }
- }
- # mysql_ping( $wgDBconnection );
- return $wgDBconnection;
-}
-
-function wfQuery( $sql, $fname = "" )
+/**
+ *
+ * @param $sql String: SQL query
+ * @param $dbi
+ * @param $fname String: name of the php function calling
+ * @return Array: first row from the database
+ */
+function wfSingleQuery( $sql, $dbi, $fname = '' ) {
+ $db = wfGetDB( $dbi );
+ $res = $db->query($sql, $fname );
+ $row = $db->fetchRow( $res );
+ $ret = $row[0];
+ $db->freeResult( $res );
+ return $ret;
+}
+
+/**
+ * Turns on (false) or off (true) the automatic generation and sending
+ * of a "we're sorry, but there has been a database error" page on
+ * database errors. Default is on (false). When turned off, the
+ * code should use wfLastErrno() and wfLastError() to handle the
+ * situation as appropriate.
+ *
+ * @param $newstate
+ * @param $dbi
+ * @return Returns the previous state.
+ */
+function wfIgnoreSQLErrors( $newstate, $dbi = DB_LAST ) {
+ $db = wfGetDB( $dbi );
+ if ( $db !== false ) {
+ return $db->ignoreErrors( $newstate );
+ } else {
+ return null;
+ }
+}
+
+/**#@+
+ * @param $res Database result handler
+ * @param $dbi
+*/
+
+/**
+ * Free a database result
+ * @return Bool: whether result is sucessful or not.
+ */
+function wfFreeResult( $res, $dbi = DB_LAST )
{
- global $wgLastDatabaseQuery, $wgOut;
-## wfProfileIn( "wfQuery" );
- $wgLastDatabaseQuery = $sql;
+ $db = wfGetDB( $dbi );
+ if ( $db !== false ) {
+ $db->freeResult( $res );
+ return true;
+ } else {
+ return false;
+ }
+}
- $conn = wfGetDB();
- $ret = mysql_query( $sql, $conn );
+/**
+ * Get an object from a database result
+ * @return object|false object we requested
+ */
+function wfFetchObject( $res, $dbi = DB_LAST ) {
+ $db = wfGetDB( $dbi );
+ if ( $db !== false ) {
+ return $db->fetchObject( $res, $dbi = DB_LAST );
+ } else {
+ return false;
+ }
+}
- if ( "" != $fname ) {
-# wfDebug( "{$fname}:SQL: {$sql}\n", true );
+/**
+ * Get a row from a database result
+ * @return object|false row we requested
+ */
+function wfFetchRow( $res, $dbi = DB_LAST ) {
+ $db = wfGetDB( $dbi );
+ if ( $db !== false ) {
+ return $db->fetchRow ( $res, $dbi = DB_LAST );
} else {
-# wfDebug( "SQL: {$sql}\n", true );
+ return false;
}
- if ( false === $ret ) {
- $wgOut->databaseError( $fname );
- exit;
+}
+
+/**
+ * Get a number of rows from a database result
+ * @return integer|false number of rows
+ */
+function wfNumRows( $res, $dbi = DB_LAST ) {
+ $db = wfGetDB( $dbi );
+ if ( $db !== false ) {
+ return $db->numRows( $res, $dbi = DB_LAST );
+ } else {
+ return false;
}
-## wfProfileOut();
- return $ret;
}
-function wfFreeResult( $res ) { mysql_free_result( $res ); }
-function wfFetchObject( $res ) { return mysql_fetch_object( $res ); }
-function wfNumRows( $res ) { return mysql_num_rows( $res ); }
-function wfNumFields( $res ) { return mysql_num_fields( $res ); }
-function wfFieldName( $res, $n ) { return mysql_field_name( $res, $n ); }
-function wfInsertId() { return mysql_insert_id( wfGetDB() ); }
-function wfDataSeek( $res, $row ) { return mysql_data_seek( $res, $row ); }
-function wfLastErrno() { return mysql_errno(); }
-function wfLastError() { return mysql_error(); }
+/**
+ * Get the number of fields from a database result
+ * @return integer|false number of fields
+ */
+function wfNumFields( $res, $dbi = DB_LAST ) {
+ $db = wfGetDB( $dbi );
+ if ( $db !== false ) {
+ return $db->numFields( $res );
+ } else {
+ return false;
+ }
+}
-function wfLastDBquery()
+/**
+ * Return name of a field in a result
+ * @param $res Mixed: Ressource link see Database::fieldName()
+ * @param $n Integer: id of the field
+ * @param $dbi Default DB_LAST
+ * @return string|false name of field
+ */
+function wfFieldName( $res, $n, $dbi = DB_LAST )
{
- global $wgLastDatabaseQuery;
- return $wgLastDatabaseQuery;
+ $db = wfGetDB( $dbi );
+ if ( $db !== false ) {
+ return $db->fieldName( $res, $n, $dbi = DB_LAST );
+ } else {
+ return false;
+ }
+}
+/**#@-*/
+
+/**
+ * @todo document function
+ * @see Database::insertId()
+ */
+function wfInsertId( $dbi = DB_LAST ) {
+ $db = wfGetDB( $dbi );
+ if ( $db !== false ) {
+ return $db->insertId();
+ } else {
+ return false;
+ }
+}
+
+/**
+ * @todo document function
+ * @see Database::dataSeek()
+ */
+function wfDataSeek( $res, $row, $dbi = DB_LAST ) {
+ $db = wfGetDB( $dbi );
+ if ( $db !== false ) {
+ return $db->dataSeek( $res, $row );
+ } else {
+ return false;
+ }
+}
+
+/**
+ * Get the last error number
+ * @see Database::lastErrno()
+ */
+function wfLastErrno( $dbi = DB_LAST ) {
+ $db = wfGetDB( $dbi );
+ if ( $db !== false ) {
+ return $db->lastErrno();
+ } else {
+ return false;
+ }
+}
+
+/**
+ * Get the last error
+ * @see Database::lastError()
+ */
+function wfLastError( $dbi = DB_LAST ) {
+ $db = wfGetDB( $dbi );
+ if ( $db !== false ) {
+ return $db->lastError();
+ } else {
+ return false;
+ }
+}
+
+/**
+ * Get the number of affected rows
+ * @see Database::affectedRows()
+ */
+function wfAffectedRows( $dbi = DB_LAST ) {
+ $db = wfGetDB( $dbi );
+ if ( $db !== false ) {
+ return $db->affectedRows();
+ } else {
+ return false;
+ }
+}
+
+/**
+ * Get the last query ran
+ * @see Database::lastQuery
+ */
+function wfLastDBquery( $dbi = DB_LAST ) {
+ $db = wfGetDB( $dbi );
+ if ( $db !== false ) {
+ return $db->lastQuery();
+ } else {
+ return false;
+ }
}
-function wfSetSQL( $table, $var, $value, $cond )
+/**
+ * @see Database::Set()
+ * @todo document function
+ * @param $table
+ * @param $var
+ * @param $value
+ * @param $cond
+ * @param $dbi Default DB_MASTER
+ */
+function wfSetSQL( $table, $var, $value, $cond, $dbi = DB_MASTER )
{
- $sql = "UPDATE $table SET $var = '" .
- wfStrencode( $value ) . "' WHERE ($cond)";
- wfQuery( $sql, "wfSetSQL" );
+ $db = wfGetDB( $dbi );
+ if ( $db !== false ) {
+ return $db->set( $table, $var, $value, $cond );
+ } else {
+ return false;
+ }
}
-function wfGetSQL( $table, $var, $cond )
+
+/**
+ * Simple select wrapper, return one field
+ * @see Database::selectField()
+ * @param $table
+ * @param $var
+ * @param $cond Default ''
+ * @param $dbi Default DB_LAST
+ */
+function wfGetSQL( $table, $var, $cond='', $dbi = DB_LAST )
{
- $sql = "SELECT $var FROM $table WHERE ($cond)";
- $result = wfQuery( $sql, "wfGetSQL" );
+ $db = wfGetDB( $dbi );
+ if ( $db !== false ) {
+ return $db->selectField( $table, $var, $cond );
+ } else {
+ return false;
+ }
+}
- $ret = "";
- if ( mysql_num_rows( $result ) > 0 ) {
- $s = mysql_fetch_object( $result );
- $ret = $s->$var;
- mysql_free_result( $result );
+/**
+ * Does a given field exist on the specified table?
+ * @see Database::fieldExists()
+ * @param $table
+ * @param $field
+ * @param $dbi Default DB_LAST
+ * @return Result of Database::fieldExists() or false.
+ */
+function wfFieldExists( $table, $field, $dbi = DB_LAST ) {
+ $db = wfGetDB( $dbi );
+ if ( $db !== false ) {
+ return $db->fieldExists( $table, $field );
+ } else {
+ return false;
}
- return $ret;
}
-function wfStrencode( $s )
-{
- return addslashes( $s );
+/**
+ * Does the requested index exist on the specified table?
+ * @see Database::indexExists()
+ * @param $table String
+ * @param $index
+ * @param $dbi Default DB_LAST
+ * @return Result of Database::indexExists() or false.
+ */
+function wfIndexExists( $table, $index, $dbi = DB_LAST ) {
+ $db = wfGetDB( $dbi );
+ if ( $db !== false ) {
+ return $db->indexExists( $table, $index );
+ } else {
+ return false;
+ }
+}
+
+/**
+ * @see Database::insert()
+ * @todo document function
+ * @param $table String
+ * @param $array Array
+ * @param $fname String, default 'wfInsertArray'.
+ * @param $dbi Default DB_MASTER
+ * @return result of Database::insert() or false.
+ */
+function wfInsertArray( $table, $array, $fname = 'wfInsertArray', $dbi = DB_MASTER ) {
+ $db = wfGetDB( $dbi );
+ if ( $db !== false ) {
+ return $db->insert( $table, $array, $fname );
+ } else {
+ return false;
+ }
}
-# Ideally we'd be using actual time fields in the db
-function wfTimestamp2Unix( $ts ) {
- return mktime( ( (int)substr( $ts, 8, 2) ),
- (int)substr( $ts, 10, 2 ), (int)substr( $ts, 12, 2 ),
- (int)substr( $ts, 4, 2 ), (int)substr( $ts, 6, 2 ),
- (int)substr( $ts, 0, 4 ) );
+/**
+ * @see Database::getArray()
+ * @todo document function
+ * @param $table String
+ * @param $vars
+ * @param $conds
+ * @param $fname String, default 'wfGetArray'.
+ * @param $dbi Default DB_LAST
+ * @return result of Database::getArray() or false.
+ */
+function wfGetArray( $table, $vars, $conds, $fname = 'wfGetArray', $dbi = DB_LAST ) {
+ $db = wfGetDB( $dbi );
+ if ( $db !== false ) {
+ return $db->getArray( $table, $vars, $conds, $fname );
+ } else {
+ return false;
+ }
+}
+
+/**
+ * @see Database::update()
+ * @param $table String
+ * @param $values
+ * @param $conds
+ * @param $fname String, default 'wfUpdateArray'
+ * @param $dbi Default DB_MASTER
+ * @return Result of Database::update()) or false;
+ * @todo document function
+ */
+function wfUpdateArray( $table, $values, $conds, $fname = 'wfUpdateArray', $dbi = DB_MASTER ) {
+ $db = wfGetDB( $dbi );
+ if ( $db !== false ) {
+ $db->update( $table, $values, $conds, $fname );
+ return true;
+ } else {
+ return false;
+ }
}
-function wfUnix2Timestamp( $unixtime ) {
- return date( "YmdHis", $unixtime );
+/**
+ * Get fully usable table name
+ * @see Database::tableName()
+ */
+function wfTableName( $name, $dbi = DB_LAST ) {
+ $db = wfGetDB( $dbi );
+ if ( $db !== false ) {
+ return $db->tableName( $name );
+ } else {
+ return false;
+ }
}
-function wfTimestampNow() {
- # return NOW
- return date( "YmdHis" );
+/**
+ * @todo document function
+ * @see Database::strencode()
+ */
+function wfStrencode( $s, $dbi = DB_LAST ) {
+ $db = wfGetDB( $dbi );
+ if ( $db !== false ) {
+ return $db->strencode( $s );
+ } else {
+ return false;
+ }
}
-# Sorting hack for MySQL 3, which doesn't use index sorts for DESC
-function wfInvertTimestamp( $ts ) {
- return strtr(
- $ts,
- "0123456789",
- "9876543210"
- );
+/**
+ * @todo document function
+ * @see Database::nextSequenceValue()
+ */
+function wfNextSequenceValue( $seqName, $dbi = DB_MASTER ) {
+ $db = wfGetDB( $dbi );
+ if ( $db !== false ) {
+ return $db->nextSequenceValue( $seqName );
+ } else {
+ return false;
+ }
}
-?>
+/**
+ * @todo document function
+ * @see Database::useIndexClause()
+ */
+function wfUseIndexClause( $index, $dbi = DB_SLAVE ) {
+ $db = wfGetDB( $dbi );
+ if ( $db !== false ) {
+ return $db->useIndexClause( $index );
+ } else {
+ return false;
+ }
+}