MDL-10689:
[moodle-linuxchix.git] / admin / multilangupgrade.php
blob705a80e7c33d7175cc4e79f13abbfd8900d2ad17
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 and $rs->RecordCount() > 0) {
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);
95 // set conversion flag - switches to new plugin automatically
96 set_config('filter_multilang_converted', 1);
98 print_simple_box_end();
100 /// Rebuild course cache which might be incorrect now
101 notify('Rebuilding course cache...', 'notifysuccess');
102 rebuild_course_cache();
103 notify('...finished', 'notifysuccess');
105 print_continue('index.php');
107 admin_externalpage_print_footer();
108 die;
111 function multilangupgrade_impl($langblock) {
112 $searchtosplit = '/<(?:lang|span) lang="([a-zA-Z0-9_-]*)".*?>(.+?)<\/(?:lang|span)>/is';
113 preg_match_all($searchtosplit, $langblock[0], $rawlanglist);
114 $return = '';
115 foreach ($rawlanglist[1] as $index=>$lang) {
116 $return .= '<span lang="'.$lang.'" class="multilang">'.$rawlanglist[2][$index].'</span>';
118 return $return;