[TASK] Clean up the phpDoc of t3lib/l10n/
[Packages/TYPO3.CMS.git] / t3lib / l10n / parser / class.t3lib_l10n_parser_llphp.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2011 Dominique Feyer <dfeyer@reelpeek.net>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * Parser for PHP locallang array.
30 *
31 * @package TYPO3
32 * @subpackage t3lib
33 * @author Dominique Feyer <dfeyer@reelpeek.net>
34 * @author Dmitry Dulepov <dmitry.dulepov@gmail.com>
35 */
36 class t3lib_l10n_parser_Llphp implements t3lib_l10n_parser {
37
38 /**
39 * @var string
40 */
41 protected $cacheFileName;
42
43 /**
44 * @var t3lib_cs
45 */
46 protected $csConvObj;
47
48 /**
49 * @var string
50 */
51 protected $hashSource;
52
53 /**
54 * @var string
55 */
56 protected $sourceCharset;
57
58 /**
59 * @var string
60 */
61 protected $targetCharset;
62
63 /**
64 * Initializes the parser.
65 *
66 * @return void
67 */
68 public function __construct() {
69 $this->createCsConvObject();
70 }
71
72 /**
73 * Returns parsed representation of PHP locallang file.
74 *
75 * @param string $sourcePath Source file path
76 * @param string $languageKey Language key
77 * @param string $charset Charset
78 * @return array
79 * @throws RuntimeException
80 */
81 public function getParsedData($sourcePath, $languageKey, $charset = '') {
82 $this->validateParameters($sourcePath, $languageKey);
83 $this->setCharsets($languageKey, $charset);
84 $this->generateCacheFileName($sourcePath, $languageKey);
85
86 if (!file_exists($this->cacheFileName)) {
87 $LOCAL_LANG = $this->generateCacheFile($sourcePath, $languageKey);
88 } else {
89 $LOCAL_LANG = $this->getContentFromCacheFile();
90 }
91
92 $xliff = $this->convertToXLIFF($LOCAL_LANG);
93
94 return $xliff;
95 }
96
97 /**
98 * Converts the LOCAL_LANG array to XLIFF structure.
99 *
100 * @param array $LOCAL_LANG
101 * @return array
102 */
103 protected function convertToXLIFF(array $LOCAL_LANG) {
104 foreach ($LOCAL_LANG as &$keysLabels) {
105 foreach ($keysLabels as &$label) {
106 $label = array(0 => array(
107 'target' => $label,
108 ));
109 }
110 unset($label);
111 }
112 return $LOCAL_LANG;
113 }
114
115 /**
116 * Creates a character conversion object.
117 *
118 * @return void
119 */
120 protected function createCsConvObject() {
121 if (is_object($GLOBALS['LANG'])) {
122 $this->csConvObj = $GLOBALS['LANG']->csConvObj;
123 } elseif (is_object($GLOBALS['TSFE'])) {
124 $this->csConvObj = $GLOBALS['TSFE']->csConvObj;
125 } else {
126 $this->csConvObj = t3lib_div::makeInstance('t3lib_cs');
127 }
128 }
129
130 /**
131 * Generates the cache file.
132 *
133 * @param string $sourcePath
134 * @param string $languageKey
135 * @return array
136 * @throws RuntimeException
137 */
138 protected function generateCacheFile($sourcePath, $languageKey) {
139 $LOCAL_LANG = array();
140
141 // Get PHP data
142 include($sourcePath);
143 if (!is_array($LOCAL_LANG)) {
144 $fileName = substr($sourcePath, strlen(PATH_site));
145 throw new RuntimeException(
146 'TYPO3 Fatal Error: "' . $fileName . '" is no TYPO3 language file!',
147 1308898491
148 );
149 }
150
151 // Converting the default language (English)
152 // This needs to be done for a few accented loan words and extension names
153 if (is_array($LOCAL_LANG['default']) && $this->targetCharset !== 'utf-8') {
154 foreach ($LOCAL_LANG['default'] as &$labelValue) {
155 $labelValue = $this->csConvObj->conv($labelValue, 'utf-8', $this->targetCharset);
156 }
157 unset($labelValue);
158 }
159
160 if ($languageKey !== 'default' && is_array($LOCAL_LANG[$languageKey]) && $this->sourceCharset != $this->targetCharset) {
161 foreach ($LOCAL_LANG[$languageKey] as &$labelValue) {
162 $labelValue = $this->csConvObj->conv($labelValue, $this->sourceCharset, $this->targetCharset);
163 }
164 unset($labelValue);
165 }
166
167 // Cache the content now:
168 $serContent = array('origFile' => $this->hashSource, 'LOCAL_LANG' => array('default' => $LOCAL_LANG['default'], $languageKey => $LOCAL_LANG[$languageKey]));
169 $res = t3lib_div::writeFileToTypo3tempDir($this->cacheFileName, serialize($serContent));
170 if ($res) {
171 throw new RuntimeException(
172 'TYPO3 Fatal Error: "' . $res,
173 1308898501
174 );
175 }
176 return $LOCAL_LANG;
177 }
178
179 /**
180 * Generates the name of the cached file.
181 *
182 * @param string $sourcePath
183 * @param string $languageKey
184 * @return void
185 */
186 protected function generateCacheFileName($sourcePath, $languageKey) {
187 $this->hashSource = substr($sourcePath, strlen(PATH_site)) . '|' . date('d-m-Y H:i:s', filemtime($sourcePath)) . '|version=2.3';
188 $this->cacheFileName = PATH_site . 'typo3temp/llxml/' .
189 substr(basename($sourcePath), 10, 15) .
190 '_' . t3lib_div::shortMD5($this->hashSource) . '.' . $languageKey .
191 '.' . $this->targetCharset . '.cache';
192 }
193
194 /**
195 * Obtains the content from the cache file.
196 *
197 * @return array
198 */
199 protected function getContentFromCacheFile() {
200 $serContent = (array)unserialize(file_get_contents($this->cacheFileName));
201 $LOCAL_LANG = $serContent['LOCAL_LANG'];
202 return (array)$LOCAL_LANG;
203 }
204
205 /**
206 * Checks if the file is within the web root.
207 *
208 * @param string $fileName
209 * @return boolean
210 */
211 protected function isWithinWebRoot($fileName) {
212 return (bool)t3lib_div::getFileAbsFileName($fileName);
213 }
214
215 /**
216 * Sets character sets for the language key.
217 *
218 * @param string $languageKey
219 * @param string $charset
220 * @return void
221 */
222 protected function setCharsets($languageKey, $charset) {
223 $this->sourceCharset = $this->csConvObj->parse_charset($this->csConvObj->charSetArray[$languageKey]
224 ? $this->csConvObj->charSetArray[$languageKey] : 'utf-8');
225 if ($charset) {
226 $this->targetCharset = $this->csConvObj->parse_charset($charset);
227 } else {
228 $this->targetCharset = 'utf-8';
229 }
230 }
231
232 /**
233 * Validates parameters for the function.
234 *
235 * @param string $sourcePath
236 * @param string $languageKey
237 * @return void
238 * @throws RuntimeException
239 */
240 protected function validateParameters($sourcePath, $languageKey) {
241 if (!$this->isWithinWebRoot($sourcePath) || !@is_file($sourcePath) || !$languageKey) {
242 throw new RuntimeException(sprintf('Invalid source path (%s) or languageKey (%s)', $sourcePath, $languageKey), 1309245002);
243 }
244 }
245 }
246
247 ?>