2 /// Search and replace strings throughout all texts in the whole database
4 define('NO_OUTPUT_BUFFERING', true);
6 require_once('../config.php');
7 require_once($CFG->dirroot
.'/course/lib.php');
8 require_once($CFG->libdir
.'/adminlib.php');
10 admin_externalpage_setup('multilangupgrade');
12 $go = optional_param('go', 0, PARAM_BOOL
);
14 ###################################################################
15 echo $OUTPUT->header();
17 echo $OUTPUT->heading(get_string('multilangupgrade', 'admin'));
19 $strmultilangupgrade = get_String('multilangupgradeinfo', 'admin');
21 if (!$go or !data_submitted() or !confirm_sesskey()) { /// Print a form
22 $optionsyes = array('go'=>1, 'sesskey'=>sesskey());
23 echo $OUTPUT->confirm($strmultilangupgrade, new moodle_url('multilangupgrade.php', $optionsyes), 'index.php');
24 echo $OUTPUT->footer();
29 if (!$tables = $DB->get_tables() ) { // No tables yet at all.
30 print_error('notables', 'debug');
33 echo $OUTPUT->box_start();
35 /// Turn off time limits, sometimes upgrades can be slow.
39 echo '<strong>Progress:</strong>';
41 $skiptables = array('config', 'user_students', 'user_teachers');
43 foreach ($tables as $table) {
44 if (strpos($table,'pma') === 0) { // Not our tables
47 if (in_array($table, $skiptables)) { // Don't process these
50 $fulltable = $DB->get_prefix().$table;
51 if ($columns = $DB->get_columns($table)) {
52 if (!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 $sql = "SELECT id, $column FROM $fulltable WHERE $column LIKE '%</lang>%' OR $column LIKE '%<span lang=%'";
59 $rs = $DB->get_recordset_sql($sql);
60 foreach ($rs as $data) {
61 $text = $data->$column;
71 if (empty($text) or is_numeric($text)) {
72 continue; // nothing to do
75 $search = '/(<(?:lang|span) lang="[a-zA-Z0-9_-]*".*?>.+?<\/(?:lang|span)>)(\s*<(?:lang|span) lang="[a-zA-Z0-9_-]*".*?>.+?<\/(?:lang|span)>)+/is';
76 $newtext = preg_replace_callback($search, 'multilangupgrade_impl', $text);
78 if (is_null($newtext)) {
79 continue; // regex error
82 if ($newtext != $text) {
83 $DB->execute("UPDATE $fulltable SET $column=? WHERE id=?", array($newtext, $id));
92 // set conversion flag - switches to new plugin automatically
93 set_config('filter_multilang_converted', 1);
95 echo $OUTPUT->box_end();
97 /// Rebuild course cache which might be incorrect now
98 echo $OUTPUT->notification('Rebuilding course cache...', 'notifysuccess');
99 rebuild_course_cache();
100 echo $OUTPUT->notification('...finished', 'notifysuccess');
102 echo $OUTPUT->continue_button('index.php');
104 echo $OUTPUT->footer();
108 function multilangupgrade_impl($langblock) {
109 $searchtosplit = '/<(?:lang|span) lang="([a-zA-Z0-9_-]*)".*?>(.+?)<\/(?:lang|span)>/is';
110 preg_match_all($searchtosplit, $langblock[0], $rawlanglist);
112 foreach ($rawlanglist[1] as $index=>$lang) {
113 $return .= '<span lang="'.$lang.'" class="multilang">'.$rawlanglist[2][$index].'</span>';