Skip to content

Commit

Permalink
MDL-25352 fix multiple issues in admin/replace.php
Browse files Browse the repository at this point in the history
Make sure admins know things can go wrong, MSSQL and Oracle were never working, fix block_html config saving issue, skip a lot more tables because the data there is serialised() or can not be changed in case of config tables, more warnings and checkbox confirmation.
  • Loading branch information
skodak committed Mar 13, 2011
1 parent c096042 commit c3b5e82
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 24 deletions.
37 changes: 16 additions & 21 deletions admin/replace.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,47 +11,42 @@

$search = optional_param('search', '', PARAM_RAW);
$replace = optional_param('replace', '', PARAM_RAW);
$sure = optional_param('sure', 0, PARAM_BOOL);

###################################################################
echo $OUTPUT->header();

echo $OUTPUT->heading('Search and replace text throughout the whole database');

if ($DB->get_dbfamily() !== 'mysql' and $DB->get_dbfamily() !== 'postgres') {
//TODO: add $DB->text_replace() to DML drivers
echo $OUTPUT->notification('Sorry, this feature is implemented only for MySQL and PostgreSQL databases.');
echo $OUTPUT->footer();
die;
}

if (!data_submitted() or !$search or !$replace or !confirm_sesskey()) { /// Print a form
if (!data_submitted() or !$search or !$replace or !confirm_sesskey() or !$sure) { /// Print a form
echo $OUTPUT->notification('This script is not supported, always make complete backup before proceeding!<br />This operations can not be rewerted!');

echo $OUTPUT->box_start();
echo '<div class="mdl-align">';
echo '<form action="replace.php" method="post">';
echo '<form action="replace.php" method="post"><div>';
echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
echo 'Search whole database for: <input type="text" name="search" /><br />';
echo 'Replace with this string: <input type="text" name="replace" /><br />';
echo '<input type="submit" value="Yes, do it now" /><br />';
echo '</form>';
echo '<div><label for="search">Search whole database for: </label><input id="search" type="text" name="search" size="40" /> (usually previous server URL)</div>';
echo '<div><label for="replace">Replace with this string: </label><input type="text" id="replace" name="replace" size="40" /> (usually new server URL)</div>';
echo '<div><label for="sure">I understand the risks of this operation: </label><input type="checkbox" id="sure" name="sure" value="1" /></div>';
echo '<div class="buttons"><input type="submit" class="singlebutton" value="Yes, do it now" /></div>';
echo '</div></form>';
echo '</div>';
echo $OUTPUT->box_end();
echo $OUTPUT->footer();
die;
}

echo $OUTPUT->box_start();

if (!db_replace($search, $replace)) {
print_error('erroroccur', debug);
}

db_replace($search, $replace);
echo $OUTPUT->box_end();

/// Try to replace some well-known serialised contents (html blocks)
echo $OUTPUT->notification('Replacing in html blocks...');
$instances = $DB->get_recordset('block_instances', array('blockname' => 'html'));
foreach ($instances as $instance) {
$blockobject = block_instance('html', $instance);
$blockobject->config->text = str_replace($search, $replace, $blockobject->config->text);
$blockobject->instance_config_commit();
}
$instances->close();

/// Rebuild course cache which might be incorrect now
echo $OUTPUT->notification('Rebuilding course cache...', 'notifysuccess');
rebuild_course_cache();
Expand Down
21 changes: 21 additions & 0 deletions blocks/html/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,24 @@ function block_html_pluginfile($course, $birecord_or_cm, $context, $filearea, $a
session_get_instance()->write_close();
send_stored_file($file, 60*60, 0, $forcedownload);
}

/**
* Perform global search replace such as when migrating site to new URL.
* @param $search
* @param $replace
* @return void
*/
function block_html_global_db_replace($search, $replace) {
global $DB;

$instances = $DB->get_recordset('block_instances', array('blockname' => 'html'));
foreach ($instances as $instance) {
// TODO: intentionally hardcoded until MDL-26800 is fixed
$config = unserialize(base64_decode($instance->configdata));
if (isset($config->text) and is_string($config->text)) {
$config->text = str_replace($search, $replace, $config->text);
$DB->set_field('block_instances', 'configdata', base64_encode(serialize($config)), array('id' => $instance->id));
}
}
$instances->close();
}
36 changes: 33 additions & 3 deletions lib/adminlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -5967,32 +5967,62 @@ function any_new_admin_settings($node) {
* @return bool success or fail
*/
function db_replace($search, $replace) {
global $DB, $CFG, $OUTPUT;

global $DB, $CFG;
// TODO: this is horrible hack, we should do whitelisting and each plugin should be responsible for proper replacing...
$skiptables = array('config', 'config_plugins', 'config_log', 'upgrade_log',
'filter_config', 'sessions', 'events_queue', 'repository_instance_config',
'block_instances', 'block_pinned_old', 'block_instance_old', '');

/// Turn off time limits, sometimes upgrades can be slow.
// Turn off time limits, sometimes upgrades can be slow.
@set_time_limit(0);

if (!$tables = $DB->get_tables() ) { // No tables yet at all.
return false;
}
foreach ($tables as $table) {

if (in_array($table, array('config'))) { // Don't process these
if (in_array($table, $skiptables)) { // Don't process these
continue;
}

if ($columns = $DB->get_columns($table)) {
$DB->set_debug(true);
foreach ($columns as $column => $data) {
if (in_array($data->meta_type, array('C', 'X'))) { // Text stuff only
//TODO: this should be definitively moved to DML driver to do the actual replace, this is not going to work for MSSQL and Oracle...
$DB->execute("UPDATE {".$table."} SET $column = REPLACE($column, ?, ?)", array($search, $replace));
}
}
$DB->set_debug(false);
}
}

// delete modinfo caches
rebuild_course_cache(0, true);

// TODO: we should ask all plugins to do the search&replace, for now let's do only blocks...
$blocks = get_plugin_list('block');
foreach ($blocks as $blockname=>$fullblock) {
if ($blockname === 'NEWBLOCK') { // Someone has unzipped the template, ignore it
continue;
}

if (!is_readable($fullblock.'/lib.php')) {
continue;
}

$function = 'block_'.$blockname.'_global_db_replace';
include_once($fullblock.'/lib.php');
if (!function_exists($function)) {
continue;
}

echo $OUTPUT->notification("Replacing in $blockname blocks...", 'notifysuccess');
$function($search, $replace);
echo $OUTPUT->notification("...finished", 'notifysuccess');
}

return true;
}

Expand Down

0 comments on commit c3b5e82

Please sign in to comment.