|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 // This file is part of Moodle - http://moodle.org/ 00003 // 00004 // Moodle is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // Moodle is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00016 00029 define('NO_OUTPUT_BUFFERING', true); 00030 00031 require('../../../config.php'); 00032 require_once($CFG->dirroot.'/course/lib.php'); 00033 require_once($CFG->libdir.'/adminlib.php'); 00034 00035 admin_externalpage_setup('toolmultilangupgrade'); 00036 00037 $go = optional_param('go', 0, PARAM_BOOL); 00038 00039 ################################################################### 00040 echo $OUTPUT->header(); 00041 00042 echo $OUTPUT->heading(get_string('pluginname', 'tool_multilangupgrade')); 00043 00044 $strmultilangupgrade = get_String('multilangupgradeinfo', 'tool_multilangupgrade'); 00045 00046 if (!$go or !data_submitted() or !confirm_sesskey()) { 00047 $optionsyes = array('go'=>1, 'sesskey'=>sesskey()); 00048 echo $OUTPUT->confirm($strmultilangupgrade, new moodle_url('/admin/tool/multilangupgrade/index.php', $optionsyes), new moodle_url('/admin/')); 00049 echo $OUTPUT->footer(); 00050 die; 00051 } 00052 00053 00054 if (!$tables = $DB->get_tables() ) { // No tables yet at all. 00055 print_error('notables', 'debug'); 00056 } 00057 00058 echo $OUTPUT->box_start(); 00059 00061 00062 @set_time_limit(0); 00063 00064 echo '<strong>Progress:</strong>'; 00065 $i = 0; 00066 $skiptables = array('config', 'block_instances', 'sessions'); // we can not process tables with serialised data here 00067 00068 foreach ($tables as $table) { 00069 if (strpos($table,'pma') === 0) { // Not our tables 00070 continue; 00071 } 00072 if (in_array($table, $skiptables)) { // Don't process these 00073 continue; 00074 } 00075 $fulltable = $DB->get_prefix().$table; 00076 if ($columns = $DB->get_columns($table)) { 00077 if (!array_key_exists('id', $columns)) { 00078 continue; // moodle tables have id 00079 } 00080 foreach ($columns as $column => $data) { 00081 if (in_array($data->type, array('text','mediumtext','longtext','varchar'))) { // Text stuff only 00082 // first find candidate records 00083 $sql = "SELECT id, $column FROM $fulltable WHERE $column LIKE '%</lang>%' OR $column LIKE '%<span lang=%'"; 00084 $rs = $DB->get_recordset_sql($sql); 00085 foreach ($rs as $data) { 00086 $text = $data->$column; 00087 $id = $data->id; 00088 if ($i % 600 == 0) { 00089 echo '<br />'; 00090 } 00091 if ($i % 10 == 0) { 00092 echo '.'; 00093 } 00094 $i++; 00095 00096 if (empty($text) or is_numeric($text)) { 00097 continue; // nothing to do 00098 } 00099 00100 $search = '/(<(?:lang|span) lang="[a-zA-Z0-9_-]*".*?>.+?<\/(?:lang|span)>)(\s*<(?:lang|span) lang="[a-zA-Z0-9_-]*".*?>.+?<\/(?:lang|span)>)+/is'; 00101 $newtext = preg_replace_callback($search, 'multilangupgrade_impl', $text); 00102 00103 if (is_null($newtext)) { 00104 continue; // regex error 00105 } 00106 00107 if ($newtext != $text) { 00108 $DB->execute("UPDATE $fulltable SET $column=? WHERE id=?", array($newtext, $id)); 00109 } 00110 } 00111 $rs->close(); 00112 } 00113 } 00114 } 00115 } 00116 00117 // set conversion flag - switches to new plugin automatically 00118 set_config('filter_multilang_converted', 1); 00119 00120 echo $OUTPUT->box_end(); 00121 00123 echo $OUTPUT->notification('Rebuilding course cache...', 'notifysuccess'); 00124 rebuild_course_cache(); 00125 echo $OUTPUT->notification('...finished', 'notifysuccess'); 00126 00127 echo $OUTPUT->continue_button(new moodle_url('/admin/')); 00128 00129 echo $OUTPUT->footer(); 00130 die; 00131 00132 00133 function multilangupgrade_impl($langblock) { 00134 $searchtosplit = '/<(?:lang|span) lang="([a-zA-Z0-9_-]*)".*?>(.+?)<\/(?:lang|span)>/is'; 00135 preg_match_all($searchtosplit, $langblock[0], $rawlanglist); 00136 $return = ''; 00137 foreach ($rawlanglist[1] as $index=>$lang) { 00138 $return .= '<span lang="'.$lang.'" class="multilang">'.$rawlanglist[2][$index].'</span>'; 00139 } 00140 return $return; 00141 } 00142