18 use Translate\Controller\Stepper;
19 use Translate\Controller\ProcessParams;
22 private $seekPathLangId;
28 private $convertEncoding;
34 private $assemblyDate;
40 private $tmpFolderPath;
43 private $totalFileCount;
67 'seekPathLangId',
'convertEncoding',
'encoding',
'assemblyDate',
68 'languageId',
'packFile',
'tmpFolderPath',
'totalFileCount'
73 self::$useTranslationRepository = Main\Localization\Translation::useTranslationRepository();
74 self::$enabledLanguages = Translate\Config::getEnabledLanguages();
75 self::$allowedEncodings = Translate\Config::getAllowedEncodings();
78 if (self::$useTranslationRepository)
80 self::$translationRepositoryLanguages = Translate\Config::getTranslationRepositoryLanguages();
91 public function run($path =
'', $runBefore =
false)
103 $path =
'/'. trim($path,
'/.\\');
105 if ($this->isNewProcess)
108 $this->totalItems = 0;
109 $this->processedItems = 0;
110 $this->totalFileCount = 0;
113 $languageId = $this->controller->getRequest()->get(
'languageId');
114 if (empty($languageId))
118 if (!in_array($languageId, self::$enabledLanguages))
124 $this->languageId = $languageId;
128 $this->convertEncoding = ($this->controller->getRequest()->get(
'convertEncoding') ===
'Y');
131 $encoding = $this->controller->getRequest()->get(
'encoding');
132 if ($encoding !==
null && in_array($encoding, self::$allowedEncodings))
134 $this->encoding = $encoding;
136 elseif ($this->convertEncoding)
141 if ($this->convertEncoding)
143 if (self::$useTranslationRepository)
145 $encodingIn = Main\Localization\Translation::getSourceEncoding($this->languageId);
146 $encodingOut = $this->encoding;
147 if ($encodingIn ===
'utf-8' && $encodingOut !==
'utf-8')
154 $encodingIn =
'utf-8';
155 $encodingOut = $this->encoding;
156 if (Translate\Config::getCultureEncoding($this->languageId) !==
'utf-8')
163 $encodingIn = Translate\Config::getCultureEncoding($this->languageId);
166 $encodingIn = Main\Localization\Translation::getCurrentEncoding();
168 $this->encoding = $encodingOut =
'utf-8';
171 $this->convertEncoding = (\mb_strtolower($encodingIn) !== \mb_strtolower($encodingOut));
175 $assemblyDate = $this->controller->getRequest()->get(
'assemblyDate');
176 if ($assemblyDate !==
null && \preg_replace(
"/[\D]+/",
"", $assemblyDate) && \mb_strlen($assemblyDate) == 8)
178 $this->assemblyDate = $assemblyDate;
187 $this->packFile = ($this->controller->getRequest()->get(
'packFile') ===
'Y');
192 $exportFolder = Translate\Config::getExportFolder();
193 if (!empty($exportFolder))
196 if ($tempDir->isExists())
207 $tempDir = Translate\IO\Directory::generateTemporalDirectory(
'translate');
208 $tempDir = $tempDir->createSubdirectory($this->languageId);
210 $this->tmpFolderPath = $tempDir->getPhysicalPath().
'/';
211 if (!$tempDir->isExists())
221 $fileDateMarkFullPath = $this->tmpFolderPath.
222 \str_replace(
'#LANG_ID#', $this->languageId, Translate\SUPD_LANG_DATE_MARK);
224 Translate\IO\Path::checkCreatePath(\dirname($fileDateMarkFullPath));
226 $fileDateMark =
new Main\IO\File($fileDateMarkFullPath);
227 if ($fileDateMark->putContents($assemblyDate) ===
false)
235 $this->totalItems = (int)Index\Internals\PathLangTable::getCount([
'=%PATH' => $path.
'%']);
236 $this->processedItems = 0;
241 'STATUS' => ($this->totalItems > 0 ? Translate\Controller\STATUS_PROGRESS : Translate\Controller\STATUS_COMPLETED),
242 'PROCESSED_ITEMS' => 0,
243 'TOTAL_ITEMS' => $this->totalItems,
249 $this->languageId = $progressParams[
'languageId'];
250 $this->convertEncoding = $progressParams[
'convertEncoding'];
251 $this->encoding = $progressParams[
'encoding'];
252 $this->assemblyDate = $progressParams[
'assemblyDate'];
253 $this->packFile = $progressParams[
'packFile'];
254 $this->tmpFolderPath = $progressParams[
'tmpFolderPath'];
256 if (isset($progressParams[
'totalItems']) && (
int)$progressParams[
'totalItems'] > 0)
258 $this->totalItems = (int)$progressParams[
'totalItems'];
259 $this->processedItems = (int)$progressParams[
'processedItems'];
260 $this->totalFileCount = (int)$progressParams[
'totalFileCount'];
263 if (isset($progressParams[
'seekPathLangId']))
265 $this->seekPathLangId = $progressParams[
'seekPathLangId'];
268 return $this->
performStep(
'runCollecting', [
'path' => $path]);
278 private function runCollecting(array $params = [])
280 if ($this->convertEncoding)
282 $sourceEncoding = Main\Localization\Translation::getSourceEncoding($this->languageId);
285 if (isset($params[
'path']))
287 $path = $params[
'path'];
295 '=%PATH' => $path.
'%'
297 if (!empty($this->seekPathLangId))
299 $pathFilter[
'>ID'] = $this->seekPathLangId;
302 $cachePathLangRes = Index\Internals\PathLangTable::getList([
303 'filter' => $pathFilter,
304 'order' => [
'ID' =>
'ASC'],
305 'select' => [
'ID',
'PATH'],
307 $processedItemCount = 0;
308 while ($pathLang = $cachePathLangRes->fetch())
310 foreach ($this->lookThroughLangFolder($pathLang[
'PATH'].
'/'.$this->languageId) as $filePaths)
312 foreach ($filePaths as $langFilePath => $fullPath)
314 $targetFolder =
new Main\IO\Directory($this->tmpFolderPath. \dirname($langFilePath));
315 if (!$targetFolder->isExists())
317 $targetFolder->create();
320 $source =
new Main\IO\File($fullPath);
321 $target =
new Main\IO\File($targetFolder->getPhysicalPath().
'/'. \basename($langFilePath));
325 $content = $source->getContents();
326 $content = \str_replace([
"\r\n",
"\r"], [
"\n",
"\n"], $content);
328 if ($this->convertEncoding)
330 $content = Main\Text\Encoding::convertEncoding($content, $sourceEncoding, $this->encoding);
333 $target->putContents($content);
334 $this->totalFileCount ++;
336 catch (Main\
IO\IoException $exception)
342 if (\connection_status() !== \CONNECTION_NORMAL)
344 throw new Main\SystemException(
'Process has been broken course user aborted connection.');
349 $processedItemCount ++;
353 $this->seekPathLangId = (int)$pathLang[
'ID'];
358 $this->processedItems += $processedItemCount;
370 $this->processToken =
null;
371 $this->seekPathLangId =
null;
374 $messagePlaceholders = [
375 '#TOTAL_FILES#' => $this->totalFileCount,
376 '#LANG#' => \mb_strtoupper($this->languageId),
379 $result[
'SUMMARY'] =
Loc::getMessage(
'TR_LANGUAGE_COLLECTED_FOLDER', $messagePlaceholders);
393 private function lookThroughLangFolder($langFolderRelPath)
398 $langFolderFullPath = Translate\IO\Path::tidy(self::$documentRoot.
'/'.$langFolderRelPath);
402 if (self::$useTranslationRepository && \in_array($this->languageId, self::$translationRepositoryLanguages))
404 $langFolderFullPath = Main\Localization\Translation::convertLangPath($langFolderFullPath, $this->languageId);
407 $childrenList = Translate\IO\FileSystemHelper::getFileList($langFolderFullPath);
408 if (!empty($childrenList))
410 foreach ($childrenList as $fullPath)
412 $name = \basename($fullPath);
413 if (\in_array(
$name, Translate\IGNORE_FS_NAMES))
418 if (Translate\
IO\Path::isPhpFile($fullPath,
true))
420 $files[$storeFolderRelPath.
'/'.
$name] = $fullPath;
426 $childrenList = Translate\IO\FileSystemHelper::getFolderList($langFolderFullPath);
427 if (!empty($childrenList))
429 $ignoreDev = \implode(
'|', Translate\IGNORE_MODULE_NAMES);
430 foreach ($childrenList as $fullPath)
432 $name = \basename($fullPath);
433 if (\in_array(
$name, Translate\IGNORE_FS_NAMES))
438 $relPath = $langFolderRelPath.
'/'.
$name;
440 if (!\is_dir($fullPath))
445 if (\in_array($relPath, Translate\IGNORE_BX_NAMES))
451 if (\preg_match(
"#^bitrix/modules/[^/]+/({$ignoreDev})#", \trim($relPath,
'/')))
456 if (\in_array(
$name, Translate\IGNORE_LANG_NAMES))
461 $folders[$langFolderRelPath.
'/'.
$name] = $langFolderRelPath.
'/'.
$name;
465 if (\count($files) > 0)
470 if (\count($folders) > 0)
472 foreach ($folders as $subFolderPath)
474 foreach ($this->lookThroughLangFolder($subFolderPath) as $subFiles)
491 return $controller::SETTING_ID;
static getMessage($code, $replace=null, $language=null)