file SafeParam.php was added on branch MOODLE_19_STABLE on 2010-05-21 11:39:40 +0000
[moodle.git] / admin / multilangupgrade.php
blob1ab04d54170e43817e83ae2fb9077fd72c8e406d
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');
8 admin_externalpage_setup('multilangupgrade');
10 $go = optional_param('go', 0, PARAM_BOOL);
12 ###################################################################
13 admin_externalpage_print_header();
15 print_heading(get_string('multilangupgrade', 'admin'));
17 $strmultilangupgrade = get_String('multilangupgradeinfo', 'admin');
19 if (!$go or !data_submitted() or !confirm_sesskey()) { /// Print a form
20 $optionsyes = array('go'=>1, 'sesskey'=>sesskey());
21 notice_yesno($strmultilangupgrade, 'multilangupgrade.php', 'index.php', $optionsyes, null, 'post', 'get');
22 admin_externalpage_print_footer();
23 die;
27 if (!$tables = $db->Metatables() ) { // No tables yet at all.
28 error("no tables");
31 print_simple_box_start('center');
33 /// Turn off time limits, sometimes upgrades can be slow.
35 @set_time_limit(0);
36 @ob_implicit_flush(true);
37 while(@ob_end_flush());
39 echo '<strong>Progress:</strong>';
40 $i = 0;
41 $skiptables = array($CFG->prefix.'config', $CFG->prefix.'user_students', $CFG->prefix.'user_teachers');//, $CFG->prefix.'sessions2');
43 foreach ($tables as $table) {
44 if (($CFG->prefix && strpos($table, $CFG->prefix) !== 0)
45 or strpos($table, $CFG->prefix.'pma') === 0) { // Not our tables
46 continue;
48 if (in_array($table, $skiptables)) { // Don't process these
49 continue;
51 if ($columns = $db->MetaColumns($table, false)) {
52 if (!array_key_exists('id', $columns) and !array_key_exists('ID', $columns)) {
53 continue; // moodle tables have id
55 foreach ($columns as $column => $data) {
56 if (in_array($data->type, array('text','mediumtext','longtext','varchar'))) { // Text stuff only
57 // first find candidate records
58 $rs = get_recordset_sql("SELECT id, $column FROM $table WHERE $column LIKE '%</lang>%' OR $column LIKE '%<span lang=%'");
59 if ($rs) {
60 while (!$rs->EOF) {
61 $text = $rs->fields[$column];
62 $id = $rs->fields['id'];
64 if ($i % 600 == 0) {
65 echo '<br />';
67 if ($i % 10 == 0) {
68 echo '.';
70 $i++;
71 $rs->MoveNext();
73 if (empty($text) or is_numeric($text)) {
74 continue; // nothing to do
77 $search = '/(<(?:lang|span) lang="[a-zA-Z0-9_-]*".*?>.+?<\/(?:lang|span)>)(\s*<(?:lang|span) lang="[a-zA-Z0-9_-]*".*?>.+?<\/(?:lang|span)>)+/is';
78 $newtext = preg_replace_callback($search, 'multilangupgrade_impl', $text);
80 if (is_null($newtext)) {
81 continue; // regex error
84 if ($newtext != $text) {
85 $newtext = addslashes($newtext);
86 execute_sql("UPDATE $table SET $column='$newtext' WHERE id=$id", false);
89 rs_close($rs);
96 // set conversion flag - switches to new plugin automatically
97 set_config('filter_multilang_converted', 1);
99 print_simple_box_end();
101 /// Rebuild course cache which might be incorrect now
102 notify('Rebuilding course cache...', 'notifysuccess');
103 rebuild_course_cache();
104 notify('...finished', 'notifysuccess');
106 print_continue('index.php');
108 admin_externalpage_print_footer();
109 die;
112 function multilangupgrade_impl($langblock) {
113 $searchtosplit = '/<(?:lang|span) lang="([a-zA-Z0-9_-]*)".*?>(.+?)<\/(?:lang|span)>/is';
114 preg_match_all($searchtosplit, $langblock[0], $rawlanglist);
115 $return = '';
116 foreach ($rawlanglist[1] as $index=>$lang) {
117 $return .= '<span lang="'.$lang.'" class="multilang">'.$rawlanglist[2][$index].'</span>';
119 return $return;