17 use Translate\Controller\Stepper;
18 use Translate\Controller\ProcessParams;
24 private $convertEncoding;
36 private $tmpFolderPath;
39 private $targetFolderPath;
42 private $sourceFolderPath;
45 private $totalFileCount;
60 private $seekAncestors;
73 'languageId',
'convertEncoding',
'encoding',
'encodingIn',
'encodingOut',
74 'tmpFolderPath',
'totalFileCount',
'sourceFolderPath',
'targetFolderPath',
'seekPath'
80 self::$useTranslationRepository = Main\Localization\Translation::useTranslationRepository();
81 self::$enabledLanguages = Translate\Config::getEnabledLanguages();
82 self::$allowedEncodings = Translate\Config::getAllowedEncodings();
84 if (self::$useTranslationRepository)
86 self::$translationRepositoryRoot = Main\Localization\Translation::getTranslationRepositoryPath();
100 $this->languageId = $progressParams[
'languageId'];
101 $this->tmpFolderPath = $progressParams[
'tmpFolderPath'];
102 $this->totalFileCount = $progressParams[
'totalFileCount'];
105 if ($this->isNewProcess)
107 $this->totalItems = $this->totalFileCount;
108 $this->processedItems = 0;
111 $languageId = $this->controller->getRequest()->get(
'languageId');
112 if (empty($languageId))
116 if (!in_array($languageId, self::$enabledLanguages))
122 $this->languageId = $languageId;
126 $this->convertEncoding = ($this->controller->getRequest()->get(
'localizeEncoding') ===
'Y');
129 $encoding = $this->controller->getRequest()->get(
'encoding');
130 if ($encoding !==
null && \in_array($encoding, self::$allowedEncodings))
132 $this->encoding = $encoding;
134 elseif ($this->convertEncoding)
139 if ($this->convertEncoding)
141 if (self::$useTranslationRepository)
143 $encodingIn = $this->encoding;
144 $encodingOut = Main\Localization\Translation::getSourceEncoding($this->languageId);
148 $encodingIn = $this->encoding;
149 $encodingOut =
'utf-8';
153 $encodingIn =
'utf-8';
154 $encodingOut = Translate\Config::getCultureEncoding($this->languageId);
157 $encodingOut = Main\Localization\Translation::getCurrentEncoding();
160 $this->convertEncoding = (\mb_strtolower($encodingIn) !== \mb_strtolower($encodingOut));
161 $this->encodingIn = $encodingIn;
162 $this->encodingOut = $encodingOut;
165 $this->sourceFolderPath = Translate\IO\Path::tidy($this->tmpFolderPath .
'/'. $this->languageId.
'/');
168 if (!$sourceDirectory->isExists())
171 new Error(
Loc::getMessage(
'TR_ERROR_CREATE_TARGET_FOLDER', [
'#PATH#' => $this->sourceFolderPath]))
176 self::$useTranslationRepository &&
180 $this->targetFolderPath = Translate\IO\Path::tidy(self::$translationRepositoryRoot.
'/'. $this->languageId.
'/');
182 if (!$targetFolder->isExists())
184 $targetFolder->create();
189 $this->targetFolderPath = Main\Application::getDocumentRoot().
'/bitrix/modules/';
195 'STATUS' => ($this->totalItems > 0 ? Translate\Controller\STATUS_PROGRESS : Translate\Controller\STATUS_COMPLETED),
196 'PROCESSED_ITEMS' => 0,
197 'TOTAL_ITEMS' => $this->totalItems,
201 $this->targetFolderPath = $progressParams[
'targetFolderPath'];
202 $this->convertEncoding = $progressParams[
'convertEncoding'];
203 $this->encodingIn = $progressParams[
'encodingIn'];
204 $this->encodingOut = $progressParams[
'encodingOut'];
205 $this->seekPath = $progressParams[
'seekPath'];
216 private function runApplying(): array
218 $processedItemCount = 0;
220 if (!empty($this->seekPath))
222 $this->seekAncestors = [];
223 $arr = \explode(
'/', \str_replace($this->sourceFolderPath,
'', $this->seekPath));
226 foreach ($arr as $part)
229 $this->seekAncestors[] = $this->sourceFolderPath. \implode(
'/', $parts);
233 foreach ($this->lookThroughTmpFolder($this->sourceFolderPath) as $filePaths)
235 foreach ($filePaths as $langFilePath => $fullPath)
237 $targetFolder =
new Main\IO\Directory($this->targetFolderPath. \dirname($langFilePath));
238 if (!$targetFolder->isExists())
240 $targetFolder->create();
244 $target =
new Main\IO\File($targetFolder->getPhysicalPath().
'/'. \basename($langFilePath));
245 if ($target->isExists())
247 $target->markWritable();
252 if ($this->convertEncoding)
254 $content = $source->getContents();
255 $content = \str_replace([
"\r\n",
"\r"], [
"\n",
"\n"], $content);
257 $content = Main\Text\Encoding::convertEncoding($content, $this->encodingIn, $this->encodingOut);
258 $target->putContents($content);
262 if (\function_exists(
'error_clear_last'))
266 if (\copy($source->getPhysicalPath(), $target->getPhysicalPath()) !==
true)
268 $error = \error_get_last();
269 $this->
addError(
new Main\
Error($error[
'message'], $error[
'type']));
274 $processedItemCount ++;
276 catch (Main\
IO\IoException $exception)
282 if (\connection_status() !== \CONNECTION_NORMAL)
284 throw new Main\SystemException(
'Process has been broken course user aborted connection.');
290 $this->seekPath = $fullPath;
295 $this->processedItems += $processedItemCount;
306 $updatePublic = $this->controller->getRequest()->get(
'updatePublic');
307 if ($updatePublic ===
'Y')
310 $this->processToken =
null;
331 private function lookThroughTmpFolder($tmpFolderFullPath): iterable
336 $tmpFolderFullPath = Translate\IO\Path::tidy(\rtrim($tmpFolderFullPath,
'/'));
337 $langFolderRelPath = \str_replace($this->sourceFolderPath,
'', $tmpFolderFullPath);
339 $childrenList = Translate\IO\FileSystemHelper::getFileList($tmpFolderFullPath);
340 if (!empty($childrenList))
342 foreach ($childrenList as $fullPath)
344 if (!empty($this->seekPath))
346 if ($this->seekPath != $fullPath)
351 $this->seekPath =
null;
352 $this->seekAncestors =
null;
355 $name = \basename($fullPath);
356 if (\in_array(
$name, Translate\IGNORE_FS_NAMES))
361 if (Translate\
IO\Path::isPhpFile($fullPath,
true))
363 $files[$langFolderRelPath.
'/'.
$name] = $fullPath;
369 $childrenList = Translate\IO\FileSystemHelper::getFolderList($tmpFolderFullPath);
370 if (!empty($childrenList))
372 foreach ($childrenList as $fullPath)
374 $name = \basename($fullPath);
375 if (\in_array(
$name, Translate\IGNORE_FS_NAMES))
380 if (!empty($this->seekPath))
382 if (\in_array($fullPath, $this->seekAncestors))
384 foreach ($this->lookThroughTmpFolder($fullPath) as $subFiles)
392 if (!\is_dir($fullPath))
397 $relPath = $langFolderRelPath.
'/'.
$name;
399 if (\in_array($relPath, Translate\IGNORE_BX_NAMES))
404 $folders[$relPath] = $fullPath;
408 if (\count($files) > 0)
413 if (\count($folders) > 0)
415 foreach ($folders as $subFolderPath)
417 foreach ($this->lookThroughTmpFolder($subFolderPath) as $subFiles)
434 return $controller::SETTING_ID;
static getMessage($code, $replace=null, $language=null)
static isDefaultTranslationLang($lang)