[!!!][TASK] Use native trigger_error and ErrorHandler for deprecations
[Packages/TYPO3.CMS.git] / typo3 / sysext / lang / Classes / Command / LanguageUpdateCommand.php
1 <?php
2 namespace TYPO3\CMS\Lang\Command;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16 use Symfony\Component\Console\Command\Command;
17 use Symfony\Component\Console\Helper\ProgressBar;
18 use Symfony\Component\Console\Input\InputArgument;
19 use Symfony\Component\Console\Input\InputInterface;
20 use Symfony\Component\Console\Output\OutputInterface;
21 use TYPO3\CMS\Core\Cache\CacheManager;
22 use TYPO3\CMS\Core\Package\PackageInterface;
23 use TYPO3\CMS\Core\Package\PackageManager;
24 use TYPO3\CMS\Core\Utility\GeneralUtility;
25 use TYPO3\CMS\Extbase\Object\ObjectManager;
26 use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
27 use TYPO3\CMS\Extbase\SignalSlot\Dispatcher as SignalSlotDispatcher;
28 use TYPO3\CMS\Lang\Domain\Repository\LanguageRepository;
29 use TYPO3\CMS\Lang\Service\RegistryService;
30 use TYPO3\CMS\Lang\Service\TranslationService;
31
32 /**
33 * Core function for updating the language files
34 */
35 class LanguageUpdateCommand extends Command
36 {
37 /**
38 * @var ObjectManagerInterface
39 */
40 protected $objectManager;
41
42 /**
43 * @param string $name
44 * @throws \Symfony\Component\Console\Exception\LogicException
45 * @throws \InvalidArgumentException
46 */
47 public function __construct($name = null)
48 {
49 parent::__construct($name);
50
51 $this->objectManager = GeneralUtility::makeInstance(ObjectManager::class);
52 }
53
54 /**
55 * Configure the command by defining the name, options and arguments
56 */
57 protected function configure()
58 {
59 $description = ($this->getName() === 'language:update')
60 ? '[Deprecated] Use lang:language:update instead'
61 : 'Update the language files of all activated extensions';
62
63 $this
64 ->setDescription($description)
65 ->addArgument(
66 'locales',
67 InputArgument::IS_ARRAY | InputArgument::OPTIONAL
68 );
69 }
70
71 /**
72 * @param InputInterface $input
73 * @param OutputInterface $output
74 *
75 * @throws \InvalidArgumentException
76 * @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException
77 *
78 * @return int
79 */
80 protected function execute(InputInterface $input, OutputInterface $output)
81 {
82 if ($this->getName() === 'language:update') {
83 $message = 'bin/typo3 language:update is deprecated, use bin/typo3 lang:language:update instead';
84 $output->writeln('<error>' . $message . '</error>');
85 trigger_error($message, E_USER_DEPRECATED);
86 }
87
88 try {
89 $localesToUpdate = $input->getArgument('locales');
90 } catch (\Exception $e) {
91 $localesToUpdate = [];
92 }
93
94 /** @var $translationService \TYPO3\CMS\Lang\Service\TranslationService */
95 $translationService = $this->objectManager->get(TranslationService::class);
96 /** @var $languageRepository \TYPO3\CMS\Lang\Domain\Repository\LanguageRepository */
97 $languageRepository = $this->objectManager->get(LanguageRepository::class);
98
99 $allLocales = array_map(function ($language) {
100 /** @var $language \TYPO3\CMS\Lang\Domain\Model\Language */
101 return $language->getLocale();
102 }, $languageRepository->findSelected());
103
104 $nonUpdatableLocales = array_diff($localesToUpdate, $allLocales);
105 $updatableLocales = array_intersect($localesToUpdate, $allLocales);
106
107 $locales = empty($updatableLocales) ? $allLocales : $updatableLocales;
108
109 foreach ($nonUpdatableLocales as $nonUpdatableLocale) {
110 $output->writeln(sprintf(
111 '<error>Skipping locale "%s" as it is not activated in the backend</error>',
112 $nonUpdatableLocale
113 ));
114 }
115
116 $output->writeln(sprintf(
117 '<info>Updating language packs of all activated extensions for locale(s) "%s"</info>',
118 implode('", "', $locales)
119 ));
120
121 /** @var RegistryService $registryService */
122 $registryService = $this->objectManager->get(RegistryService::class);
123
124 /** @var PackageManager $packageManager */
125 $packageManager = $this->objectManager->get(PackageManager::class);
126 $this->emitPackagesMayHaveChangedSignal();
127 $packages = $packageManager->getAvailablePackages();
128
129 $progressBar = new ProgressBar($output, count($locales) * count($packages));
130 foreach ($locales as $locale) {
131 /** @var PackageInterface $package */
132 foreach ($packages as $package) {
133 $extensionKey = $package->getPackageKey();
134 $result = $translationService->updateTranslation($extensionKey, $locale);
135 if (empty($result[$extensionKey][$locale]['error'])) {
136 $registryService->set($locale, $GLOBALS['EXEC_TIME']);
137 }
138 $progressBar->advance();
139 }
140 }
141 $progressBar->finish();
142
143 // Flush language cache
144 GeneralUtility::makeInstance(CacheManager::class)->getCache('l10n')->flush();
145
146 return 0;
147 }
148
149 /**
150 * Emits packages may have changed signal
151 *
152 * @throws \TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotException
153 * @throws \TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotReturnException
154 */
155 protected function emitPackagesMayHaveChangedSignal()
156 {
157 $this->objectManager->get(SignalSlotDispatcher::class)->dispatch('PackageManagement', 'packagesMayHaveChanged');
158 }
159 }