MDL-30070 message: Optimised search for users over multiple courses
[moodle.git] / filter / multilang / filter.php
CommitLineData
4317f92f 1<?php
71c235bc 2
35716b86
PS
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * @package filter
20 * @subpackage multilang
21 * @copyright Gaetan Frenoy <gaetan@frenoy.net>
22 * @copyright 2004 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
71c235bc 27
28// Given XML multilinguage text, return relevant text according to
f16242ce 29// current language:
30// - look for multilang blocks in the text.
71c235bc 31// - if there exists texts in the currently active language, print them.
32// - else, if there exists texts in the current parent language, print them.
71c235bc 33// - else, print the first language in the text.
9fecfdfb 34// Please note that English texts are not used as default anymore!
71c235bc 35//
f16242ce 36// This version is based on original multilang filter by Gaetan Frenoy,
37// rewritten by Eloy and skodak.
38//
39// Following new syntax is not compatible with old one:
40// <span lang="XX" class="multilang">one lang</span><span lang="YY" class="multilang">another language</span>
71c235bc 41
35716b86 42class filter_multilang extends moodle_text_filter {
dcfffe30 43 function filter($text, array $options = array()) {
9e3f34d1 44 global $CFG;
f16242ce 45
9e3f34d1 46 // [pj] I don't know about you but I find this new implementation funny :P
47 // [skodak] I was laughing while rewriting it ;-)
48 // [nicolasconnault] Should support inverted attributes: <span class="multilang" lang="en"> (Doesn't work curently)
4317f92f 49 // [skodak] it supports it now, though it is slower - any better idea?
af2fc69d 50
9e3f34d1 51 if (empty($text) or is_numeric($text)) {
52 return $text;
53 }
54
55 if (empty($CFG->filter_multilang_force_old) and !empty($CFG->filter_multilang_converted)) {
56 // new syntax
57 $search = '/(<span(\s+lang="[a-zA-Z0-9_-]+"|\s+class="multilang"){2}\s*>.*?<\/span>)(\s*<span(\s+lang="[a-zA-Z0-9_-]+"|\s+class="multilang"){2}\s*>.*?<\/span>)+/is';
58 } else {
59 // old syntax
60 $search = '/(<(?:lang|span) lang="[a-zA-Z0-9_-]*".*?>.*?<\/(?:lang|span)>)(\s*<(?:lang|span) lang="[a-zA-Z0-9_-]*".*?>.*?<\/(?:lang|span)>)+/is';
61 }
af2fc69d 62
35716b86 63 $result = preg_replace_callback($search, 'filter_multilang_impl', $text);
af2fc69d 64
9e3f34d1 65 if (is_null($result)) {
66 return $text; //error during regex processing (too many nested spans?)
67 } else {
68 return $result;
69 }
af2fc69d 70 }
fde4a094 71}
71c235bc 72
35716b86 73function filter_multilang_impl($langblock) {
f16242ce 74 global $CFG;
75
3a915b06 76 $mylang = current_language();
9fecfdfb 77 static $parentcache;
78 if (!isset($parentcache)) {
79 $parentcache = array();
80 }
81 if (!array_key_exists($mylang, $parentcache)) {
c9042db5 82 $parentlang = get_parent_language($mylang);
9fecfdfb 83 $parentcache[$mylang] = $parentlang;
84 } else {
85 $parentlang = $parentcache[$mylang];
71c235bc 86 }
fde4a094 87
244691e2 88 $searchtosplit = '/<(?:lang|span)[^>]+lang="([a-zA-Z0-9_-]+)"[^>]*>(.*?)<\/(?:lang|span)>/is';
f16242ce 89
244691e2 90 if (!preg_match_all($searchtosplit, $langblock[0], $rawlanglist)) {
91 //skip malformed blocks
92 return $langblock[0];
93 }
9fecfdfb 94
95 $langlist = array();
96 foreach ($rawlanglist[1] as $index=>$lang) {
3a915b06 97 $lang = str_replace('-','_',strtolower($lang)); // normalize languages
9fecfdfb 98 $langlist[$lang] = $rawlanglist[2][$index];
71c235bc 99 }
100
9fecfdfb 101 if (array_key_exists($mylang, $langlist)) {
102 return $langlist[$mylang];
103 } else if (array_key_exists($parentlang, $langlist)) {
104 return $langlist[$parentlang];
105 } else {
106 $first = array_shift($langlist);
107 return $first;
108 }
71c235bc 109}
110
4317f92f 111