translater/inc/class.dc.translater.php

454 lines
14 KiB
PHP
Raw Normal View History

2021-08-18 19:42:30 +00:00
<?php
2021-09-02 21:44:01 +00:00
/**
* @brief translater, a plugin for Dotclear 2
2021-11-01 21:32:32 +00:00
*
2021-09-02 21:44:01 +00:00
* @package Dotclear
* @subpackage Plugin
2021-11-01 21:32:32 +00:00
*
2021-09-02 21:44:01 +00:00
* @author Jean-Christian Denis & contributors
2021-11-01 21:32:32 +00:00
*
2021-09-02 21:44:01 +00:00
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
2021-08-18 22:48:47 +00:00
if (!defined('DC_CONTEXT_ADMIN')) {
return;
}
2021-08-18 19:42:30 +00:00
class dcTranslaterDefaultSettings
{
/** @var boolean Show tranlsater button on plugins list */
public $plugin_menu = false;
/** @var boolean Show tranlsater button on themes list */
public $theme_menu = false;
/** @var boolean Create language backup on save */
public $backup_auto = false;
/** @var integer Backups number limit */
public $backup_limit = 20;
/** @var string Backup main folder */
public $backup_folder = 'module';
/** @var string Default ui start page */
public $start_page = '-';
/** @var boolean Write .lang.php file (deprecated) */
public $write_langphp = false;
/** @var boolean SCan also template files for translations */
public $scan_tpl = true;
/** @var boolean Disable translation of know dotclear strings */
public $parse_nodc = true;
/** @var boolean Hide official modules */
public $hide_default = true;
/** @var boolean Add comment to translations files */
public $parse_comment = false;
/** @var boolean Parse user info to translations files */
public $parse_user = false;
/** @var string User infos to parse */
public $parse_userinfo = 'displayname, email';
/** @var boolean Overwrite existing languages on import */
public $import_overwrite = false;
/** @var string Filename of exported lang */
public $export_filename = 'type-module-l10n-timestamp';
/** @var string Default service for external proposal tool */
public $proposal_tool = 'google';
/** @var string Default lang for external proposal tool */
public $proposal_lang = 'en';
/**
* get default settings
*
* @return array Settings key/value pair
*/
public static function getDefaultSettings()
{
return get_class_vars('dcTranslaterDefaultSettings');
}
}
2021-08-18 19:42:30 +00:00
/**
* Translater tools.
*/
class dcTranslater extends dcTranslaterDefaultSettings
2021-08-18 19:42:30 +00:00
{
/** @var array $allowed_backup_folders List of allowed backup folder */
public static $allowed_backup_folders = [];
/** @var array $allowed_l10n_groups List of place of tranlsations */
public static $allowed_l10n_groups = [
2022-11-13 17:40:31 +00:00
'main', 'public', 'theme', 'admin', 'date', 'error',
];
/** @var array $allowed_user_informations List of user info can be parsed */
public static $allowed_user_informations = [
2022-11-13 17:40:31 +00:00
'firstname', 'displayname', 'name', 'email', 'url',
];
/** @var array $default_distrib_modules List of distributed plugins and themes */
public static $default_distrib_modules = ['plugin' => [], 'theme' => []];
2021-09-02 21:44:01 +00:00
2021-09-19 13:31:57 +00:00
/** @var array $modules List of modules we could work on */
private $modules = [];
2021-09-02 21:44:01 +00:00
2021-09-19 13:31:57 +00:00
/**
* translater instance
2021-11-01 21:32:32 +00:00
*
* @param boolean $full Also load modules
2021-09-19 13:31:57 +00:00
*/
2022-11-13 17:40:31 +00:00
public function __construct(bool $full = true)
2021-08-18 22:48:47 +00:00
{
2022-11-13 17:40:31 +00:00
dcCore::app()->blog->settings->addNamespace('translater');
$this->loadSettings();
2021-09-25 15:24:38 +00:00
if ($full) {
$this->loadModules();
}
self::$allowed_backup_folders = [
__('locales folders of each module') => 'module',
__('plugins folder root') => 'plugin',
__('public folder root') => 'public',
__('cache folder of Dotclear') => 'cache',
2022-11-13 17:40:31 +00:00
__('locales folder of translater') => 'translater',
];
self::$default_distrib_modules = [
2021-11-01 21:32:32 +00:00
'plugin' => explode(',', DC_DISTRIB_PLUGINS),
2022-11-13 17:40:31 +00:00
'theme' => explode(',', DC_DISTRIB_THEMES),
];
2021-08-18 22:48:47 +00:00
}
2021-09-02 21:44:01 +00:00
2021-09-19 13:31:57 +00:00
/// @name settings methods
//@{
/**
* Load settings from db
2021-09-19 13:31:57 +00:00
*/
public function loadSettings(): void
2021-08-18 22:48:47 +00:00
{
foreach ($this->getDefaultSettings() as $key => $value) {
2022-11-13 17:40:31 +00:00
$this->$key = dcCore::app()->blog->settings->translater->get('translater_' . $key);
2021-09-02 21:44:01 +00:00
try {
settype($this->$key, gettype($value));
} catch (Exception $e) {
}
}
2021-08-18 22:48:47 +00:00
}
2021-09-02 21:44:01 +00:00
2021-09-19 13:31:57 +00:00
/**
* Write settings to db
2021-11-01 21:32:32 +00:00
*
* @param boolean $overwrite Overwrite existing settings
2021-09-19 13:31:57 +00:00
*/
public function writeSettings($overwrite = true): void
2021-08-18 22:48:47 +00:00
{
foreach ($this->getDefaultSettings() as $key => $value) {
2022-11-13 17:40:31 +00:00
dcCore::app()->blog->settings->translater->drop('translater_' . $key);
dcCore::app()->blog->settings->translater->put('translater_' . $key, $this->$key, gettype($value), '', true, true);
2021-08-18 22:48:47 +00:00
}
}
2021-09-19 13:31:57 +00:00
//@}
/// @name modules methods
//@{
2021-11-01 21:32:32 +00:00
/**
2021-09-19 13:31:57 +00:00
* Load array of modules infos by type of modules
*/
private function loadModules(): void
2021-08-18 22:48:47 +00:00
{
2021-09-19 13:31:57 +00:00
$this->modules['theme'] = $this->modules['plugin'] = [];
2021-09-02 21:44:01 +00:00
2022-11-13 17:40:31 +00:00
$themes = new dcThemes();
$themes->loadModules(dcCore::app()->blog->themes_path, null);
2021-09-02 21:44:01 +00:00
$list = [
'theme' => $themes->getModules(),
2022-11-13 17:40:31 +00:00
'plugin' => dcCore::app()->plugins->getModules(),
];
2021-11-01 21:32:32 +00:00
foreach ($list as $type => $modules) {
foreach ($modules as $id => $info) {
if (!$info['root_writable']) {
// continue;
}
2021-11-01 21:32:32 +00:00
$info['id'] = $id;
$info['type'] = $type;
$this->modules[$type][$id] = new dcTranslaterModule($this, $info);
2021-08-18 22:48:47 +00:00
}
}
}
2021-08-18 19:42:30 +00:00
2021-11-01 21:32:32 +00:00
/**
2021-09-19 13:31:57 +00:00
* Return array of modules infos by type of modules
2021-11-01 21:32:32 +00:00
*
* @param string $type The modules type
2022-11-13 17:40:31 +00:00
*
* @return array The list of modules infos
2021-09-19 13:31:57 +00:00
*/
public function getModules(string $type = ''): array
2021-08-18 22:48:47 +00:00
{
2021-11-01 21:32:32 +00:00
return in_array($type, ['plugin', 'theme']) ?
$this->modules[$type] :
2021-08-18 22:48:47 +00:00
array_merge($this->modules['theme'], $this->modules['plugin']);
}
2021-09-02 21:44:01 +00:00
2021-11-01 21:32:32 +00:00
/**
* Return module class of a particular module for a given type of module
2021-11-01 21:32:32 +00:00
*
* @param string $type The module type
* @param string $id The module id
2022-11-13 17:40:31 +00:00
*
* @return dcTranslaterModule The dcTranslaterModule instance
2021-09-19 13:31:57 +00:00
*/
public function getModule(string $type, string $id)
2021-08-18 22:48:47 +00:00
{
if (!isset($this->modules[$type][$id])) {
2021-09-19 13:31:57 +00:00
throw new Exception(
sprintf(__('Failed to find module %s'), $id)
2021-09-19 13:31:57 +00:00
);
2021-08-18 22:48:47 +00:00
}
2021-11-01 21:32:32 +00:00
return $this->modules[$type][$id];
}
2021-09-02 21:44:01 +00:00
2021-11-01 21:32:32 +00:00
/**
* Return module class of a particular module for a given type of module
2021-11-01 21:32:32 +00:00
*
* @param dcTranslaterModule $module dcTranslaterModule instance
* @param string $lang The lang iso code
2022-11-13 17:40:31 +00:00
*
* @return dcTranslaterLang dcTranslaterLang instance or false
*/
public function getLang(dcTranslaterModule $module, string $lang)
{
if (!l10n::isCode($lang)) {
throw new Exception(
sprintf(__('Failed find language %s'), $lang)
);
2021-08-18 22:48:47 +00:00
}
2021-11-01 21:32:32 +00:00
return new dcTranslaterLang($module, $lang);
2021-08-18 22:48:47 +00:00
}
2021-09-19 13:31:57 +00:00
//@}
/// @name helper methods
//@{
2021-11-01 21:32:32 +00:00
/**
* Scan recursively a folder and return files and folders names
2021-11-01 21:32:32 +00:00
*
* @param string $path The path to scan
* @param string $dir Internal recursion
* @param array $res Internal recursion
* @return array List of path
2021-09-19 13:31:57 +00:00
*/
2021-09-25 15:38:46 +00:00
public static function scandir(string $path, string $dir = '', array $res = []): array
2021-08-18 22:48:47 +00:00
{
$path = (string) path::real($path, false);
if (empty($path) || !is_dir($path) || !is_readable($path)) {
return [];
}
2021-08-18 19:42:30 +00:00
$files = files::scandir($path);
2021-11-01 21:32:32 +00:00
foreach ($files as $file) {
if (in_array($file, ['.', '..'])) {
continue;
}
2021-08-18 19:42:30 +00:00
if (is_dir($path . '/' . $file)) {
$res[] = $file;
2021-11-01 21:32:32 +00:00
$res = self::scanDir($path . '/' . $file, $dir . '/' . $file, $res);
} else {
$res[] = empty($dir) ? $file : $dir . '/' . $file;
2021-08-18 22:48:47 +00:00
}
}
2021-11-01 21:32:32 +00:00
return $res;
2021-08-18 22:48:47 +00:00
}
2021-09-02 21:44:01 +00:00
2021-09-25 15:38:46 +00:00
/**
* Encode a string
2021-11-01 21:32:32 +00:00
*
2021-09-25 15:38:46 +00:00
* @param string $str The string to encode
* @return string The encoded string
*/
public static function encodeMsg(string $str): string
2021-08-18 22:48:47 +00:00
{
2021-09-25 15:38:46 +00:00
return text::toUTF8(stripslashes(trim($str)));
}
/**
* Clean a po string
2021-11-01 21:32:32 +00:00
*
2021-09-25 15:38:46 +00:00
* @param string $string The string to clean
* @param boolean $reverse Un/escape string
* @return string The cleaned string
*/
public static function poString(string $string, bool $reverse = false): string
{
if ($reverse) {
2021-11-01 21:32:32 +00:00
$smap = ['"', "\n", "\t", "\r"];
$rmap = ['\\"', '\\n"' . "\n" . '"', '\\t', '\\r'];
2021-09-25 15:38:46 +00:00
return trim((string) str_replace($smap, $rmap, $string));
}
2021-11-01 21:32:32 +00:00
$smap = ['/"\s+"/', '/\\\\n/', '/\\\\r/', '/\\\\t/', '/\\\"/'];
$rmap = ['', "\n", "\r", "\t", '"'];
return trim((string) preg_replace($smap, $rmap, $string));
2021-08-18 22:48:47 +00:00
}
2021-08-18 19:42:30 +00:00
2021-09-25 15:38:46 +00:00
/**
* Try if a file is a .po file
2021-11-01 21:32:32 +00:00
*
2021-09-25 15:38:46 +00:00
* @param string $file The path to test
* @return boolean Success
*/
public static function isPoFile(string $file): bool
2021-08-18 22:48:47 +00:00
{
return files::getExtension($file) == 'po';
2021-08-18 22:48:47 +00:00
}
2021-08-18 19:42:30 +00:00
2021-09-25 15:38:46 +00:00
/**
* Try if a file is a .lang.php file
2021-11-01 21:32:32 +00:00
*
2021-09-25 15:38:46 +00:00
* @param string $file The path to test
* @return boolean Success
*/
public static function isLangphpFile(string $file): bool
{
return files::getExtension($file) == 'php' && stristr($file, '.lang.php');
}
2021-09-19 13:31:57 +00:00
/**
* Check limit number of backup for a module
2021-11-01 21:32:32 +00:00
*
* @param string $id The module id
* @param string $root The backups root
* @param integer $limit The backups limit
2021-09-19 13:31:57 +00:00
* @param boolean $throw Silently failed
* @return boolean True if limit is riched
*/
public static function isBackupLimit(string $id, string $root, int $limit = 10, bool $throw = false): bool
2021-08-18 22:48:47 +00:00
{
$count = 0;
2021-11-01 21:32:32 +00:00
foreach (self::scandir($root) as $file) {
if (!is_dir($root . '/' . $file)
2021-11-01 21:32:32 +00:00
&& preg_match('/^(l10n-' . $id . '(.*?).bck.zip)$/', $root)
2021-09-19 13:31:57 +00:00
) {
2021-08-18 22:48:47 +00:00
$count++;
}
}
2021-09-02 21:44:01 +00:00
2021-08-18 22:48:47 +00:00
# Limite exceed
if ($count >= $limit) {
2021-08-18 22:48:47 +00:00
if ($throw) {
2021-09-19 13:31:57 +00:00
throw new Exception(
sprintf(__('Limit of %s backups for module %s exceed'), $limit, $id)
2021-09-19 13:31:57 +00:00
);
2021-08-18 22:48:47 +00:00
}
2021-11-01 21:32:32 +00:00
2021-08-18 22:48:47 +00:00
return true;
}
2021-11-01 21:32:32 +00:00
2021-09-19 13:31:57 +00:00
return false;
2021-08-18 22:48:47 +00:00
}
2021-08-18 19:42:30 +00:00
2021-09-19 13:31:57 +00:00
/**
* Extract messages from a php contents
*
* support plurals
2021-11-01 21:32:32 +00:00
*
* @param string $content The contents
* @param string $func The function name
2021-09-25 13:15:16 +00:00
* @return array The messages
2021-09-19 13:31:57 +00:00
*/
public static function extractPhpMsgs(string $content, string $func = '__'): array
{
$duplicate = $final_strings = $lines = [];
// split content by line to combine match/line on the end
$content = str_replace("\r\n", "\n", $content);
2021-11-01 21:32:32 +00:00
$o = 0;
$parts = explode("\n", $content);
foreach ($parts as $li => $part) {
$m = explode($func . '(', $part);
2021-11-01 21:32:32 +00:00
for ($i = 1; $i < count($m); $i++) {
$lines[$o] = $li + 1;
$o++;
}
}
// split content by translation function
$parts = explode($func . '(', $content);
// remove fisrt element from array
array_shift($parts);
// walk through parts
$p = 0;
2021-11-01 21:32:32 +00:00
foreach ($parts as $part) {
// should start with quote
2021-11-01 21:32:32 +00:00
if (!in_array(substr($part, 0, 1), ['"', "'"])) {
$p++;
2021-11-01 21:32:32 +00:00
continue;
}
// put back first parenthesis
2021-11-01 21:32:32 +00:00
$part = '(' . $part;
// find pairs of parenthesis
preg_match_all("/\((?:[^\)\(]+|(?R))*+\)/s", $part, $subparts);
// find quoted strings (single or double)
preg_match_all("/\'(?:[^\']+)\'|\"(?:[^\"]+)\"/s", $subparts[0][0], $strings);
// strings exist
if (!empty($strings[0])) {
// remove quotes
2021-11-01 21:32:32 +00:00
$strings[0] = array_map(function ($v) { return substr($v, 1, -1);}, $strings[0]);
// filter duplicate strings (only check first string for plurals form)
if (!in_array($strings[0][0], $duplicate)) {
// fill final array
$final_strings[] = [$strings[0], $lines[$p]];
2021-11-01 21:32:32 +00:00
$duplicate[] = $strings[0][0];
2021-08-18 22:48:47 +00:00
}
}
$p++;
2021-08-18 22:48:47 +00:00
}
2021-11-01 21:32:32 +00:00
return $final_strings;
2021-08-18 22:48:47 +00:00
}
2021-09-25 13:15:16 +00:00
/**
* Extract messages from a tpl contents
*
* @param string $content The contents
* @param string $func The function name
* @return array The messages
*/
public static function extractTplMsgs(string $content, string $func = 'tpl:lang'): array
{
$duplicate = $final_strings = $lines = [];
// split content by line to combine match/line on the end
$content = str_replace("\r\n", "\n", $content);
2021-11-01 21:32:32 +00:00
$o = 0;
$parts = explode("\n", $content);
foreach ($parts as $li => $part) {
2021-09-25 13:15:16 +00:00
$m = explode('{{' . $func . ' ', $part);
2021-11-01 21:32:32 +00:00
for ($i = 1; $i < count($m); $i++) {
$lines[$o] = $li + 1;
2021-09-25 13:15:16 +00:00
$o++;
}
}
// split content by translation function
if (!preg_match_all('/\{\{' . preg_quote($func) . '\s([^}]+)\}\}/', $content, $parts)) {
return $final_strings;
}
// walk through parts
$p = 0;
2021-11-01 21:32:32 +00:00
foreach ($parts[1] as $part) {
2021-09-25 13:15:16 +00:00
// strings exist
if (!empty($part)) {
// filter duplicate strings
if (!in_array($part, $duplicate)) {
// fill final array
$final_strings[] = [[$part], $lines[$p]];
2021-11-01 21:32:32 +00:00
$duplicate[] = $part;
2021-09-25 13:15:16 +00:00
}
}
$p++;
}
2021-11-01 21:32:32 +00:00
2021-09-25 13:15:16 +00:00
return $final_strings;
}
//@}
2021-11-01 21:32:32 +00:00
}