Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
cleanethalon.php
1<?php
3
8
15{
16 use Translate\Controller\Stepper;
17 use Translate\Controller\ProcessParams;
18
19 const SETTING_ID = 'TRANSLATE_CLEAN';
20
22 private $pathList;
23
25 private $seekOffset;
26
28 private $seekLangPath;
29
30
38 public function __construct($name, Main\Engine\Controller $controller, array $config = [])
39 {
40 $this->keepField(['pathList', 'seekOffset', 'seekLangPath']);
41
42 parent::__construct($name, $controller, $config);
43 }
44
45
53 public function run($pathList)
54 {
55 if ($this->isNewProcess)
56 {
57 $pathList = \preg_split("/[\r\n]+/", $pathList);
58 \array_walk($pathList, 'trim');
59 $pathList = \array_unique(\array_filter($pathList));
60
61 if (empty($pathList))
62 {
63 $this->addError(new Main\Error(Loc::getMessage('TR_CLEAN_EMPTY_PATH_LIST')));
64
65 return [
66 'STATUS' => Translate\Controller\STATUS_COMPLETED,
67 ];
68 }
69
70 foreach ($pathList as $testPath)
71 {
72 if (Translate\IO\Path::isPhpFile($testPath))
73 {
74 if (Translate\IO\Path::isLangDir($testPath))
75 {
76 $this->pathList[] = $testPath;
77 }
78 else
79 {
80 $this->addError(new Main\Error(Loc::getMessage('TR_CLEAN_FILE_NOT_LANG', ['#FILE#' => $testPath])));
81 }
82 }
83 else
84 {
85 if (Translate\IO\Path::isLangDir($testPath))
86 {
87 $this->pathList[] = $testPath;
88 }
89 else
90 {
91 // load lang folders
92 $pathFilter = [];
93 $pathFilter[] = [
94 'LOGIC' => 'OR',
95 '=PATH' => \rtrim($testPath, '/'),
96 '=%PATH' => \rtrim($testPath, '/'). '/%'
97 ];
98 $pathLangRes = Index\Internals\PathLangTable::getList([
99 'filter' => $pathFilter,
100 'order' => ['ID' => 'ASC'],
101 'select' => ['PATH'],
102 ]);
103 while ($pathLang = $pathLangRes->fetch())
104 {
105 $this->pathList[] = $pathLang['PATH'];
106 }
107 }
108 }
109 }
110
111 $this->totalItems = \count($this->pathList);
112 $this->processedItems = 0;
113
114 if ($this->totalItems == 0)
115 {
116 return [
117 'STATUS' => Translate\Controller\STATUS_COMPLETED,
118 'PROCESSED_ITEMS' => 0,
119 'TOTAL_ITEMS' => 0,
120 ];
121 }
122
123 $this->saveProgressParameters();
124 $this->isNewProcess = false;
125 }
126
127 return $this->performStep('runClearing');
128 }
129
135 private function runClearing(): array
136 {
137 $processedItemCount = 0;
138 for ($pos = ((int)$this->seekOffset > 0 ? (int)$this->seekOffset : 0), $total = \count($this->pathList); $pos < $total; $pos ++)
139 {
140 $testPath = $this->pathList[$pos];
141
142 // file
143 if (Translate\IO\Path::isPhpFile($testPath))
144 {
145 $this->cleanLangFile($testPath);
146 }
147
148 // folder
149 else
150 {
151 if (\mb_substr($testPath, -5) === '/lang')
152 {
153 $testPath .= '/#LANG_ID#';
154 }
155 else
156 {
157 $testPath = Translate\IO\Path::replaceLangId($testPath, '#LANG_ID#');
158 }
159
160 foreach ($this->lookThroughLangFolder($testPath) as $filePaths)
161 {
162 foreach ($filePaths as $langFilePath => $fullPaths)
163 {
164 if (!empty($this->seekLangPath))
165 {
166 if ($this->seekLangPath == $langFilePath)
167 {
168 $this->seekLangPath = null;
169 }
170
171 continue;
172 }
173
174 $this->cleanLangFile($langFilePath);
175
176 if ($this->instanceTimer()->hasTimeLimitReached())
177 {
178 $this->seekLangPath = $langFilePath;
179 break 3;
180 }
181 }
182
183 $this->seekLangPath = null;
184 }
185 }
186
187 $processedItemCount ++;
188
189 if (isset($this->pathList[$pos + 1]))
190 {
191 $this->seekOffset = $pos + 1;//next
192 }
193 else
194 {
195 $this->seekOffset = null;
196 $this->declareAccomplishment();
198 }
199
200 if ($this->instanceTimer()->hasTimeLimitReached())
201 {
202 break;
203 }
204 }
205
206 $this->processedItems += $processedItemCount;
207
208 if ($this->instanceTimer()->hasTimeLimitReached() !== true)
209 {
210 $this->declareAccomplishment();
212 }
213
214 return [
215 'PROCESSED_ITEMS' => $this->processedItems,
216 'TOTAL_ITEMS' => $this->totalItems,
217 ];
218 }
219
220
228 private function cleanLangFile($relLangPath): void
229 {
230 $currentLang = Loc::getCurrentLang();
231 $langPath = Translate\IO\Path::replaceLangId($relLangPath, $currentLang);
232 $langFullPath = Translate\IO\Path::tidy(self::$documentRoot. '/'. $langPath);
233 $langFullPath = Main\Localization\Translation::convertLangPath($langFullPath, $currentLang);
234
235 try
236 {
237 $ethalonFile = Translate\File::instantiateByPath($langFullPath);
238 }
239 catch (Main\ArgumentException $ex)
240 {
241 return;
242 }
243 if ($ethalonFile instanceof Translate\File)
244 {
245 $ethalonFile
246 ->setLangId($currentLang)
247 ->setOperatingEncoding(Main\Localization\Translation::getSourceEncoding($currentLang));
248
249 $isEthalonExists = false;
250 if ($ethalonFile->isExists())
251 {
252 $isEthalonExists = $ethalonFile->loadTokens() || $ethalonFile->load();
253 }
254 if (!$isEthalonExists)
255 {
256 $this->deletePhraseIndex($ethalonFile);
257 }
258
259 foreach (self::$enabledLanguagesList as $langId)
260 {
261 if ($langId == $currentLang)
262 {
263 continue;
264 }
265
266 $langPath = Translate\IO\Path::replaceLangId($relLangPath, $langId);
267 $langFullPath = Translate\IO\Path::tidy(self::$documentRoot.'/'.$langPath);
268 $langFullPath = Main\Localization\Translation::convertLangPath($langFullPath, $langId);
269
270 try
271 {
272 $langFile = Translate\File::instantiateByPath($langFullPath);
273 }
274 catch (Main\ArgumentException $ex)
275 {
276 continue;
277 }
278 if ($langFile instanceof Translate\File)
279 {
280 $langFile
281 ->setLangId($langId)
282 ->setOperatingEncoding(Main\Localization\Translation::getSourceEncoding($langId));
283
284 if ($langFile->isExists())
285 {
286 if ($isEthalonExists && ($langFile->loadTokens() || $langFile->load()))
287 {
288 $affected = false;
289 foreach ($langFile as $code => $phrase)
290 {
291 if (!isset($ethalonFile[$code]))
292 {
293 unset($langFile[$code]);
294 $affected = true;
295 }
296 }
297 if ($affected)
298 {
299 $this->updateLangFile($langFile);
300 $this->updatePhraseIndex($langFile);
301 }
302 }
303 else
304 {
305 $this->deletePhraseIndex($langFile);
306 $this->deleteLangFile($langFile);
307 }
308 }
309 else
310 {
311 $langFile->deletePhraseIndex();
312 }
313 }
314 }
315 }
316 }
317
318
325 {
326 return self::SETTING_ID;
327 }
328}
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
__construct($name, Main\Engine\Controller $controller, array $config=[])
updatePhraseIndex(Translate\File $langFile)
updateLangFile(Translate\File $langFile)
Definition operation.php:42
deleteLangFile(Translate\File $langFile)
deletePhraseIndex(Translate\File $langFile)
performStep($action, array $params=[])
Definition stepper.php:75
declareAccomplishment(bool $flag=true)
Definition stepper.php:136
addError(Main\Error $error)
Definition error.php:22