PDF rausgenommen

This commit is contained in:
aschwarz
2023-01-23 11:03:31 +01:00
parent 82d562a322
commit a6523903eb
28078 changed files with 4247552 additions and 2 deletions

View File

@ -0,0 +1,62 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\LanguagesManager\TranslationWriter\Filter;
class ByBaseTranslations extends FilterAbstract
{
protected $baseTranslations = array();
/**
* Sets base translations
*
* @param array $baseTranslations
*/
public function __construct($baseTranslations = array())
{
$this->baseTranslations = $baseTranslations;
}
/**
* Removes all translations that aren't present in the base translations set in constructor
*
* @param array $translations
*
* @return array filtered translations
*/
public function filter($translations)
{
$cleanedTranslations = array();
foreach ($translations as $pluginName => $pluginTranslations) {
if (empty($this->baseTranslations[$pluginName])) {
$this->filteredData[$pluginName] = $pluginTranslations;
continue;
}
foreach ($pluginTranslations as $key => $translation) {
if (isset($this->baseTranslations[$pluginName][$key])) {
$cleanedTranslations[$pluginName][$key] = $translation;
}
}
if (!empty($cleanedTranslations[$pluginName])) {
$diff = array_diff($translations[$pluginName], $cleanedTranslations[$pluginName]);
} else {
$diff = $translations[$pluginName];
}
if (!empty($diff)) {
$this->filteredData[$pluginName] = $diff;
}
}
return $cleanedTranslations;
}
}

View File

@ -0,0 +1,85 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\LanguagesManager\TranslationWriter\Filter;
class ByParameterCount extends FilterAbstract
{
protected $baseTranslations = array();
/**
* Sets base translations
*
* @param array $baseTranslations
*/
public function __construct($baseTranslations = array())
{
$this->baseTranslations = $baseTranslations;
}
/**
* Removes all translations where the placeholder parameter count differs to base translation
*
* @param array $translations
*
* @return array filtered translations
*/
public function filter($translations)
{
$cleanedTranslations = array();
foreach ($translations as $pluginName => $pluginTranslations) {
foreach ($pluginTranslations as $key => $translation) {
if (isset($this->baseTranslations[$pluginName][$key])) {
$baseTranslation = $this->baseTranslations[$pluginName][$key];
} else {
// english string was deleted, do not error
continue;
}
// ensure that translated strings have the same number of %s as the english source strings
$baseCount = $this->_getParametersCountToReplace($baseTranslation);
$translationCount = $this->_getParametersCountToReplace($translation);
if ($baseCount != $translationCount) {
$this->filteredData[$pluginName][$key] = $translation;
continue;
}
$cleanedTranslations[$pluginName][$key] = $translation;
}
}
return $cleanedTranslations;
}
/**
* Counts the placeholder parameters n given string
*
* @param string $string
* @return array
*/
protected function _getParametersCountToReplace($string)
{
$sprintfParameters = array('%s', '%1$s', '%2$s', '%3$s', '%4$s', '%5$s', '%6$s', '%7$s', '%8$s', '%9$s');
$count = array();
foreach ($sprintfParameters as $parameter) {
$placeholderCount = substr_count($string, $parameter);
if ($placeholderCount > 0) {
$count[$parameter] = $placeholderCount;
}
}
return $count;
}
}

View File

@ -0,0 +1,44 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\LanguagesManager\TranslationWriter\Filter;
class EmptyTranslations extends FilterAbstract
{
/**
* Removes all empty translations
*
* @param array $translations
*
* @return array filtered translations
*/
public function filter($translations)
{
$translationsBefore = $translations;
foreach ($translations as $plugin => &$pluginTranslations) {
$pluginTranslations = array_filter($pluginTranslations, function ($value) {
return !empty($value) && '' != trim($value);
});
$diff = array_diff($translationsBefore[$plugin], $pluginTranslations);
if (!empty($diff)) {
$this->filteredData[$plugin] = $diff;
}
}
// remove plugins without translations
$translations = array_filter($translations, function ($value) {
return !empty($value) && count($value);
});
return $translations;
}
}

View File

@ -0,0 +1,58 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\LanguagesManager\TranslationWriter\Filter;
use Piwik\Translate;
class EncodedEntities extends FilterAbstract
{
protected $baseTranslations = array();
/**
* Sets base translations
*
* @param array $baseTranslations
*/
public function __construct($baseTranslations = array())
{
$this->baseTranslations = $baseTranslations;
}
/**
* Decodes all encoded entities in the given translations
*
* @param array $translations
*
* @return array filtered translations
*/
public function filter($translations)
{
foreach ($translations as $pluginName => $pluginTranslations) {
foreach ($pluginTranslations as $key => $translation) {
if (isset($this->baseTranslations[$pluginName][$key]) &&
$this->baseTranslations[$pluginName][$key] != Translate::clean($this->baseTranslations[$pluginName][$key])) {
continue; // skip if base translation already contains encoded entities
}
// remove encoded entities
$decoded = Translate::clean($translation);
if ($translation != $decoded) {
$this->filteredData[$pluginName][$key] = $translation;
$translations[$pluginName][$key] = $decoded;
continue;
}
}
}
return $translations;
}
}

