File: replace.php

package info (click to toggle)
moodle 1.6.3-2%2Betch3
  • links: PTS
  • area: main
  • in suites: etch
  • size: 37,172 kB
  • ctags: 51,688
  • sloc: php: 231,916; sql: 5,631; xml: 2,688; sh: 1,185; perl: 638; makefile: 48; pascal: 36
file content (58 lines) | stat: -rw-r--r-- 1,835 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php /// $Id: replace.php,v 1.3 2006/04/09 21:25:30 skodak Exp $
      /// Search and replace strings throughout all texts in the whole database

require_once('../config.php');

$search  = optional_param('search', '', PARAM_RAW);
$replace = optional_param('replace', '', PARAM_RAW);

require_login();

if (!isadmin()) {
    error("Admins only");
}

###################################################################
print_header('Search and replace throughout the whole database', 'Replace text within the whole database');


if (!$search or !$replace or !confirm_sesskey()) {   /// Print a form

    print_simple_box_start('center');
    echo '<div align="center">';
    echo '<form action="replace.php">';
    echo '<input type="hidden" name="sesskey" value="'.$USER->sesskey.'">';
    echo 'Search whole database for: <input type="text" name="search"><br />';
    echo 'Replace with this string: <input type="text" name="replace"><br /></br />';
    echo '<input type="submit" value="Yes, do it now"><br />';
    echo '</form>';
    echo '</div>';
    print_simple_box_end();
    die;
}


if (!$tables = $db->Metatables() ) {    // No tables yet at all.
    error("no tables");
}

print_simple_box_start('center');
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;
            }
        }
    }
}
print_simple_box_end();

print_continue('index.php');

?>