Skip to content

Commit 4b38cac

Browse files
committed
Improve search for entities
* Search also in entity names if search on full entity fails (useful for "perfect match") * Don't display the table header if there are no results
1 parent f3dabbb commit 4b38cac

File tree

8 files changed

+109
-18
lines changed

8 files changed

+109
-18
lines changed

app/classes/Transvision/ShowResults.php

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public static function highlight($string, $locale = 'fr')
162162
{
163163
$replacements = [
164164
' ' => '<span class="highlight-gray"> </span>',
165-
'' => '<span class="highlight-gray">…</span>',
165+
'' => '<span class="highlight-gray">…</span>',
166166
];
167167

168168
switch ($locale) {
@@ -217,9 +217,9 @@ public static function resultsTable($search_results, $recherche, $locale1, $loca
217217

218218
// Don't highlight search matches in entities when searching strings
219219
if ($search_options['search_type'] == 'strings') {
220-
$result_entity = ShowResults::formatEntity($key);
220+
$result_entity = Self::formatEntity($key);
221221
} else {
222-
$result_entity = ShowResults::formatEntity($key, $recherche[0]);
222+
$result_entity = Self::formatEntity($key, $recherche[0]);
223223
}
224224

225225
$component = explode('/', $key)[0];
@@ -268,9 +268,9 @@ public static function resultsTable($search_results, $recherche, $locale1, $loca
268268
}
269269

270270
$replacements = [
271-
' ' => '<span class="highlight-gray" title="Non breakable space"> </span>', // nbsp highlight
272-
'' => '<span class="highlight-red" title="Thin space"> </span>', // thin space highlight
273-
'' => '<span class="highlight-gray">…</span>', // right ellipsis highlight
271+
' ' => '<span class="highlight-gray" title="Non breakable space"> </span>', // nbsp highlight
272+
'' => '<span class="highlight-red" title="Thin space"> </span>', // thin space highlight
273+
'' => '<span class="highlight-gray">…</span>', // right ellipsis highlight
274274
'&hellip;' => '<span class="highlight-gray">…</span>', // right ellipsis highlight
275275
];
276276

@@ -403,4 +403,34 @@ public static function resultsTable($search_results, $recherche, $locale1, $loca
403403

404404
return $table;
405405
}
406+
407+
/**
408+
* Search entity names: search full entity IDs (including path and filename),
409+
* then search entity names (without the full path) if there are no results.
410+
*
411+
* @param array $source_strings Array of source strings
412+
* @param string $regex Regular expression to search entity names
413+
*
414+
* @return array List of matching entity names
415+
*/
416+
public static function searchEntities($source_strings, $regex)
417+
{
418+
// Search through the full entity ID
419+
$entities = preg_grep($regex, array_keys($source_strings));
420+
421+
/* If there are no results, search also through the entity names.
422+
* This is needed for "perfect match" when only the entity name is
423+
* provided.
424+
*/
425+
if (empty($entities)) {
426+
$entity_names = [];
427+
foreach ($source_strings as $entity => $translation) {
428+
$entity_names[$entity] = explode(':', $entity)[1];
429+
}
430+
$entities = preg_grep($regex, $entity_names);
431+
$entities = array_keys($entities);
432+
}
433+
434+
return $entities;
435+
}
406436
}

app/models/api/repository_search.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
$regex = '~' . $whole_word . trim('^' . preg_quote($initial_search, '~') . '$') .
2323
$whole_word . '~' . $case_sensitive . 'u';
2424
if ($request->parameters[2] == 'entities') {
25-
$entities = preg_grep($regex, array_keys($source_strings));
25+
$entities = ShowResults::searchEntities($source_strings, $regex);
2626
$source_strings = array_intersect_key($source_strings, array_flip($entities));
2727
} else {
2828
$source_strings = preg_grep($regex, $source_strings);
@@ -33,7 +33,7 @@
3333
$regex = '~' . $whole_word . preg_quote($word, '~') .
3434
$whole_word . '~' . $case_sensitive . 'u';
3535
if ($request->parameters[2] == 'entities') {
36-
$entities = preg_grep($regex, array_keys($source_strings));
36+
$entities = ShowResults::searchEntities($source_strings, $regex);
3737
$source_strings = array_intersect_key($source_strings, array_flip($entities));
3838
} else {
3939
$source_strings = preg_grep($regex, $source_strings);

app/models/mainsearch_entities.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
$extra_column_header = '';
1313
}
1414

15-
// Display a search hint for the closest string we have if we have no search results
16-
$entities = preg_grep($main_regex, array_keys($tmx_source));
15+
$entities = ShowResults::searchEntities($tmx_source, $main_regex);
1716

17+
// Display a search hint for the closest string we have if we have no search results
1818
if (count($entities) == 0) {
1919
$merged_strings = [];
2020

app/models/mainsearch_strings.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
}
1717

1818
if ($check['search_type'] == 'strings_entities') {
19-
$entities = preg_grep($main_regex, array_keys($tmx_source));
19+
$entities = ShowResults::searchEntities($tmx_source, $main_regex);
2020
foreach ($entities as $entity) {
2121
$locale1_strings[$entity] = $tmx_source[$entity];
2222
}

app/views/results_entities.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
<span class='celltitle'>Entity</span>
8888
<a class='resultpermalink tag' id='{$anchor_name}' href='#{$anchor_name}' title='Permalink to this result'>link</a>
8989
<a class='l10n tag' href='/string/?entity={$entity}&amp;repo={$check['repo']}' title='List all translations for this entity'>l10n</a>
90-
<a class='link_to_entity' href='/{$entity_link}'>" . ShowResults::formatEntity($entity, $my_search) . "</a>
90+
<a class='link_to_entity' href='/{$entity_link}'>" . ShowResults::formatEntity($entity, $initial_search) . "</a>
9191
</td>
9292
<td dir='{$direction1}'>
9393
<span class='celltitle'>{$source_locale}</span>
@@ -109,5 +109,6 @@
109109
}
110110

111111
$table .= "</table>\n\n";
112-
113-
print $table;
112+
if ($entities) {
113+
print $table;
114+
}

tests/functional/api.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
['v1/search/strings/central/en-US/fr/tralala/', 200, '[]'],
3232
['v1/locales/central/', 200, '["ar","ast","cs","de","en-GB","en-US","eo","es-AR","es-CL","es-ES","es-MX","fa","fr","fy-NL","gl","he","hu","id","it","ja","ja-JP-mac","kk","ko","lt","lv","nb-NO","nl","nn-NO","pl","pt-BR","pt-PT","ru","sk","sl","sv-SE","th","tr","uk","vi","zh-CN","zh-TW"]'],
3333
['v1/locales/iDontExist/', 400, '{"error":"The repo queried (iDontExist) doesn\'t exist."}'],
34-
['v1/repositories/', 200, '["release","beta","aurora","central","gaia_1_3","gaia_1_4","gaia_2_0","gaia_2_1","gaia","mozilla_org"]'],
34+
['v1/repositories/', 200, '["release","beta","aurora","central","gaia_1_3","gaia_1_4","gaia_2_0","gaia_2_1","gaia_2_2","gaia","mozilla_org"]'],
3535
['v1/tm/central/en-US/fr/Bookmark/?max_results=3&min_quality=80', 200, '[{"source":"Bookmark","target":"Marquer cette page","quality":100},{"source":"Bookmark","target":"Marque-page","quality":100},{"source":"Bookmarks","target":"Marque-pages","quality":88.888888888889}]'],
3636
['v1/versions/', 200, '{"v1":"stable"}'],
3737
];

tests/units/Transvision/ShowResults.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,4 +205,64 @@ public function testGeRepositorySearchResults($a, $b, $c)
205205
->array($obj->getRepositorySearchResults($a, $b))
206206
->isIdenticalTo($c);
207207
}
208+
209+
public function searchEntitiesDP()
210+
{
211+
return [
212+
[
213+
[
214+
'browser/chrome/browser/migration/migration.properties:sourceNameIE' => 'test',
215+
'browser/chrome/browser/migration/migration.properties:sourceNameIE1' => 'test2',
216+
],
217+
'~^browser/chrome/browser/migration/migration.properties:sourceNameIE$~i',
218+
[
219+
'browser/chrome/browser/migration/migration.properties:sourceNameIE',
220+
],
221+
],
222+
[
223+
[
224+
'browser/chrome/browser/migration/migration.properties:sourceNameIE' => 'test',
225+
'browser/chrome/browser/migration/migration.properties:sourceNameIE1' => 'test2',
226+
],
227+
'~sourceNameIE~i',
228+
[
229+
'browser/chrome/browser/migration/migration.properties:sourceNameIE',
230+
'browser/chrome/browser/migration/migration.properties:sourceNameIE1',
231+
],
232+
],
233+
[
234+
[
235+
'browser/chrome/browser/migration/migration.properties:sourceNameIE' => 'test',
236+
'browser/chrome/browser/migration/migration.properties:sourceNameIE1' => 'test2',
237+
],
238+
'~^sourceNameIE$~i',
239+
[
240+
'browser/chrome/browser/migration/migration.properties:sourceNameIE',
241+
],
242+
],
243+
[
244+
[
245+
'browser/chrome/browser/migration/migration.properties:sourceNameIE' => 'test',
246+
],
247+
'~foobar~',
248+
[],
249+
],
250+
[
251+
[],
252+
'~foobar~',
253+
[],
254+
],
255+
];
256+
}
257+
258+
/**
259+
* @dataProvider searchEntitiesDP
260+
*/
261+
public function testSearchEntities($a, $b, $c)
262+
{
263+
$obj = new _ShowResults();
264+
$this
265+
->array($obj->searchEntities($a, $b))
266+
->isEqualTo($c);
267+
}
208268
}

tests/units/Transvision/Strings.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ public function multipleStringReplaceDP()
9191
return [
9292
[
9393
[
94-
' ' => '<span class="highlight-gray" title="Non breakable space"> </span>', // nbsp highlight
95-
'' => '<span class="highlight-red" title="Thin space"> </span>', // thin space highlight
96-
'' => '<span class="highlight-gray">…</span>', // right ellipsis highlight
94+
' ' => '<span class="highlight-gray" title="Non breakable space"> </span>', // nbsp highlight
95+
'' => '<span class="highlight-red" title="Thin space"> </span>', // thin space highlight
96+
'' => '<span class="highlight-gray">…</span>', // right ellipsis highlight
9797
'&hellip;' => '<span class="highlight-gray">…</span>', // right ellipsis highlight
9898
],
9999
'&hellip;  …',

0 commit comments

Comments
 (0)