MDL-20636 Fix upgrade of numerical questions.
[moodle.git] / admin / multilangupgrade.php
blob77c221905dbf2a0e9956b499293a1da0e4ca3640
1 <?php
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();
25 die;
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.
37 @set_time_limit(0);
39 echo '<strong>Progress:</strong>';
40 $i = 0;
41 $skiptables = array('config', 'user_students', 'user_teachers');
43 foreach ($tables as $table) {
44 if (strpos($table,'pma') === 0) { // Not our tables
45 continue;
47 if (in_array($table, $skiptables)) { // Don't process these
48 continue;
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;
62 $id = $data->id;
63 if ($i % 600 == 0) {
64 echo '<br />';
66 if ($i % 10 == 0) {
67 echo '.';
69 $i++;
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));
86 $rs->close();
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();
105 die;
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);
111 $return = '';
112 foreach ($rawlanglist[1] as $index=>$lang) {
113 $return .= '<span lang="'.$lang.'" class="multilang">'.$rawlanglist[2][$index].'</span>';
115 return $return;