PageRenderTime 51ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/LanguagesManager/Commands/FetchFromOTrance.php

https://github.com/CodeYellowBV/piwik
PHP | 176 lines | 122 code | 41 blank | 13 comment | 12 complexity | 228c4b2a93a30d66319527f1663a5e4f 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\Unzip;
  12. use Symfony\Component\Console\Input\InputInterface;
  13. use Symfony\Component\Console\Input\InputOption;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. /**
  16. */
  17. class FetchFromOTrance extends ConsoleCommand
  18. {
  19. const DOWNLOADPATH = 'tmp/oTrance';
  20. protected function configure()
  21. {
  22. $this->setName('translations:fetch')
  23. ->setDescription('Fetches translations files from oTrance to '.self::DOWNLOADPATH)
  24. ->addOption('username', 'u', InputOption::VALUE_OPTIONAL, 'oTrance username')
  25. ->addOption('password', 'p', InputOption::VALUE_OPTIONAL, 'oTrance password')
  26. ->addOption('keep-english', 'k', InputOption::VALUE_NONE, 'keep english file');
  27. }
  28. protected function execute(InputInterface $input, OutputInterface $output)
  29. {
  30. $output->writeln("Starting to fetch latest language pack");
  31. $dialog = $this->getHelperSet()->get('dialog');
  32. $cookieFile = self::getDownloadPath() . DIRECTORY_SEPARATOR . 'cookie.txt';
  33. @unlink($cookieFile);
  34. $username = $input->getOption('username');
  35. $password = $input->getOption('password');
  36. while (!file_exists($cookieFile)) {
  37. if (empty($username)) {
  38. $username = $dialog->ask($output, 'What is your oTrance username? ');
  39. }
  40. if (empty($password)) {
  41. $password = $dialog->askHiddenResponse($output, 'What is your oTrance password? ');
  42. }
  43. // send login request to oTrance and save the login cookie
  44. $curl = curl_init('http://translations.piwik.org/public/index/login');
  45. curl_setopt($curl, CURLOPT_POSTFIELDS, sprintf("user=%s&pass=%s&autologin=1", $username, $password));
  46. curl_setopt($curl, CURLOPT_COOKIEJAR, $cookieFile);
  47. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  48. curl_exec($curl);
  49. curl_close($curl);
  50. if (strpos(file_get_contents($cookieFile), 'oTranCe_autologin') !== false) {
  51. break;
  52. }
  53. $username = null;
  54. $password = null;
  55. @unlink($cookieFile);
  56. $output->writeln("Invalid oTrance credentials. Please try again...");
  57. }
  58. // send request to create a new download package using the cookie file
  59. $createNewPackage = true;
  60. if ($input->isInteractive()) {
  61. $createNewPackage = $dialog->askConfirmation($output, 'Shall we create a new language pack? ');
  62. }
  63. if ($createNewPackage) {
  64. $curl = curl_init('http://translations.piwik.org/public/export/update.all');
  65. curl_setopt($curl, CURLOPT_COOKIEFILE, $cookieFile);
  66. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  67. curl_exec($curl);
  68. curl_close($curl);
  69. }
  70. // request download page to search for available packages
  71. $curl = curl_init('http://translations.piwik.org/public/downloads/');
  72. curl_setopt($curl, CURLOPT_COOKIEFILE, $cookieFile);
  73. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  74. $response = curl_exec($curl);
  75. curl_close($curl);
  76. preg_match_all('/language\_pack\-[0-9]{8}\-[0-9]{6}\.tar\.gz/i', $response, $matches);
  77. if (empty($matches[0])) {
  78. $output->writeln("No packages found for download. Please try again.");
  79. return;
  80. }
  81. $downloadPackage = array_shift($matches[0]);
  82. $continueWithPackage = true;
  83. if ($input->isInteractive()) {
  84. $continueWithPackage = $dialog->askConfirmation($output, "Found language pack $downloadPackage. Proceed? ");
  85. }
  86. if (!$continueWithPackage) {
  87. $output->writeln('Aborted.');
  88. return;
  89. }
  90. // download language pack
  91. $packageHandle = fopen(self::getDownloadPath() . DIRECTORY_SEPARATOR . 'language_pack.tar.gz', 'w');
  92. $curl = curl_init('http://translations.piwik.org/public/downloads/download/file/'.$downloadPackage);
  93. curl_setopt($curl, CURLOPT_COOKIEFILE, self::getDownloadPath() . DIRECTORY_SEPARATOR . 'cookie.txt');
  94. curl_setopt($curl, CURLOPT_FILE, $packageHandle);
  95. curl_exec($curl);
  96. curl_close($curl);
  97. @unlink($cookieFile);
  98. $output->writeln("Extracting package...");
  99. $unzipper = Unzip::factory('tar.gz', self::getDownloadPath() . DIRECTORY_SEPARATOR . 'language_pack.tar.gz');
  100. $unzipper->extract(self::getDownloadPath());
  101. if (!$input->getOption('keep-english')) {
  102. @unlink(self::getDownloadPath() . DIRECTORY_SEPARATOR . 'en.php');
  103. @unlink(self::getDownloadPath() . DIRECTORY_SEPARATOR . 'en.json');
  104. }
  105. @unlink(self::getDownloadPath() . DIRECTORY_SEPARATOR . 'language_pack.tar.gz');
  106. $filesToConvert = _glob(self::getDownloadPath() . DIRECTORY_SEPARATOR . '*.php');
  107. $output->writeln("Converting downloaded php files to json");
  108. $progress = $this->getHelperSet()->get('progress');
  109. $progress->start($output, count($filesToConvert));
  110. foreach ($filesToConvert AS $filename) {
  111. require_once $filename;
  112. $basename = explode(".", basename($filename));
  113. $nested = array();
  114. foreach ($translations as $key => $value) {
  115. list($plugin, $nkey) = explode("_", $key, 2);
  116. $nested[$plugin][$nkey] = $value;
  117. }
  118. $translations = $nested;
  119. $data = json_encode($translations, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  120. $newFile = sprintf("%s/%s.json", self::getDownloadPath(), $basename[0]);
  121. file_put_contents($newFile, $data);
  122. @unlink($filename);
  123. $progress->advance();
  124. }
  125. $progress->finish();
  126. $output->writeln("Finished fetching new language files from oTrance");
  127. }
  128. public static function getDownloadPath() {
  129. $path = PIWIK_DOCUMENT_ROOT . DIRECTORY_SEPARATOR . self::DOWNLOADPATH;
  130. if (!is_dir($path)) {
  131. mkdir($path);
  132. }
  133. return $path;
  134. }
  135. }