limitOffset = 0; } /** * @static * @param failFunction * @param $flags */ static function newFromParams( $server, $user, $password, $dbName, $failFunction = false, $flags = 0 ) { return new DatabaseADODB( $server, $user, $password, $dbName, $failFunction, $flags ); } /** * Usually aborts on failure * If the failFunction is set to a non-zero integer, returns success */ function open( $server, $user, $password, $dbName ) { global $wguname; wfProfileIn( __METHOD__ ); $this->close(); $this->mServer = $server; $this->mUser = $user; $this->mPassword = $password; $this->mDBname = $dbName; $success = false; wfProfileIn("dbconnect-$server"); # LIVE PATCH by Tim, ask Domas for why: retry loop $this->mConn = false; $max = 3; $dsn = "$server;DATABASE=$dbName;AutoTranslate=no"; for ( $i = 0; $i < $max && !$this->mConn; $i++ ) { if ( $i > 1 ) { usleep( 1000 ); } if ( $this->mFlags & DBO_PERSISTENT ) { try { $this->mConn = &ADONewConnection( 'ado_mssql' ); $this->mConn->PConnect($dsn, $user, $password, 'mssql' ); } catch (exception $pe) { throw new DBConnectionError($this, $pe->getMessage()); } } else { # Create a new connection... try { $this->mConn = &ADONewConnection( 'ado_mssql'); $this->mConn->Connect($dsn, $user, $password, 'mssql' ); } catch (exception $pe) { throw new DBConnectionError($this, $pe->getMessage()); } } if ($this->mConn === false) { $iplus = $i + 1; wfLogDBError("Connect loop error $iplus of $max ($server): " . " - " . "\n"); } } $this->mConn->Execute("SET NOCOUNT OFF"); $this->mConn->SetFetchMode(ADODB_FETCH_NUM); wfProfileOut("dbconnect-$server"); $success = (bool)$this->mConn; if ( $success ) { // Setting the character set is handled differently from MySQL } else { $this->reportConnectionError(); } $this->mOpened = $success; wfProfileOut( __METHOD__ ); return $success; } /**@}}*/ public function resetConnection() { return $this->open( $this->mServer, $this->mUser, $this->mPassword, $this->mDBname); } /** * Closes a database connection. * if it is open : commits any open transactions * * @return bool operation success. true if already closed. */ function close() { $this->mOpened = false; if ( $this->mConn ) { if ( $this->trxLevel() ) { $this->immediateCommit(); } $this->mConn = null; return true; } else { return true; } } /** * Usually aborts on failure * If errors are explicitly ignored, returns success */ function query( $sql, $fname = '', $tempIgnore = false ) { global $wgProfiling; if ( !strncmp( "LOCK", $sql, 4) ) return; if ( !strncmp( "UNLOCK", $sql, 6) ) return; if ( $wgProfiling ) { # generalizeSQL will probably cut down the query to reasonable # logging size most of the time. The substr is really just a sanity check. # Who's been wasting my precious column space? -- TS #$profName = 'query: ' . $fname . ' ' . substr( Database::generalizeSQL( $sql ), 0, 255 ); if ( is_null( $this->getLBInfo( 'master' ) ) ) { $queryProf = 'query: ' . substr( Database::generalizeSQL( $sql ), 0, 255 ); $totalProf = 'Database::query'; } else { $queryProf = 'query-m: ' . substr( Database::generalizeSQL( $sql ), 0, 255 ); $totalProf = 'Database::query-master'; } wfProfileIn( $totalProf ); wfProfileIn( $queryProf ); } $this->mLastQuery = $sql; # Add a comment for easy SHOW PROCESSLIST interpretation if ( $fname ) { $commentedSql = preg_replace('/\s/', " /* $fname */ ", $sql, 1); } else { $commentedSql = $sql; } # If DBO_TRX is set, start a transaction if ( ( $this->mFlags & DBO_TRX ) && !$this->trxLevel() && $sql != 'BEGIN' && $sql != 'COMMIT' && $sql != 'ROLLBACK' ) { $this->begin(); } if ( $this->debug() ) { $sqlx = substr( $commentedSql, 0, 500 ); $sqlx = strtr( $sqlx, "\t\n", ' ' ); wfDebug( "SQL: $sqlx\n" ); } # Do the query and handle errors $ret = $this->doQuery( $commentedSql ); # Try reconnecting if the connection was lost if ( false === $ret && ( $this->lastErrno() == 2013 || $this->lastErrno() == 2006 ) ) { # Transaction is gone, like it or not $this->mTrxLevel = 0; wfDebug( "Connection lost, reconnecting...\n" ); if ( $this->ping() ) { wfDebug( "Reconnected\n" ); $ret = $this->doQuery( $commentedSql ); } else { wfDebug( "Failed\n" ); } } if ( false === $ret ) { $this->reportQueryError( $this->lastError(), $this->lastErrno(), $sql, $fname, $tempIgnore ); } if ( $wgProfiling ) { wfProfileOut( $queryProf ); wfProfileOut( $totalProf ); } return $ret; } /** * The DBMS-dependent part of query() * @param string $sql SQL query. */ function doQuery( $sql ) { $ret = false; try { $ret = $this->mConn->_Execute( $sql ); } catch (exception $e) { //adodb_backtrace($e->gettrace()); } return $ret; } /**#@+ * @param mixed $res A SQL result */ /** * Free a result object */ function freeResult(ADORecordset $res ) { $res->Free(); } /** * Fetch the next row from the given result object, in object form */ function fetchObject(ADORecordSet $res ) { if ($this->limitOffset != 0) { $res->Move($this->limitOffset); $this->limitOffset = 0; } $row = $res->FetchNextObj(false); if( $this->lastErrno() ) { throw new DBUnexpectedError( $this, 'Error in fetchObject(): ' . htmlspecialchars( $this->lastError() ) ); } return $row; } function fetchNextObject(ADORecordSet $res ) { return $this->fetchObject( $res ); } /** * Fetch the next row from the given result object * Returns an array */ function fetchRow(ADORecordSet $res ) { if ($this->limitOffset != 0) { $res->Move($this->limitOffset); $this->limitOffset = 0; } $row = $res->FetchRow(); if ( $this->lastErrno() ) { throw new DBUnexpectedError( $this, 'Error in fetchRow(): ' . htmlspecialchars( $this->lastError() ) ); } return $row; } /** * Get the number of rows in a result object * Lies about the number of row if there was an offset! */ function numRows(ADORecordSet $res ) { $retVal = $res->NumRows(); if ( $this->limitOffset != 0) { return $retVal - $this->limitOffset; } else { return $retVal; } } /** * Get the number of fields in a result object * See documentation for mssql_num_fields() */ function numFields(ADORecordSet $res ) { return $res->FieldCount(); } /** * Get a field name in a result object * See documentation for mssql_field_name(): * http://www.php.net/mssql_field_name */ function fieldName(ADORecordSet $res, $n ) { $fldObj = $res->FetchField($n); return $fldObj->name; } /** * Get the inserted value of an auto-increment row * * The value inserted should be fetched from nextSequenceValue() * * Example: * $id = $dbw->nextSequenceValue('page_page_id_seq'); * $dbw->insert('page',array('page_id' => $id)); * $id = $dbw->insertId(); */ function insertId() { return $this->mConn->Insert_ID(); } /** * Change the position of the cursor in a result object * See mssql_data_seek() */ function dataSeek(ADORecordSet $res, $row ) { return $res->Move($row); } /** * Get the last error number * See mssql_errno() */ function lastErrno() { if ( $this->mConn ) { return $this->mConn->ErrorNo(); } else { return -1; } } /** * Get a description of the last error * See mssql_error() for more details */ function lastError() { if ( $this->mConn ) { # Even if it's non-zero, it can still be invalid wfSuppressWarnings(); $error = $this->mConn->ErrorMsg(); wfRestoreWarnings(); } else { $error = "No Connection"; } if( $error ) { $error = $error[2] . ' (' . $this->mServer . ')'; } return $error; } /** * Get the number of rows affected by the last write query * See mssql_affected_rows() for more details */ function affectedRows() { return $this->mConn->Affected_Rows(); } /**#@-*/ // end of template : @param $result /** * Simple SELECT wrapper, returns a single field, input must be encoded * Usually aborts on failure * If errors are explicitly ignored, returns FALSE on failure */ function selectField( $table, $var, $cond='', $fname = 'Database::selectField', $options = array() ) { if ( !is_array( $options ) ) { $options = array( $options ); } #$options['LIMIT'] = 1; #$this->setFetchModeAssoc(); $res = $this->select( $table, $var, $cond, $fname, $options ); #$this->setFetchModeNum(); if ( $res === false || !$this->numRows( $res ) ) { return false; } $row = $this->fetchRow( $res ); if ( $row !== false ) { $this->freeResult( $res ); return $row[0]; } else { return false; } } /** * Returns an optional USE INDEX clause to go after the table, and a * string to go at the end of the query * * @private * * @param array $options an associative array of options to be turned into * an SQL query, valid keys are listed in the function. * @return array */ //function makeSelectOptions( $options ) { // $tailOpts = ''; // $startOpts = ''; // // $noKeyOptions = array(); // foreach ( $options as $key => $option ) { // if ( is_numeric( $key ) ) { // $noKeyOptions[$option] = true; // } // } // // if ( isset( $options['GROUP BY'] ) ) $tailOpts .= " GROUP BY {$options['GROUP BY']}"; // if ( isset( $options['ORDER BY'] ) ) $tailOpts .= " ORDER BY {$options['ORDER BY']}"; // // if (isset($options['LIMIT'])) { // $startOpts .= $this->limitResult('', $options['LIMIT'], // isset($options['OFFSET']) ? $options['OFFSET'] : false); // } // // //if ( isset( $noKeyOptions['FOR UPDATE'] ) ) $tailOpts .= ' FOR UPDATE'; // //if ( isset( $noKeyOptions['LOCK IN SHARE MODE'] ) ) $tailOpts .= ' LOCK IN SHARE MODE'; // if ( isset( $noKeyOptions['DISTINCT'] ) && isset( $noKeyOptions['DISTINCTROW'] ) ) $startOpts .= 'DISTINCT'; // // # Various MySQL extensions // #if ( isset( $noKeyOptions['STRAIGHT_JOIN'] ) ) $startOpts .= ' /*! STRAIGHT_JOIN */'; // #if ( isset( $noKeyOptions['HIGH_PRIORITY'] ) ) $startOpts .= ' HIGH_PRIORITY'; // #if ( isset( $noKeyOptions['SQL_BIG_RESULT'] ) ) $startOpts .= ' SQL_BIG_RESULT'; // #if ( isset( $noKeyOptions['SQL_BUFFER_RESULT'] ) ) $startOpts .= ' SQL_BUFFER_RESULT'; // #if ( isset( $noKeyOptions['SQL_SMALL_RESULT'] ) ) $startOpts .= ' SQL_SMALL_RESULT'; // #if ( isset( $noKeyOptions['SQL_CALC_FOUND_ROWS'] ) ) $startOpts .= ' SQL_CALC_FOUND_ROWS'; // #if ( isset( $noKeyOptions['SQL_CACHE'] ) ) $startOpts .= ' SQL_CACHE'; // #if ( isset( $noKeyOptions['SQL_NO_CACHE'] ) ) $startOpts .= ' SQL_NO_CACHE'; // # // #if ( isset( $options['USE INDEX'] ) && ! is_array( $options['USE INDEX'] ) ) { // # $useIndex = $this->useIndexClause( $options['USE INDEX'] ); // #} else { // # $useIndex = ''; // #} // $useIndex = ''; // return array( $startOpts, $useIndex, $tailOpts ); //} function makeSelectOptions( $options ) { $preLimitTail = $postLimitTail = ''; $startOpts = ''; $noKeyOptions = array(); foreach ( $options as $key => $option ) { if ( is_numeric( $key ) ) { $noKeyOptions[$option] = true; } } if ( isset( $options['GROUP BY'] ) ) $preLimitTail .= " GROUP BY {$options['GROUP BY']}"; if ( isset( $options['HAVING'] ) ) $preLimitTail .= " HAVING {$options['HAVING']}"; if ( isset( $options['ORDER BY'] ) ) $preLimitTail .= " ORDER BY {$options['ORDER BY']}"; //if (isset($options['LIMIT'])) { // $tailOpts .= $this->limitResult('', $options['LIMIT'], // isset($options['OFFSET']) ? $options['OFFSET'] // : false); //} //if ( isset( $noKeyOptions['FOR UPDATE'] ) ) $postLimitTail .= ' FOR UPDATE'; //if ( isset( $noKeyOptions['LOCK IN SHARE MODE'] ) ) $postLimitTail .= ' LOCK IN SHARE MODE'; if ( isset( $noKeyOptions['DISTINCT'] ) || isset( $noKeyOptions['DISTINCTROW'] ) ) $startOpts .= 'DISTINCT'; # Various MySQL extensions if ( isset( $noKeyOptions['STRAIGHT_JOIN'] ) ) $startOpts .= ' /*! STRAIGHT_JOIN */'; //if ( isset( $noKeyOptions['HIGH_PRIORITY'] ) ) $startOpts .= ' HIGH_PRIORITY'; //if ( isset( $noKeyOptions['SQL_BIG_RESULT'] ) ) $startOpts .= ' SQL_BIG_RESULT'; //if ( isset( $noKeyOptions['SQL_BUFFER_RESULT'] ) ) $startOpts .= ' SQL_BUFFER_RESULT'; //if ( isset( $noKeyOptions['SQL_SMALL_RESULT'] ) ) $startOpts .= ' SQL_SMALL_RESULT'; //if ( isset( $noKeyOptions['SQL_CALC_FOUND_ROWS'] ) ) $startOpts .= ' SQL_CALC_FOUND_ROWS'; //if ( isset( $noKeyOptions['SQL_CACHE'] ) ) $startOpts .= ' SQL_CACHE'; //if ( isset( $noKeyOptions['SQL_NO_CACHE'] ) ) $startOpts .= ' SQL_NO_CACHE'; if ( isset( $options['USE INDEX'] ) && ! is_array( $options['USE INDEX'] ) ) { $useIndex = $this->useIndexClause( $options['USE INDEX'] ); } else { $useIndex = ''; } return array( $startOpts, $useIndex, $preLimitTail, $postLimitTail ); } /** * SELECT wrapper * * @param mixed $table Array or string, table name(s) (prefix auto-added) * @param mixed $vars Array or string, field name(s) to be retrieved * @param mixed $conds Array or string, condition(s) for WHERE * @param string $fname Calling function name (use __METHOD__) for logs/profiling * @param array $options Associative array of options (e.g. array('GROUP BY' => 'page_title')), * see Database::makeSelectOptions code for list of supported stuff * @return mixed Database result resource (feed to Database::fetchObject or whatever), or false on failure */ function select( $table, $vars, $conds='', $fname = 'Database::select', $options = array(), $join_conds = array() ) { if( is_array( $vars ) ) { $vars = implode( ',', $vars ); } if( !is_array( $options ) ) { $options = array( $options ); } if( is_array( $table ) ) { if ( !empty($join_conds) || isset( $options['USE INDEX'] ) && is_array( @$options['USE INDEX'] ) ) $from = ' FROM ' . $this->tableNamesWithUseIndexOrJoin( $table, $options['USE INDEX'], $join_conds ); else $from = ' FROM ' . implode( ',', array_map( array( &$this, 'tableName' ), $table ) ); } elseif ($table!='') { //if ( strpos($table, '[') < strpos($table, ']') ) { // // This will handle clauses like: [user] LEFT JOIN [user_groups] ON user_id=ug_user // $from = ' FROM ' . $table ; //} else { // $from = ' FROM ' . $this->tableName( $table ); //} // From DatabaseMssql if ($table{0}==' ') { $from = ' FROM ' . $table; } else { $from = ' FROM ' . $this->tableName( $table ); } } else { $from = ''; } //list( $startOpts, $useIndex, $tailOpts ) = $this->makeSelectOptions( $options ); // //if( !empty( $conds ) ) { // if ( is_array( $conds ) ) { // $conds = $this->makeList( $conds, LIST_AND ); // } // $sql = "SELECT $startOpts $vars $from $useIndex WHERE $conds $tailOpts"; //} else { // $sql = "SELECT $startOpts $vars $from $useIndex $tailOpts"; //} //// Take out the LIMIT clause //#echo "SQL IS " . $sql; //$sql = preg_replace('/LIMIT\s+\d+/i', '', $sql); //return $this->query( $sql, $fname ); list( $startOpts, $useIndex, $preLimitTail, $postLimitTail ) = $this->makeSelectOptions( $options ); if( !empty( $conds ) ) { if ( is_array( $conds ) ) { $conds = $this->makeList( $conds, LIST_AND ); } $sql = "SELECT $startOpts $vars $from $useIndex WHERE $conds $preLimitTail"; } else { $sql = "SELECT $startOpts $vars $from $useIndex $preLimitTail"; } if (isset($options['LIMIT'])) $sql = $this->limitResult($sql, $options['LIMIT'], isset($options['OFFSET']) ? $options['OFFSET'] : false); $sql = "$sql $postLimitTail"; if (isset($options['EXPLAIN'])) { $sql = 'EXPLAIN ' . $sql; } return $this->query( $sql, $fname ); } /** * Single row SELECT wrapper * Aborts or returns FALSE on error * * $vars: the selected variables * $conds: a condition map, terms are ANDed together. * Items with numeric keys are taken to be literal conditions * Takes an array of selected variables, and a condition map, which is ANDed * e.g: selectRow( "page", array( "page_id" ), array( "page_namespace" => * NS_MAIN, "page_title" => "Astronomy" ) ) would return an object where * $obj- >page_id is the ID of the Astronomy article * * @todo migrate documentation to phpdocumentor format */ function selectRow( $table, $vars, $conds, $fname = 'Database::selectRow', $options = array() ) { #$options['LIMIT'] = 1; $res = $this->select( $table, $vars, $conds, $fname, $options ); if ( $res === false ) return false; if ( $res->FieldCount() == 0 ) { $this->freeResult($res); return false; } $obj = $this->fetchObject( $res ); $this->freeResult( $res ); return $obj; } /** * Removes most variables from an SQL query and replaces them with X or N for numbers. * It's only slightly flawed. Don't use for anything important. * * @param string $sql A SQL Query * @static */ static function generalizeSQL( $sql ) { # This does the same as the regexp below would do, but in such a way # as to avoid crashing php on some large strings. # $sql = preg_replace ( "/'([^\\\\']|\\\\.)*'|\"([^\\\\\"]|\\\\.)*\"/", "'X'", $sql); $sql = str_replace ( "\\\\", '', $sql); $sql = str_replace ( "\\'", '', $sql); $sql = str_replace ( "\\\"", '', $sql); $sql = preg_replace ("/'.*'/s", "'X'", $sql); $sql = preg_replace ('/".*"/s', "'X'", $sql); # All newlines, tabs, etc replaced by single space $sql = preg_replace ( '/\s+/', ' ', $sql); # All numbers => N $sql = preg_replace ('/-?[0-9]+/s', 'N', $sql); return $sql; } /** * Determines whether a field exists in a table * Usually aborts on failure * If errors are explicitly ignored, returns NULL on failure */ function fieldExists( $table, $field, $fname = 'Database::fieldExists' ) { #$table = $this->tableName( $table ); $res = $this->query( "SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '" . $table . "' AND column_name = '" . $field . "'", $fname ); if ( !$res ) { return NULL; } $found = false; while ( $row = $this->fetchObject( $res ) ) { if ( $row->COLUMN_NAME == $field ) { $found = true; break; } } return $found; } /** * Determines whether an index exists * Usually aborts on failure * If errors are explicitly ignored, returns NULL on failure */ function indexExists( $table, $index, $fname = 'Database::indexExists' ) { $info = $this->indexInfo( $table, $index, $fname ); if ( is_null( $info ) ) { return NULL; } else { return $info !== false; } } /** * Get information about an index into an object * Returns false if the index does not exist */ function indexInfo( $table, $index, $fname = 'Database::indexInfo' ) { # This does not return the same info as MYSQL would, but that's OK because MediaWiki never uses the # returned value except to check for the existance of indexes. #$table = $this->tableName( $table ); $sql = "sp_helpindex '" .$table . "'"; $res = $this->query( $sql, $fname ); if ( !$res ) { return NULL; } $result = array(); while ( $row = $res->FetchNextObj() ) { if ( $row->index_name == $index ) { $row->Non_unique = !stristr($row->index_description, "unique"); $cols = explode(", ", $row->index_keys); foreach ($cols as $col) { $row->Column_name = trim($col); $result[] = clone $row; } } else if ( $index == 'PRIMARY' && stristr($row->index_description, 'PRIMARY') ) { $row->Non_unique = 0; $cols = explode(", ", $row->index_keys); foreach ($cols as $col) { $row->Column_name = trim($col); $result[] = clone $row; } } } #$this->freeResult($res); return empty($result) ? false : $result; } /** * Query whether a given table exists */ function tableExists( $table ) { $qs = "SELECT COUNT(*) AS numtables FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '" . $table . "'"; $res = $this->mConn->query($qs); $retVal = $res->fields[0]; if( $retVal ) { #$this->freeResult( $res ); return true; } else { return false; } } /** * mssql_fetch_field() wrapper * Returns false if the field doesn't exist * * @param $table * @param $field */ function fieldInfo( $table, $field ) { #$table = $this->tableName( $table ); $res = $this->query( "SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='". $table . "' AND COLUMN_NAME='" . $field . "'" ); $colinfo = $res->FetchObject(true); if (! is_object($colinfo) ) return false; // We don't need any particular kind of object, just one with the fields the caller might expect. $retValue = new ADOdbMetaData(); $retValue->name = $colinfo->COLUMN_NAME; $retValue->table = $colinfo->TABLE_NAME; $retValue->def = $colinfo->COLUMN_DEFAULT; $retValue->max_length = $colinfo->CHARACTER_MAXIMUM_LENGTH; $retValue->not_null = $colinfo->IS_NULLABLE == "NO"; $retValue->primary_key = 0; // temporary $retValue->unique_key = 0; // temporarynn $retValue->multiple_key = 0; // temporary $retValue->blob = 0; // temporary $retValue->type = $colinfo->DATA_TYPE; $retValue->unsigned = 0; $retValue->zerofill = 0; $vres = $this->query("SELECT * FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_NAME='" . $table . "'"); if ( $vres->NumRows() > 0 ) $res = $this->query( "sp_helpindex '" . $table . "2'"); else $res = $this->query( "sp_helpindex '" . $table . "'" ); while ( $helpInfo = $res->FetchNextObj() ) { $keys = explode(", ", $helpInfo->index_keys); if ( in_array($field, $keys) ) { if ( stristr($helpInfo->index_description, "primary_key") ) { $retValue->primary_key = 1; } if ( stristr($helpInfo->index_description, "unique") ) { $retValue->unique_key = 1; $retValue->Non_unique = 0; } else { $retValue->multiple_key = 1; } } } return $retValue; } /** * mssql_field_type() wrapper */ function fieldType(ADORecordSet $res, $index ) { $fo = $res->FetchField($index); return $fo->type; } /** * INSERT wrapper, inserts an array into a table * * $a may be a single associative array, or an array of these with numeric keys, for * multi-row insert. * * Usually aborts on failure * If errors are explicitly ignored, returns success */ function insert( $table, $a, $fname = 'Database::insert', $options = array() ) { # No rows to insert, easy just return now if ( !count( $a ) ) { return true; } if( !is_array( $options ) ) { $options = array( $options ); } $table = $this->tableName( $table ); if ( isset( $a[0] ) && is_array( $a[0] ) ) { $multi = true; $keys = array_keys( $a[0] ); $values = array_values ( $a[0] ); $record = $a[0]; } else { $multi = false; $keys = array_keys( $a ); $values = array_values( $a ); $record = $a; } // Microsoft SQL Server doesn't allow any options on an INSERT $sql = "INSERT INTO $table (" . implode( ',', $keys ) . ') VALUES ('; $first = true; foreach ( $keys as $row ) { if ( $first ) { $first = false; $sql .= '?'; } else { $sql .= ',?'; } } $sql .= ')'; $ok = false; try { $ok = $this->mConn->Execute($sql, $a); } catch (ADODB_Exception $ex) { if (! in_array('IGNORE', $options) ) throw new DBQueryError( $this, $this->mConn->ErrorMsg(), $this->mConn->ErrorNo(), $sql, $fname ); } return $ok; } /** * Change the current database */ function selectDB( $db ) { $this->mDBname = $db; $cmd = "USE " . $db; return $this->mConn->exec( $cmd ); } /** * Format a table name ready for use in constructing an SQL query * * This does two important things: it brackets table names which as necessary, * and it adds a table prefix if there is one. * * All functions of this object which require a table name call this function * themselves. Pass the canonical name to such functions. This is only needed * when calling query() directly. * * @param string $name database table name */ function tableName( $name ) { global $wgSharedDB; # Skip quoted literals if ( $name{0} != '[' ) { if ( $this->mTablePrefix !== '' && strpos( '.', $name ) === false ) { $name = "{$this->mTablePrefix}$name"; } if ( isset( $wgSharedDB ) && "{$this->mTablePrefix}user" == $name ) { $name = "[$wgSharedDB].[$name]"; } else { # Standard quoting $name = "[$name]"; } } return $name; } /** * @private */ function tableNamesWithUseIndexOrJoin( $tables, $use_index = array(), $join_conds = array() ) { $ret = array(); $retJOIN = array(); $use_index_safe = is_array($use_index) ? $use_index : array(); $join_conds_safe = is_array($join_conds) ? $join_conds : array(); foreach ( $tables as $table ) { // Is there a JOIN and INDEX clause for this table? if ( isset($join_conds_safe[$table]) && isset($use_index_safe[$table]) ) { $tableClause = $join_conds_safe[$table][0] . ' ' . $this->tableName( $table ); $tableClause .= ' ' . $this->useIndexClause( implode( ',', (array)$use_index_safe[$table] ) ); $tableClause .= ' ON (' . $this->makeList((array)$join_conds_safe[$table][1], LIST_AND) . ')'; $retJOIN[] = $tableClause; // Is there an INDEX clause? } else if ( isset($use_index_safe[$table]) ) { $tableClause = $this->tableName( $table ); $tableClause .= ' ' . $this->useIndexClause( implode( ',', (array)$use_index_safe[$table] ) ); $ret[] = $tableClause; // Is there a JOIN clause? } else if ( isset($join_conds_safe[$table]) ) { $tableClause = $join_conds_safe[$table][0] . ' ' . $this->tableName( $table ); $tableClause .= ' ON (' . $this->makeList((array)$join_conds_safe[$table][1], LIST_AND) . ')'; $retJOIN[] = $tableClause; } else { $tableClause = $this->tableName( $table ); $ret[] = $tableClause; } } // We can't separate explicit JOIN clauses with ',', use ' ' for those $straightJoins = !empty($ret) ? implode( ',', $ret ) : ""; $otherJoins = !empty($retJOIN) ? implode( ' ', $retJOIN ) : ""; // Compile our final table clause return implode(' ',array($straightJoins,$otherJoins) ); } /** * Wrapper for addslashes() * @param string $s String to be slashed. * @return string slashed string. */ function strencode( $s ) { $s = $this->mConn->qstr($s, get_magic_quotes_gpc()); #$s = substr($s, 1, -1); // qstr adds quotes, which the caller is presumably going to do again. return $s; } /** * If it's a string, adds quotes and backslashes * Otherwise returns as-is */ function addQuotes( $s ) { if ( is_null( $s ) ) { return 'NULL'; } else if ( is_numeric( $s ) ) { return "'" . $s . "'"; } else { # This will also quote numeric values. This should be harmless, # and protects against weird problems that occur when they really # _are_ strings such as article titles and string->number->string # conversion is not 1:1. return $this->strencode( $s ); } } /** * Escape string for safe LIKE usage */ function escapeLike( $s ) { //$s=$this->strencode( $s ); $s=str_replace(array('%','_'),array('\%','\_'),$s); return $s; } /** * USE INDEX clause * SQL Server doesn't have them and returns "" */ function useIndexClause( $index ) { return ""; } /** * REPLACE query wrapper * PostgreSQL simulates this with a DELETE followed by INSERT * $row is the row to insert, an associative array * $uniqueIndexes is an array of indexes. Each element may be either a * field name or an array of field names * * It may be more efficient to leave off unique indexes which are unlikely to collide. * However if you do this, you run the risk of encountering errors which wouldn't have * occurred in MySQL * * @todo migrate comment to phodocumentor format */ function replace( $table, $uniqueIndexes, $rows, $fname = 'Database::replace' ) { $table = $this->tableName( $table ); # Single row case if ( !is_array( reset( $rows ) ) ) { $rows = array( $rows ); } foreach( $rows as $row ) { # Delete rows which collide if ( $uniqueIndexes ) { $sql = "DELETE FROM $table WHERE "; $first = true; foreach ( $uniqueIndexes as $index ) { if ( $first ) { $first = false; $sql .= "("; } else { $sql .= ') OR ('; } if ( is_array( $index ) ) { $first2 = true; foreach ( $index as $col ) { if ( $first2 ) { $first2 = false; } else { $sql .= ' AND '; } $sql .= $col.'=' . $this->addQuotes( $row[$col] ); } } else { $sql .= $index.'=' . $this->addQuotes( $row[$index] ); } } $sql .= ')'; try { $this->query( $sql, $fname ); } catch (ADODB_Exception $exc) { } } # Now insert the row $sql = "INSERT INTO $table (" . $this->makeList( array_keys( $row ), LIST_NAMES ) .') VALUES (' . $this->makeList( $row, LIST_COMMA ) . ')'; $this->query( $sql, $fname ); } } /** * DELETE where the condition is a join * MySQL does this with a multi-table DELETE syntax, PostgreSQL does it with sub-selects * Still need to check what SQL Server should do. For now, this is the same as the inherited implementation. * * @param array $conds Condition array of field names mapped to variables, ANDed together in the WHERE clause */ function deleteJoin( $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = 'Database::deleteJoin' ) { if ( !$conds ) { throw new DBUnexpectedError( $this, 'Database::deleteJoin() called with empty $conds' ); } $delTable = $this->tableName( $delTable ); $joinTable = $this->tableName( $joinTable ); $sql = "DELETE $delTable FROM $delTable, $joinTable WHERE $delVar=$joinVar "; if ( $conds != '*' ) { $sql .= ' AND ' . $this->makeList( $conds, LIST_AND ); } return $this->query( $sql, $fname ); } /** * @return string Returns the text of the low priority option if it is supported, or a blank string otherwise */ function lowPriorityOption() { return ''; } /** * Construct a LIMIT query with optional offset * This is used for query pages * $sql string SQL query we will append the limit too * $limit integer the SQL limit * $offset integer the SQL offset (default false) */ function limitResult($sql, $limit, $offset=false) { if( !is_numeric($limit) ) { throw new DBUnexpectedError( $this, "Invalid non-numeric limit passed to limitResult()\n" ); } if ( is_numeric($offset) && $offset != 0) { $this->limitOffset = $offset; $limit += $offset; } else { $this->limitOffset = 0; } if ( strpos($sql, "SELECT") === false ) { return "TOP {$limit} " . $sql; } else { return preg_replace("/SELECT/", "SELECT TOP {$limit}", $sql); } } function limitResultForUpdate($sql, $num) { # Can't really do this on Microsoft SQL? return $sql; #return $this->limitResult($sql, $num, 0); } /** * Returns an SQL expression for a simple conditional. * Uses CASE on SQL Server * * @param string $cond SQL expression which will result in a boolean value * @param string $trueVal SQL expression to return if true * @param string $falseVal SQL expression to return if false * @return string SQL fragment */ function conditional( $cond, $trueVal, $falseVal ) { return " CASE WHEN $cond THEN $trueVal ELSE $falseVal END "; } /** * Determines if the last failure was due to a deadlock */ function wasDeadlock() { return $this->lastErrno() == 1213; } /** * Perform a deadlock-prone transaction. * * This function invokes a callback function to perform a set of write * queries. If a deadlock occurs during the processing, the transaction * will be rolled back and the callback function will be called again. * * Usage: * $dbw->deadlockLoop( callback, ... ); * * Extra arguments are passed through to the specified callback function. * * Returns whatever the callback function returned on its successful, * iteration, or false on error, for example if the retry limit was * reached. */ function deadlockLoop() { $myFname = 'Database::deadlockLoop'; $this->begin(); $args = func_get_args(); $function = array_shift( $args ); $oldIgnore = $this->ignoreErrors( true ); $tries = DEADLOCK_TRIES; if ( is_array( $function ) ) { $fname = $function[0]; } else { $fname = $function; } do { $retVal = call_user_func_array( $function, $args ); $error = $this->lastError(); $errno = $this->lastErrno(); $sql = $this->lastQuery(); if ( $errno ) { if ( $this->wasDeadlock() ) { # Retry usleep( mt_rand( DEADLOCK_DELAY_MIN, DEADLOCK_DELAY_MAX ) ); } else { $this->reportQueryError( $error, $errno, $sql, $fname ); } } } while( $this->wasDeadlock() && --$tries > 0 ); $this->ignoreErrors( $oldIgnore ); if ( $tries <= 0 ) { $this->rollback(); $this->reportQueryError( $error, $errno, $sql, $fname ); return false; } else { $this->commit(); return $retVal; } } /** * Do a SELECT MASTER_POS_WAIT() * * @param string $file the binlog file * @param string $pos the binlog position * @param integer $timeout the maximum number of seconds to wait for synchronisation */ function masterPosWait( $file, $pos, $timeout ) { $fname = 'Database::masterPosWait'; wfProfileIn( $fname ); # Commit any open transactions $this->immediateCommit(); # Call doQuery() directly, to avoid opening a transaction if DBO_TRX is set $encFile = $this->strencode( $file ); $sql = "SELECT MASTER_POS_WAIT('$encFile', $pos, $timeout)"; $res = $this->doQuery( $sql ); if ( $res && $row = $this->fetchRow( $res ) ) { $this->freeResult( $res ); wfProfileOut( $fname ); return $row[0]; } else { wfProfileOut( $fname ); return false; } } /** * Get the position of the master from SHOW SLAVE STATUS */ function getSlavePos() { $res = $this->query( 'SHOW SLAVE STATUS', 'Database::getSlavePos' ); $row = $this->fetchObj( $res ); if ( $row ) { return array( $row->Master_Log_File, $row->Read_Master_Log_Pos ); } else { return array( false, false ); } } /** * Get the position of the master from SHOW MASTER STATUS */ function getMasterPos() { $res = $this->query( 'SHOW MASTER STATUS', 'Database::getMasterPos' ); $row = $this->fetchObj( $res ); if ( $row ) { return array( $row->File, $row->Position ); } else { return array( false, false ); } } /** * Begin a transaction, committing any previously open transaction */ function begin( $fname = 'Database::begin' ) { if ( $this->mTrxLevel != 1 ) $this->mConn->BeginTrans(); $this->mTrxLevel = 1; } /** * End a transaction */ function commit( $fname = 'Database::commit' ) { if ($this->mTrxLevel == 1 ) $this->mConn->CommitTrans(); $this->mTrxLevel = 0; } /** * Rollback a transaction */ function rollback( $fname = 'Database::rollback' ) { if ( $this->mTrxLevel == 1 ) $this->mConn->RollbackTrans(); $this->mTrxLevel = 0; } /** * @return string wikitext of a link to the server software's web site */ function getSoftwareLink() { return "[http://adodb.sourceforge.net/ ADOdb Database AbstractionLibrary for PHP and Python]"; } /** * @return string Version information from the database */ function getServerVersion() { $result = $this->mConn->query("SELECT @@VERSION AS version"); return $result->fields[0]; } /** * Ping the server and try to reconnect if it there is no connection */ function ping() { wfDebug( "Tried to call mssql_ping but this is the SQL Server version. Faking it!\n" ); return true; } /** * Get slave lag. * At the moment, this will only work if the DB user has the PROCESS privilege */ function getLag() { return false; // Still need to figure out what to do on SQL Server $res = $this->query( 'SHOW PROCESSLIST' ); # Find slave SQL thread. Assumed to be the second one running, which is a bit # dubious, but unfortunately there's no easy rigorous way while ( $row = $this->FetchNextObj( $res ) ) { /* This should work for most situations - when default db * for thread is not specified, it had no events executed, * and therefore it doesn't know yet how lagged it is. * * Relay log I/O thread does not select databases. */ if ( $row->User == 'system user' && $row->State != 'Waiting for master to send event' && $row->State != 'Connecting to master' && $row->State != 'Queueing master event to the relay log' && $row->State != 'Waiting for master update' && $row->State != 'Requesting binlog dump' ) { # This is it, return the time (except -ve) if ( $row->Time > 0x7fffffff ) { return false; } else { return $row->Time; } } } return false; } /** * Get status information from SHOW STATUS in an associative array */ function getStatus($which="%") { return false; // Still need to figure this out for SQL Server $res = $this->query( "SHOW STATUS LIKE '{$which}'" ); $status = array(); while ( $row = $this->FetchNextObj( $res ) ) { $status[$row->Variable_name] = $row->Value; } return $status; } function encodeBlob($b) { return $this->mConn->BlobEncode($b); } function decodeBlob($b) { return $this->mConn->BlobDecode($b); } /** * Override database's default connection timeout. * May be useful for very long batch queries such as * full-wiki dumps, where a single query reads out * over hours or days. * @param int $timeout in seconds */ public function setTimeout( $timeout ) { //$this->query( "SET net_read_timeout=$timeout" ); //$this->query( "SET net_write_timeout=$timeout" ); } function setup_database() { global $wgVersion, $wgDBmwschema, $wgDBts2schema, $wgDBport; dbsource( "../maintenance/sqlserver/tables.sql", $this); ## Update version information $mwv = $this->addQuotes($wgVersion); $pgv = $this->addQuotes($this->getServerVersion()); $pgu = $this->addQuotes($this->mUser); $mws = $this->addQuotes($wgDBmwschema); $tss = $this->addQuotes($wgDBts2schema); $pgp = $this->addQuotes($wgDBport); $dbn = $this->addQuotes($this->mDBname); $ctype = "UNKNOWN"; //$ctype = odbc_fetch_result($this->doQuery("SHOW lc_ctype"),0,0); $SQL = "UPDATE mediawiki_version SET mw_version=$mwv, pg_version=$pgv, pg_user=$pgu, ". "mw_schema = $mws, ts2_schema = $tss, pg_port=$pgp, pg_dbname=$dbn, ". "ctype = '$ctype' ". "WHERE type = 'Creation'"; $this->query($SQL); ## Avoid the non-standard "REPLACE INTO" syntax $f = fopen( "../maintenance/interwiki.sql", 'r' ); if ($f == false ) { dieout( "