Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 67 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
tao_helpers_translation_SourceCodeExtractor | |
0.00% |
0 / 67 |
|
0.00% |
0 / 9 |
812 | |
0.00% |
0 / 1 |
extract | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
recursiveSearch | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
56 | |||
__construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getFileTypes | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setFileTypes | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getTranslationsInFile | |
0.00% |
0 / 25 |
|
0.00% |
0 / 1 |
90 | |||
getBannedFileType | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setBannedFileType | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getTranslationPhrases | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
20 |
1 | <?php |
2 | |
3 | /* |
4 | * This program is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU General Public License |
6 | * as published by the Free Software Foundation; under version 2 |
7 | * of the License (non-upgradable). |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write to the Free Software |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | * |
18 | * Copyright (c) 2008-2010 (original work) Deutsche Institut für Internationale Pädagogische Forschung |
19 | * (under the project TAO-TRANSFER); |
20 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
21 | * (under the project TAO-SUSTAIN & TAO-DEV); |
22 | * 2013-2014 (update and modification) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
23 | */ |
24 | |
25 | /** |
26 | * A TranslationExtractor that focuses on the extraction of Translation Units |
27 | * source code. It searches for calls to the __() function. The generated |
28 | * units will get the first parameter of the __() function as their source. |
29 | * |
30 | * @access public |
31 | * @author Jerome Bogaerts |
32 | * @package tao |
33 | * @since 2.2 |
34 | |
35 | * @version 1.0 |
36 | */ |
37 | class tao_helpers_translation_SourceCodeExtractor extends tao_helpers_translation_TranslationExtractor |
38 | { |
39 | // --- ASSOCIATIONS --- |
40 | |
41 | |
42 | // --- ATTRIBUTES --- |
43 | |
44 | /** |
45 | * Short description of attribute filesTypes |
46 | * |
47 | * @var array |
48 | */ |
49 | private $filesTypes = []; |
50 | |
51 | /** |
52 | * |
53 | * @var array |
54 | */ |
55 | private $bannedFileType = [ '.min.js' ]; |
56 | // --- OPERATIONS --- |
57 | |
58 | /** |
59 | * Short description of method extract |
60 | * |
61 | * @access public |
62 | * @author firstname and lastname of author, <author@example.org> |
63 | */ |
64 | public function extract() |
65 | { |
66 | |
67 | $this->setTranslationUnits([]); |
68 | foreach ($this->getPaths() as $dir) { |
69 | // Directories should come with a trailing slash. |
70 | $d = strrev($dir); |
71 | if ($d[0] !== '/') { |
72 | $dir = $dir . '/'; |
73 | } |
74 | |
75 | $this->recursiveSearch($dir); |
76 | } |
77 | } |
78 | |
79 | /** |
80 | * Short description of method recursiveSearch |
81 | * |
82 | * @access private |
83 | * @author firstname and lastname of author, <author@example.org> |
84 | * @param string $directory |
85 | */ |
86 | private function recursiveSearch($directory) |
87 | { |
88 | |
89 | if (is_dir($directory)) { |
90 | // We get the list of files and directories. |
91 | if (($files = scandir($directory)) !== false) { |
92 | foreach ($files as $fd) { |
93 | if (!preg_match("/^\./", $fd) && $fd != "ext") { |
94 | // If it is a directory ... |
95 | if (is_dir($directory . $fd . "/")) { |
96 | $this->recursiveSearch($directory . $fd . "/"); |
97 | // If it is a file ... |
98 | } else { |
99 | // Retrieve from the file ... |
100 | $this->getTranslationsInFile($directory . $fd); |
101 | } |
102 | } |
103 | } |
104 | } |
105 | } |
106 | } |
107 | |
108 | /** |
109 | * Creates a SourceCodeExtractor for a given set of paths. Only file |
110 | * that matches an entry in the $fileTypes array will be processed. |
111 | * |
112 | * @access public |
113 | * @author firstname and lastname of author, <author@example.org> |
114 | * @param array $paths |
115 | * @param array $fileTypes |
116 | * @return mixed |
117 | */ |
118 | public function __construct($paths, array $fileTypes) |
119 | { |
120 | parent::__construct($paths); |
121 | $this->setFileTypes($fileTypes); |
122 | } |
123 | |
124 | /** |
125 | * Gets an array of file extensions that will be processed. It acts as a |
126 | * |
127 | * @access public |
128 | * @author firstname and lastname of author, <author@example.org> |
129 | * @return array |
130 | */ |
131 | public function getFileTypes() |
132 | { |
133 | return $this->filesTypes; |
134 | } |
135 | |
136 | /** |
137 | * Sets an array that contains the extensions of files that have to be |
138 | * during the invokation of the SourceCodeExtractor::extract method. |
139 | * |
140 | * @access public |
141 | * @author firstname and lastname of author, <author@example.org> |
142 | * @param array $fileTypes |
143 | */ |
144 | public function setFileTypes(array $fileTypes) |
145 | { |
146 | $this->filesTypes = $fileTypes; |
147 | } |
148 | |
149 | /** |
150 | * Short description of method getTranslationsInFile |
151 | * |
152 | * @access private |
153 | * @author firstname and lastname of author, <author@example.org> |
154 | * @param string $filePath |
155 | */ |
156 | private function getTranslationsInFile($filePath) |
157 | { |
158 | |
159 | // File extension ? |
160 | $extOk = in_array(\Jig\Utils\FsUtils::getFileExtension($filePath), $this->getFileTypes()); |
161 | |
162 | if ($extOk) { |
163 | foreach ($this->getBannedFileType() as $bannedExt) { |
164 | $extOk &= substr_compare( |
165 | $filePath, |
166 | $bannedExt, |
167 | strlen($filePath) - strlen($bannedExt), |
168 | strlen($bannedExt) |
169 | ) !== 0; |
170 | } |
171 | } |
172 | |
173 | if ($extOk) { |
174 | // We read the file. |
175 | $lines = file($filePath); |
176 | foreach ($lines as $line) { |
177 | $strings = $this->getTranslationPhrases($line); |
178 | //preg_match_all("/__(\(| )+['\"](.*?)['\"](\))?/u", $line, $string); |
179 | |
180 | //lookup for __('to translate') or __ 'to translate' |
181 | // preg_match_all("/__(\(| )+(('(.*?)')|(\"(.*?)\"))(\))?/u", $line, $string); |
182 | |
183 | foreach ($strings as $s) { |
184 | $tu = new tao_helpers_translation_TranslationUnit(); |
185 | $tu->setSource(tao_helpers_translation_POUtils::sanitize($s)); |
186 | $tus = $this->getTranslationUnits(); |
187 | $found = false; |
188 | |
189 | // We must add the string as a new TranslationUnit only |
190 | // if a similiar source does not exist. |
191 | foreach ($tus as $t) { |
192 | if ($tu->getSource() == $t->getSource()) { |
193 | $found = true; |
194 | break; |
195 | } |
196 | } |
197 | |
198 | if (!$found) { |
199 | $tus[] = $tu; |
200 | $this->setTranslationUnits($tus); |
201 | } |
202 | } |
203 | } |
204 | } |
205 | } |
206 | |
207 | /** |
208 | * @return array |
209 | */ |
210 | public function getBannedFileType() |
211 | { |
212 | return $this->bannedFileType; |
213 | } |
214 | |
215 | /** |
216 | * @param array $bannedFileType |
217 | */ |
218 | public function setBannedFileType($bannedFileType) |
219 | { |
220 | $this->bannedFileType = $bannedFileType; |
221 | } |
222 | |
223 | /** |
224 | * @param $line |
225 | * |
226 | * @return array |
227 | */ |
228 | protected function getTranslationPhrases($line) |
229 | { |
230 | $strings = []; |
231 | $patternMatch1 = []; |
232 | $patternMatch2 = []; |
233 | |
234 | // for php and JS helper function |
235 | preg_match_all("/__\\(([\\\"'])(?:(?=(\\\\?))\\2.)*?\\1/u", $line, $patternMatch1); |
236 | preg_match_all("/\{\{__ ['\"](.*?)['\"]\}\}/u", $line, $patternMatch2); //used for parsing templates |
237 | |
238 | if (! empty($patternMatch1[0])) { |
239 | $strings = array_reduce( |
240 | $patternMatch1[0], |
241 | function ($m, $str) use ($patternMatch1) { |
242 | $found = preg_match( |
243 | "/([\"'])(?:(?=(\\\\?))\\2.)*?\\1/u", |
244 | $str, |
245 | $matches |
246 | ); //matches first passed argument only |
247 | $m[] = $found ? trim($matches[0], '"\'') : $patternMatch1[1]; |
248 | |
249 | return $m; |
250 | }, |
251 | [] |
252 | ); |
253 | } |
254 | if (! empty($patternMatch2[1])) { |
255 | $strings = array_merge($strings, $patternMatch2[1]); |
256 | |
257 | return $strings; |
258 | } |
259 | |
260 | return $strings; |
261 | } |
262 | } |