/** * Search and replace strings in the entire database. * Used for xhtml upgrade, but can also be used, for eg, to change links. * This function prints every query * (Adapted from moodle function of same name) * * @param array $replacearray keys = search, values = replacements. */ function db_replace($replacearray) { global $db; /// Turn off time limits, sometimes upgrades can be slow. @set_time_limit(0); @ob_implicit_flush(true); while (@ob_end_flush()) { } if (!($tables = $db->Metatables())) { // No tables yet at all. return false; } foreach ($tables as $table) { if (in_array($table, array('config', 'adodb_logsql'))) { // Don't process these continue; } if ($columns = $db->MetaColumns($table, false)) { $db->debug = true; foreach ($columns as $column => $data) { if (in_array($data->type, array('text', 'mediumtext', 'longtext', 'varchar'))) { // Text stuff only foreach ($replacearray as $s => $r) { $db->execute('UPDATE ' . db_quote_table_placeholders('{' . $table . '}') . ' SET ' . db_quote_identifier($column) . ' = REPLACE(' . db_quote_identifier($column) . ', ?, ?)', array($s, $r)); } } } $db->debug = false; } } }
/** * Reset all database tables to default values. * @static * @return bool true if reset done, false if skipped */ public static function reset_database() { $tables = get_tables_from_xmldb(); $prefix = get_config('dbprefix'); if (!table_exists(new XMLDBTable('config'))) { // not installed yet return false; } if (!($data = self::get_tabledata())) { // not initialised yet return false; } if (!($structure = self::get_tablestructure())) { // not initialised yet return false; } $unmodifiedorempties = self::guess_unmodified_empty_tables(); db_begin(); // Temporary drop current foreign key contraints $foreignkeys = array(); foreach ($tables as $table) { $tablename = $table->getName(); $foreignkeys = array_merge($foreignkeys, get_foreign_keys($tablename)); } // Drop foreign key contraints if (is_mysql()) { foreach ($foreignkeys as $key) { execute_sql('ALTER TABLE ' . db_quote_identifier($key['table']) . ' DROP FOREIGN KEY ' . db_quote_identifier($key['constraintname'])); } } else { foreach ($foreignkeys as $key) { execute_sql('ALTER TABLE ' . db_quote_identifier($key['table']) . ' DROP CONSTRAINT IF EXISTS ' . db_quote_identifier($key['constraintname'])); } } foreach ($tables as $table) { $tablename = $table->getName(); if (isset($unmodifiedorempties[$tablename])) { continue; } if (!isset($data[$tablename])) { continue; } // Empty the table execute_sql('DELETE FROM {' . $tablename . '}'); // Restore the table from the backup file if ($data[$tablename]) { foreach ($data[$tablename] as $record) { insert_record($tablename, $record); if ($tablename == 'usr' && $record->username == 'root' && is_mysql()) { // gratuitous mysql workaround set_field('usr', 'id', 0, 'username', 'root'); execute_sql('ALTER TABLE {usr} AUTO_INCREMENT=1'); } } } } // Re-add foreign key contraints foreach ($foreignkeys as $key) { execute_sql('ALTER TABLE ' . db_quote_identifier($key['table']) . ' ADD CONSTRAINT ' . db_quote_identifier($key['constraintname']) . ' FOREIGN KEY ' . '(' . implode(',', array_map('db_quote_identifier', $key['fields'])) . ')' . ' REFERENCES ' . db_quote_identifier($key['reftable']) . '(' . implode(',', array_map('db_quote_identifier', $key['reffields'])) . ')'); } db_commit(); // reset all next record ids - aka sequences self::reset_all_database_sequences($unmodifiedorempties); return true; }