Completed
Push — master ( 394ecf...2ae0a1 )
by Adam
02:35 queued 01:17
created

diff.php (3 issues)

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
                        <?php
24 View Code Duplication
                        foreach ($langcodes as $language) {
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...
25
                            echo '<option value="'.$language[0].'" ';
26
                            if (isset($_SESSION['lang']) && $language[0] == $_SESSION['lang']) {
27
                                echo 'selected';
28
                            }
29
                            echo '>'.$language[1].'</option>';
30
                        }?>
31
                    </select>
32
                </div>
33
            </div>
34
            <div class="form-group">
35
                <label class="col-md-4 control-label" for="dir">Directories:</label>
36
                <div class="col-md-4">
37
                <select id="dir" name="dir" class="form-control">
38
                    <option value="1">base, boot</option>
39
                    <option value="2" <?php if (isset($_GET['dir']) && $_GET['dir'] == '2') {
40
                            echo 'selected';
41
                        }?>>dll</option>
42
                    <option value="3" <?php if (isset($_GET['dir']) && $_GET['dir'] == '3') {
43
                            echo 'selected';
44
                        }?>>media, subsystems, win32ss</option>
45
                    <option value="100" <?php if (isset($_GET['dir']) && $_GET['dir'] == '100') {
46
                            echo 'selected';
47
                        }?>>All ReactOS Source dir</option>
48
                </select>
49
                </div>
50
            </div>
51
            <div class="form-group">
52
                <button type="submit" class="btn btn-primary">Search</button>
53
            </div>
54
        </fieldset>
55
    </form>
56
</center>
57
<br>
58
59
<?php
60
if (isset($_GET['lang']) && !empty($_GET['lang']) && isset($_GET['dir']) && is_numeric($_GET['dir'])) {
61
                            $it = new AppendIterator();
62
63
                            // Switch for directories
64 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...
65
        case '1':
66
            $directories = [
67
                new RecursiveDirectoryIterator($ROSDir.'base/applications'),
68
                new RecursiveDirectoryIterator($ROSDir.'base/setup'),
69
                new RecursiveDirectoryIterator($ROSDir.'base/shell'),
70
                new RecursiveDirectoryIterator($ROSDir.'base/system'),
71
                new RecursiveDirectoryIterator($ROSDir.'boot/freeldr/fdebug'),
72
            ];
73
            break;
74
75
        case '2':
76
            $directories = [
77
                new RecursiveDirectoryIterator($ROSDir.'dll/cpl'),
78
                new RecursiveDirectoryIterator($ROSDir.'dll/shellext'),
79
                new RecursiveDirectoryIterator($ROSDir.'dll/win32'),
80
            ];
81
            break;
82
83
        case '3':
84
            $directories = [
85
                new RecursiveDirectoryIterator($ROSDir.'media/themes'),
86
                new RecursiveDirectoryIterator($ROSDir.'subsystems/mvdm/ntvdm'),
87
                new RecursiveDirectoryIterator($ROSDir.'win32ss/user'),
88
            ];
89
            break;
90
91
        case '100':
92
            $directories = [
93
                new RecursiveDirectoryIterator($ROSDir),
94
            ];
95
            break;
96
97
        default:
98
            echo 'Something is wrong! Please try again.';
99
            exit;
100
    }
101
102
                            foreach ($directories as $directory) {
103
                                $it->append(new RecursiveIteratorIterator($directory));
104
                            }
105
106
                            function diff_versions($leftContent, $rightContent)
107
                            {
108
                                $rightVersion = null;
0 ignored issues
show
$rightVersion is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
109
110
                                $pattern = '/^(?!FONT|\\s*\\*|\\#include|\\s*ICON)[^"\\n]*"\\K(?!\\s*(?:"|\\n))([^"]+)/m';
111
112
                                if (preg_match_all($pattern, $leftContent, $matches) <= 0) {
113
                                    throw new Exception('Left content has no version line.');
114
                                }
115
116
                                $leftVersion = $matches[1];
117
118
                                if (preg_match_all($pattern, $rightContent, $matches) <= 0) {
119
                                    throw new Exception('Right content has no version line.');
120
                                }
121
122
                                $rightVersion = $matches[1];
123
124
                                return [
125
                                    'diff'         => array_intersect($leftVersion, $rightVersion),
126
                                    'leftVersion'  => $leftVersion,
127
                                    'rightVersion' => $rightVersion,
128
                                ];
129
                            }
