From: toyomoyo Date: Mon, 3 Dec 2007 04:46:37 +0000 (+0000) Subject: wrap search/replace in a function so that it could be used in cron X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=8f2469611f2e689b47c583ba146ef47d41cb8e76;p=moodle.git wrap search/replace in a function so that it could be used in cron --- diff --git a/admin/replace.php b/admin/replace.php index b47d26a3c7..064b024c7a 100644 --- a/admin/replace.php +++ b/admin/replace.php @@ -32,32 +32,10 @@ if (!data_submitted() or !$search or !$replace or !confirm_sesskey()) { /// Pr die; } - -if (!$tables = $db->Metatables() ) { // No tables yet at all. - error("no tables"); -} - print_simple_box_start('center'); -/// Turn off time limits, sometimes upgrades can be slow. - -@set_time_limit(0); -@ob_implicit_flush(true); -while(@ob_end_flush()); - -foreach ($tables as $table) { - if (in_array($table, array($CFG->prefix.'config'))) { // Don't process these - continue; - } - if ($columns = $db->MetaColumns($table, false)) { - foreach ($columns as $column => $data) { - if (in_array($data->type, array('text','mediumtext','longtext','varchar'))) { // Text stuff only - $db->debug = true; - execute_sql("UPDATE $table SET $column = REPLACE($column, '$search', '$replace');"); - $db->debug = false; - } - } - } +if (!db_replace($search, $replace)) { + error('An error has occured during this process'); } print_simple_box_end(); diff --git a/lib/adminlib.php b/lib/adminlib.php index 360228d37b..5f45718dff 100644 --- a/lib/adminlib.php +++ b/lib/adminlib.php @@ -3222,4 +3222,43 @@ function any_new_admin_settings(&$node) { } + +/** + * Moved from admin/replace.php so that we can use this in cron + * @param string $search - string to look for + * @param string $replace - string to replace + * @return bool - success or fail + */ +function db_replace($search, $replace) { + + global $db, $CFG; + + /// 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($CFG->prefix.'config'))) { // Don't process these + continue; + } + + if ($columns = $db->MetaColumns($table, false)) { + foreach ($columns as $column => $data) { + if (in_array($data->type, array('text','mediumtext','longtext','varchar'))) { // Text stuff only + $db->debug = true; + execute_sql("UPDATE $table SET $column = REPLACE($column, '$search', '$replace');"); + $db->debug = false; + } + } + } + } + + return true; +} + ?>