-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter.php
More file actions
173 lines (149 loc) · 6.64 KB
/
filter.php
File metadata and controls
173 lines (149 loc) · 6.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* @package filter_automultilang
* @copyright 2023 Tina John <johnt.22.tijo@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
use filter_automultilang\deepltranslate;
use filter_automultilang\writetranslationtodb;
/**
* Implementation of the Moodle filter API for the Automulti-lang filter.
*
* @copyright 2023 Tina John <tina.john@th-luebeck.de>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class filter_automultilang extends moodle_text_filter {
/**
* Calculate contenthash of a given content string
*
* @param string|null $content
* @return string
*/
public static function get_contenthash(?string $content = null): string {
return sha1($content ?? '');
}
public static function replace_images_with_placeholder($text) {
// Define a pattern to match <img src="data:image/..."> tags with base64 images
$pattern = '/<img\s+[^>]*>/i';
// Use preg_match_all to find all matches and their base64 content
preg_match_all($pattern, $text, $matches);
// Create an array to store the base64 content
$base64_images = [];
// Loop through the matches and replace them with placeholders
foreach ($matches[0] as $index => $img_tag) {
// Generate a unique placeholder
$placeholder = '{{ANY_IMAGE_' . $index . '}}';
// Store the base64 content in the array
$base64_images[$placeholder] = $img_tag;
// Replace the img tag with the placeholder in the text
$text = str_replace($img_tag, $placeholder, $text);
}
// Return an array containing the modified text and the array of base64 images
return [$text, $base64_images];
}
public static function restore_images_from_placeholder($text, $base64_images) {
// Loop through the base64 images array and replace placeholders with the original base64 images
foreach ($base64_images as $placeholder => $img_tag) {
$text = str_replace($placeholder, $img_tag, $text);
}
return $text;
}
function filter($text, array $options = array()) {
global $CFG, $DB;
if (empty($text) or is_numeric($text)) {
return $text;
}
if (!class_exists('\file_storage')) {
return $text;
}
$result = $text . "automultilangfilter";
$lang = current_language();
$notranslang = get_config('local_h5ptranslate','notranslationforlang');
if($lang == $notranslang) {
return $text;
}
$texthash = self::get_contenthash($text);
//debugecho " - texthash " . $texthash;
$translation = null;
// get translated text from db
$newRecord = FALSE;
$sql = "SELECT transcontent FROM {filter_automultilang} WHERE texthash = ? AND lang = ?";
$filterRecord = $DB->get_record_sql($sql, [$texthash, $lang]);
if(!$filterRecord) {
// clean up base64 images - will throw 413 Error
// Replace base64 images with placeholders
list($modified_text, $base64_images) = self::replace_images_with_placeholder($text);
//$text = $modified_text;
//tinjohnartprep echo " automultilang- no record found - try to translate onthefly:-".$text."-";
$translationinfo = deepltranslate::transWithDeeplHTML($modified_text, $lang);
if($translationinfo->translationdone) {
// Typo aber WANN SOLL DAS PASSIERT SEIN????
// if($transstringinfo->translationdone) {
$translation = $translationinfo->transstring;
// Restore the base64 images from placeholders
$restored_text = self::restore_images_from_placeholder($translation, $base64_images);
writeTranslationToDB::writeTranslationToDB ($lang, $restored_text, $texthash);
$sql = "SELECT transcontent FROM {filter_automultilang} WHERE texthash = ? AND lang = ?";
$filterRecord = $DB->get_record_sql($sql, [$texthash, $lang]);
if($filterRecord->transcontent) {
$newRecord = TRUE;
}
} else {
// Print the script to send the message to the browser console
//echo '<script>console.error("filter_automultitrans: NO translation returned by DeepL - Not written to db");</script>';
debugging("filter_automultitrans: NO translation returned by DeepL - check API KEy and URL in settings - Not written to db", DEBUG_NORMAL);
}
}
if ($filterRecord || $newRecord) {
//debug
//echo "- load transversion ";
$translation = $filterRecord->transcontent;
} else {
//echo " no translation available";
}
if (is_null($translation)) {
return $text;
} else {
//writeTranslationToDB ($lang, $translation, $hash)
return $translation;
}
}
/**
* Puts some caching around get_parent_language().
*
* Also handle parent == 'en' in a way that works better for us.
*
* @param string $lang a Moodle language code, e.g. 'fr'.
* @return string the parent language.
*/
protected function get_parent_lang(string $lang): string {
static $parentcache;
if (!isset($parentcache)) {
$parentcache = ['en' => ''];
}
if (!isset($parentcache[$lang])) {
$parentcache[$lang] = get_parent_language($lang);
// The standard get_parent_language method returns '' for parent == 'en'.
// That is less helpful for us, so change it back.
if ($parentcache[$lang] === '') {
$parentcache[$lang] = 'en';
}
}
return $parentcache[$lang];
}
}