Completed
Push — master ( 914472...99cacd )
by Adam
01:40 queued 14s
created

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/* PROJECT:     ReactOS Translation Tool
3
 * LICENSE:     GPL
4
 * AUTHORS:     Adam Stachowicz <[email protected]>
5
 * AUTHOR URL:  http://it-maniak.pl/
6
 */
7
8
include_once 'header.php';
9
include_once 'langcodes.php';
10
?>
11
12
<h1>Search missing translation strings</h1>
13
14
<div id="body">
15
<center>
16
    <form method="GET" class="form-horizontal">
17
        <fieldset>
18
            <legend>Please choose your language and the directories, where you want to search for untranslated strings, from the lists below.</legend>
19
            <div class="form-group">
20
                <label class="col-md-4 control-label" for="lang">Language:</label>
21
                <div class="col-md-4">
22
                    <select id="lang" name="lang" class="form-control" required="required">
23 View Code Duplication
                        <?php foreach ($langcodes as $language) {
24
    echo '<option value="'.$language[0].'" ';
25
    if (isset($_SESSION['lang']) && $language[0] == $_SESSION['lang']) {
26
        echo 'selected';
27
    }
28
    echo '> $language[1]</option>';
29
}?>
30
                    </select>
31
                </div>
32
            </div>
33
            <div class="form-group">
34
                <label class="col-md-4 control-label" for="dir">Directories:</label>
35
                <div class="col-md-4">
36
                <select id="dir" name="dir" class="form-control">
37
                    <option value="1">base, boot</option>
38
                    <option value="2" <?php if (isset($_GET['dir']) && $_GET['dir'] == '2') {
39
    echo 'selected';
40
}?>>dll</option>
41
                    <option value="3" <?php if (isset($_GET['dir']) && $_GET['dir'] == '3') {
42
    echo 'selected';
43
}?>>media, subsystems, win32ss</option>
44
                    <option value="100" <?php if (isset($_GET['dir']) && $_GET['dir'] == '100') {
45
    echo 'selected';
46
}?>>All ReactOS Source dir</option>
47
                </select>
48
                </div>
49
            </div>
50
            <div class="form-group">
51
                <button type="submit" class="btn btn-primary">Search</button>
52
            </div>
53
        </fieldset>
54
    </form>
55
</center>
56
<br>
57
58
<?php
59
if (isset($_GET['lang']) && !empty($_GET['lang']) && isset($_GET['dir']) && is_numeric($_GET['dir'])) {
60
    $it = new AppendIterator();
61
62
    // Switch for directories
63 View Code Duplication
    switch ($_GET['dir']) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
        case '1':
65
            $directories = [
66
                new RecursiveDirectoryIterator($ROSDir.'base/applications'),
67
                new RecursiveDirectoryIterator($ROSDir.'base/setup'),
68
                new RecursiveDirectoryIterator($ROSDir.'base/shell'),
69
                new RecursiveDirectoryIterator($ROSDir.'base/system'),
70
                new RecursiveDirectoryIterator($ROSDir.'boot/freeldr/fdebug'),
71
            ];
72
            break;
73
74
        case '2':
75
            $directories = [
76
                new RecursiveDirectoryIterator($ROSDir.'dll/cpl'),
77
                new RecursiveDirectoryIterator($ROSDir.'dll/shellext'),
78
                new RecursiveDirectoryIterator($ROSDir.'dll/win32'),
79
            ];
80
            break;
81
82
        case '3':
83
            $directories = [
84
                new RecursiveDirectoryIterator($ROSDir.'media/themes'),
85
                new RecursiveDirectoryIterator($ROSDir.'subsystems/mvdm/ntvdm'),
86
                new RecursiveDirectoryIterator($ROSDir.'win32ss/user'),
87
            ];
88
            break;
89
90
        case '100':
91
            $directories = [
92
                new RecursiveDirectoryIterator($ROSDir),
93
            ];
94
            break;
95
96
        default:
97
            echo 'Something is wrong! Please try again.';
98
            exit;
99
    }
100
101
    foreach ($directories as $directory) {
102
        $it->append(new RecursiveIteratorIterator($directory));
103
    }
104
105
    function diff_versions($leftContent, $rightContent)
