Passed
Push — scrutinizer-migrate-to-new-eng... ( 58afd6 )
by Alexander
18:11
created

TranslationController   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 2
dl 0
loc 121
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\build\controllers;
9
10
use DirectoryIterator;
11
use Yii;
12
use yii\console\Controller;
13
use yii\helpers\Html;
14
15
/**
16
 * TranslationController handles tasks related to framework translations.
17
 *
18
 * build translation "../docs/guide" "../docs/guide-ru" "Russian guide translation report" > report_guide_ru.html
19
 *
20
 * @author Alexander Makarov <[email protected]>
21
 */
22
class TranslationController extends Controller
23
{
24
    public $defaultAction = 'report';
25
26
    /**
27
     * Creates a report about documentation updates since last update of same named translations.
28
     *
29
     * @param string $sourcePath the directory where the original documentation files are
30
     * @param string $translationPath the directory where the translated documentation files are
31
     * @param string $title custom title to use for report
32
     * @return string
33
     */
34
    public function actionReport($sourcePath, $translationPath, $title = 'Translation report')
35
    {
36
        $sourcePath = trim($sourcePath, '/\\');
37
        $translationPath = trim($translationPath, '/\\');
38
39
        $results = [];
40
41
        $dir = new DirectoryIterator($sourcePath);
42
        foreach ($dir as $fileinfo) {
43
            /* @var $fileinfo DirectoryIterator */
44
            if (!$fileinfo->isDot() && !$fileinfo->isDir()) {
45
                $translatedFilePath = $translationPath . '/' . $fileinfo->getFilename();
46
                $sourceFilePath = $sourcePath . '/' . $fileinfo->getFilename();
47
48
                $errors = $this->checkFiles($translatedFilePath);
49
                $diff = empty($errors) ? $this->getDiff($translatedFilePath, $sourceFilePath) : '';
50
                if (!empty($diff)) {
51
                    $errors[] = 'Translation outdated.';
52
                }
53
54
                $result = [
55
                    'errors' => $errors,
56
                    'diff' => $diff,
57
                ];
58
59
                $results[$fileinfo->getFilename()] = $result;
60
            }
61
        }
62
63
        // checking if there are obsolete translation files
64
        $dir = new DirectoryIterator($translationPath);
65
        foreach ($dir as $fileinfo) {
66
            /* @var $fileinfo \DirectoryIterator */
67
            if (!$fileinfo->isDot() && !$fileinfo->isDir()) {
68
                $translatedFilePath = $translationPath . '/' . $fileinfo->getFilename();
69
70
                $errors = $this->checkFiles(null, $translatedFilePath);
71
                if (!empty($errors)) {
72
                    $results[$fileinfo->getFilename()]['errors'] = $errors;
73
                }
74
            }
75
        }
76
77
        echo $this->renderFile(__DIR__ . '/views/translation/report_html.php', [
78
            'results' => $results,
79
            'sourcePath' => $sourcePath,
80
            'translationPath' => $translationPath,
81
            'title' => $title,
82
        ]);
83
    }
84
85
    /**
86
     * Checks for files existence.
87
     *
88
     * @param string $translatedFilePath
89
     * @param string $sourceFilePath
90
     * @return array errors
91
     */
92
    protected function checkFiles($translatedFilePath = null, $sourceFilePath = null)
93
    {
94
        $errors = [];
95
        if ($translatedFilePath !== null && !file_exists($translatedFilePath)) {
96
            $errors[] = 'Translation does not exist.';
97
        }
98
99
        if ($sourceFilePath !== null && !file_exists($sourceFilePath)) {
100
            $errors[] = 'Source does not exist.';
101
        }
102
103
        return $errors;
104
    }
105
106
    /**
107
     * Getting DIFF from git.
108
     *
109
     * @param string $translatedFilePath path pointing to translated file
110
     * @param string $sourceFilePath path pointing to original file
111
     * @return string DIFF
112
     */
113
    protected function getDiff($translatedFilePath, $sourceFilePath)
114
    {
115
        $lastTranslationHash = shell_exec('git log -1 --format=format:"%H" -- ' . $translatedFilePath);
116
        return shell_exec('git diff ' . $lastTranslationHash . '..HEAD -- ' . $sourceFilePath);
117
    }
118
119
    /**
120
     * Adds all necessary HTML tags and classes to diff output.
121
     *
122
     * @param string $diff DIFF
123
     * @return string highlighted DIFF
124
     */
125
    public function highlightDiff($diff)
126
    {
127
        $lines = explode("\n", $diff);
128
        foreach ($lines as $key => $val) {
129
            if (mb_substr($val, 0, 1, 'utf-8') === '@') {
130
                $lines[$key] = '<span class="info">' . Html::encode($val) . '</span>';
131
            } elseif (mb_substr($val, 0, 1, 'utf-8') === '+') {
132
                $lines[$key] = '<ins>' . Html::encode($val) . '</ins>';
133
            } elseif (mb_substr($val, 0, 1, 'utf-8') === '-') {
134
                $lines[$key] = '<del>' . Html::encode($val) . '</del>';
135
            } else {
136
                $lines[$key] = Html::encode($val);
137
            }
138
        }
139
140
        return implode("\n", $lines);
141
    }
142
}
143