130
131
                            function exceptions_error_handler($severity, $message, $filename, $lineno)
132
                            {
133
                                if (error_reporting() == 0) {
134
                                    return;
135
                                }
136
                                if (error_reporting() & $severity) {
137
                                    throw new ErrorException($message, 0, $severity, $filename, $lineno);
138
                                }
139
                            }
140
141
                            set_error_handler('exceptions_error_handler');
142
143
                            $regex = new RegexIterator($it, '/^.+'.$langDir.'.+('.$originLang.')\.'.$fileExt.'$/i', RecursiveRegexIterator::GET_MATCH);
144
145
                            $missing = $allStrings = 0;
146
147
                            $lang = htmlspecialchars($_GET['lang']);
148
                            // Search for eg. PL,Pl,pl
149
                            $fileSearch = strtoupper($lang).','.ucfirst($lang).','.strtolower($lang);
150
151
                            // ReactOS and Wine Strings - array
152
                            $ignoredROSStrings = file($ROSSpellFilename, FILE_IGNORE_NEW_LINES);
153
                            $ignoredWineStrings = file($wineSpellFilename, FILE_IGNORE_NEW_LINES);
154
155
                            $regex->rewind();
156
                            while ($regex->valid()) {
157
                                if (!$regex->isDot()) {
158
                                    $file = glob($regex->getPathInfo().'/*{'.$fileSearch.'}*.'.$fileExt, GLOB_BRACE);
159
160
                                    $isFile = array_filter($file);
161
162
                                    if (empty($isFile)) {
163
                                        echo '<b>No translation</b> for path '.$regex->getPathInfo().'<hr>';
164
                                    } else {
165
                                        $fileContent1 = file_get_contents($regex->key());
166
                                        $fileContent2 = file_get_contents($file[0]);
167
168
                                        $array = diff_versions($fileContent1, $fileContent2);
169
170
                                        if ($array['diff']) {
171
                                            $currentMissing = $missing;
172
                                            $missingTextMessage = null;
173
174
                                            foreach ($array['leftVersion'] as $index => $english) {
175
                                                // Catch offset error
176
                                                try {
177
                                                    // Check if this same and ignore some words
178
                                                    if ($english === $array['rightVersion'][$index] && !in_array($english, $ignoredROSStrings) && !in_array($english, $ignoredWineStrings)) {
179
                                                        $missingTextMessage .= '<b>Missing translation:</b> '.htmlspecialchars($english).'<br>';
180
                                                        $missing++;
181
                                                    }
182
                                                    $allStrings++;
183
                                                } catch (Exception $e) {
184
                                                    $missingTextMessage .= 'Missing stuff in your language<br>';
185
                                                    $allStrings++;
186
                                                    $missing++;
187
                                                }
188
                                            }
189
190
                                            if ($currentMissing == $missing) {
191
                                                $messageForFile = 'Seems <b>OK :)</b> Some strings was ignored by ReactOS and Wine spell files.<br>';
192
                                            } elseif ($detailsTag) {
193
                                                $messageForFile = '<details open><summary><strong>Click here to see/hide missing translations in file ('.($missing - $currentMissing).')</strong></summary>'.$missingTextMessage.'</details>';
194
                                            } else {
195
                                                $messageForFile = $missingTextMessage;
196
                                            }
197
198
                                            if ($currentMissing != $missing || $showTranslationOK) {
199
                                                $pathFromRoot = str_replace($ROSDir, '', $regex->getPathInfo());
200
                                                echo $regex->getPathInfo().' (<a href="https://github.com/reactos/reactos/tree/master/'.$pathFromRoot.'">Go to GitHub</a>)<br><br>'.$messageForFile.'<hr>';
201
                                            }
202
                                        }
203
                                    }
204
                                }
205
                                $regex->next();
206
                            }
207
208
                            $languppercase = strtoupper($lang);
209
210
                            echo "<h3>All strings for english: $allStrings</h3>";
211
                            echo "<h3>Missing translations for your language ($languppercase): $missing</h3>";
212
213
                            // Rounded percent
214
                            $percent = round((($allStrings - $missing) / $allStrings) * 100, 2);
215
                            echo "<h3>Language $languppercase translated in $percent%</h3>";
216
                        }
217
218
include_once 'footer.php';
219