From f16242ce612c659e8711b9e2695a5a4b40ccf544 Mon Sep 17 00:00:00 2001 From: skodak Date: Tue, 12 Dec 2006 10:39:17 +0000 Subject: [PATCH] MDL-7887 Multilang upgrade to new tags in Moodle 1.8 --- admin/index.php | 8 +- admin/multilangupgrade.php | 112 +++++++++++++++++++++++++++ admin/settings/misc.php | 1 + filter/multilang/README.txt | 10 ++- filter/multilang/defaultsettings.php | 14 ++++ filter/multilang/filter.php | 36 ++++++--- filter/multilang/filterconfig.html | 14 ++++ lang/en_utf8/admin.php | 4 + 8 files changed, 185 insertions(+), 14 deletions(-) create mode 100644 admin/multilangupgrade.php create mode 100644 filter/multilang/defaultsettings.php create mode 100644 filter/multilang/filterconfig.html diff --git a/admin/index.php b/admin/index.php index 070ba8d122..ca9dc7624b 100644 --- a/admin/index.php +++ b/admin/index.php @@ -183,7 +183,8 @@ 'guestloginbutton' => 1, 'style' => 'default', 'template' => 'default', - 'theme' => 'standardwhite')); + 'theme' => 'standardwhite', + 'filter_multilang_converted' => 1)); notify($strdatabasesuccess, "green"); } else { @@ -501,6 +502,11 @@ print_simple_box(get_string('cronwarning', 'admin')." ".$helpbutton, 'center', '60%'); } +/// Print multilang upgrade notice if needed + if (empty($CFG->filter_multilang_converted)) { + print_simple_box(get_string('multilangupgradenotice', 'admin'), 'center', '60%'); + } + /// Alert if we are currently in maintenance mode if (file_exists($CFG->dataroot.'/1/maintenance.html')) { print_simple_box(get_string('sitemaintenancewarning', 'admin') , 'center', '60%'); diff --git a/admin/multilangupgrade.php b/admin/multilangupgrade.php new file mode 100644 index 0000000000..c0e63cb32e --- /dev/null +++ b/admin/multilangupgrade.php @@ -0,0 +1,112 @@ +dirroot.'/course/lib.php'); +require_once($CFG->libdir.'/adminlib.php'); +$adminroot = admin_get_root(); +admin_externalpage_setup('multilangupgrade', $adminroot); + +$go = optional_param('go', 0, PARAM_BOOL); + +require_login(); + +require_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM, SITEID)); + +################################################################### +admin_externalpage_print_header($adminroot); + +print_heading(get_string('multilangupgrade', 'admin')); + +$strmultilangupgrade = get_String('multilangupgradeinfo', 'admin'); + +if (!$go or !data_submitted() or !confirm_sesskey()) { /// Print a form + $optionsyes = array('go'=>1, 'sesskey'=>sesskey()); + notice_yesno($strmultilangupgrade, 'multilangupgrade.php', 'index.php', $optionsyes, null, 'post', 'get'); + admin_externalpage_print_footer($adminroot); + die; +} + + +if (!$tables = $db->Metatables() ) { // No tables yet at all. + error("no tables"); +} + +print_simple_box_start('center'); + +/// Turn off time limits, sometimes upgrades can be slow. + +@set_time_limit(0); +@ob_implicit_flush(true); +while(@ob_end_flush()); + +echo 'Progress:'; +$i = 0; +$skiptables = array($CFG->prefix.'config');//, $CFG->prefix.'sessions2'); + +foreach ($tables as $table) { + if (strpos($table, $CFG->prefix) !== 0 + or strpos($table, $CFG->prefix.'pma') === 0) { // Not our tables + continue; + } + if (in_array($table, $skiptables)) { // Don't process these + continue; + } + if ($columns = $db->MetaColumns($table, false)) { + if (!array_key_exists('id', $columns) and !array_key_exists('ID', $columns)) { + continue; // moodle tables have id + } + foreach ($columns as $column => $data) { + if (in_array($data->type, array('text','mediumtext','longtext','varchar'))) { // Text stuff only + // first find candidate records + $rs = get_recordset_sql("SELECT id, $column FROM $table WHERE $column LIKE '%%' OR $column LIKE '%RecordCount() > 0) { + while (!$rs->EOF) { + $text = $rs->fields[$column]; + $search = '/(<(?:lang|span) lang="[a-zA-Z0-9_-]*".*?>.+?<\/(?:lang|span)>)(\s*<(?:lang|span) lang="[a-zA-Z0-9_-]*".*?>.+?<\/(?:lang|span)>)+/is'; + $newtext = preg_replace_callback($search, 'multilangupgrade_impl', $text); + if ($newtext != $text) { + $newtext = addslashes($newtext); + execute_sql("UPDATE $table SET $column='$newtext' WHERE id=".$rs->fields['id'], false); + } + if ($i % 600 == 0) { + echo '
'; + } + if ($i % 10 == 0) { + echo '.'; + } + $i++; + $rs->MoveNext(); + } + } + } + } + } +} + +// set conversion flag - switches to new plugin automatically +set_config('filter_multilang_converted', 1); + +print_simple_box_end(); + +/// Rebuild course cache which might be incorrect now +notify('Rebuilding course cache...', 'notifysuccess'); +rebuild_course_cache(); +notify('...finished', 'notifysuccess'); + +print_continue('index.php'); + +admin_externalpage_print_footer($adminroot); +die; + + +function multilangupgrade_impl($langblock) { + $searchtosplit = '/<(?:lang|span) lang="([a-zA-Z0-9_-]*)".*?>(.+?)<\/(?:lang|span)>/is'; + preg_match_all($searchtosplit, $langblock[0], $rawlanglist); + $return = ''; + foreach ($rawlanglist[1] as $index=>$lang) { + $return .= ''.$rawlanglist[2][$index].''; + } + return $return; +} +?> diff --git a/admin/settings/misc.php b/admin/settings/misc.php index f14c8e552a..db0a06d9c7 100644 --- a/admin/settings/misc.php +++ b/admin/settings/misc.php @@ -16,5 +16,6 @@ $ADMIN->add('misc', new admin_externalpage('xmldbeditor', get_string('xmldbedito $ADMIN->add('misc', new admin_externalpage('oacleanup', 'Online Assignment Cleanup', $CFG->wwwroot.'/'.$CFG->admin.'/oacleanup.php', 'moodle/site:config', true)); $ADMIN->add('misc', new admin_externalpage('upgradeforumread', 'Upgrade forum', $CFG->wwwroot.'/'.$CFG->admin.'/upgradeforumread.php', 'moodle/site:config', true)); $ADMIN->add('misc', new admin_externalpage('upgradelogs', 'Upgrade logs', $CFG->wwwroot.'/'.$CFG->admin.'/upgradelogs.php', 'moodle/site:config', true)); +$ADMIN->add('misc', new admin_externalpage('multilangupgrade', get_string('multilangupgrade', 'admin'), $CFG->wwwroot.'/'.$CFG->admin.'/multilangupgrade.php', 'moodle/site:config', !empty($CFG->filter_multilang_converted))); ?> diff --git a/filter/multilang/README.txt b/filter/multilang/README.txt index 5e7beffcbc..39ae791fd2 100644 --- a/filter/multilang/README.txt +++ b/filter/multilang/README.txt @@ -6,7 +6,7 @@ To Install it: To Use it: - Create your contents in multiple languages. - Enclose every language content between: - your_content_here + your_content_hereyour_content_other_lang - Test it (by changing your language). How it works: @@ -23,9 +23,9 @@ Definition of "lang block": One example in action: - This text: - Hello!Hola! + Hello!Hola! This text is common for every language because it's out from any lang block. - Bye!Ciao! + Bye!Ciao! - Will print, if current language is English: Hello! @@ -41,3 +41,7 @@ One example in action: Ciao, Eloy :-) stronk7@moodle.org 2005-11-16 + +Syntax was changed in 1.8, the conversion of existing text is done from admin/multilangupgrade.php +Ciao, skodak :-) +2006-12-11 \ No newline at end of file diff --git a/filter/multilang/defaultsettings.php b/filter/multilang/defaultsettings.php new file mode 100644 index 0000000000..ae57b75cd0 --- /dev/null +++ b/filter/multilang/defaultsettings.php @@ -0,0 +1,14 @@ +filter_multilang_force_old) or $forcereset) { + set_config('filter_multilang_force_old', 0); + } + +?> diff --git a/filter/multilang/filter.php b/filter/multilang/filter.php index 8e76ae5249..3fc0b68801 100644 --- a/filter/multilang/filter.php +++ b/filter/multilang/filter.php @@ -23,29 +23,38 @@ /////////////////////////////////////////////////////////////////////////// // Given XML multilinguage text, return relevant text according to -// current language. i.e.= -// - look for lang sections in the code. +// current language: +// - look for multilang blocks in the text. // - if there exists texts in the currently active language, print them. // - else, if there exists texts in the current parent language, print them. // - else, print the first language in the text. // Please note that English texts are not used as default anymore! // -// This is an improved version of the original multilang filter by Gaetan Frenoy. -// It should be 100% compatible with the original one. Some new features are: -// - Supports a new "short" syntax to make things easier. Simply use: -// -// - Needs less resources and executes faster. -// - Allows any type of content to be used. No restrictions at all! +// This version is based on original multilang filter by Gaetan Frenoy, +// rewritten by Eloy and skodak. +// +// Following new syntax is not compatible with old one: +// one langanother language function multilang_filter($courseid, $text) { + global $CFG; // [pj] I don't know about you but I find this new implementation funny :P // [skodak] I was laughing while rewriting it ;-) - $search = '/(<(?:lang|span) lang="[a-zA-Z0-9_-]*".*?>.+?<\/(?:lang|span)>\s*)+/is'; + + if (empty($CFG->filter_multilang_force_old) and !empty($CFG->filter_multilang_converted)) { + // new syntax + $search = '/(.+?<\/span>)(\s*.+?<\/span>)+/is'; + } else { + // old syntax + $search = '/(<(?:lang|span) lang="[a-zA-Z0-9_-]*".*?>.+?<\/(?:lang|span)>)(\s*<(?:lang|span) lang="[a-zA-Z0-9_-]*".*?>.+?<\/(?:lang|span)>)+/is'; + } return preg_replace_callback($search, 'multilang_filter_impl', $text); } function multilang_filter_impl($langblock) { + global $CFG; + $mylang = str_replace('_utf8', '', current_language()); static $parentcache; if (!isset($parentcache)) { @@ -58,7 +67,14 @@ function multilang_filter_impl($langblock) { $parentlang = $parentcache[$mylang]; } - $searchtosplit = '/<(?:lang|span) lang="([a-zA-Z0-9_-]*)".*?>(.+?)<\/(?:lang|span)>/is'; + if (empty($CFG->filter_multilang_force_old) and !empty($CFG->filter_multilang_converted)) { + // new syntax + $searchtosplit = '/(.+?)<\/span>/is'; + } else { + // old syntax + $searchtosplit = '/<(?:lang|span) lang="([a-zA-Z0-9_-]*)".*?>(.+?)<\/(?:lang|span)>/is'; + } + preg_match_all($searchtosplit, $langblock[0], $rawlanglist); $langlist = array(); diff --git a/filter/multilang/filterconfig.html b/filter/multilang/filterconfig.html new file mode 100644 index 0000000000..009ceb800a --- /dev/null +++ b/filter/multilang/filterconfig.html @@ -0,0 +1,14 @@ + + + + + + + +
filter_multilang_force_old, ''); ?> +
diff --git a/lang/en_utf8/admin.php b/lang/en_utf8/admin.php index 1c4a4c4c61..2e45751603 100644 --- a/lang/en_utf8/admin.php +++ b/lang/en_utf8/admin.php @@ -336,6 +336,10 @@ $string['mediapluginwmv'] = 'Enable .wmv filter'; $string['messaging'] = 'Enable messaging system'; $string['misc'] = 'Miscellaneous'; $string['modulesecurity'] = 'Module security'; +$string['multilangforceold'] = 'Force old multilang syntax: <span> without the class=\"multilang\" and <lang>'; +$string['multilangupgrade'] = 'Multilang upgrade'; +$string['multilangupgradeinfo'] = 'The multilang filter syntax was changed in 1.8, <lang> tag is not supported any more.

Example: <span lang=\"en\" class=\"multilang\">Hello!</span><span lang=\"es\" class=\"multilang\">Hola!</span>

Do you want to upgrade the syntax in all existing texts now?'; +$string['multilangupgradenotice'] = 'Your site is probably using old multilang syntax, multilang upgrade is recommended.'; $string['mustenablestats'] = 'Stats have not yet been enabled on this site.'; $string['mymoodle'] = 'My Moodle'; $string['mymoodleredirect'] = 'Force users to use My Moodle'; -- 2.39.5