MDL-9506 More work in the unit tests
[moodle-pu.git] / admin / replace.php
blob561054f1a2c16366b5a7e4f0560d0b7ad31f4699
1 <?php /// $Id$
2 /// Search and replace strings throughout all texts in the whole database
4 require_once('../config.php');
5 require_once($CFG->dirroot.'/course/lib.php');
6 require_once($CFG->libdir.'/adminlib.php');
7 $adminroot = admin_get_root();
8 admin_externalpage_setup('replace', $adminroot);
10 $search = optional_param('search', '', PARAM_RAW);
11 $replace = optional_param('replace', '', PARAM_RAW);
13 require_login();
15 require_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM, SITEID));
17 ###################################################################
18 admin_externalpage_print_header($adminroot);
20 print_heading('Search and replace text throughout the whole database');
23 if (!data_submitted() or !$search or !$replace or !confirm_sesskey()) { /// Print a form
25 print_simple_box_start('center');
26 echo '<div align="center">';
27 echo '<form action="replace.php" method="post">';
28 echo '<input type="hidden" name="sesskey" value="'.$USER->sesskey.'" />';
29 echo 'Search whole database for: <input type="text" name="search" /><br />';
30 echo 'Replace with this string: <input type="text" name="replace" /><br />';
31 echo '<input type="submit" value="Yes, do it now" /><br />';
32 echo '</form>';
33 echo '</div>';
34 print_simple_box_end();
35 admin_externalpage_print_footer($adminroot);
36 die;
40 if (!$tables = $db->Metatables() ) { // No tables yet at all.
41 error("no tables");
44 print_simple_box_start('center');
46 /// Turn off time limits, sometimes upgrades can be slow.
48 @set_time_limit(0);
49 @ob_implicit_flush(true);
50 while(@ob_end_flush());
52 foreach ($tables as $table) {
53 if (in_array($table, array($CFG->prefix.'config'))) { // Don't process these
54 continue;
56 if ($columns = $db->MetaColumns($table, false)) {
57 foreach ($columns as $column => $data) {
58 if (in_array($data->type, array('text','mediumtext','longtext','varchar'))) { // Text stuff only
59 $db->debug = true;
60 execute_sql("UPDATE $table SET $column = REPLACE($column, '$search', '$replace');");
61 $db->debug = false;
67 print_simple_box_end();
69 /// Rebuild course cache which might be incorrect now
70 notify('Rebuilding course cache...');
71 rebuild_course_cache();
72 notify('...finished');
74 print_continue('index.php');
76 admin_externalpage_print_footer($adminroot);