From 9c21aa31ff6e0bd4cdcba33329577a56b75c6691 Mon Sep 17 00:00:00 2001 From: filip Date: Sat, 17 Sep 2016 14:21:30 +0200 Subject: move functions to lib --- langs/lib.php | 246 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 246 insertions(+) (limited to 'langs/lib.php') diff --git a/langs/lib.php b/langs/lib.php index 1b016c8a4..522e80a14 100644 --- a/langs/lib.php +++ b/langs/lib.php @@ -487,6 +487,252 @@ function build_language_and_resource_summary($report, $all_languages_only_one_re ); } +/** + * Transifex API implementation in php detailed on http://docs.transifex.com/developer/api/projects + * + * Returns $tx_result_array from Transifex request + * + * @param string $tx_request request + * @param string $project default project + * + * @return array +*/ +function tx_call($tx_request, $project = 'project/mageia/') +{ + global $errors; + $tx_url_prefix = "www.transifex.com/api/2/"; + $tx_url = $tx_url_prefix . $project . $tx_request; + + $user = "filip_mageia"; + $pass = "report"; + + $tx_result = @file_get_contents("https://$user:$pass@$tx_url"); + + $tx_result_array = json_decode($tx_result, TRUE); + $json_last_error = json_last_error(); + if (JSON_ERROR_NONE !== $json_last_error) { + $error = "There was an error during API call to Transifex $tx_request ($json_last_error)."; + $errors['tx_json_error'] = "$error Please reload the page later and report this on mailing list if it persist."; + $tx_result_array = array(); + } else if (FALSE === $tx_result) { + $error = "API call to Transifex $tx_request failed."; + $errors['tx_call'] = "$error Please reload the page later and report this on mailing list if it persist."; + $tx_result_array = array(); + } + return $tx_result_array; +} + +/** + * Convert git resource name to Transifex one or reverse + * + * @param string $resource_name like 'about/constitution' (git name) + * @param string $category like 'Webpages', 'Cauldron' or 'Documentation' + * @param boolean $tx_to_git_name_conversion direction of conversion + * + * @return string $resource_name like 'page-about-constitution' (tx name) +*/ +function resource_name_conversion($resource_name, $category = '') +{ + if ('Webpages' == $category) { + $tx_names = array('nav', '-'); + $git_names = array('_nav/langs/en', '/'); + $resource_name = 'page-' . str_replace($git_names, $tx_names, $resource_name); + } else if ('Cauldron' == $category) { + $tx_names = array('mageia-welcome', 'identity-catdap'); + $git_names = array('Mageia%20Welcome', 'Identity%20(CatDap)'); + $resource_name = str_replace($git_names, $tx_names, $resource_name); + } + + return $resource_name; +} + +/** + * Build Transifex and git links with numbers of untranslated strings + * + * Returns text string + * + * @param string $git_resource_name like 'about/constitution' + * @param string $language_code like 'sl' + * @param string $resource_type like 'Webpages' + * @param array $stat_data with numbers of untranslated strings + * + * @return string +*/ +function build_links($git_resource_name, $language_code, $resource_type, $stat_data) +{ + $tx_resource_name = resource_name_conversion($git_resource_name); + $locale_name = locale_underscore_to_hyphen($language_code); + if ('Webpages' == $resource_type) { + if ('nav' == $git_resource_name) { + $git_link = sprintf('http://gitweb.mageia.org/web/nav/tree/langs/%s.po', $locale_name); + } else { + $git_link = sprintf('http://gitweb.mageia.org/web/www/tree/langs/%s/%s.po', $locale_name, $git_resource_name); + } + } else { + // fixing exceptions as there is a different naming convention between Tx and git for some lanuages: + if ('system-config-printer' == $git_resource_name && 'sr@latin' == $language_code) { + $lang_code = 'sr@latin'; + } else if ('identity-catdap' == $git_resource_name && 'en_GB' == $language_code) { + $lang_code = 'en_gb'; + } else { + $tx_array = array('sr@latin', 'uz@Cyrl'); + $git_array = array('sr@Latn', 'uz@cyrillic'); + $lang_code = str_replace($tx_array, $git_array, $language_code); + } + // treat TS files differently + if (false !== strpos($stat_data['pot_name'], '_en.ts')) { + $partial_git_resource_name = substr($stat_data['pot_name'], 0, -6); // cuts '_en.ts' + $git_link = sprintf('%s%s_%s.ts', $stat_data['git_path'], $partial_git_resource_name, $lang_code); // mageiaSync_sl.ts + } else { + $git_link = sprintf('%s/%s.po', $stat_data['git_path'], $lang_code); + } + } + $links_and_num = build_transifex_link($language_code, 'Tx', $resource_type, $tx_resource_name) . ": "; + $links_and_num .= $stat_data['tx_untran']; + $links_and_num .= (0 == $stat_data['tx_untran'] ? ' - full' : ''); + $links_and_num .= ', git: '; + $links_and_num .= $stat_data['git_untran']; + $links_and_num .= ($stat_data['num_of_all'] == $stat_data['git_untran'] ? ' - empty' : ''); + + return $links_and_num; +} + +/** + * Build customized Transifex link + * + * @param string $tx_language_code like 'pt_BR' + * @param string $link_name is the visible link name + * @param string $tx_category like 'Webpages' + * @param string $tx_resource_name like 'page-4' + * + * @return string customized Transifex link +*/ +function build_transifex_link($tx_language_code, $link_name = NULL, $tx_category = NULL, $tx_resource_name = NULL) +{ +// Transifex cripled direct link access in a new version in a way that limiting +// the result by the category or resource name is no longer possible +// $prefix = ""; + if(is_null($link_name)) { + $transifex_url .= $tx_language_code; + } else { + $transifex_url .= $link_name; + } + $transifex_url .= ""; + + return $transifex_url; +} + +/** + * Generating report about git resources statistics + * + * @param array $language_codes list + * @param array $resource_names list + * @param string $path to the git, otherwise NULL + * + * @return array +*/ +function generating_report($language_codes, $resource_names, $path = NULL, $pot_name = NULL, $compared = NULL) +{ + $report = array(); + foreach ($resource_names as $f) { + $source_strings = NULL; + foreach ($language_codes as $l) { + $references = ''; + if (is_null($pot_name)) { + $resource = _extract_resource($f); + } else { + $resource = $pot_name; + } + $langF = _po_file_switch($f, $l); + if (strstr($f, '../_nav/langs/en.') !== FALSE) { + $langF = '../_nav/langs/' . $l . '.po' . (($l == 'en') ? 't' : ''); + } + if (!is_null($path) || file_exists($langF)) { + // treat TS files differently + if (false !== strpos($pot_name, '_en.ts')) { + $partial_pot_name = substr($pot_name, 0, -6); // cuts '_en.ts' from mageiaSync_en.ts + $stat = _ts_diff($l, $partial_pot_name, $source_strings, $path); + } else { + $stat = _po_diff($l, $resource, $source_strings, $path, $compared); + } + $num_of_fuzzy_or_missing = count($stat['fuzzy_or_missing']); + } else { // file $langF doesn't exists in 'Webpages' $resource_type + $stat = _po_diff('en', $resource, $source_strings); + $num_of_fuzzy_or_missing = 0; + } + $num_of_untranslated = count($stat['notrans']); + $source_strings = $stat['source_strings']; + // unify resource names, navigation is a special exception + $resource_name = str_replace(array('../_nav/langs/en', 'en/', '.pot'), array('nav', '', ''), $f); + // fixing exceptions as there is a different naming convention between Tx and git for some lanuages: + if ('system-config-printer' == $resource_name && 'sr@latin' == $l) { + $web_language_code = 'sr@latin'; + } else if ('identity-catdap' == $resource_name && 'en_gb' == $l) { + $web_language_code = 'en_GB'; + } else { + $git_array = array('sr@Latn', 'uz@cyrillic'); + $tx_array = array('sr@latin', 'uz@Cyrl'); + // create pt_BR from pt-br and alike to unify languages + $web_language_code = locale_hyphen_underscore($l, true); + $web_language_code = str_replace($git_array, $tx_array, $web_language_code); + } + $num_of_not_fully_trans = $num_of_fuzzy_or_missing + $num_of_untranslated; + $report[] = array( + 'num_of_all_strings' => $stat['a'], +// 'fuzzy_or_missing_str' => $stat['fuzzy_or_missing'], +// 'untranslated_strings' => $stat['notrans'], +// 'source_strings' => $stat['source_strings'], + 'differences' => $stat['differences'], + 'resource_name' => $resource_name, + 'web_language_code' => $web_language_code, + 'num_of_not_fully_trans' => $num_of_not_fully_trans, + 'webgit_path' => $path, + 'pot_name' => $pot_name, + ); + } + } + return $report; +} + +/** + * Returns native language name from Mageia web site if exists otherwise English name from Transifex + * + * @param string $language_code for language + * + * @return string +*/ +function get_language_name($language_code) +{ + global $langs; + static $tx_languages_details = NULL; + $web_language_code = locale_underscore_to_hyphen($language_code); + if (array_key_exists($web_language_code, $langs)) { + $language_name = $langs[$web_language_code]; + } else { + if (is_null($tx_languages_details)) { + $tx_languages_details = tx_call("languages", ''); + } + // is language code in the $tx_languages_details array? + $key_exists = recursive_array_search($language_code, $tx_languages_details); + if ($key_exists !== FALSE) { + $language_name = $tx_languages_details[$key_exists]['name']; + } else { + $language_name = $language_code; + } + } + return $language_name; +} + /** * from http://www.php.net/manual/en/function.array-search.php#91365 * -- cgit v1.2.1