View File

@ -0,0 +1,34 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\LanguagesManager\TranslationWriter\Filter;
abstract class FilterAbstract
{
protected $filteredData = array();
/**
* Filter the given translations
*
* @param array $translations
*
* @return array filtered translations
*/
abstract public function filter($translations);
/**
* Returnes the data filtered out by the filter
*
* @return array
*/
public function getFilteredData()
{
return $this->filteredData;
}
}

View File

@ -0,0 +1,62 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\LanguagesManager\TranslationWriter\Filter;
class UnnecassaryWhitespaces extends FilterAbstract
{
protected $baseTranslations = array();
/**
* Sets base translations
*
* @param array $baseTranslations
*/
public function __construct($baseTranslations = array())
{
$this->baseTranslations = $baseTranslations;
}
/**
* Removes all unnecassary whitespaces and newlines from the given translations
*
* @param array $translations
*
* @return array filtered translations
*/
public function filter($translations)
{
foreach ($translations as $pluginName => $pluginTranslations) {
foreach ($pluginTranslations as $key => $translation) {
$baseTranslation = '';
if (isset($this->baseTranslations[$pluginName][$key])) {
$baseTranslation = $this->baseTranslations[$pluginName][$key];
}
// remove excessive line breaks (and leading/trailing whitespace) from translations
$stringNoLineBreak = trim($translation);
$stringNoLineBreak = str_replace("\r", "", $stringNoLineBreak); # remove useless carrige renturns
$stringNoLineBreak = preg_replace('/(\n[ ]+)/', "\n", $stringNoLineBreak); # remove useless white spaces after line breaks
$stringNoLineBreak = preg_replace('/([\n]{2,})/', "\n\n", $stringNoLineBreak); # remove excessive line breaks
if (empty($baseTranslation) || !substr_count($baseTranslation, "\n")) {
$stringNoLineBreak = preg_replace("/[\n]+/", " ", $stringNoLineBreak); # remove all line breaks if english string doesn't contain any
}
$stringNoLineBreak = preg_replace('/([ ]{2,})/', " ", $stringNoLineBreak); # remove excessive white spaces again as there might be any now, after removing line breaks
if ($translation !== $stringNoLineBreak) {
$this->filteredData[$pluginName][$key] = $translation;
$translations[$pluginName][$key] = $stringNoLineBreak;
continue;
}
}
}
return $translations;
}
}

View File

@ -0,0 +1,88 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\LanguagesManager\TranslationWriter\Validate;
use Piwik\Container\StaticContainer;
use Piwik\Intl\Data\Provider\LanguageDataProvider;
use Piwik\Intl\Data\Provider\RegionDataProvider;
class CoreTranslations extends ValidateAbstract
{
/**
* Error States
*/
const ERRORSTATE_LOCALEREQUIRED = 'Locale required';
const ERRORSTATE_TRANSLATORINFOREQUIRED = 'Translator info required';
const ERRORSTATE_LOCALEINVALID = 'Locale is invalid';
const ERRORSTATE_LOCALEINVALIDLANGUAGE = 'Locale is invalid - invalid language code';
const ERRORSTATE_LOCALEINVALIDCOUNTRY = 'Locale is invalid - invalid country code';
protected $baseTranslations = array();
/**
* Sets base translations
*
* @param array $baseTranslations
*/
public function __construct($baseTranslations = array())
{
$this->baseTranslations = $baseTranslations;
}
/**
* Validates the given translations
* * There need to be more than 250 translations present
* * Locale and TranslatorName needs to be set in plugin General
* * Locale must be valid (format, language & country)
*
* @param array $translations
*
* @return boolean
*/
public function isValid($translations)
{
$this->message = null;
if (empty($translations['General']['Locale'])) {
$this->message = self::ERRORSTATE_LOCALEREQUIRED;
return false;
}
if (empty($translations['General']['TranslatorName'])) {
$this->message = self::ERRORSTATE_TRANSLATORINFOREQUIRED;
return false;
}
/** @var LanguageDataProvider $languageDataProvider */
$languageDataProvider = StaticContainer::get('Piwik\Intl\Data\Provider\LanguageDataProvider');
/** @var RegionDataProvider $regionDataProvider */
$regionDataProvider = StaticContainer::get('Piwik\Intl\Data\Provider\RegionDataProvider');
$allLanguages = $languageDataProvider->getLanguageList();
$allCountries = $regionDataProvider->getCountryList();
if ('eo.UTF-8' === $translations['General']['Locale']) {
return true;
}
if (!preg_match('/^([a-z]{2})_([A-Z]{2})\.UTF-8$/', $translations['General']['Locale'], $matches)) {
$this->message = self::ERRORSTATE_LOCALEINVALID;
return false;
} else if (!array_key_exists($matches[1], $allLanguages)) {
$this->message = self::ERRORSTATE_LOCALEINVALIDLANGUAGE;
return false;
} else if (!array_key_exists(strtolower($matches[2]), $allCountries)) {
$this->message = self::ERRORSTATE_LOCALEINVALIDCOUNTRY;
return false;
}
return true;
}
}

