From cdf59074d31ce7cdc8cb03e910fdc3311eb6879a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Thu, 14 Nov 2024 05:37:00 -0300 Subject: [PATCH] Replace languages.inc globals with I18n\Languages consts (#1121) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The include/languages.inc file was not removed as it's used in other repositories. It should be removed after migration. Tests were added to ensure that the global variables and the constants are in sync with each other. Signed-off-by: MaurĂ­cio Meneghini Fauth --- docs.php | 13 +++--- download-docs.php | 11 +++-- error.php | 8 ++-- include/errors.inc | 6 ++- include/langchooser.inc | 3 +- include/languages.inc | 31 ++----------- include/layout.inc | 3 +- include/shared-manual.inc | 10 ++--- include/site.inc | 9 ++-- js/search-index.php | 4 +- manual/help-translate.php | 5 ++- mirror.php | 5 ++- my.php | 3 +- src/I18n/Languages.php | 74 +++++++++++++++++++++++++++++++ tests/Unit/I18n/LanguagesTest.php | 63 ++++++++++++++++++++++++++ 15 files changed, 190 insertions(+), 58 deletions(-) create mode 100644 src/I18n/Languages.php create mode 100644 tests/Unit/I18n/LanguagesTest.php diff --git a/docs.php b/docs.php index 04dd13107..3119d5bf7 100644 --- a/docs.php +++ b/docs.php @@ -1,4 +1,7 @@ "docs"]); $langname) { +$lastlang = array_key_last(Languages::ACTIVE_ONLINE_LANGUAGES); +foreach (Languages::ACTIVE_ONLINE_LANGUAGES as $langcode => $langname) { if (!file_exists($_SERVER["DOCUMENT_ROOT"] . "/manual/{$langcode}/index.php")) { continue; } // Make preferred language bold - if ($langcode == $LANG) { echo ""; } + if ($langcode === $LANG) { echo ""; } echo '' . $langname . ''; - echo ($lastlang != $langname) ? ",\n" : "\n"; + echo ($lastlang !== $langcode) ? ",\n" : "\n"; - if ($langcode == $LANG) { echo ""; } + if ($langcode === $LANG) { echo ""; } } ?> diff --git a/download-docs.php b/download-docs.php index 0d31fd10f..9abfc80f5 100644 --- a/download-docs.php +++ b/download-docs.php @@ -1,9 +1,12 @@ $language) { - if (isset($INACTIVE_ONLINE_LANGUAGES[$langcode]) && $MYSITE !== 'http://docs.php.net/') { +foreach (Languages::LANGUAGES as $langcode => $language) { + if (isset(Languages::INACTIVE_ONLINE_LANGUAGES[$langcode]) && $MYSITE !== 'http://docs.php.net/') { continue; } @@ -180,7 +183,7 @@ if (count($found_formats) == 0) { $cellclass = ""; } - echo "\n" . $LANGUAGES[$langcode] . "\n"; + echo "\n" . Languages::LANGUAGES[$langcode] . "\n"; foreach ($formats as $formatname => $extension) { diff --git a/error.php b/error.php index 2801188f1..423135a29 100644 --- a/error.php +++ b/error.php @@ -9,11 +9,11 @@ */ +use phpweb\I18n\Languages; use phpweb\UserPreferences; // Ensure that our environment is set up include_once __DIR__ . '/include/prepend.inc'; -include_once __DIR__ . '/include/languages.inc'; include_once __DIR__ . '/include/errors.inc'; // Get URI for this request, strip leading slash @@ -79,7 +79,7 @@ if (preg_match("!(.*\\.php)3$!", $URI, $array)) { // default language manual accessibility on mirror sites through /manual/filename) // @todo do we rely on this? how about removing it... if (preg_match("!^manual/([^/]*)$!", $URI, $array)) { - if (!isset($INACTIVE_ONLINE_LANGUAGES[$array[1]])) { + if (!isset(Languages::INACTIVE_ONLINE_LANGUAGES[$array[1]])) { mirror_redirect("/manual/$LANG/$array[1]"); } } elseif (preg_match("!^manual/html/([^/]+)$!", $URI, $array)) { @@ -705,10 +705,10 @@ if (preg_match("!^manual/(.+)/function\.(.+)-(.+).php$!", $URI, $array)) { // ============================================================================ // For manual pages for inactive languages, point visitors to the English page if (preg_match("!^manual/([^/]+)/([^/]+).php$!", $URI, $match) && - isset($INACTIVE_ONLINE_LANGUAGES[$match[1]])) { + isset(Languages::INACTIVE_ONLINE_LANGUAGES[$match[1]])) { $try = find_manual_page("en", $match[2]); if ($try) { - error_inactive_manual_page($INACTIVE_ONLINE_LANGUAGES[$match[1]], $try); + error_inactive_manual_page(Languages::INACTIVE_ONLINE_LANGUAGES[$match[1]], $try); } } diff --git a/include/errors.inc b/include/errors.inc index 17fd86912..76637c2da 100644 --- a/include/errors.inc +++ b/include/errors.inc @@ -5,6 +5,8 @@ not available. */ +use phpweb\I18n\Languages; + // A 'good looking' 404 error message page function error_404(): void { @@ -37,7 +39,7 @@ function error_404_manual(): void // An error message page for manual pages from inactive languages function error_inactive_manual_page($lang_name, $en_page): void { - global $MYSITE, $ACTIVE_ONLINE_LANGUAGES; + global $MYSITE; status_header(404); site_header('Page gone', ["noindex"]); echo "

Page gone

\n" . @@ -48,7 +50,7 @@ function error_inactive_manual_page($lang_name, $en_page): void echo "

The English page is available at {$en_url}

\n"; echo "

Several other languages are also available:

\n"; echo "