106
    {
107
        $rightVersion = null;
108
109
        $pattern = '/^(?!FONT|\\s*\\*|\\#include|\\s*ICON)[^"\\n]*"\\K(?!\\s*(?:"|\\n))([^"]+)/m';
110
111
        if (preg_match_all($pattern, $leftContent, $matches) <= 0) {
112
            throw new Exception('Left content has no version line.');
113
        }
114
115
        $leftVersion = $matches[1];
116
117
        if (preg_match_all($pattern, $rightContent, $matches) <= 0) {
118
            throw new Exception('Right content has no version line.');
119
        }
120
121
        $rightVersion = $matches[1];
122
123
        return [
124
            'diff'         => array_intersect($leftVersion, $rightVersion),
125
            'leftVersion'  => $leftVersion,
126
            'rightVersion' => $rightVersion,
127
        ];
128
    }
129
130
    function exceptions_error_handler($severity, $message, $filename, $lineno)
131
    {
132
        if (error_reporting() == 0) {
133
            return;
134
        }
135
        if (error_reporting() & $severity) {
136
            throw new ErrorException($message, 0, $severity, $filename, $lineno);
137
        }
138
    }
139
140
    set_error_handler('exceptions_error_handler');
141
142
    $regex = new RegexIterator($it, '/^.+'.$langDir.'.+('.$originLang.')\.'.$fileExt.'$/i', RecursiveRegexIterator::GET_MATCH);
143
144
    $missing = $allStrings = 0;
145
146
    $lang = htmlspecialchars($_GET['lang']);
147
    // Search for eg. PL,Pl,pl
148
    $fileSearch = strtoupper($lang).','.ucfirst($lang).','.strtolower($lang);
149
150
    // ReactOS and Wine Strings - array
151
    $ignoredROSStrings = file($ROSSpellFilename, FILE_IGNORE_NEW_LINES);
152
    $ignoredWineStrings = file($wineSpellFilename, FILE_IGNORE_NEW_LINES);
153
154
    $regex->rewind();
155
    while ($regex->valid()) {
156
        if (!$regex->isDot()) {
157
            $file = glob($regex->getPathInfo().'/*{'.$fileSearch.'}*.'.$fileExt, GLOB_BRACE);
158
159
            $isFile = array_filter($file);
160
161
            if (empty($isFile)) {
162
                echo '<b>No translation</b> for path '.$regex->getPathInfo().'<hr>';
163
            } else {
164
                $fileContent1 = file_get_contents($regex->key());
165
                $fileContent2 = file_get_contents($file[0]);
166
167
                $array = diff_versions($fileContent1, $fileContent2);
168
169
                if ($array['diff']) {
170
                    $currentMissing = $missing;
171
                    $missingTextMessage = null;
172
173
                    foreach ($array['leftVersion'] as $index => $english) {
174
                        // Catch offset error
175
                        try {
176
                            // Check if this same and ignore some words
177
                            if ($english === $array['rightVersion'][$index] && !in_array($english, $ignoredROSStrings) && !in_array($english, $ignoredWineStrings)) {
178
                                $missingTextMessage .= '<b>Missing translation:</b> '.htmlspecialchars($english).'<br>';
179
                                $missing++;
180
                            }
181
                            $allStrings++;
182
                        } catch (Exception $e) {
183
                            $missingTextMessage .= 'Missing stuff in your language<br>';
184
                            $allStrings++;
185
                            $missing++;
186
                        }
187
                    }
188
189
                    if ($currentMissing == $missing) {
190
                        $messageForFile = 'Seems <b>OK :)</b> Some strings was ignored by ReactOS and Wine spell files.<br>';
191
                    } elseif ($detailsTag) {
192
                        $messageForFile = '<details open><summary><strong>Click here to see/hide missing translations in file ('.($missing - $currentMissing).')</strong></summary>'.$missingTextMessage.'</details>';
193
                    } else {
194
                        $messageForFile = $missingTextMessage;
195
                    }
196
197
                    if ($currentMissing != $missing || $showTranslationOK) {
198
                        $pathFromRoot = str_replace($ROSDir, '', $regex->getPathInfo());
199
                        echo $regex->getPathInfo().' (<a href="https://github.com/reactos/reactos/tree/master/'.$pathFromRoot.'">Go to GitHub</a>)<br><br>'.$messageForFile.'<hr>';
200
                    }
201
                }
202
            }
203
        }
204
        $regex->next();
205
    }
206
207
    $languppercase = strtoupper($lang);
208
209
    echo "<h3>All strings for english: $allStrings</h3>";
210
    echo "<h3>Missing translations for your language ($languppercase): $missing</h3>";
211
212
    // Rounded percent
213
    $percent = round((($allStrings - $missing) / $allStrings) * 100, 2);
214
    echo "<h3>Language $languppercase translated in $percent%</h3>";
215
}
216
217
include_once 'footer.php';
218