Skip to content

Commit

Permalink
MDL-55197 filter_multilang: move preg_callback fn into class
Browse files Browse the repository at this point in the history
  • Loading branch information
timhunt authored and junpataleta committed May 23, 2019
1 parent c5b884b commit d2680db
Showing 1 changed file with 43 additions and 35 deletions.
78 changes: 43 additions & 35 deletions filter/multilang/filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@
// 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>


/**
* @copyright Gaetan Frenoy <gaetan@frenoy.net>
* @copyright 2004 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class filter_multilang extends moodle_text_filter {
function filter($text, array $options = array()) {
global $CFG;
Expand All @@ -59,52 +65,54 @@ function filter($text, array $options = array()) {
$search = '/(<(?:lang|span) lang="[a-zA-Z0-9_-]*".*?>.*?<\/(?:lang|span)>)(\s*<(?:lang|span) lang="[a-zA-Z0-9_-]*".*?>.*?<\/(?:lang|span)>)+/is';
}

$result = preg_replace_callback($search, 'filter_multilang_impl', $text);
$result = preg_replace_callback($search, [$this, 'process_match'], $text);

if (is_null($result)) {
return $text; //error during regex processing (too many nested spans?)
} else {
return $result;
}
}
}

function filter_multilang_impl($langblock) {
global $CFG;

$mylang = current_language();
static $parentcache;
if (!isset($parentcache)) {
$parentcache = array();
}
if (!array_key_exists($mylang, $parentcache)) {
$parentlang = get_parent_language($mylang);
$parentcache[$mylang] = $parentlang;
} else {
$parentlang = $parentcache[$mylang];
}
/**
* This is the callback used by the preg_replace_callback call above.
*
* @param array $langblock one of the matches from the regex match.
* @return string the replacement string (one of the possible translations).
*/
protected function process_match(array $langblock): string {
$mylang = current_language();
static $parentcache;
if (!isset($parentcache)) {
$parentcache = array();
}
if (!array_key_exists($mylang, $parentcache)) {
$parentlang = get_parent_language($mylang);
$parentcache[$mylang] = $parentlang;
} else {
$parentlang = $parentcache[$mylang];
}

$searchtosplit = '/<(?:lang|span)[^>]+lang="([a-zA-Z0-9_-]+)"[^>]*>(.*?)<\/(?:lang|span)>/is';
$searchtosplit = '/<(?:lang|span)[^>]+lang="([a-zA-Z0-9_-]+)"[^>]*>(.*?)<\/(?:lang|span)>/is';

if (!preg_match_all($searchtosplit, $langblock[0], $rawlanglist)) {
//skip malformed blocks
return $langblock[0];
}
if (!preg_match_all($searchtosplit, $langblock[0], $rawlanglist)) {
//skip malformed blocks
return $langblock[0];
}

$langlist = array();
foreach ($rawlanglist[1] as $index=>$lang) {
$lang = str_replace('-','_',strtolower($lang)); // normalize languages
$langlist[$lang] = $rawlanglist[2][$index];
}
$langlist = array();
foreach ($rawlanglist[1] as $index=>$lang) {
$lang = str_replace('-','_',strtolower($lang)); // normalize languages
$langlist[$lang] = $rawlanglist[2][$index];
}

if (array_key_exists($mylang, $langlist)) {
return $langlist[$mylang];
} else if (array_key_exists($parentlang, $langlist)) {
return $langlist[$parentlang];
} else {
$first = array_shift($langlist);
return $first;
if (array_key_exists($mylang, $langlist)) {
return $langlist[$mylang];
} else if (array_key_exists($parentlang, $langlist)) {
return $langlist[$parentlang];
} else {
$first = array_shift($langlist);
return $first;
}
}
}


0 comments on commit d2680db

Please sign in to comment.