PageRenderTime 57ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/LanguagesManager/Commands/CompareKeys.php

https://github.com/CodeYellowBV/piwik
PHP | 111 lines | 82 code | 20 blank | 9 comment | 13 complexity | 3c0212eec681451026fd1779d4f268ad MD5 | raw file
Possible License(s): LGPL-3.0, JSON, MIT, GPL-3.0, LGPL-2.1, GPL-2.0, AGPL-1.0, BSD-2-Clause, BSD-3-Clause
  1. <?php
  2. /**
  3. * Piwik - free/libre analytics platform
  4. *
  5. * @link http://piwik.org
  6. * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  7. *
  8. */
  9. namespace Piwik\Plugins\LanguagesManager\Commands;
  10. use Piwik\Plugin\ConsoleCommand;
  11. use Piwik\Translate;
  12. use Symfony\Component\Console\Input\ArrayInput;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Input\InputOption;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. /**
  17. */
  18. class CompareKeys extends ConsoleCommand
  19. {
  20. protected function configure()
  21. {
  22. $this->setName('translations:compare')
  23. ->setDescription('Updates translation files')
  24. ->addOption('username', 'u', InputOption::VALUE_OPTIONAL, 'oTrance username')
  25. ->addOption('password', 'p', InputOption::VALUE_OPTIONAL, 'oTrance password');
  26. }
  27. protected function execute(InputInterface $input, OutputInterface $output)
  28. {
  29. $dialog = $this->getHelperSet()->get('dialog');
  30. $command = $this->getApplication()->find('translations:fetch');
  31. $arguments = array(
  32. 'command' => 'translations:fetch',
  33. '--username' => $input->getOption('username'),
  34. '--password' => $input->getOption('password'),
  35. '--keep-english' => true,
  36. );
  37. $inputObject = new ArrayInput($arguments);
  38. $inputObject->setInteractive($input->isInteractive());
  39. $command->run($inputObject, $output);
  40. $englishFromOTrance = FetchFromOTrance::getDownloadPath() . DIRECTORY_SEPARATOR . 'en.json';
  41. if (!file_exists($englishFromOTrance)) {
  42. $output->writeln("English file from oTrance missing. Aborting");
  43. return;
  44. }
  45. $englishFromOTrance = json_decode(file_get_contents($englishFromOTrance), true);
  46. Translate::reloadLanguage('en');
  47. $availableTranslations = $GLOBALS['Piwik_translations'];
  48. $categories = array_unique(array_merge(array_keys($englishFromOTrance), array_keys($availableTranslations)));
  49. sort($categories);
  50. $unnecessary = $outdated = $missing = array();
  51. foreach ($categories AS $category)
  52. {
  53. if (!empty($englishFromOTrance[$category])) {
  54. foreach ($englishFromOTrance[$category] AS $key => $value) {
  55. if(!array_key_exists($category, $availableTranslations) || !array_key_exists($key, $availableTranslations[$category])) {
  56. $unnecessary[] = sprintf('%s_%s', $category, $key);
  57. continue;
  58. } else if (html_entity_decode($availableTranslations[$category][$key]) != html_entity_decode($englishFromOTrance[$category][$key])) {
  59. $outdated[] = sprintf('%s_%s', $category, $key);
  60. continue;
  61. }
  62. }
  63. }
  64. if (!empty($availableTranslations[$category])) {
  65. foreach ($availableTranslations[$category] AS $key => $value) {
  66. if(!array_key_exists($category, $englishFromOTrance) || !array_key_exists($key, $englishFromOTrance[$category])) {
  67. $missing[] = sprintf('%s_%s', $category, $key);
  68. continue;
  69. }
  70. }
  71. }
  72. }
  73. $output->writeln("");
  74. if (!empty($missing)) {
  75. $output->writeln("<bg=yellow;options=bold>-- Following keys are missing on oTrance --</bg=yellow;options=bold>");
  76. $output->writeln(implode("\n", $missing));
  77. $output->writeln("");
  78. }
  79. if (!empty($unnecessary)) {
  80. $output->writeln("<bg=yellow;options=bold>-- Following keys might be unnecessary on oTrance --</bg=yellow;options=bold>");
  81. $output->writeln(implode("\n", $unnecessary));
  82. $output->writeln("");
  83. }
  84. if (!empty($outdated)) {
  85. $output->writeln("<bg=yellow;options=bold>-- Following keys are outdated on oTrance --</bg=yellow;options=bold>");
  86. $output->writeln(implode("\n", $outdated));
  87. $output->writeln("");
  88. }
  89. $output->writeln("Finished.");
  90. }
  91. }