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