[FEATURE] Add task to auto update languages
[Packages/TYPO3.CMS.git] / typo3 / sysext / lang / Classes / Service / UpdateTranslationService.php
1 <?php
2 namespace TYPO3\CMS\Lang\Service;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2013 Wouter Wolters <typo3@wouterwolters.nl>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 /**
28 * Update languages service
29 */
30 class UpdateTranslationService {
31
32 /**
33 * Status codes for AJAX response
34 */
35 const TRANSLATION_NOT_AVAILABLE = 0;
36 const TRANSLATION_AVAILABLE = 1;
37 const TRANSLATION_FAILED = 2;
38 const TRANSLATION_OK = 3;
39 const TRANSLATION_INVALID = 4;
40 const TRANSLATION_UPDATED = 5;
41
42 /**
43 * @var \TYPO3\CMS\Lang\Domain\Repository\LanguageRepository
44 * @inject
45 */
46 protected $languageRepository;
47
48 /**
49 * @var \TYPO3\CMS\Extensionmanager\Utility\Repository\Helper
50 * @inject
51 */
52 protected $repositoryHelper;
53
54 /**
55 * @var \TYPO3\CMS\Lang\Utility\Connection\Ter
56 * @inject
57 */
58 protected $terConnection;
59
60 /**
61 * @var array
62 */
63 protected $translationStates = array();
64
65 /**
66 * Update translation for given extension
67 *
68 * @param string $extension The extension key
69 * @param string $locales Comma separated list of locales to update
70 * @return array
71 */
72 public function updateTranslation($extension, $locales) {
73 if (is_string($locales)) {
74 $locales = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $locales);
75 }
76 $locales = array_flip((array) $locales);
77
78 foreach ($locales as $locale => $key) {
79 $state = static::TRANSLATION_INVALID;
80 try {
81 $state = $this->getTranslationStateForExtension($extension, $locale);
82 if ($state === static::TRANSLATION_AVAILABLE) {
83 $state = $this->updateTranslationForExtension($extension, $locale);
84 }
85 } catch (\Exception $exception) {
86 $error = $exception->getMessage();
87 }
88 $locales[$locale] = array(
89 'state' => $state,
90 'error' => $error,
91 );
92 }
93 return $locales;
94 }
95
96 /**
97 * Returns the translation state for an extension
98 *
99 * @param string $extensionKey The extension key
100 * @param string $locale Locale to return
101 * @return integer Translation state
102 */
103 protected function getTranslationStateForExtension($extensionKey, $locale) {
104 if (empty($extensionKey) || empty($locale)) {
105 return static::TRANSLATION_INVALID;
106 }
107
108 $identifier = $extensionKey . '-' . $locale;
109 if (isset($this->translationStates[$identifier])) {
110 return $this->translationStates[$identifier];
111 }
112
113 $selectedLanguages = $this->languageRepository->findSelected();
114 if (empty($selectedLanguages) || !is_array($selectedLanguages)) {
115 return static::TRANSLATION_INVALID;
116 }
117
118 $mirrorUrl = $this->repositoryHelper->getMirrors()->getMirrorUrl();
119 $status = $this->terConnection->fetchTranslationStatus($extensionKey, $mirrorUrl);
120
121 foreach ($selectedLanguages as $language) {
122 $stateLocale = $language->getLocale();
123 $stateIdentifier = $extensionKey . '-' . $stateLocale;
124 $this->translationStates[$stateIdentifier] = static::TRANSLATION_INVALID;
125
126 if (empty($status[$stateLocale]) || !is_array($status[$stateLocale])) {
127 $this->translationStates[$stateIdentifier] = static::TRANSLATION_NOT_AVAILABLE;
128 continue;
129 }
130
131 $md5 = $this->getTranslationFileMd5($extensionKey, $stateLocale);
132 if ($md5 !== $status[$stateLocale]['md5']) {
133 $this->translationStates[$stateIdentifier] = static::TRANSLATION_AVAILABLE;
134 continue;
135 }
136
137 $this->translationStates[$stateIdentifier] = static::TRANSLATION_OK;
138 }
139
140 return $this->translationStates[$identifier];
141 }
142
143 /**
144 * Returns the md5 of a translation file
145 *
146 * @param string $extensionKey The extension key
147 * @param string $locale The locale
148 * @return string The md5 value
149 */
150 protected function getTranslationFileMd5($extensionKey, $locale) {
151 if (empty($extensionKey) || empty($locale)) {
152 return '';
153 }
154 $fileName = PATH_site . 'typo3temp' . DIRECTORY_SEPARATOR . $extensionKey . '-l10n-' . $locale . '.zip';
155 if (is_file($fileName)) {
156 return md5_file($fileName);
157 }
158 return '';
159 }
160
161 /**
162 * Update the translation for an extension
163 *
164 * @param string $extensionKey The extension key
165 * @param string $locale Locale to update
166 * @return integer Translation state
167 */
168 protected function updateTranslationForExtension($extensionKey, $locale) {
169 if (empty($extensionKey) || empty($locale)) {
170 return static::TRANSLATION_INVALID;
171 }
172
173 $state = static::TRANSLATION_FAILED;
174 $mirrorUrl = $this->repositoryHelper->getMirrors()->getMirrorUrl();
175 $updateResult = $this->terConnection->updateTranslation($extensionKey, $locale, $mirrorUrl);
176 if ($updateResult === TRUE) {
177 $state = static::TRANSLATION_UPDATED;
178 }
179
180 return $state;
181 }
182
183 }
184 ?>