Skip to content

Commit

Permalink
MDL-7887 Multilang upgrade to new tags in Moodle 1.8
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Dec 12, 2006
1 parent 9016b06 commit f16242c
Show file tree
Hide file tree
Showing 8 changed files with 185 additions and 14 deletions.
8 changes: 7 additions & 1 deletion admin/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@
'guestloginbutton' => 1,
'style' => 'default',
'template' => 'default',
'theme' => 'standardwhite'));
'theme' => 'standardwhite',
'filter_multilang_converted' => 1));

notify($strdatabasesuccess, "green");
} else {
Expand Down Expand Up @@ -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%');
Expand Down
112 changes: 112 additions & 0 deletions admin/multilangupgrade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php /// $Id$
/// Search and replace strings throughout all texts in the whole database

require_once('../config.php');
require_once($CFG->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 '<strong>Progress:</strong>';
$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 '%</lang>%' OR $column LIKE '%<span lang=%'");
if ($rs and $rs->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 '<br />';
}
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 .= '<span lang="'.$lang.'" class="multilang">'.$rawlanglist[2][$index].'</span>';
}
return $return;
}
?>
1 change: 1 addition & 0 deletions admin/settings/misc.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
$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)));

?>
10 changes: 7 additions & 3 deletions filter/multilang/README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ To Install it:
To Use it:
- Create your contents in multiple languages.
- Enclose every language content between:
<span lang="XX">your_content_here</span>
<span lang="XX" class="multilang">your_content_here</span><span lang="YY" class="multilang">your_content_other_lang</span>
- Test it (by changing your language).

How it works:
Expand All @@ -23,9 +23,9 @@ Definition of "lang block":

One example in action:
- This text:
<span lang="en">Hello!</span><span lang="es">Hola!</span>
<span lang="en" class="multilang">Hello!</span><span lang="es" class="multilang">Hola!</span>
This text is common for every language because it's out from any lang block.
<span lang="en">Bye!</span><span lang="it">Ciao!</span>
<span lang="en" class="multilang">Bye!</span><span lang="it" class="multilang">Ciao!</span>

- Will print, if current language is English:
Hello!
Expand All @@ -41,3 +41,7 @@ One example in action:
Ciao, Eloy :-)
[email protected]
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
14 changes: 14 additions & 0 deletions filter/multilang/defaultsettings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
// defaultsettings.php
// deafault settings are done here, saves doing all this twice in
// both the rendering routine and the config screen

if (!isset($forcereset)) {
$forcereset = false;
}

if (!isset($CFG->filter_multilang_force_old) or $forcereset) {
set_config('filter_multilang_force_old', 0);
}

?>
36 changes: 26 additions & 10 deletions filter/multilang/filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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:
// <span lang="XX">
// - 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:
// <span lang="XX" class="multilang">one lang</span><span lang="YY" class="multilang">another language</span>

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 lang="[a-zA-Z0-9_-]+" class="multilang">.+?<\/span>)(\s*<span lang="[a-zA-Z0-9_-]+" class="multilang">.+?<\/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)) {
Expand All @@ -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 lang="([a-zA-Z0-9_-]+)" class="multilang">(.+?)<\/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();
Expand Down
14 changes: 14 additions & 0 deletions filter/multilang/filterconfig.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
// check the default settings
require_once "defaultsettings.php";

?>

<table cellpadding="9" cellspacing="0">
<tr valign="top">
<td align="right"><?php print_string('multilangforceold', 'admin'); ?></td>
<td><?php choose_from_menu(array(get_String('no'), get_String('yes')), 'filter_multilang_force_old',
$CFG->filter_multilang_force_old, ''); ?></td>
<td />
</tr>
</table>
4 changes: 4 additions & 0 deletions lang/en_utf8/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,10 @@
$string['messaging'] = 'Enable messaging system';
$string['misc'] = 'Miscellaneous';
$string['modulesecurity'] = 'Module security';
$string['multilangforceold'] = 'Force old multilang syntax: &lt;span&gt; without the class=\"multilang\" and &lt;lang&gt;';
$string['multilangupgrade'] = 'Multilang upgrade';
$string['multilangupgradeinfo'] = 'The multilang filter syntax was changed in 1.8, &lt;lang&gt; tag is not supported any more. <br /><br />Example: &lt;span lang=\"en\" class=\"multilang\">Hello!&lt;/span&gt;&lt;span lang=\"es\" class=\"multilang\">Hola!&lt;/span&gt;<br /><br /><strong>Do you want to upgrade the syntax in all existing texts now?</strong>';
$string['multilangupgradenotice'] = 'Your site is probably using old multilang syntax, <a href=\"multilangupgrade.php\">multilang upgrade</a> 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';
Expand Down

0 comments on commit f16242c

Please sign in to comment.