mnethosturl was never being restored. Detected while checking MDL-9309
[pfb-moodle.git] / admin / multilangupgrade.php
blobfb88a4d04d7e43b9a0ed0561475b678d6450e39f
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('multilangupgrade', $adminroot);
10 $go = optional_param('go', 0, PARAM_BOOL);
12 require_login();
14 require_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM, SITEID));
16 ###################################################################
17 admin_externalpage_print_header($adminroot);
19 print_heading(get_string('multilangupgrade', 'admin'));
21 $strmultilangupgrade = get_String('multilangupgradeinfo', 'admin');
23 if (!$go or !data_submitted() or !confirm_sesskey()) { /// Print a form
24 $optionsyes = array('go'=>1, 'sesskey'=>sesskey());
25 notice_yesno($strmultilangupgrade, 'multilangupgrade.php', 'index.php', $optionsyes, null, 'post', 'get');
26 admin_externalpage_print_footer($adminroot);
27 die;
31 if (!$tables = $db->Metatables() ) { // No tables yet at all.
32 error("no tables");
35 print_simple_box_start('center');
37 /// Turn off time limits, sometimes upgrades can be slow.
39 @set_time_limit(0);
40 @ob_implicit_flush(true);
41 while(@ob_end_flush());
43 echo '<strong>Progress:</strong>';
44 $i = 0;
45 $skiptables = array($CFG->prefix.'config');//, $CFG->prefix.'sessions2');
47 foreach ($tables as $table) {
48 if (($CFG->prefix && strpos($table, $CFG->prefix) !== 0)
49 or strpos($table, $CFG->prefix.'pma') === 0) { // Not our tables
50 continue;
52 if (in_array($table, $skiptables)) { // Don't process these
53 continue;
55 if ($columns = $db->MetaColumns($table, false)) {
56 if (!array_key_exists('id', $columns) and !array_key_exists('ID', $columns)) {
57 continue; // moodle tables have id
59 foreach ($columns as $column => $data) {
60 if (in_array($data->type, array('text','mediumtext','longtext','varchar'))) { // Text stuff only
61 // first find candidate records
62 $rs = get_recordset_sql("SELECT id, $column FROM $table WHERE $column LIKE '%</lang>%' OR $column LIKE '%<span lang=%'");
63 if ($rs and $rs->RecordCount() > 0) {
64 while (!$rs->EOF) {
65 $text = $rs->fields[$column];
66 $search = '/(<(?:lang|span) lang="[a-zA-Z0-9_-]*".*?>.+?<\/(?:lang|span)>)(\s*<(?:lang|span) lang="[a-zA-Z0-9_-]*".*?>.+?<\/(?:lang|span)>)+/is';
67 $newtext = preg_replace_callback($search, 'multilangupgrade_impl', $text);
68 if ($newtext != $text) {
69 $newtext = addslashes($newtext);
70 execute_sql("UPDATE $table SET $column='$newtext' WHERE id=".$rs->fields['id'], false);
72 if ($i % 600 == 0) {
73 echo '<br />';
75 if ($i % 10 == 0) {
76 echo '.';
78 $i++;
79 $rs->MoveNext();
87 // set conversion flag - switches to new plugin automatically
88 set_config('filter_multilang_converted', 1);
90 print_simple_box_end();
92 /// Rebuild course cache which might be incorrect now
93 notify('Rebuilding course cache...', 'notifysuccess');
94 rebuild_course_cache();
95 notify('...finished', 'notifysuccess');
97 print_continue('index.php');
99 admin_externalpage_print_footer($adminroot);
100 die;
103 function multilangupgrade_impl($langblock) {
104 $searchtosplit = '/<(?:lang|span) lang="([a-zA-Z0-9_-]*)".*?>(.+?)<\/(?:lang|span)>/is';
105 preg_match_all($searchtosplit, $langblock[0], $rawlanglist);
106 $return = '';
107 foreach ($rawlanglist[1] as $index=>$lang) {
108 $return .= '<span lang="'.$lang.'" class="multilang">'.$rawlanglist[2][$index].'</span>';
110 return $return;