forked from mozfr/transvision
/
ShowResults.php
444 lines (390 loc) · 18.1 KB
/
ShowResults.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
<?php
namespace Transvision;
/**
* ShowResults class
*
* This class is used to get search results and display them
*
* @package Transvision
*/
class ShowResults
{
/**
* Create an array for search results with this format:
* 'entity' => ['string_locale 1', 'string_locale 2']
* @param array $entities Haystack of entities to search in
* @param array $array_strings The strings to look into [locale1 strings, locale2 strings]
* @return array Results with entities as keys and translations as values
*/
public static function getTMXResults($entities, $array_strings)
{
$search_results = [];
foreach ($entities as $entity) {
$temp = [];
foreach ($array_strings as $strings) {
$temp[] = self::getStringFromEntity($entity, $strings);
}
$search_results[$entity] = $temp;
}
return $search_results;
}
/**
* Return an array of search results from our Translation Memory API
* service with a quality index based on the levenshtein distance.
*
* @param array $entities The entities we want to analyse
* @param array $array_strings The strings to look into [locale1 strings, locale2 strings]
* @param string $search The string to search for
* @param int $max_results Optional, default to 200, the max number of results we return
* @param int $min_quality Optional, default to 0, The minimal quality index to filter result
* @return array An array of strings as [source => string, target => string, quality=> Levenshtein index]
*/
public static function getTranslationMemoryResults($entities, $array_strings, $search, $max_results = 200, $min_quality = 0)
{
$search_results = array_values(self::getTMXResults($entities, $array_strings));
$output = [];
foreach ($search_results as $set) {
// We only want results for which we have a translation
if ($set[1]) {
$quality = Strings::levenshteinQuality($search, $set[0]);
if ($quality >= $min_quality) {
$output[] = [
'source' => $set[0],
'target' => $set[1],
'quality' => $quality,
];
}
}
}
// Remove duplicate results
$output = array_unique($output, SORT_REGULAR);
// We sort by quality to get the best results first
usort($output, function ($a, $b) {
return $a['quality'] < $b['quality'];
});
if ($max_results > 0) {
array_splice($output, $max_results);
}
return $output;
}
/**
* Return search results in a repository on strings/entities for the API
*
* @param array $entities An array of all the entities we want to return
* @param array $array_strings The strings to look into [locale1 strings, locale2 strings]
* @return array An array of strings with the entity as key [entity => [English => French]]
*/
public static function getRepositorySearchResults($entities, $array_strings)
{
$search_results = self::getTMXResults($entities, $array_strings);
$output = [];
$clean_string = function ($string) {
return trim(htmlspecialchars_decode($string, ENT_QUOTES));
};
foreach ($search_results as $entity => $set) {
// we only want results for which we have a translation
if ($set[1]) {
$output[$entity] = [
$clean_string($set[0]) => $clean_string($set[1]),
];
}
}
return $output;
}
/**
* Returns the string from its entity or false
*
* @param string $entity Entity we are looking for
* @param array $strings Haystack of strings to search in
* @return string The string for the entity or false if no matching result
*/
public static function getStringFromEntity($entity, $strings)
{
return isset($strings[$entity]) && $strings[$entity] != ''
? $strings[$entity]
: false;
}
/**
* Nicely format entities for tables by splitting them in subpaths and styling them
*
* @param string $entity
* @param boolean $highlight Optional. Default to false. Use a highlighting style
* @return string Entity reformated with html markup and css classes for styling
*/
public static function formatEntity($entity, $highlight = false)
{
// let's analyse the entity for the search string
$chunk = explode(':', $entity);
if ($highlight) {
$entity = array_pop($chunk);
$highlight = preg_quote($highlight, '/');
$entity = preg_replace("/($highlight)/i", '<span class="highlight">$1</span>', $entity);
$entity = '<span class="red">' . $entity . '</span>';
} else {
$entity = '<span class="red">' . array_pop($chunk) . '</span>';
}
// let's analyse the entity for the search string
$chunk = explode('/', $chunk[0]);
$repo = '<span class="green">' . array_shift($chunk) . '</span>';
$path = implode('<span class="superset"> ⊃ </span>', $chunk);
return $repo . '<span class="superset"> ⊃ </span>' . $path . '<br>' . $entity;
}
/**
* Highlight specific elements in the string for locales.
* Can also highlight specific per locale sub-strings.
* For example in French non-breaking spaces used in typography
*
* @param string $string Source text
* @param string $locale Optional. Locale code. Defaults to French.
* @return string Same string with specific sub-strings in span elements
* for styling with CSS (.hightlight-gray class)
*/
public static function highlight($string, $locale = 'fr')
{
$replacements = [
' ' => '<span class="highlight-gray"> </span>',
'…' => '<span class="highlight-gray">…</span>',
];
switch ($locale) {
case 'fr':
default:
$replacements['…'] = '<span class="highlight-gray">…</span>'; // right ellipsis highlight
break;
}
return Strings::multipleStringReplace($replacements, $string);
}
/**
* Html table of search results used by the main view (needs a lot of refactoring)
*
* @param array $search_results list of rows
* @param string $recherche The words searched for
* @param string $locale1 Reference locale to search in, usually en-US
* @param string $locale2 Target locale to search in
* @param array $search_options All the search options from the query
*
* @return string html table to insert in the view
*/
public static function resultsTable($search_results, $recherche, $locale1, $locale2, $search_options)
{
$direction1 = RTLSupport::getDirection($locale1);
$direction2 = RTLSupport::getDirection($locale2);
if (isset($search_options['extra_locale'])) {
$locale3 = $search_options['extra_locale'];
$direction3 = RTLSupport::getDirection($locale3);
$extra_column_header = "<th>{$locale3}</th>";
} else {
$extra_column_header = '';
}
$table = "<table class='collapsable'>
<tr class='column_headers'>
<th>Entity</th>
<th>{$locale1}</th>
<th>{$locale2}</th>
{$extra_column_header}
</tr>";
if (!$search_options['whole_word'] && !$search_options['perfect_match']) {
$recherche = Utils::uniqueWords($recherche);
} else {
$recherche = [$recherche];
}
foreach ($search_results as $key => $strings) {
// Don't highlight search matches in entities when searching strings
if ($search_options['search_type'] == 'strings') {
$result_entity = self::formatEntity($key);
} else {
$result_entity = self::formatEntity($key, $recherche[0]);
}
$component = explode('/', $key)[0];
$source_string = trim($strings[0]);
$target_string = trim($strings[1]);
$entity_link = "?sourcelocale={$locale1}"
. "&locale={$locale2}"
. "&repo={$search_options['repo']}"
. "&search_type=entities&recherche={$key}";
$bz_link = [Bugzilla::reportErrorLink(
$locale2, $key, $source_string, $target_string, $search_options['repo'], $entity_link
)];
if (isset($search_options['extra_locale'])) {
$target_string2 = trim($strings[2]);
$entity_link = "?sourcelocale={$locale1}"
. "&locale={$search_options['extra_locale']}"
. "&repo={$search_options['repo']}"
. "&search_type=entities&recherche={$key}";
$bz_link[] = Bugzilla::reportErrorLink(
$search_options['extra_locale'], $key, $source_string, $target_string2, $search_options['repo'], $entity_link
);
} else {
$target_string2 = '';
}
foreach ($recherche as $val) {
$source_string = Utils::markString($val, $source_string);
$target_string = Utils::markString($val, $target_string);
if (isset($search_options["extra_locale"])) {
$target_string2 = Utils::markString($val, $target_string2);
}
}
// Escape HTML before highlighing search terms
$source_string = htmlspecialchars($source_string);
$target_string = htmlspecialchars($target_string);
$source_string = Utils::highlightString($source_string);
$target_string = Utils::highlightString($target_string);
if (isset($search_options["extra_locale"])) {
$target_string2 = htmlspecialchars($target_string2);
$target_string2 = Utils::highlightString($target_string2);
}
$replacements = [
' ' => '<span class="highlight-gray" title="Non breakable space"> </span>', // nbsp highlight
' ' => '<span class="highlight-red" title="Thin space"> </span>', // thin space highlight
'…' => '<span class="highlight-gray">…</span>', // right ellipsis highlight
'…' => '<span class="highlight-gray">…</span>', // right ellipsis highlight
];
$target_string = Strings::multipleStringReplace($replacements, $target_string);
$temp = explode('-', $locale1);
$locale1_short_code = $temp[0];
$temp = explode('-', $locale2);
$locale2_short_code = $temp[0];
if ($search_options['repo'] == 'mozilla_org') {
$locale1_path = VersionControl::gitPath($locale1, $search_options['repo'], $key);
$locale2_path = VersionControl::gitPath($locale2, $search_options['repo'], $key);
} elseif ($search_options['repo'] == 'firefox_ios') {
$locale1_path = VersionControl::svnPath($locale1, $search_options['repo'], $key);
$locale2_path = VersionControl::svnPath($locale2, $search_options['repo'], $key);
} else {
$locale1_path = VersionControl::hgPath($locale1, $search_options['repo'], $key);
$locale2_path = VersionControl::hgPath($locale2, $search_options['repo'], $key);
}
// errors
$error_message = '';
// check for final dot
if (substr(strip_tags($source_string), -1) == '.'
&& substr(strip_tags($target_string), -1) != '.') {
$error_message = '<em class="error"> No final dot?</em>';
}
// check abnormal string length
$length_diff = Utils::checkAbnormalStringLength($source_string, $target_string);
if ($length_diff) {
switch ($length_diff) {
case 'small':
$error_message = $error_message . '<em class="error"> Small string?</em>';
break;
case 'large':
$error_message = $error_message . '<em class="error"> Large String?</em>';
break;
}
}
// Missing string error
if (!$source_string) {
$source_string = '<em class="error">warning: missing string</em>';
$error_message = '';
}
if (!$target_string) {
$target_string = '<em class="error">warning: missing string</em>';
$error_message = '';
}
if (!$target_string2) {
$target_string2 = '<em class="error">warning: missing string</em>';
$error_message = '';
}
// Replace / and : in the key name and use it as an anchor name
$anchor_name = str_replace(['/', ':'], '_', $key);
// 3locales view
if (isset($search_options["extra_locale"])) {
if ($search_options['repo'] == 'mozilla_org') {
$locale3_path = VersionControl::gitPath($locale3, $search_options['repo'], $key);
} elseif ($search_options['repo'] == 'firefox_ios') {
$locale3_path = VersionControl::svnPath($locale3, $search_options['repo'], $key);
} else {
$locale3_path = VersionControl::hgPath($locale3, $search_options['repo'], $key);
}
$extra_column_rows = "
<td dir='{$direction3}' lang='{$locale3}'>
<span class='celltitle'>{$locale3}</span>
<div class='string'>{$target_string2}</div>
<div dir='ltr' class='result_meta_link'>
<a class='source_link' href='{$locale3_path}'>
<source>
</a>
<a class='bug_link' target='_blank' href='{$bz_link[1]}'>
<report a bug>
</a>
</div>
</td>";
} else {
$extra_column_rows = '';
}
$table .= "
<tr class='{$component}'>
<td>
<span class='celltitle'>Entity</span>
<a class='resultpermalink tag' id='{$anchor_name}' href='#{$anchor_name}' title='Permalink to this result'>link</a>
<a class='l10n tag' href='/string/?entity={$key}&repo={$search_options['repo']}' title='List all translations for this entity'>l10n</a>
<a class='link_to_entity' href=\"/{$entity_link}\">{$result_entity}</a>
</td>
<td dir='{$direction1}' lang='{$locale1}'>
<span class='celltitle'>{$locale1}</span>
<div class='string'>
{$source_string}
</div>
<div dir='ltr' class='result_meta_link'>
<a class='source_link' href='{$locale1_path}'>
<source>
</a>
<span>Translate with:</span>
<a href='http://translate.google.com/#{$locale1_short_code}/{$locale2_short_code}/"
// We use html_entity_decode twice because we can have strings as &amp; stored
. urlencode(strip_tags(html_entity_decode(html_entity_decode($source_string))))
. "' target='_blank'>Google</a>
<a href='http://www.bing.com/translator/?from={$locale1_short_code}&to={$locale2_short_code}&text="
. urlencode(strip_tags(html_entity_decode(html_entity_decode($source_string))))
. "' target='_blank'>BING</a>
</div>
</td>
<td dir='{$direction2}' lang='{$locale2}'>
<span class='celltitle'>{$locale2}</span>
<div class='string'>{$target_string}</div>
<div dir='ltr' class='result_meta_link'>
<a class='source_link' href='{$locale2_path}'>
<source>
</a>
<a class='bug_link' target='_blank' href='{$bz_link[0]}'>
<report a bug>
</a>
{$error_message}
</div>
</td>
{$extra_column_rows}
</tr>";
}
$table .= " </table>";
return $table;
}
/**
* Search entity names: search full entity IDs (including path and filename),
* then search entity names (without the full path) if there are no results.
*
* @param array $source_strings Array of source strings
* @param string $regex Regular expression to search entity names
*
* @return array List of matching entity names
*/
public static function searchEntities($source_strings, $regex)
{
// Search through the full entity ID
$entities = preg_grep($regex, array_keys($source_strings));
/* If there are no results, search also through the entity names.
* This is needed for "perfect match" when only the entity name is
* provided.
*/
if (empty($entities)) {
$entity_names = [];
foreach ($source_strings as $entity => $translation) {
$entity_names[$entity] = explode(':', $entity)[1];
}
$entities = preg_grep($regex, $entity_names);
$entities = array_keys($entities);
}
return $entities;
}
}