summaryrefslogtreecommitdiffstats
path: root/modules-available/translation/page.inc.php
diff options
context:
space:
mode:
Diffstat (limited to 'modules-available/translation/page.inc.php')
-rw-r--r--modules-available/translation/page.inc.php161
1 files changed, 72 insertions, 89 deletions
diff --git a/modules-available/translation/page.inc.php b/modules-available/translation/page.inc.php
index 34389b75..4563b8e5 100644
--- a/modules-available/translation/page.inc.php
+++ b/modules-available/translation/page.inc.php
@@ -1,5 +1,7 @@
<?php
+use JetBrains\PhpStorm\NoReturn;
+
/**
* The pages where you can administrate the website translations
*/
@@ -45,12 +47,12 @@ class Page_Translation extends Page
$this->builtInSections = array('template', 'messages', 'module', 'menucategory', 'custom');
}
- private function isValidSection($section)
+ private function isValidSection(string $section): bool
{
return in_array($section, $this->builtInSections);
}
- private function loadCustomHandler($moduleName)
+ private function loadCustomHandler(string $moduleName): ?array
{
$path = 'modules/' . $moduleName . '/hooks/translation.inc.php';
$HANDLER = array();
@@ -75,7 +77,7 @@ class Page_Translation extends Page
}
}
if (empty($backup))
- return false;
+ return null;
return $backup;
}
@@ -83,7 +85,8 @@ class Page_Translation extends Page
* Redirect to the closest matching page, as extracted from
* get/post parameters.
*/
- private function redirect($level = 99)
+ #[NoReturn]
+ private function redirect(int $level = 99): void
{
$params = array('do' => 'translation');
if ($level > 0 && $this->module !== false) {
@@ -329,9 +332,9 @@ class Page_Translation extends Page
Render::addTemplate('menu-category-list', $data);
}
- private function showModuleCustom()
+ private function showModuleCustom(): void
{
- if ($this->customHandler === false)
+ if ($this->customHandler === null)
return;
foreach ($this->customHandler['subsections'] as $subsection) {
$this->showModuleCustomSubsection($subsection);
@@ -420,13 +423,13 @@ class Page_Translation extends Page
/**
* Get all tags used by templates of the given module.
- * @param \Module $module module in question, false to use the one being edited.
+ * @param \Module $module module in question, null to use the one being edited.
*
* @return array of array(tag => array of templates using that tag)
*/
- private function loadUsedTemplateTags($module = false)
+ private function loadUsedTemplateTags(Module $module = null): array
{
- if ($module === false) {
+ if ($module === null) {
$module = $this->module;
}
$tags = array();
@@ -451,12 +454,12 @@ class Page_Translation extends Page
* 'files' => array(filename => occurencecount, ...)
* )
*
- * @param \Module $module module in question, false to use the one being edited
+ * @param \Module $module module in question, null to use the one being edited
* @return array see above
*/
- private function loadUsedMessageTags($module = false)
+ private function loadUsedMessageTags(Module $module = null): array
{
- if ($module === false) {
+ if ($module === null) {
$module = $this->module;
}
$allFiles = $this->getAllFiles('modules', '.php');
@@ -477,20 +480,18 @@ class Page_Translation extends Page
$tags[$p[1]] = $tag;
}
}
- $tags = $this->loadTagsFromPhp('/Message\s*::\s*add\w+\s*\(\s*[\'"](?<tag>[^\'"\.]*)[\'"]\s*(?<data>\)|\,.*)/i',
+ return $this->loadTagsFromPhp('/Message\s*::\s*add\w+\s*\(\s*[\'"](?<tag>[^\'"\.]*)[\'"]\s*(?<data>\)|\,.*)/i',
$this->getModulePhpFiles($module), $tags);
- return $tags;
}
/**
* Get all module tags used/required.
*
- * @param string $module
- * @return array of array(tagname => (bool)required)
+ * @return array (<tagname> => (bool)required)
*/
- private function loadUsedModuleTags($module = false)
+ private function loadUsedModuleTags(Module $module = null): array
{
- if ($module === false) {
+ if ($module === null) {
$module = $this->module;
}
$tags = $this->loadTagsFromPhp('/Dictionary\s*::\s*translate\s*\(\s*[\'"](?<tag>[^\'"\.]*)[\'"]\s*[\),]/i',
@@ -500,7 +501,7 @@ class Page_Translation extends Page
}
unset($tag);
// Fixup special tags
- if ($module->getCategory() === false) {
+ if ($module->getCategory() === null) {
unset($tags['module_name']);
unset($tags['page_title']);
} else {
@@ -510,17 +511,15 @@ class Page_Translation extends Page
return $tags;
}
- private function loadUsedMenuCategories($module = false)
+ private function loadUsedMenuCategories(): array
{
- if ($module === false) {
- $module = $this->module;
- }
+ $module = $this->module;
$skip = strlen($module->getIdentifier()) + 1;
$match = $module->getIdentifier() . '.';
$want = array();
foreach (Module::getAll() as $module) {
$cat = $module->getCategory();
- if (is_string($cat) && substr($cat, 0, $skip) === $match) {
+ if ($cat !== null && substr($cat, 0, $skip) === $match) {
$want[substr($cat, $skip)] = true;
}
}
@@ -536,14 +535,14 @@ class Page_Translation extends Page
* @param string $subsection Name of subsection
* @return array|false List of tags as KEYS of array
*/
- private function loadUsedCustomTags($subsection)
+ private function loadUsedCustomTags(string $subsection)
{
if (!isset($this->customHandler['grep_'.$subsection]))
return false;
return $this->customHandler['grep_'.$subsection]($this->module);
}
- private function getTagsFromTemplate($templateFile)
+ private function getTagsFromTemplate(string $templateFile)
{
//checks if the template is valid
if (!file_exists($templateFile)) {
@@ -564,10 +563,10 @@ class Page_Translation extends Page
*
* @param string $lang lang to use
* @param array $tags Array of tags, where the tag names are the keys
- * @param \Module|false $module the module to work with, defaults to the currently edited module
+ * @param Module $module the module to work with, defaults to the currently edited module
* @return array [missingCount, unusedCount]
*/
- private function getModuleTemplateStatus($lang, $tags = false, $module = false)
+ private function getModuleTemplateStatus(string $lang, array $tags, Module $module = null): array
{
return $this->getModuleTranslationStatus($lang, 'template-tags', true, $tags, $module);
}
@@ -579,14 +578,14 @@ class Page_Translation extends Page
*
* @param string $lang lang cc to use
* @param string $file the name of the translation file to load for checking
- * @param boolean $fallback whether to check the global-tags of the main module as fallback
+ * @param bool $fallback whether to check the global-tags of the main module as fallback
* @param array $tags list of tags that are expected to exist. Tags are the array keys!
- * @param \Module|false $module the module to work with, defaults to the currently edited module
+ * @param Module $module the module to work with, defaults to the currently edited module
* @return array [missingCount, unusedCount]
*/
- private function getModuleTranslationStatus($lang, $file, $fallback, $tags, $module = false)
+ private function getModuleTranslationStatus(string $lang, string $file, bool $fallback, array $tags, Module $module = null): array
{
- if ($module === false) {
+ if ($module === null) {
$module = $this->module;
}
if ($fallback) {
@@ -619,7 +618,7 @@ class Page_Translation extends Page
return array($missing, $unused);
}
- private function checkModuleTranslation($module)
+ private function checkModuleTranslation(Module $module): string
{
$templateTags = $this->loadUsedTemplateTags($module);
$messageTags = $this->loadUsedMessageTags($module);
@@ -654,7 +653,7 @@ class Page_Translation extends Page
*
* @return array of all php file names
*/
- private function getAllFiles($dir, $extension)
+ private function getAllFiles(string $dir, string $extension): array
{
$php = array();
$extLen = -strlen($extension);
@@ -674,10 +673,10 @@ class Page_Translation extends Page
/**
* Finds and returns all PHP files of current module.
*
- * @param \Module $module Module to get the php files of
+ * @param Module $module Module to get the php files of
* @return array of php file names
*/
- private function getModulePhpFiles($module)
+ private function getModulePhpFiles(Module $module): array
{
return $this->getAllFiles('modules/' . $module->getIdentifier(), '.php');
}
@@ -685,10 +684,9 @@ class Page_Translation extends Page
/**
* Get array to pass to edit page with all the tags and translations.
*
- * @param string $path the template's path
* @return array structure to pass to the tags list in the edit template
*/
- private function loadTemplateEditArray()
+ private function loadTemplateEditArray(): array
{
$tags = $this->loadUsedTemplateTags();
$table = $this->buildTranslationTable('template-tags', array_keys($tags), true);
@@ -707,10 +705,9 @@ class Page_Translation extends Page
/**
* Get array to pass to edit page with all the message ids.
*
- * @param string $path the template's path
* @return array structure to pass to the tags list in the edit template
*/
- private function loadMessagesEditArray()
+ private function loadMessagesEditArray(): array
{
$tags = $this->loadUsedMessageTags();
$table = $this->buildTranslationTable('messages', array_keys($tags), true);
@@ -734,34 +731,29 @@ class Page_Translation extends Page
/**
* Get array to pass to edit page with all the message ids.
*
- * @param string $path the template's path
* @return array structure to pass to the tags list in the edit template
*/
- private function loadModuleEditArray()
+ private function loadModuleEditArray(): array
{
$tags = $this->loadUsedModuleTags();
- $table = $this->buildTranslationTable('module', array_keys($tags), true);
- return $table;
+ return $this->buildTranslationTable('module', array_keys($tags), true);
}
- private function loadMenuCategoryEditArray()
+ private function loadMenuCategoryEditArray(): array
{
$tags = $this->loadUsedMenuCategories();
- $table = $this->buildTranslationTable('categories', array_keys($tags), true);
- return $table;
+ return $this->buildTranslationTable('categories', array_keys($tags), true);
}
/**
* Get array to pass to edit page with all the message ids.
*
- * @param string $path the template's path
* @return array structure to pass to the tags list in the edit template
*/
- private function loadCustomEditArray()
+ private function loadCustomEditArray(): array
{
$tags = $this->loadUsedCustomTags($this->subsection);
- $table = $this->buildTranslationTable($this->subsection, array_keys($tags), true);
- return $table;
+ return $this->buildTranslationTable($this->subsection, array_keys($tags), true);
}
/**
@@ -773,7 +765,7 @@ class Page_Translation extends Page
* @param string $str the partial method call
* @return int number of arguments to the method, minus the message id
*/
- private function countMessageParams($str)
+ private function countMessageParams(string $str): int
{
$quote = false;
$escape = false;
@@ -781,7 +773,7 @@ class Page_Translation extends Page
$len = strlen($str);
$depth = 0;
for ($i = 0; $i < $len; ++$i) {
- $char = $str{$i};
+ $char = $str[$i];
// Last char was backslash? Ignore this char
if ($escape) {
$escape = false;
@@ -832,11 +824,11 @@ class Page_Translation extends Page
* )).
*
* @param string $regexp regular expression
- * @param array $files list of files to scan
- * @param array $tags existing tag array to append to
+ * @param string[] $files list of files to scan
+ * @param string[] $tags existing tag array to append to
* @return array of all tags found, where the tag is the key, and the value is as described above
*/
- private function loadTagsFromPhp($regexp, $files, $tags = [])
+ private function loadTagsFromPhp(string $regexp, array $files, array $tags = []): array
{
// Get all php files, so we can find all strings that need to be translated
// Now find all tags in all php files. Only works for literal usage, not something like $foo = 'bar'; Dictionary::translate($foo);
@@ -861,32 +853,28 @@ class Page_Translation extends Page
/**
* @param string $file Source dictionary
- * @param string[]|false $requiredTags Tags that are considered required
+ * @param string[] $requiredTags Tags that are considered required
* @param bool $findAlreadyTranslated If true, try to find a translation for this string in another language
* @return array numeric array suitable for passing to mustache
*/
- private function buildTranslationTable($file, $requiredTags = false, $findAlreadyTranslated = false)
+ private function buildTranslationTable(string $file, array $requiredTags, bool $findAlreadyTranslated = false): array
{
$tags = array();
- if (is_array($requiredTags)) {
- foreach ($requiredTags as $tagName) {
- $tags[$tagName] = array('tag' => $tagName, 'required' => true);
- }
+ foreach ($requiredTags as $tagName) {
+ $tags[$tagName] = array('tag' => $tagName, 'required' => true);
}
// Sort here, so all tags known to be used are in alphabetical order
ksort($tags);
// Finds every tag within the JSON language file
$jsonTags = Dictionary::getArray($this->module->getIdentifier(), $file, $this->destLang);
- if (is_array($jsonTags)) {
- // Sort these separately so unused tags will be at the bottom of the list, but still ordered alphabetically
- ksort($jsonTags);
- foreach ($jsonTags as $tag => $translation) {
- $tags[$tag]['translation'] = $translation;
- if (strpos($translation, "\n") !== false) {
- $tags[$tag]['big'] = true;
- }
- $tags[$tag]['tag'] = $tag;
+ // Sort these separately so unused tags will be at the bottom of the list, but still ordered alphabetically
+ ksort($jsonTags);
+ foreach ($jsonTags as $tag => $translation) {
+ $tags[$tag]['translation'] = $translation;
+ if (strpos($translation, "\n") !== false) {
+ $tags[$tag]['big'] = true;
}
+ $tags[$tag]['tag'] = $tag;
}
if ($findAlreadyTranslated) {
// For each tag, include a translated string from another language as reference
@@ -900,17 +888,15 @@ class Page_Translation extends Page
$tagid = 0;
foreach ($tags as &$tag) {
$tag['tagid'] = $tagid++;
- if ($requiredTags !== false) {
- // We have a list of required tags, so mark those that are missing or unused
- if (!isset($tag['required'])) {
- $tag['unused'] = true;
- } elseif (!isset($tag['translation']) && !isset($globals[$tag['tag']])) {
- $tag['missing'] = true;
- }
- if (isset($globals[$tag['tag']])) {
- $tag['isglobal'] = true;
- $tag['placeholder'] = $globals[$tag['tag']];
- }
+ // We have a list of required tags, so mark those that are missing or unused
+ if (!isset($tag['required'])) {
+ $tag['unused'] = true;
+ } elseif (!isset($tag['translation']) && !isset($globals[$tag['tag']])) {
+ $tag['missing'] = true;
+ }
+ if (isset($globals[$tag['tag']])) {
+ $tag['isglobal'] = true;
+ $tag['placeholder'] = $globals[$tag['tag']];
}
}
// Finally remove tagname from the keys so mustache will iterate over them via {{#..}}
@@ -925,7 +911,7 @@ class Page_Translation extends Page
* @param string $file translation unit
* @param array $tags list of tags, formatted as used in buildTranslationTable()
*/
- private function findTranslationSamples($file, &$tags)
+ private function findTranslationSamples(string $file, array &$tags): void
{
$srcLangs = array_unique(array_merge(array(LANG), array('en'), Dictionary::getLanguages()));
if (($key = array_search($this->destLang, $srcLangs)) !== false) {
@@ -933,8 +919,6 @@ class Page_Translation extends Page
}
foreach ($srcLangs as $lang) {
$otherLang = Dictionary::getArray($this->module->getIdentifier(), $file, $lang);
- if (!is_array($otherLang))
- continue;
$missing = false;
foreach (array_keys($tags) as $tag) {
if (isset($tags[$tag]['samplelang']))
@@ -951,7 +935,7 @@ class Page_Translation extends Page
}
}
- private function getJsonFile()
+ private function getJsonFile(): string
{
$prefix = 'modules/' . $this->module->getIdentifier() . '/lang/' . $this->destLang;
// File
@@ -969,7 +953,7 @@ class Page_Translation extends Page
}
// Custom submodule
if ($this->section === 'custom') {
- if ($this->customHandler === false || !isset($this->customHandler['subsections'])) {
+ if ($this->customHandler === null || !isset($this->customHandler['subsections'])) {
Message::addError('no-custom-handlers');
$this->redirect(1);
}
@@ -981,13 +965,12 @@ class Page_Translation extends Page
}
Message::addError('invalid-section', $this->section);
$this->redirect(1);
- return false;
}
/**
* Updates a JSON file with it's new tags or/and tags values
*/
- private function updateJson()
+ private function updateJson(): void
{
$this->ensureValidDestLanguage();
if ($this->module === false) {