1<?php 2/** 3 * Deepl Autotranslate Plugin 4 * 5 * @author Jennifer Graul <me@netali.de> 6 */ 7 8if(!defined('DOKU_INC')) die(); 9 10use \dokuwiki\HTTP\DokuHTTPClient; 11use \dokuwiki\plugin\deeplautotranslate\MenuItem; 12 13class action_plugin_deeplautotranslate extends DokuWiki_Action_Plugin { 14 15 // manual mapping of ISO-languages to DeepL-languages to fix inconsistent naming 16 private $langs = [ 17 'bg' => 'BG', 18 'cs' => 'CS', 19 'da' => 'DA', 20 'de' => 'DE', 21 'de-informal' => 'DE', 22 'el' => 'EL', 23 'en' => 'EN-GB', 24 'es' => 'ES', 25 'et' => 'ET', 26 'fi' => 'FI', 27 'fr' => 'FR', 28 'hu' => 'HU', 29 'hu-formal' => 'HU', 30 'it' => 'IT', 31 'ja' => 'JA', 32 'lt' => 'LT', 33 'lv' => 'LV', 34 'nl' => 'NL', 35 'pl' => 'PL', 36 'pt' => 'PT-PT', 37 'ro' => 'RO', 38 'ru' => 'RU', 39 'sk' => 'SK', 40 'sl' => 'SL', 41 'sv' => 'SV', 42 'zh' => 'ZH' 43 ]; 44 45 /** 46 * Register its handlers with the DokuWiki's event controller 47 */ 48 public function register(Doku_Event_Handler $controller) { 49 $controller->register_hook('ACTION_ACT_PREPROCESS','BEFORE', $this, 'preprocess'); 50 $controller->register_hook('COMMON_PAGETPL_LOAD','AFTER', $this, 'autotrans_editor'); 51 $controller->register_hook('MENU_ITEMS_ASSEMBLY', 'AFTER', $this, 'add_menu_button'); 52 } 53 54 public function add_menu_button(Doku_Event $event) { 55 global $ID; 56 global $ACT; 57 global $conf; 58 59 if ($ACT != 'show') return; 60 61 if ($event->data['view'] != 'page') return; 62 63 if (!$this->getConf('show_button')) return; 64 65 $split_id = explode(':', $ID); 66 $lang_ns = array_shift($split_id); 67 // check if we are in a language namespace 68 if (array_key_exists($lang_ns, $this->langs)) { 69 if($this->getConf('default_lang_in_ns') and $lang_ns === $conf['lang']) { 70 // if the default lang is in a namespace and we are in that namespace --> check for push translation 71 if (!$this->check_do_push_translate()) return; 72 } else { 73 // in language namespace --> check if we should translate 74 if (!$this->check_do_translation(true)) return; 75 } 76 } else { 77 // do not show the button if we are not in a language namespace and the default language is in a namespace 78 if($this->getConf('default_lang_in_ns')) return; 79 // not in language namespace and default language is npt in a namespace --> check if we should show the push translate button 80 if (!$this->check_do_push_translate()) return; 81 } 82 83 array_splice($event->data['items'], -1, 0, [new MenuItem()]); 84 } 85 86 public function preprocess(Doku_Event $event, $param): void { 87 global $ID; 88 global $conf; 89 90 // check if action is show or translate 91 if ($event->data != 'show' and $event->data != 'translate') return; 92 93 $split_id = explode(':', $ID); 94 $lang_ns = array_shift($split_id); 95 // check if we are in a language namespace 96 if (array_key_exists($lang_ns, $this->langs)) { 97 if($this->getConf('default_lang_in_ns') and $lang_ns === $conf['lang']) { 98 // if the default lang is in a namespace and we are in that namespace --> push translate 99 $this->push_translate($event); 100 } else { 101 // in language namespace --> autotrans direct 102 $this->autotrans_direct($event); 103 } 104 } else { 105 // not in language namespace --> push translate 106 $this->push_translate($event); 107 } 108 } 109 110 private function autotrans_direct(Doku_Event $event): void { 111 global $ID; 112 113 // abort if action is translate and the translate button is disabled 114 if ($event->data == 'translate' and !$this->getConf('show_button')) return; 115 116 // do nothing on show action when mode is not direct 117 if ($event->data == 'show' and $this->get_mode() != 'direct') return; 118 119 // allow translation of existing pages is we are in the translate action 120 $allow_existing = ($event->data == 'translate'); 121 122 // reset action to show 123 $event->data = 'show'; 124 125 if (!$this->check_do_translation($allow_existing)) { 126 send_redirect(wl($ID)); 127 return; 128 } 129 130 $org_page_text = $this->get_org_page_text(); 131 $translated_text = $this->deepl_translate($org_page_text, $this->langs[$this->get_target_lang()]); 132 133 if ($translated_text === '') { 134 send_redirect(wl($ID)); 135 return; 136 } 137 138 saveWikiText($ID, $translated_text, 'Automatic translation'); 139 140 msg($this->getLang('msg_translation_success'), 1); 141 142 // reload the page after translation 143 send_redirect(wl($ID)); 144 } 145 146 public function autotrans_editor(Doku_Event $event, $param): void { 147 if ($this->get_mode() != 'editor') return; 148 149 if (!$this->check_do_translation()) return; 150 151 $org_page_text = $this->get_org_page_text(); 152 153 $event->data['tpl'] = $this->deepl_translate($org_page_text, $this->langs[$this->get_target_lang()]); 154 } 155 156 private function push_translate(Doku_Event $event): void { 157 global $ID; 158 159 // check if action is translate 160 if ($event->data != 'translate') return; 161 162 // check if button is enabled 163 if (!$this->getConf('show_button')) { 164 send_redirect(wl($ID)); 165 return; 166 } 167 168 if (!$this->check_do_push_translate()) { 169 send_redirect(wl($ID)); 170 return; 171 } 172 173 // push translate 174 $push_langs = $this->get_push_langs(); 175 $org_page_text = rawWiki($ID); 176 foreach ($push_langs as $lang) { 177 // skip invalid languages 178 if (!array_key_exists($lang, $this->langs)) { 179 msg($this->getLang('msg_translation_fail_invalid_lang') . $lang, -1); 180 continue; 181 } 182 183 if ($this->getConf('default_lang_in_ns')) { 184 // if default lang is in ns: replace language namespace in ID 185 $split_id = explode(':', $ID); 186 array_shift($split_id); 187 $lang_id = implode(':', $split_id); 188 $lang_id = $lang . ':' . $lang_id; 189 } else { 190 // if default lang is not in ns: add language namespace to ID 191 $lang_id = $lang . ':' . $ID; 192 } 193 194 // check permissions 195 $perm = auth_quickaclcheck($ID); 196 $exists = page_exists($lang_id); 197 if (($exists and $perm < AUTH_EDIT) or (!$exists and $perm < AUTH_CREATE)) { 198 msg($this->getLang('msg_translation_fail_no_permissions') . $lang_id, -1); 199 continue; 200 } 201 202 $translated_text = $this->deepl_translate($org_page_text, $this->langs[$lang]); 203 saveWikiText($lang_id, $translated_text, 'Automatic push translation'); 204 } 205 206 msg($this->getLang('msg_translation_success'), 1); 207 208 // reload the page after translation to clear the action 209 send_redirect(wl($ID)); 210 } 211 212 private function get_mode(): string { 213 global $ID; 214 if ($this->getConf('editor_regex')) { 215 if (preg_match('/' . $this->getConf('editor_regex') . '/', $ID) === 1) return 'editor'; 216 } 217 if ($this->getConf('direct_regex')) { 218 if (preg_match('/' . $this->getConf('direct_regex') . '/', $ID) === 1) return 'direct'; 219 } 220 return $this->getConf('mode'); 221 } 222 223 private function get_target_lang(): string { 224 global $ID; 225 $split_id = explode(':', $ID); 226 return array_shift($split_id); 227 } 228 229 private function get_org_page_text(): string { 230 global $ID; 231 global $conf; 232 233 $split_id = explode(':', $ID); 234 array_shift($split_id); 235 $org_id = implode(':', $split_id); 236 237 // if default lang is in ns: add default ns in front of org id 238 if ($this->getConf('default_lang_in_ns')) { 239 $org_id = $conf['lang'] . ':' . $org_id; 240 } 241 242 return rawWiki($org_id); 243 } 244 245 private function check_do_translation($allow_existing = false): bool { 246 global $INFO; 247 global $ID; 248 global $conf; 249 250 // only translate if the current page does not exist 251 if ($INFO['exists'] and !$allow_existing) return false; 252 253 // permission check 254 $perm = auth_quickaclcheck($ID); 255 if (($INFO['exists'] and $perm < AUTH_EDIT) or (!$INFO['exists'] and $perm < AUTH_CREATE)) return false; 256 257 // skip blacklisted namespaces and pages 258 if ($this->getConf('blacklist_regex')) { 259 if (preg_match('/' . $this->getConf('blacklist_regex') . '/', $ID) === 1) return false; 260 } 261 262 $split_id = explode(':', $ID); 263 $lang_ns = array_shift($split_id); 264 // only translate if the current page is in a language namespace 265 if (!array_key_exists($lang_ns, $this->langs)) return false; 266 267 $org_id = implode(':', $split_id); 268 269 // if default lang is in ns: add default ns in front of org id 270 if ($this->getConf('default_lang_in_ns')) { 271 $org_id = $conf['lang'] . ':' . $org_id; 272 } 273 274 // check if the original page exists 275 if (!page_exists($org_id)) return false; 276 277 return true; 278 } 279 280 private function check_do_push_translate(): bool { 281 global $ID; 282 global $INFO; 283 global $conf; 284 285 if (!$INFO['exists']) return false; 286 287 // if default language is in namespace: only allow push translation from that namespace 288 if($this->getConf('default_lang_in_ns')) { 289 $split_id = explode(':', $ID); 290 $lang_ns = array_shift($split_id); 291 292 if ($lang_ns !== $conf['lang']) return false; 293 } 294 295 $push_langs = $this->get_push_langs(); 296 // push_langs empty --> push_translate disabled --> abort 297 if (empty($push_langs)) return false; 298 299 // skip blacklisted namespaces and pages 300 if ($this->getConf('blacklist_regex')) { 301 // blacklist regex match --> abort 302 if (preg_match('/' . $this->getConf('blacklist_regex') . '/', $ID) === 1) return false; 303 } 304 305 return true; 306 } 307 308 private function deepl_translate($text, $target_lang): string { 309 if (!trim($this->getConf('api_key'))) return ''; 310 311 $text = $this->insert_ignore_tags($text); 312 313 $data = [ 314 'auth_key' => $this->getConf('api_key'), 315 'target_lang' => $target_lang, 316 'tag_handling' => 'xml', 317 'ignore_tags' => 'ignore,php', 318 'text' => $text 319 ]; 320 321 if ($this->getConf('api') == 'free') { 322 $url = 'https://api-free.deepl.com/v2/translate'; 323 } else { 324 $url = 'https://api.deepl.com/v2/translate'; 325 } 326 327 $http = new DokuHTTPClient(); 328 $raw_response = $http->post($url, $data); 329 330 if ($http->status >= 400) { 331 // add error messages 332 switch ($http->status) { 333 case 403: 334 msg($this->getLang('msg_translation_fail_bad_key'), -1); 335 break; 336 case 456: 337 msg($this->getLang('msg_translation_fail_quota_exceeded'), -1); 338 break; 339 default: 340 msg($this->getLang('msg_translation_fail'), -1); 341 break; 342 } 343 344 // if any error occurred return an empty string 345 return ''; 346 } 347 348 $json_response = json_decode($raw_response, true); 349 $translated_text = $json_response['translations'][0]['text']; 350 351 $translated_text = $this->remove_ignore_tags($translated_text); 352 353 return $translated_text; 354 } 355 356 private function get_push_langs(): array { 357 $push_langs = trim($this->getConf('push_langs')); 358 359 if ($push_langs === '') return array(); 360 361 return explode(' ', $push_langs); 362 } 363 364 private function insert_ignore_tags($text): string { 365 $text = str_replace('[[', '<ignore>[[', $text); 366 $text = str_replace('{{', '<ignore>{{', $text); 367 $text = str_replace(']]', ']]</ignore>', $text); 368 $text = str_replace('}}', '}}</ignore>', $text); 369 $text = str_replace("''", "<ignore>''</ignore>", $text); 370 371 $text = preg_replace('/(<file[\s\S]*?>[\s\S]*?<\/file>)/', '<ignore>${1}</ignore>', $text); 372 $text = preg_replace('/(<code[\s\S]*?>[\s\S]*?<\/code>)/', '<ignore>${1}</ignore>', $text); 373 374 $ignored_expressions = explode(':', $this->getConf('ignored_expressions')); 375 376 foreach ($ignored_expressions as $expression) { 377 $text = str_replace($expression, '<ignore>' . $expression . '</ignore>', $text); 378 } 379 380 return $text; 381 } 382 383 private function remove_ignore_tags($text): string { 384 $text = str_replace('<ignore>[[', '[[', $text); 385 $text = str_replace('<ignore>{{', '{{', $text); 386 $text = str_replace(']]</ignore>', ']]', $text); 387 $text = str_replace('}}</ignore>', '}}', $text); 388 $text = str_replace("<ignore>''</ignore>", "''", $text); 389 390 $text = preg_replace('/<ignore>(<file[\s\S]*?>[\s\S]*?<\/file>)<\/ignore>/', '${1}', $text); 391 $text = preg_replace('/<ignore>(<code[\s\S]*?>[\s\S]*?<\/code>)<\/ignore>/', '${1}', $text); 392 393 // restore < and > for example from arrows (-->) in wikitext 394 $text = str_replace('>', '>', $text); 395 $text = str_replace('<', '<', $text); 396 397 $ignored_expressions = explode(':', $this->getConf('ignored_expressions')); 398 399 foreach ($ignored_expressions as $expression) { 400 $text = str_replace('<ignore>' . $expression . '</ignore>', $expression, $text); 401 } 402 403 return $text; 404 } 405} 406 407