Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

MDL-25352 fix multiple issues in admin/replace.php

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...
commit c3b5e8290bd231dd80d4562834f6e108a390320d 1 parent c096042
@skodak skodak authored
Showing with 70 additions and 24 deletions.
  1. +16 −21 admin/replace.php
  2. +21 −0 blocks/html/lib.php
  3. +33 −3 lib/adminlib.php
View
37 admin/replace.php
@@ -11,23 +11,32 @@
$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();
@@ -35,23 +44,9 @@
}
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();
View
21 blocks/html/lib.php
@@ -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();
+}
View
36 lib/adminlib.php
@@ -5967,10 +5967,14 @@ 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.
@@ -5978,7 +5982,7 @@ function db_replace($search, $replace) {
}
foreach ($tables as $table) {
- if (in_array($table, array('config'))) { // Don't process these
+ if (in_array($table, $skiptables)) { // Don't process these
continue;
}
@@ -5986,6 +5990,7 @@ function db_replace($search, $replace) {
$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));
}
}
@@ -5993,6 +5998,31 @@ function db_replace($search, $replace) {
}
}
+ // 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;
}
Please sign in to comment.
Something went wrong with that request. Please try again.