PageRenderTime 73ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/LanguagesManager/Commands/Update.php

https://github.com/CodeYellowBV/piwik
PHP | 162 lines | 110 code | 36 blank | 16 comment | 7 complexity | 46c3b175e288af3385f879da2eba8289 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\Plugins\LanguagesManager\API;
  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\NullOutput;
  16. use Symfony\Component\Console\Output\OutputInterface;
  17. /**
  18. */
  19. class Update extends ConsoleCommand
  20. {
  21. protected function configure()
  22. {
  23. $this->setName('translations:update')
  24. ->setDescription('Updates translation files')
  25. ->addOption('username', 'u', InputOption::VALUE_OPTIONAL, 'oTrance username')
  26. ->addOption('password', 'p', InputOption::VALUE_OPTIONAL, 'oTrance password')
  27. ->addOption('plugin', 'P', InputOption::VALUE_OPTIONAL, 'optional name of plugin to update translations for');
  28. }
  29. protected function execute(InputInterface $input, OutputInterface $output)
  30. {
  31. $dialog = $this->getHelperSet()->get('dialog');
  32. $command = $this->getApplication()->find('translations:fetch');
  33. $arguments = array(
  34. 'command' => 'translations:fetch',
  35. '--username' => $input->getOption('username'),
  36. '--password' => $input->getOption('password')
  37. );
  38. $inputObject = new ArrayInput($arguments);
  39. $inputObject->setInteractive($input->isInteractive());
  40. $command->run($inputObject, $output);
  41. $languages = API::getInstance()->getAvailableLanguageNames();
  42. $languageCodes = array();
  43. foreach ($languages AS $languageInfo) {
  44. $languageCodes[] = $languageInfo['code'];
  45. }
  46. $plugin = $input->getOption('plugin');
  47. $files = _glob(FetchFromOTrance::getDownloadPath() . DIRECTORY_SEPARATOR . '*.json');
  48. $output->writeln("Starting to import new language files");
  49. if (!$input->isInteractive()) {
  50. $output->writeln("(!) Non interactive mode: New languages will be skipped");
  51. }
  52. $progress = $this->getHelperSet()->get('progress');
  53. $progress->start($output, count($files));
  54. foreach ($files AS $filename) {
  55. $progress->advance();
  56. $code = basename($filename, '.json');
  57. if (!in_array($code, $languageCodes)) {
  58. if (!empty($plugin)) {
  59. continue; # never create a new language for plugin only
  60. }
  61. $createNewFile = false;
  62. if ($input->isInteractive()) {
  63. $createNewFile = $dialog->askConfirmation($output, "\nLanguage $code does not exist. Should it be added? ", false);
  64. }
  65. if (!$createNewFile) {
  66. continue; # do not create a new file for the language
  67. }
  68. @touch(PIWIK_DOCUMENT_ROOT . DIRECTORY_SEPARATOR . 'lang' . DIRECTORY_SEPARATOR . $code . '.json');
  69. API::unsetInstance(); // unset language manager instance, so valid names are refetched
  70. }
  71. $command = $this->getApplication()->find('translations:set');
  72. $arguments = array(
  73. 'command' => 'translations:set',
  74. '--code' => $code,
  75. '--file' => $filename,
  76. '--plugin' => $plugin
  77. );
  78. $inputObject = new ArrayInput($arguments);
  79. $inputObject->setInteractive($input->isInteractive());
  80. $command->run($inputObject, new NullOutput());
  81. // update core modules that aren't in their own repo
  82. if (empty($plugin)) {
  83. foreach (self::getPluginsInCore() AS $pluginName) {
  84. // update translation files
  85. $command = $this->getApplication()->find('translations:set');
  86. $arguments = array(
  87. 'command' => 'translations:set',
  88. '--code' => $code,
  89. '--file' => $filename,
  90. '--plugin' => $pluginName
  91. );
  92. $inputObject = new ArrayInput($arguments);
  93. $inputObject->setInteractive($input->isInteractive());
  94. $command->run($inputObject, new NullOutput());
  95. }
  96. }
  97. }
  98. $progress->finish();
  99. $output->writeln("Finished.");
  100. }
  101. /**
  102. * Returns all plugins having their own translations that are bundled in core
  103. * @return array
  104. */
  105. public static function getPluginsInCore()
  106. {
  107. static $pluginsInCore;
  108. if (!empty($pluginsInCore)) {
  109. return $pluginsInCore;
  110. }
  111. $submodules = shell_exec('git submodule status');
  112. preg_match_all('/plugins\/([a-zA-z]+) /', $submodules, $matches);
  113. $submodulePlugins = $matches[1];
  114. // ignore complete new plugins aswell
  115. $changes = shell_exec('git status');
  116. preg_match_all('/plugins\/([a-zA-z]+)\/\n/', $changes, $matches);
  117. $newPlugins = $matches[1];
  118. $pluginsNotInCore = array_merge($submodulePlugins, $newPlugins);
  119. $pluginsWithTranslations = glob(sprintf('%s/plugins/*/lang/en.json', PIWIK_INCLUDE_PATH));
  120. $pluginsWithTranslations = array_map(function($elem){
  121. return str_replace(array(sprintf('%s/plugins/', PIWIK_INCLUDE_PATH), '/lang/en.json'), '', $elem);
  122. }, $pluginsWithTranslations);
  123. $pluginsInCore = array_diff($pluginsWithTranslations, $pluginsNotInCore);
  124. return $pluginsInCore;
  125. }
  126. }