View File

@ -0,0 +1,39 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\LanguagesManager\TranslationWriter\Validate;
class NoScripts extends ValidateAbstract
{
/**
* Validates the given translations
* * No script like parts should be present in any part of the translations
*
* @param array $translations
*
* @return boolean
*/
public function isValid($translations)
{
$this->message = null;
// check if any translation contains restricted script tags
$serializedStrings = serialize($translations);
$invalids = array("<script", 'document.', 'javascript:', 'src=', 'background=', 'onload=');
foreach ($invalids as $invalid) {
if (stripos($serializedStrings, $invalid) !== false) {
$this->message = 'script tags restricted for language files';
return false;
}
}
return true;
}
}

View File

@ -0,0 +1,35 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\LanguagesManager\TranslationWriter\Validate;
abstract class ValidateAbstract
{
protected $message = null;
/**
* Returns if the given translations are valid
*
* @param array $translations
*
* @return boolean
*/
abstract public function isValid($translations);
/**
* Returns an array of messages that explain why the most recent isValid()
* call returned false.
*
* @return array
*/
public function getMessage()
{
return $this->message;
}
}

View File

@ -0,0 +1,388 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\LanguagesManager\TranslationWriter;
use Exception;
use Piwik\Container\StaticContainer;
use Piwik\Filesystem;
use Piwik\Piwik;
use Piwik\Plugin\Manager;
use Piwik\Plugins\LanguagesManager\TranslationWriter\Filter\FilterAbstract;
use Piwik\Plugins\LanguagesManager\TranslationWriter\Validate\ValidateAbstract;
/**
* Writes translations to file.
*/
class Writer
{
/**
* current language to write files for
*
* @var string
*/
protected $language = '';
/**
* Name of a plugin (if set in constructor)
*
* @var string|null
*/
protected $pluginName = null;
/**
* translations to write to file
*
* @var array
*/
protected $translations = array();
/**
* Validators to check translations with
*
* @var ValidateAbstract[]
*/
protected $validators = array();
/**
* Message why validation failed
*
* @var string|null
*/
protected $validationMessage = null;
/**
* Filters to to apply to translations
*
* @var FilterAbstract[]
*/
protected $filters = array();
/**
* Messages which filter changed the data
*
* @var array
*/
protected $filterMessages = array();
const UNFILTERED = 'unfiltered';
const FILTERED = 'filtered';
protected $currentState = self::UNFILTERED;
/**
* If $pluginName is given, Writer will be initialized for the given plugin if it exists
* Otherwise it will be initialized for core translations
*
* @param string $language ISO 639-1 alpha-2 language code
* @param string $pluginName optional plugin name
* @throws \Exception
*/
public function __construct($language, $pluginName = null)
{
$this->setLanguage($language);
if (!empty($pluginName)) {
$installedPlugins = \Piwik\Plugin\Manager::getInstance()->readPluginsDirectory();
if (!in_array($pluginName, $installedPlugins)) {
throw new Exception(Piwik::translate('General_ExceptionLanguageFileNotFound', array($pluginName)));
}
$this->pluginName = $pluginName;
}
}
/**
* @param string $language ISO 639-1 alpha-2 language code
*
* @throws \Exception
*/
public function setLanguage($language)
{
if (!preg_match('/^([a-z]{2,3}(-[a-z]{2,3})?)$/i', $language)) {
throw new Exception(Piwik::translate('General_ExceptionLanguageFileNotFound', array($language)));
}
$this->language = strtolower($language);
}
/**
* @return string ISO 639-1 alpha-2 language code
*/
public function getLanguage()
{
return $this->language;
}
/**
* Returns if there are translations available or not
* @return bool
*/
public function hasTranslations()
{
return !empty($this->translations);
}
/**
* Set the translations to write (and cleans them)
*
* @param $translations
*/
public function setTranslations($translations)
{
$this->currentState = self::UNFILTERED;
$this->translations = $translations;
$this->applyFilters();
}
/**
* Get translations from file
*
* @param string $lang ISO 639-1 alpha-2 language code
* @throws Exception
* @return array Array of translations ( plugin => ( key => translated string ) )
*/
public function getTranslations($lang)
{
$path = $this->getTranslationPathBaseDirectory('lang', $lang);
if (!is_readable($path)) {
return array();
}
$data = file_get_contents($path);
$translations = json_decode($data, true);
return $translations;
}
/**
* Returns the temporary path for translations
*
* @return string
*/
public function getTemporaryTranslationPath()
{
return $this->getTranslationPathBaseDirectory('tmp');
}
/**
* Returns the path to translation files
*
* @return string
*/
public function getTranslationPath()
{
return $this->getTranslationPathBaseDirectory('lang');
}
/**
* Get translation file path based on given params
*
* @param string $base Optional base directory (either 'lang' or 'tmp')
* @param string|null $lang forced language
* @throws \Exception
* @return string path
*/
protected function getTranslationPathBaseDirectory($base, $lang = null)
{
if (empty($lang)) {
$lang = $this->getLanguage();
}
if (!empty($this->pluginName)) {
if ($base == 'tmp') {
return sprintf('%s/plugins/%s/lang/%s.json', StaticContainer::get('path.tmp'), $this->pluginName, $lang);
} else {
return sprintf('%s/lang/%s.json', Manager::getPluginDirectory($this->pluginName), $lang);
}
}
if ($base == 'tmp') {
return sprintf('%s/%s.json', StaticContainer::get('path.tmp'), $lang);
}
return sprintf('%s/%s/%s.json', PIWIK_INCLUDE_PATH, $base, $lang);
}
/**
* Converts translations to a string that can be written to a file
*
* @return string
*/
public function __toString()
{
/*
* Use JSON_UNESCAPED_UNICODE and JSON_PRETTY_PRINT for PHP >= 5.4
*/
$options = 0;
if (defined('JSON_UNESCAPED_UNICODE')) {
$options |= JSON_UNESCAPED_UNICODE;
}
if (defined('JSON_PRETTY_PRINT')) {
$options |= JSON_PRETTY_PRINT;
}
return json_encode($this->translations, $options);
}
/**
* Save translations to file; translations should already be cleaned.
*
* @throws \Exception
* @return bool|int False if failure, or number of bytes written
*/
public function save()
{
$this->applyFilters();
if (!$this->hasTranslations() || !$this->isValid()) {
throw new Exception('unable to save empty or invalid translations');
}
$path = $this->getTranslationPath();
Filesystem::mkdir(dirname($path));
return file_put_contents($path, $this->__toString());
}
/**
* Save translations to temporary file; translations should already be cleansed.
*
* @throws \Exception
* @return bool|int False if failure, or number of bytes written
*/
public function saveTemporary()
{
$this->applyFilters();
if (!$this->hasTranslations() || !$this->isValid()) {
throw new Exception('unable to save empty or invalid translations');
}
$path = $this->getTemporaryTranslationPath();
Filesystem::mkdir(dirname($path));
return file_put_contents($path, $this->__toString());
}
/**
* Adds an validator to check before saving
*
* @param ValidateAbstract $validator
*/
public function addValidator(ValidateAbstract $validator)
{
$this->validators[] = $validator;
}
/**
* Returns if translations are valid to save or not
*
* @return bool
*/
public function isValid()
{
$this->applyFilters();
$this->validationMessage = null;
foreach ($this->validators as $validator) {
if (!$validator->isValid($this->translations)) {
$this->validationMessage = $validator->getMessage();
return false;
}
}
return true;
}
/**
* Returns last validation message
*
* @return null|string
*/
public function getValidationMessage()
{
return $this->validationMessage;
}
/**
* Returns if the were translations removed while cleaning
*
* @return bool
*/
public function wasFiltered()
{
return !empty($this->filterMessages);
}
/**
* Returns the cleaning errors
*
* @return array
*/
public function getFilterMessages()
{
return $this->filterMessages;
}
/**
* @param FilterAbstract $filter
*/
public function addFilter(FilterAbstract $filter)
{
$this->filters[] = $filter;
}
/**
* @throws \Exception
*
* @return bool error state
*/
protected function applyFilters()
{
// skip if already cleaned
if ($this->currentState == self::FILTERED) {
return $this->wasFiltered();
}
$this->filterMessages = array();
// skip if not translations available
if (!$this->hasTranslations()) {
$this->currentState = self::FILTERED;
return false;
}
$cleanedTranslations = $this->translations;
foreach ($this->filters as $filter) {
$cleanedTranslations = $filter->filter($cleanedTranslations);
$filteredData = $filter->getFilteredData();
if (!empty($filteredData)) {
$this->filterMessages[] = get_class($filter) . " changed: " . var_export($filteredData, 1);
}
}
$this->currentState = self::FILTERED;
if ($cleanedTranslations != $this->translations) {
$this->filterMessages[] = 'translations have been cleaned';
}
$this->translations = $cleanedTranslations;
return $this->wasFiltered();
}
}