Skip to content

Commit

Permalink
Cache logic in complex views (accesskeys, variables, shortcuts) (#946)
Browse files Browse the repository at this point in the history
  • Loading branch information
flodolo authored May 3, 2018
1 parent 22a17a5 commit 91a685f
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 130 deletions.
2 changes: 1 addition & 1 deletion app/controllers/check_variables.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
and display the results as JSON instead of loading view and template.
*/
if (isset($_GET['json'])) {
$json = $var_errors;
$json = array_column($var_errors, 'string_id');
include VIEWS . 'json.php';
die();
}
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/commandkeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
and display the results as JSON instead of loading view and template.
*/
if (isset($_GET['json'])) {
$json = $commandkey_results;
$json = array_column($commandkey_results, 'id');
include VIEWS . 'json.php';
die();
}
Expand Down
143 changes: 81 additions & 62 deletions app/models/accesskeys.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace Transvision;

use Cache\Cache;

$error_messages = [];
$reference_locale = Project::getReferenceLocale($repo);
$supported_locales = Project::getRepositoryLocales($repo, [$reference_locale]);
Expand All @@ -25,86 +27,103 @@
}
$channel_selector = Utils::getHtmlSelectOptions($channels, $repo, true);

// Get strings
$source = Utils::getRepoStrings($reference_locale, $repo);
$target = Utils::getRepoStrings($locale, $repo);
$cache_id = $repo . $locale . 'accesskeys';
$ak_results = Cache::getKey($cache_id);
if ($ak_results === false) {
// Get strings
$source = Utils::getRepoStrings($reference_locale, $repo);
$target = Utils::getRepoStrings($locale, $repo);

// Get strings with 'accesskey' in the string ID
$ak_string_ids = array_filter(
array_keys($target),
function ($entity) {
return strpos($entity, '.accesskey') !== false;
}
);
// Get strings with 'accesskey' in the string ID
$ak_string_ids = array_filter(
array_keys($target),
function ($entity) {
return strpos($entity, '.accesskey') !== false;
}
);

// Possible labels associated to an access key
$ak_labels = ['.label', '.title', '.message', ''];
// Possible labels associated to an access key
$ak_labels = ['.label', '.title', '.message', ''];

// Some keys map to a different string ID than the one identified by the
// algorithm
$known_mappings = [
'browser/chrome/browser/browser.properties:decoder.noCodecs.accesskey' => 'browser/chrome/browser/browser.properties:decoder.noCodecs.button',
'browser/chrome/browser/preferences/sync.dtd:engine.tabs.accesskey' => 'browser/chrome/browser/preferences/sync.dtd:engine.tabs.label2',
];
// Some keys map to a different string ID than the one identified by the
// algorithm
$known_mappings = [
'browser/chrome/browser/browser.properties:decoder.noCodecs.accesskey' => 'browser/chrome/browser/browser.properties:decoder.noCodecs.button',
'browser/chrome/browser/preferences/sync.dtd:engine.tabs.accesskey' => 'browser/chrome/browser/preferences/sync.dtd:engine.tabs.label2',
];

$ak_results = [];
foreach ($ak_string_ids as $ak_string_id) {
// Exclude accesskey if it's in a FTL file and includes PLATFORM()
if (strpos($ak_string_id, '.ftl:') !== false && strpos($source[$ak_string_id], 'PLATFORM()') !== false) {
continue;
}
$ak_results = [];
foreach ($ak_string_ids as $ak_string_id) {
// Exclude accesskey if it's in a FTL file and includes PLATFORM()
if (strpos($ak_string_id, '.ftl:') !== false && strpos($source[$ak_string_id], 'PLATFORM()') !== false) {
continue;
}

if (isset($known_mappings[$ak_string_id])) {
$entity = $known_mappings[$ak_string_id];
// Check if the label is translated
if (isset($target[$entity]) && $target[$entity] !== '') {
$current_ak = $target[$ak_string_id];
if (($current_ak == '') || (mb_stripos($target[$entity], $current_ak) === false)) {
$ak_results[$ak_string_id] = $entity;
if (isset($known_mappings[$ak_string_id])) {
$entity = $known_mappings[$ak_string_id];
// Check if the label is translated
if (isset($target[$entity]) && $target[$entity] !== '') {
$current_ak = $target[$ak_string_id];
if (($current_ak == '') || (mb_stripos($target[$entity], $current_ak) === false)) {
$ak_results[] = [
'label_id' => $entity,
'label_txt' => $target[$entity],
'accesskey_id' => $ak_string_id,
'accesskey_txt' => $current_ak,
];
}
}
}
} else {
foreach ($ak_labels as $ak_label) {
/*
Replace 'accesskey' with one of the known IDs used for labels.
E.g.:
* foo.accesskey -> foo.label
* foo.accesskey -> foo.title
* foo.accesskey -> foo.message
* foo.accesskey -> foo (common in devtools)
*/
$entity = str_replace('.accesskey', $ak_label, $ak_string_id);
$current_ak = $target[$ak_string_id];
} else {
foreach ($ak_labels as $ak_label) {
/*
Replace 'accesskey' with one of the known IDs used for labels.
E.g.:
* foo.accesskey -> foo.label
* foo.accesskey -> foo.title
* foo.accesskey -> foo.message
* foo.accesskey -> foo (common in devtools)
*/
$entity = str_replace('.accesskey', $ak_label, $ak_string_id);
$current_ak = $target[$ak_string_id];

/*
Ignore:
* Strings not available or empty in target locale.
* Empty access keys in source locale.
*/
if (isset($target[$entity]) && $target[$entity] !== '' && $source[$ak_string_id] !== '') {
/*
Store the string if the access key is empty or using a
character not available in the label.
Ignore:
* Strings not available or empty in target locale.
* Empty access keys in source locale.
*/
if (($current_ak == '') || (mb_stripos($target[$entity], $current_ak) === false)) {
$ak_results[$ak_string_id] = $entity;
if (isset($target[$entity]) && $target[$entity] !== '' && $source[$ak_string_id] !== '') {
/*
Store the string if the access key is empty or using a
character not available in the label.
*/
if (($current_ak == '') || (mb_stripos($target[$entity], $current_ak) === false)) {
$ak_results[] = [
'label_id' => $entity,
'label_txt' => $target[$entity],
'accesskey_id' => $ak_string_id,
'accesskey_txt' => $current_ak,
];
}
}
}

/*
If we found an entity with the expected name in source strings,
we should stop cycling through $ak_labels.
*/
if (isset($source[$entity])) {
break;
/*
If we found an entity with the expected name in source strings,
we should stop cycling through $ak_labels.
*/
if (isset($source[$entity])) {
break;
}
}
}
}
Cache::setKey($cache_id, $ak_results);
unset($source);
unset($target);
}

// Build components filter
if (in_array($repo, $desktop_repos)) {
$components = Project::getComponents(array_flip($ak_results));
$components = Project::getComponents(array_flip(array_column($ak_results, 'accesskey_id')));
$filter_block = ShowResults::buildComponentsFilter($components);
}

Expand Down
47 changes: 32 additions & 15 deletions app/models/check_variables.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
<?php
namespace Transvision;

use Cache\Cache;

$reference_locale = Project::getReferenceLocale($repo);
$supported_locales = Project::getRepositoryLocales($repo, [$reference_locale]);
// If the requested locale is not available, fall back to the first
if (! in_array($locale, $supported_locales)) {
$locale = array_shift($supported_locales);
}

$source = Utils::getRepoStrings($reference_locale, $repo);
$target = Utils::getRepoStrings($locale, $repo);

// Set up channel selector, ignore mozilla.org
$channels = Project::getSupportedRepositories();
unset($channels['mozilla_org']);
Expand All @@ -19,22 +18,40 @@
// Build the target locale switcher
$target_locales_list = Utils::getHtmlSelectOptions($supported_locales, $locale);

$source = array_map(['Transvision\AnalyseStrings', 'cleanUpEntities'], $source);
$target = array_map(['Transvision\AnalyseStrings', 'cleanUpEntities'], $target);

// We need to ignore some strings because of false positives
$ignored_strings = [
'mail/chrome/messenger/aboutRights.dtd:rights.webservices-term4',
'suite/chrome/branding/aboutRights.dtd:rights.webservices-term4',
'toolkit/chrome/global/aboutRights.dtd:rights.webservices-term5',
];

$var_errors = AnalyseStrings::differences($source, $target, $repo, $ignored_strings);
$cache_id = $repo . $locale . 'variables';
$var_errors = Cache::getKey($cache_id);
if ($var_errors === false) {
$source = Utils::getRepoStrings($reference_locale, $repo);
$target = Utils::getRepoStrings($locale, $repo);
$var_errors = [];

$source = array_map(['Transvision\AnalyseStrings', 'cleanUpEntities'], $source);
$target = array_map(['Transvision\AnalyseStrings', 'cleanUpEntities'], $target);

// We need to ignore some strings because of false positives
$ignored_strings = [
'mail/chrome/messenger/aboutRights.dtd:rights.webservices-term4',
'suite/chrome/branding/aboutRights.dtd:rights.webservices-term4',
'toolkit/chrome/global/aboutRights.dtd:rights.webservices-term5',
];

$string_ids = AnalyseStrings::differences($source, $target, $repo, $ignored_strings);
foreach ($string_ids as $string_id) {
$var_errors[] = [
'string_id' => $string_id,
'source_string' => $source[$string_id],
'target_string' => $target[$string_id],
];
}
Cache::setKey($cache_id, $var_errors);
unset($source);
unset($target);
}
$error_count = count($var_errors);

// Build components filter
if (in_array($repo, $desktop_repos)) {
$components = Project::getComponents(array_flip($var_errors));
$components = Project::getComponents(array_flip(array_column($var_errors, 'string_id')));
$filter_block = ShowResults::buildComponentsFilter($components);
}

Expand Down
81 changes: 47 additions & 34 deletions app/models/commandkeys.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace Transvision;

use Cache\Cache;

$error_messages = [];
$reference_locale = Project::getReferenceLocale($repo);
$supported_locales = Project::getRepositoryLocales($repo, [$reference_locale]);
Expand All @@ -25,49 +27,60 @@
}
$channel_selector = Utils::getHtmlSelectOptions($channels, $repo, true);

// Get strings
$source = Utils::getRepoStrings($reference_locale, $repo);
$target = Utils::getRepoStrings($locale, $repo);
$cache_id = $repo . $locale . 'commandkeys';
$commandkey_results = Cache::getKey($cache_id);
if ($commandkey_results === false) {
// Get strings
$source = Utils::getRepoStrings($reference_locale, $repo);
$target = Utils::getRepoStrings($locale, $repo);

// Get string IDs in target ending with '.key' or '.commandkey'
$commandkey_ids = array_filter(
array_keys($target),
function ($entity) {
return Strings::endsWith($entity, ['.key', '.commandkey']);
}
);
// Get string IDs in target ending with '.key' or '.commandkey'
$commandkey_ids = array_filter(
array_keys($target),
function ($entity) {
return Strings::endsWith($entity, ['.key', '.commandkey']);
}
);

// Known false positives to ignore
$ignored_ids = [
'browser/chrome/browser/browser.properties:addonPostInstall.okay.key',
'devtools/client/webconsole.properties:table.key',
];
$ignored_files = [
'extensions/irc/chrome/chatzilla.properties',
'toolkit/chrome/global/charsetMenu.properties',
];
// Known false positives to ignore
$ignored_ids = [
'browser/chrome/browser/browser.properties:addonPostInstall.okay.key',
'devtools/client/webconsole.properties:table.key',
];
$ignored_files = [
'extensions/irc/chrome/chatzilla.properties',
'toolkit/chrome/global/charsetMenu.properties',
];

$commandkey_results = [];
/*
Get keyboard shortcuts different from English (ignoring case), and
exclude known false positives.
*/
foreach ($commandkey_ids as $commandkey_id) {
if (in_array($commandkey_id, $ignored_ids)) {
continue;
}
$commandkey_results = [];
/*
Get keyboard shortcuts different from English (ignoring case), and
exclude known false positives.
*/
foreach ($commandkey_ids as $commandkey_id) {
if (in_array($commandkey_id, $ignored_ids)) {
continue;
}

if (Strings::startsWith($commandkey_id, $ignored_files)) {
continue;
}
if (Strings::startsWith($commandkey_id, $ignored_files)) {
continue;
}

if (mb_strtoupper($target[$commandkey_id]) != mb_strtoupper($source[$commandkey_id])) {
$commandkey_results[] = $commandkey_id;
if (mb_strtoupper($target[$commandkey_id]) != mb_strtoupper($source[$commandkey_id])) {
$commandkey_results[] = [
'id' => $commandkey_id,
'source_shortcut' => $source[$commandkey_id],
'target_shortcut' => $target[$commandkey_id],
];
}
}
Cache::setKey($cache_id, $commandkey_results);
unset($source);
unset($target);
}

// Build components filter
$components = Project::getComponents(array_flip($commandkey_results));
$components = Project::getComponents(array_flip(array_column($commandkey_results, 'id')));
$filter_block = ShowResults::buildComponentsFilter($components);

// RTL support
Expand Down
Loading

0 comments on commit 91a685f

Please sign in to comment.