Completed
Pull Request — master (#5)
by
unknown
04:36
created

diff.php (1 issue)

Labels
Severity

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
<!DOCTYPE html>
2
3
<?php
4
/* PROJECT:     ReactOS Translation Tool
5
 * LICENSE:     GPL
6
 * AUTHORS:     Adam Stachowicz <[email protected]>
7
 * AUTHOR URL:  http://it-maniak.pl/
8
 */
9
10
include_once 'header.php';
11
?>
12
13
<h1>Search missing translation strings</h1>
14
15
<html>
16
    <div id="body">
17
        <center>
18
            <form method="GET" class="form-horizontal">
19
                <fieldset>
20
                    <legend>Please type your <a href="https://beta.wikiversity.org/wiki/List_of_ISO_639-1_codes">language code in ISO 639-1</a>. For example: pl for Polish, de for German</legend>
21
                    <div class="form-group">
22
                        <label class="col-md-4 control-label" for="lang">Language code:</label>
23
                        <div class="col-md-4">
24
                            <input type="text" value="<?php echo isset($_SESSION['lang']) ? $_SESSION['lang'] : '' ?>" id="lang" name="lang" class="form-control input-md" required="required" autofocus="autofocus" pattern="[A-Za-z]{2}" title="Two letter language code"/>
25
                        </div>
26
                    </div>
27
                    <div class="form-group">
28
                        <label class="col-md-4 control-label" for="dir">Directories:</label>
29
                        <div class="col-md-4">
30
                            <select id="dir" name="dir" class="form-control">
31
                                <option value="1">base, boot</option>
32
                                <option value="2" <?php if (isset($_GET['dir']) && $_GET['dir'] == '2') {
33
                                echo 'selected';
34
                                }?>>dll</option>
35
                                <option value="3" <?php if (isset($_GET['dir']) && $_GET['dir'] == '3') {
36
                                echo 'selected';
37
                                }?>>media, subsystems, win32ss</option>
38
                                <option value="100" <?php if (isset($_GET['dir']) && $_GET['dir'] == '100') {
39
                                echo 'selected';
40
                                }?>>All ReactOS Source dir</option>
41
                            </select>
42
                        </div>
43
                    </div>
44
                    <div class="form-group">
45
                        <button type="submit" class="btn btn-primary">Search</button>
46
                    </div>
47
                </fieldset>
48
            </form>
49
        </center>
50
    </div>
51
</html>
52
<br>
53
54
<?php
55
if (isset($_GET['lang']) && !empty($_GET['lang']) && isset($_GET['dir']) && is_numeric($_GET['dir'])) {
56
    $it = new AppendIterator();
57
58
    // Switch for directories
59
    switch ($_GET['dir']) {
60
        case '1':
61
            $directories = [
62
                new RecursiveDirectoryIterator($ROSDir.'base/applications'),
63
                new RecursiveDirectoryIterator($ROSDir.'base/setup'),
64
                new RecursiveDirectoryIterator($ROSDir.'base/shell'),
65
                new RecursiveDirectoryIterator($ROSDir.'base/system'),
66
                new RecursiveDirectoryIterator($ROSDir.'boot/freeldr/fdebug'),
67
            ];
68
            break;
69
70
        case '2':
71
            $directories = [
72
                new RecursiveDirectoryIterator($ROSDir.'dll/cpl'),
73
                new RecursiveDirectoryIterator($ROSDir.'dll/shellext'),
74
                new RecursiveDirectoryIterator($ROSDir.'dll/win32'),
75
            ];
76
            break;
77
78
        case '3':
79
            $directories = [
80
                new RecursiveDirectoryIterator($ROSDir.'media/themes'),
81
                new RecursiveDirectoryIterator($ROSDir.'subsystems/mvdm/ntvdm'),
82
                new RecursiveDirectoryIterator($ROSDir.'win32ss/user'),
83
            ];
84
            break;
85
86
        case '100':
87
            $directories = [
88
                new RecursiveDirectoryIterator($ROSDir),
89
            ];
90
            break;
91
92
        default:
93
            echo 'Something is wrong! Please try again.';
94
            exit;
95
    }
96
97
    foreach ($directories as $directory) {
98
        $it->append(new RecursiveIteratorIterator($directory));
99
    }
100
101
    function diff_versions($leftContent, $rightContent)
102
    {
103
        $leftVersion = $rightVersion = null;
104
105
        // FIXME: Search multi-line with ""some text""
106
        $pattern = '/^(?!FONT|\\s*\\*|\\#\\include|\\s*\\ICON)[^"\\n]*"\\K(?!\\s*(?:"|\\n))([^"]+)/m';
107
108
        if (preg_match_all($pattern, $leftContent, $matches) <= 0) {
109
            throw new Exception('Left content has no version line.');
110
        }
111
112
        $leftVersion = $matches[1];
113
114
        if (preg_match_all($pattern, $rightContent, $matches) <= 0) {
115
            throw new Exception('Right content has no version line.');
116
        }
117
118
        $rightVersion = $matches[1];
119
120
        return [
121
            'diff'         => array_intersect($leftVersion, $rightVersion),
122
            'leftVersion'  => $leftVersion,
123
            'rightVersion' => $rightVersion,
124
        ];
125
    }
126
127
    function exceptions_error_handler($severity, $message, $filename, $lineno)
128
    {
129
        if (error_reporting() == 0) {
130
            return;
131
        }
132
        if (error_reporting() & $severity) {
133
            throw new ErrorException($message, 0, $severity, $filename, $lineno);
134
        }
135
    }
136
137
    set_error_handler('exceptions_error_handler');
138
139
    $regex = new RegexIterator($it, '/^.+'.$langDir.'.+('.$originLang.')\.'.$fileExt.'$/i', RecursiveRegexIterator::GET_MATCH);
140
141
    $missing = $allStrings = 0;
142
143
    $lang = htmlspecialchars($_GET['lang']);
144
    // Search for eg. PL,Pl,pl
145
    $fileSearch = strtoupper($lang).','.ucfirst($lang).','.strtolower($lang);
146
147
    // ReactOS and Wine Strings - array
148
    $ignoredROSStrings = file($ROSSpellFilename, FILE_IGNORE_NEW_LINES);
149
    $ignoredWineStrings = file($wineSpellFilename, FILE_IGNORE_NEW_LINES);
150
151
    $regex->rewind();
152
    while ($regex->valid()) {
153
        if (!$regex->isDot()) {
154
            $file = glob($regex->getPathInfo().'/*{'.$fileSearch.'}*.'.$fileExt, GLOB_BRACE);
155
156
            $isFile = array_filter($file);
157
158
            if (empty($isFile)) {
159
                echo '<b>No translation</b> for path '.$regex->getPathInfo().'<hr>';
160
            } else {
161
                $fileContent1 = file_get_contents($regex->key());
162
                $fileContent2 = file_get_contents($file[0]);
163
164
                $array = diff_versions($fileContent1, $fileContent2);
165
166
                if ($array['diff']) {
167
                    echo $regex->getPathInfo().'<br><br>';
168
169
                    $currentMissing = $missing;
170
171
                    foreach ($array['leftVersion'] as $index => $english) {
172
                        // Catch offset error
173
                        try {
174
                            // Check if this same and ignore some words
175
                            if ($english === $array['rightVersion'][$index] && !in_array($english, $ignoredROSStrings) && !in_array($english, $ignoredWineStrings)) {
176
                                echo '<b>Missing translation:</b> '.htmlspecialchars($english).'<br>';
177
                                $missing++;
178
                            }
179
                            $allStrings++;
180
                        } catch (Exception $e) {
181
                            echo 'Missing stuff in your language<br>';
182
                            $allStrings++;
183
                            $missing++;
184
                        }
185
                    }
186
                    if ($currentMissing == $missing) {
187
                        echo 'Seems <b>OK :)</b> Some strings was ignored by ReactOS and Wine spell files.<br>';
188
                    }
189
190
                    echo '<hr>';
191
                }
192
            }
193
        }
194
        $regex->next();
195
    }
196
    
197
    $languppercase = strtoupper($lang)
198
    
199
    echo "<h3>All strings for english: $allStrings</h3>";
0 ignored issues
show
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_ECHO
Loading history...
200
    echo "<h3>Missing translations for your language ($languppercase): $missing</h3>";
201
202
    // Rounded percent
203
    $percent = round((($allStrings - $missing) / $allStrings) * 100, 2);
204
    echo "<h3>Language $languppercase translated in $percent%</h3>";
205
}
206
207
include_once 'footer.php';
208