Completed
Pull Request — master (#9)
by
unknown
01:19
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">
23
                        <option value="" selected disabled hidden>Choose here</option>
24
                        <?php foreach($langcodes as $language)
25
                        {
26
                            echo"<option value='$language[0]'>$language[1]</option>";
27
                        }?>
28
                    </select>
29
                </div>
30
            </div>
31
            <div class="form-group">
32
                <label class="col-md-4 control-label" for="dir">Directories:</label>
33
                <div class="col-md-4">
34
                <select id="dir" name="dir" class="form-control">
35
                    <option value="1">base, boot</option>
36
                    <option value="2" <?php if (isset($_GET['dir']) && $_GET['dir'] == '2') {
37
    echo 'selected';
38
}?>>dll</option>
39
                    <option value="3" <?php if (isset($_GET['dir']) && $_GET['dir'] == '3') {
40
    echo 'selected';
41
}?>>media, subsystems, win32ss</option>
42
                    <option value="100" <?php if (isset($_GET['dir']) && $_GET['dir'] == '100') {
43
    echo 'selected';
44
}?>>All ReactOS Source dir</option>
45
                </select>
46
                </div>
47
            </div>
48
            <div class="form-group">
49
                <button type="submit" class="btn btn-primary">Search</button>
50
            </div>
51
        </fieldset>
52
    </form>
53
</center>
54
<br>
55
56
<?php
57
if (isset($_GET['lang']) && !empty($_GET['lang']) && isset($_GET['dir']) && is_numeric($_GET['dir'])) {
58
    $it = new AppendIterator();
59
60
    // Switch for directories
61 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...
62
        case '1':
63
            $directories = [
64
                new RecursiveDirectoryIterator($ROSDir.'base/applications'),
65
                new RecursiveDirectoryIterator($ROSDir.'base/setup'),
66
                new RecursiveDirectoryIterator($ROSDir.'base/shell'),
67
                new RecursiveDirectoryIterator($ROSDir.'base/system'),
68
                new RecursiveDirectoryIterator($ROSDir.'boot/freeldr/fdebug'),
69
            ];
70
            break;
71
72
        case '2':
73
            $directories = [
74
                new RecursiveDirectoryIterator($ROSDir.'dll/cpl'),
75
                new RecursiveDirectoryIterator($ROSDir.'dll/shellext'),
76
                new RecursiveDirectoryIterator($ROSDir.'dll/win32'),
77
            ];
78
            break;
79
80
        case '3':
81
            $directories = [
82
                new RecursiveDirectoryIterator($ROSDir.'media/themes'),
83
                new RecursiveDirectoryIterator($ROSDir.'subsystems/mvdm/ntvdm'),
84
                new RecursiveDirectoryIterator($ROSDir.'win32ss/user'),
85
            ];
86
            break;
87
88
        case '100':
89
            $directories = [
90
                new RecursiveDirectoryIterator($ROSDir),
91
            ];
92
            break;
93
94
        default:
95
            echo 'Something is wrong! Please try again.';
96
            exit;
97
    }
98
99
    foreach ($directories as $directory) {
100
        $it->append(new RecursiveIteratorIterator($directory));
101
    }
102
103
    function diff_versions($leftContent, $rightContent)
104
    {
105
        $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...
106
107
        $pattern = '/^(?!FONT|\\s*\\*|\\#\\include|\\s*\\ICON)[^"\\n]*"\\K(?!\\s*(?:"|\\n))([^"]+)/m';
108
109
        if (preg_match_all($pattern, $leftContent, $matches) <= 0) {
110
            throw new Exception('Left content has no version line.');
111
        }
112
113
        $leftVersion = $matches[1];
114
115
        if (preg_match_all($pattern, $rightContent, $matches) <= 0) {
116
            throw new Exception('Right content has no version line.');
117
        }
118
119
        $rightVersion = $matches[1];
120
121
        return [
122
            'diff'         => array_intersect($leftVersion, $rightVersion),
123
            'leftVersion'  => $leftVersion,
124
            'rightVersion' => $rightVersion,
125
        ];
126
    }
127
128
    function exceptions_error_handler($severity, $message, $filename, $lineno)
129
    {
130
        if (error_reporting() == 0) {
131
            return;
132
        }
133
        if (error_reporting() & $severity) {
134
            throw new ErrorException($message, 0, $severity, $filename, $lineno);
135
        }
136
    }
137
138
    set_error_handler('exceptions_error_handler');
139
140
    $regex = new RegexIterator($it, '/^.+'.$langDir.'.+('.$originLang.')\.'.$fileExt.'$/i', RecursiveRegexIterator::GET_MATCH);
141
142
    $missing = $allStrings = 0;
143
144
    $lang = htmlspecialchars($_GET['lang']);
145
    // Search for eg. PL,Pl,pl
146
    $fileSearch = strtoupper($lang).','.ucfirst($lang).','.strtolower($lang);
147
148
    // ReactOS and Wine Strings - array
149
    $ignoredROSStrings = file($ROSSpellFilename, FILE_IGNORE_NEW_LINES);
150
    $ignoredWineStrings = file($wineSpellFilename, FILE_IGNORE_NEW_LINES);
151
152
    $regex->rewind();
153
    while ($regex->valid()) {
154
        if (!$regex->isDot()) {
155
            $file = glob($regex->getPathInfo().'/*{'.$fileSearch.'}*.'.$fileExt, GLOB_BRACE);
156
157
            $isFile = array_filter($file);
158
159
            if (empty($isFile)) {
160
                echo '<b>No translation</b> for path '.$regex->getPathInfo().'<hr>';
161
            } else {
162
                $fileContent1 = file_get_contents($regex->key());
163
                $fileContent2 = file_get_contents($file[0]);
164
165
                $array = diff_versions($fileContent1, $fileContent2);
166
167
                if ($array['diff']) {
168
                    $currentMissing = $missing;
169
                    $missingTextMessage = null;
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
                                $missingTextMessage .= '<b>Missing translation:</b> '.htmlspecialchars($english).'<br>';
177
                                $missing++;
178
                            }
179
                            $allStrings++;
180
                        } catch (Exception $e) {
181
                            $missingTextMessage .= 'Missing stuff in your language<br>';
182
                            $allStrings++;
183
                            $missing++;
184
                        }
185
                    }
186
187
                    if ($currentMissing == $missing) {
188
                        $messageForFile = 'Seems <b>OK :)</b> Some strings was ignored by ReactOS and Wine spell files.<br>';
189
                    } elseif ($detailsTag) {
190
                        $messageForFile = '<details open><summary><strong>Click here to see/hide missing translations in file ('.($missing - $currentMissing).')</strong></summary>'.$missingTextMessage.'</details>';
191
                    } else {
192
                        $messageForFile = $missingTextMessage;
193
                    }
194
195
                    if ($currentMissing != $missing || $showTranslationOK) {
196
                        $pathFromRoot = str_replace($ROSDir, '', $regex->getPathInfo());
197
                        echo $regex->getPathInfo().' (<a href="https://github.com/reactos/reactos/tree/master/'.$pathFromRoot.'">Go to GitHub</a>)<br><br>'.$messageForFile.'<hr>';
198
                    }
199
                }
200
            }
201
        }
202
        $regex->next();
203
    }
204
205
    $languppercase = strtoupper($lang);
206
207
    echo "<h3>All strings for english: $allStrings</h3>";
208
    echo "<h3>Missing translations for your language ($languppercase): $missing</h3>";
209
210
    // Rounded percent
211
    $percent = round((($allStrings - $missing) / $allStrings) * 100, 2);
212
    echo "<h3>Language $languppercase translated in $percent%</h3>";
213
}
214
215
include_once 'footer.php';
216