Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
applypublic.php
1<?php
3
8
9
16{
17 use Translate\Controller\Stepper;
18 use Translate\Controller\ProcessParams;
19
21 private $languageId;
22
24 private $tmpFolderPath;
25
27 private $sourceFolderPath;
28
30 private $totalFileCount;
31
33 public static $documentRoot;
37 public static $enabledLanguages;
40
42 private $seekPath;
44 private $seekModule;
46 private $seekType;
48 private $seekAncestors;
49
50
52 'component' => '#BX_ROOT#/components/bitrix',
53 'activities' => '#BX_ROOT#/activities/bitrix',
54 'gadgets' => '#BX_ROOT#/gadgets/bitrix',
55 'wizards' => '#BX_ROOT#/wizards/bitrix',
56 'blocks' => '#BX_ROOT#/blocks/bitrix',
57 'template' => '#BX_ROOT#/templates',
58 'mobileapp' => '#BX_ROOT#/mobileapp',
59 'js' => '#BX_ROOT#/js',
60 ];
62 'component' => '/install/components/bitrix',
63 'activities' => '/install/activities/bitrix',
64 'gadgets' => '/install/gadgets/bitrix',
65 'wizards' => '/install/wizards/bitrix',
66 'blocks' => '/install/blocks/bitrix',
67 'template' => '/install/templates',
68 'mobileapp' => '/install/mobileapp',
69 'js' => '/install/js',
70 ];
71
72
73
81 public function __construct($name, Main\Engine\Controller $controller, array $config = [])
82 {
83 $this->keepField([
84 'languageId', 'tmpFolderPath', 'totalFileCount', 'sourceFolderPath',
85 'seekPath', 'seekModule', 'seekType'
86 ]);
87
88 parent::__construct($name, $controller, $config);
89
90 self::$useTranslationRepository = Main\Localization\Translation::useTranslationRepository();
91 self::$enabledLanguages = Translate\Config::getEnabledLanguages();
92 if (self::$useTranslationRepository)
93 {
94 self::$translationRepositoryRoot = Main\Localization\Translation::getTranslationRepositoryPath();
95 }
96 self::$documentRoot = rtrim(Translate\IO\Path::tidy(Main\Application::getDocumentRoot()), '/');
97 }
98
104 public function run()
105 {
106 // continue previous process
107 $progressParams = $this->getProgressParameters();
108
109 $updatePublic = $this->controller->getRequest()->get('updatePublic');
110 if ($updatePublic === null || $updatePublic !== 'Y')
111 {
112 // finish it
113 return [
114 'STATUS' => Translate\Controller\STATUS_COMPLETED,
115 'PROCESSED_ITEMS' => $progressParams['totalFileCount'],
116 'TOTAL_ITEMS' => $progressParams['totalItems'],
117 ];
118 }
119
120
121 $this->languageId = $progressParams['languageId'];
122 $this->tmpFolderPath = $progressParams['tmpFolderPath'];
123 $this->totalFileCount = $progressParams['totalFileCount'];
124
125
126 if ($this->isNewProcess)
127 {
128 $this->totalItems = $this->totalFileCount;
129 $this->processedItems = 0;
130
131 // language
132 $languageId = $this->controller->getRequest()->get('languageId');
133 if (empty($languageId))
134 {
135 $this->addError(new Main\Error(Loc::getMessage('TR_ERROR_SELECT_LANGUAGE')));
136 }
137 if (!in_array($languageId, self::$enabledLanguages))
138 {
139 $this->addError(new Main\Error(Loc::getMessage('TR_ERROR_LANGUAGE_ID')));
140 }
141 else
142 {
143 $this->languageId = $languageId;
144 }
145
146 $this->sourceFolderPath = Translate\IO\Path::tidy($this->tmpFolderPath .'/'. $this->languageId. '/');
147
148 $sourceDirectory = new Translate\IO\Directory($this->sourceFolderPath);
149 if (!$sourceDirectory->isExists())
150 {
151 $this->addError(
152 new Error(Loc::getMessage('TR_ERROR_CREATE_TARGET_FOLDER', ['#PATH#' => $this->sourceFolderPath]))
153 );
154 }
155
156 $this->saveProgressParameters();
157
158 return [
159 'STATUS' => ($this->totalItems > 0 ? Translate\Controller\STATUS_PROGRESS : Translate\Controller\STATUS_COMPLETED),
160 'PROCESSED_ITEMS' => 0,
161 'TOTAL_ITEMS' => $this->totalItems,
162 ];
163 }
164
165 $this->targetFolderPath = $progressParams['targetFolderPath'];
166 $this->seekPath = $progressParams['seekPath'];
167 $this->seekModule = $progressParams['seekModule'];
168 $this->seekType = $progressParams['seekType'];
169
170 return $this->performStep('runApplying');
171 }
172
173
179 private function runApplying(): array
180 {
181 $processedItemCount = 0;
182
183 if (!empty($this->seekPath))
184 {
185 $this->seekAncestors = [];
186 $arr = \explode('/', \str_replace($this->sourceFolderPath, '', $this->seekPath));
187 \array_pop($arr);//last file
188 $parts = [];
189 foreach ($arr as $part)
190 {
191 $parts[] = $part;
192 $this->seekAncestors[] = $this->sourceFolderPath. \implode('/', $parts);
193 }
194 }
195
196 $sourceDirectory = new Translate\IO\Directory($this->sourceFolderPath);
197 foreach ($sourceDirectory->getChildren() as $entry)
198 {
199 $moduleName = $entry->getName();
200 if (\in_array($moduleName, Translate\IGNORE_FS_NAMES) || !$entry->isDirectory())
201 {
202 continue;
203 }
204 if (!empty($this->seekModule))
205 {
206 if ($this->seekModule !== $moduleName)
207 {
208 continue;
209 }
210 $this->seekModule = null;
211 }
212
213 foreach (self::SOURCE_FOLDERS as $type => $typeSourcePath)
214 {
215 if (!empty($this->seekType))
216 {
217 if ($this->seekType !== $type)
218 {
219 continue;
220 }
221 $this->seekType = null;
222 }
223
224 $sourceFolder = new Main\IO\Directory($entry->getPhysicalPath(). $typeSourcePath);
225 if ($sourceFolder->isExists())
226 {
227 $targetFolderPath = \str_replace('#BX_ROOT#', self::$documentRoot. ''. \BX_ROOT, self::TARGET_FOLDERS[$type]);
228
229 foreach ($this->lookThroughTmpFolder($sourceFolder->getPhysicalPath()) as $filePaths)
230 {
231 foreach ($filePaths as $langFilePath => $sourceFullPath)
232 {
233 $targetPath =
234 $targetFolderPath .'/'.
235 \str_replace($moduleName. $typeSourcePath .'/', '', \dirname($langFilePath));
236
237 $targetFolder = new Main\IO\Directory($targetPath);
238 if (!$targetFolder->isExists())
239 {
240 $targetFolder->create();
241 }
242
243 $moduleSourcePath = self::$documentRoot. ''. \BX_ROOT. '/modules/'. $langFilePath;
244 $source = new Main\IO\File($moduleSourcePath);
245 if (!$source->isExists())
246 {
247 continue;
248 }
249
250 $target = new Main\IO\File($targetFolder->getPhysicalPath(). '/'. \basename($langFilePath));
251 if ($target->isExists())
252 {
253 $target->markWritable();
254 }
255
256 try
257 {
258 if (\function_exists('error_clear_last'))
259 {
260 \error_clear_last();
261 }
262 if (\copy($source->getPhysicalPath(), $target->getPhysicalPath()) !== true)
263 {
264 $error = \error_get_last();
265 $this->addError(new Main\Error($error['message'], $error['type']));
266 continue;
267 }
268
269 $processedItemCount ++;
270 }
271 catch (Main\IO\IoException $exception)
272 {
273 $this->addError(new Main\Error($exception->getMessage()));
274 }
275 }
276 }
277
278 if ($this->instanceTimer()->hasTimeLimitReached())
279 {
280 $this->seekPath = $sourceFullPath;
281 $this->seekModule = $moduleName;
282 $this->seekType = $type;
283 break 2;
284 }
285 }
286
287 // check user abortion
288 if (\connection_status() !== \CONNECTION_NORMAL)
289 {
290 throw new Main\SystemException('Process has been broken course user aborted connection.');
291 }
292 }
293 }
294
295 $this->processedItems += $processedItemCount;
296
297 $result = [
298 'PROCESSED_ITEMS' => $this->processedItems,
299 'TOTAL_ITEMS' => $this->totalItems,
300 ];
301
302 if ($this->instanceTimer()->hasTimeLimitReached() !== true)
303 {
304 $this->declareAccomplishment();
306
307 $result['SUMMARY'] = Loc::getMessage('TR_LANGUAGE_DOWNLOADED');
308 }
309
310 return $result;
311 }
312
313
321 private function lookThroughTmpFolder($tmpFolderFullPath): iterable
322 {
323 $files = [];
324 $folders = [];
325
326 $tmpFolderFullPath = Translate\IO\Path::tidy(\rtrim($tmpFolderFullPath, '/'));
327 $langFolderRelPath = \str_replace($this->sourceFolderPath, '', $tmpFolderFullPath);
328
329 $childrenList = Translate\IO\FileSystemHelper::getFileList($tmpFolderFullPath);
330 if (!empty($childrenList))
331 {
332 foreach ($childrenList as $fullPath)
333 {
334 if (!empty($this->seekPath))
335 {
336 if ($this->seekPath != $fullPath)
337 {
338 continue;
339 }
340
341 $this->seekPath = null;
342 $this->seekAncestors = null;
343 }
344
345 $name = \basename($fullPath);
346 if (\in_array($name, Translate\IGNORE_FS_NAMES))
347 {
348 continue;
349 }
350
351 if (Translate\IO\Path::isPhpFile($fullPath, true))
352 {
353 $files[$langFolderRelPath.'/'.$name] = $fullPath;
354 }
355 }
356 }
357
358 // dir only
359 $childrenList = Translate\IO\FileSystemHelper::getFolderList($tmpFolderFullPath);
360 if (!empty($childrenList))
361 {
362 foreach ($childrenList as $fullPath)
363 {
364 $name = \basename($fullPath);
365 if (\in_array($name, Translate\IGNORE_FS_NAMES))
366 {
367 continue;
368 }
369
370 if (!empty($this->seekPath))
371 {
372 if (\in_array($fullPath, $this->seekAncestors))
373 {
374 foreach ($this->lookThroughTmpFolder($fullPath) as $subFiles)// go deeper
375 {
376 yield $subFiles;
377 }
378 }
379 continue;
380 }
381
382 if (!\is_dir($fullPath))
383 {
384 continue;
385 }
386
387 $relPath = $langFolderRelPath.'/'.$name;
388
389 if (\in_array($relPath, Translate\IGNORE_BX_NAMES))
390 {
391 continue;
392 }
393
394 $folders[$relPath] = $fullPath;
395 }
396 }
397
398 if (\count($files) > 0)
399 {
400 yield $files;
401 }
402
403 if (\count($folders) > 0)
404 {
405 foreach ($folders as $subFolderPath)
406 {
407 foreach ($this->lookThroughTmpFolder($subFolderPath) as $subFiles)// go deeper
408 {
409 yield $subFiles;
410 }
411 }
412 }
413 }
414
421 {
422 $controller = $this->getController();
423 return $controller::SETTING_ID;
424 }
425}
addError(Error $error)
Definition action.php:200
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
__construct($name, Main\Engine\Controller $controller, array $config=[])
performStep($action, array $params=[])
Definition stepper.php:75
declareAccomplishment(bool $flag=true)
Definition stepper.php:136