<?php
/**
 * Report the diff of 's' file
 * against matching file in language 'l', if it exists.
*/
define('HLANG', true);
include 'lib.php';

$source_file = get_sane_string('s');
$target_lang = get_sane_string('l');

if (empty($source_file) ||
    empty($target_lang)) {

    header('Location: /langs/report.php');
    die;
}

if (!file_exists($source_file)) {
    die('no source');
}

$resource    = _extract_resource($source_file);
$target_file = _po_file_switch($source_file, $target_lang);

if (!file_exists($target_file)) {
    die('no target');
}

if($resource =='about/constitution') {
    $constitution = true;
} else {
    $constitution = false;
}

if($resource =='about/license') {
    $license = true;
} else {
    $license = false;
}

if (false !== strstr($source_file, '../_nav/langs/en.')) {
    $file   = substr($target_file, 14);
    $note   = '<span style="color: red; font-weight: bold"> NOTE: THIS IS TRANSLATION OF NAVIGATION SO LOCATION FOR COMMIT IS DIFFERENT!</span>';
    $commit = '<a href="http://gitweb.mageia.org/web/nav/tree/langs/' . $file . '">/langs/' . $file . '</a>';
} else {
    $file   = $target_file;
    $note   = '';
    $commit = '<a href="http://gitweb.mageia.org/web/www/tree/langs/' . $file . '">/langs/' . $file . '</a>';
}

$source_file_path = realpath($source_file);
$target_file_path = realpath($target_file);

if (false === strstr($source_file_path, '/langs/') ||
    false === strstr($target_file_path, '/langs/')) {
    die('no sorry');
}

$diff = _po_diff($target_lang, $resource);

$issues = array(
    'missing' => 'missing string',
    'fuzzy_or_missing' => 'fuzzy or missing string',
    'notrans' => 'untranslated string',
    'extra' => 'unused (old) string',
    'dup_str' => 'duplicate string',
);

$s = '<a href="report.php?l=' . $target_lang . '">&laquo; back to the report page for ' . $langs[$target_lang] . '</a>';
$s .= sprintf('<h1>Differences between %s source and %s target</h1>',
    $source_file, $target_lang);

$tx_resource_name = 'page-' . str_replace(array('../_nav/langs/en', '/'), array('nav', '-'), $resource);
$tx_language_code = locale_hyphen_underscore($target_lang, true);
$s .= '<p>You can translate most of our web page resources online in ';
$s .= "<a href=\"http://www.transifex.com/organization/MageiaLinux/dashboard/all_resources/";
$s .= "$tx_language_code/#1/?s=$tx_resource_name&amp;c=Webpages\">Transifex</a>.</p>";


$s .= '<p>Please take a look off the translation guide on the <a href="https://wiki.mageia.org/en/Internationalisation_Team_(i18n)#Website_translation">localization Wiki page</a> too.</p>';

$additional_file = '';

if($constitution) {
    $s .= '<h2 style="color: red;">Please translate constitution fully into ' . $langs[$target_lang] . ' first</h2>';
    $s .= '<p>You can find it in <a href="http://gitweb.mageia.org/web/www/tree/en/about/constitution/">git</a>. ';

    $constitution_results = aproximate_number_of_untranslated_constitution_lines(G_APP_ROOT, 'en');
    $unique_lines_in_eng_constitution = $constitution_results['unique_lines_in_constitution'];
    $lang_constitution_results = aproximate_number_of_untranslated_constitution_lines(G_APP_ROOT, $target_lang, $unique_lines_in_eng_constitution);
    $constitution_readable = $lang_constitution_results['constitution_readable'];
    $diff['untranslated_lines_in_constitution'] = $lang_constitution_results['untranslated_lines_in_constitution']; // add untranslated license sentences
    $dest_constitution = sprintf('%s/%s_%s.md"', 'href="http://gitweb.mageia.org/web/www/tree/en/about/constitution', 'mageia.org_statutes', ($constitution_readable ? $target_lang : 'en'));
    $issues['untranslated_lines_in_constitution'] = 'of all untranslated lines in separate <a ' . $dest_constitution . '>constitution md format file</a>';
    $additional_file = "\t\t<li>same goes for <a $dest_constitution>constitution</a></li>";
}

if($license) {
    require_once('../en/about/license/license.php');
    $license_array = read_license_from_vcs($target_lang);
    $license_numbers = array_pop($license_array);
    $num_of_untranslated_strings = $license_numbers["untran"]; // number of all license sentences
    $diff['untranslated_sentences_in_license'] = $license_numbers['untranslated_sentences']; // add untranslated license sentences
    $s .= '<h2 style="color: red;">Please translate <a href="http://gitweb.mageia.org/software/drakx/tree/perl-install/share/po/' . $target_lang . '.po">separate license file</a> fully into ' . $langs[$target_lang] . ' first</h2>';
    $s .= '<p>You can find it as a part of a separate <a href="http://gitweb.mageia.org/software/drakx/tree/perl-install/share/po/' . $target_lang . '.po">';
    $s .= 'drakx translation file in git</a> or in <a href="https://www.transifex.com/organization/MageiaLinux/dashboard/all_resources/';
    $s .= $tx_language_code . '/#1/?s=drakx_share">Transifex</a>. '; // git doesn't work yet on https
    $issues['untranslated_sentences_in_license'] = 'untranslated msgid strings in ' . $num_of_untranslated_strings . ' license sentences as a part of a separate <a href="http://gitweb.mageia.org/software/drakx/tree/perl-install/share/po/' . $target_lang . '.po">drakx translation file</a> (it starts before the line with the string "1. License Agreement")';
}

if($constitution || $license) {
    $s .= 'Please read <a href="https://wiki.mageia.org/en/Internationalisation_Team_%28i18n%29#Special_cases_of_web_pages">page on wiki for more information</a> about that.</p>';
}

$s .= '<p>After translation:</p>';
$s .= "\t<ul>";
$s .= "\t\t<li>if you're in <a href=\"http://people.mageia.org/g/mga-i18n-committers.html\">mga-i18n-committers group</a>,";
$s .= " you may commit updated po file directly in <code>$commit.</code>$note</li>";
$s .= "\t\t<li>or attach it to a mail to <a href=\"https://ml.mageia.org/l/info/i18n-discuss\">Mageia localization team</a> (<code>mageia-i18n</code>, <a href=\"irc://irc.freenode.net/#mageia-i18n\">#mageia-i18n</a> on Freenode).</li>";
$s .= $additional_file;
$s .= "\t</ul>";

$new_line = "\n\n";
$filename = ' in ' . $commit; // $file
$title_prefix = 'Report found ';
foreach ($issues as $type => $name) {
    if (isset($diff[$type]) && count($diff[$type]) > 0) {
        if ($type == 'untranslated_lines_in_constitution') {
            $prefix = '';
            $new_line = "\n";
            $filename = '';
        } else if ($type == 'untranslated_sentences_in_license') {
            $prefix = 'msgid: ';
            $filename = '';
        } else {
            $prefix = ';';
        }
        $s .= sprintf('<h2>%s %d %s%s:</h2>', $title_prefix, count($diff[$type]), $name . ((count($diff[$type]) > 1) ? 's' : ''), $filename);
        $s .= '<pre>';
        foreach ($diff[$type] as $l)
            $s .= sprintf("%s%s%s", $prefix, rtrim(htmlspecialchars($l)), $new_line); // cut at 200: substr(rtrim(htmlspecialchars($l)), 0, 200)
        $s .= '</pre>';
        $filename = '';
        $title_prefix = 'and also ';
    }
}

header('Content-Type: text/html;charset=utf-8');

?><!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="robot" content="noindex,nofollow,nosnippet">
    <?php echo sprintf('<title>Differences between %s source and %s target</title>',
    $source_file, $target_lang); ?>
    <style>
    pre { background: #eee; padding: 0.6em; }
    </style>
</head>
<body>
    <?php echo $s; ?>
<hr>
</body>
</html>