15 use Translate\Controller\Stepper;
16 use Translate\Controller\ProcessParams;
19 private array $pathList = [];
22 private array $codeList = [];
24 private int $seekOffset = 0;
26 private int $seekPathLangId = 0;
28 private string $seekLangFilePath =
'';
29 private string $seekPhraseCode =
'';
41 $this->
keepField([
'pathList',
'codeList',
'seekOffset',
'seekPathLangId',
'seekPhraseCode',
'seekLangFilePath']);
57 public function run(
string $path =
'',
bool $runBefore =
false): array
64 if ($this->isNewProcess)
66 $pathList = $this->controller->getRequest()->get(
'pathList');
68 $pathList = \preg_split(
"/[\r\n]+/", $pathList);
69 \array_walk($pathList,
'trim');
70 $pathList = \array_unique(\array_filter($pathList));
76 'STATUS' => Translate\Controller\STATUS_COMPLETED,
80 foreach ($pathList as $testPath)
82 if (Translate\
IO\Path::isPhpFile($testPath))
84 if (Translate\
IO\Path::isLangDir($testPath))
86 $this->pathList[] = $testPath;
91 $this->pathList[] = $testPath;
96 $codeList = $this->controller->getRequest()->get(
'codeList');
97 if (!empty($codeList))
99 $codeList = \preg_split(
"/[\r\n]+/", $codeList);
100 \array_walk($codeList,
'trim');
101 $this->codeList = \array_unique(\array_filter($codeList));
104 $this->totalItems = \count($this->pathList);
105 $this->processedItems = 0;
107 if ($this->totalItems > 0)
110 $csvFile = $this->createExportTempFile($this->exportFileName);
111 $this->exportFilePath = $csvFile->getPhysicalPath();
112 $this->exportFileSize = $csvFile->getSize();
114 if ($this->appendSamples)
117 $sampleFile = $this->createExportTempFile($this->samplesFileName);
118 $this->samplesFilePath = $sampleFile->getPhysicalPath();
119 $this->samplesFileSize = $sampleFile->getSize();
125 'STATUS' => ($this->totalItems > 0 ? Translate\Controller\STATUS_PROGRESS : Translate\Controller\STATUS_COMPLETED),
126 'PROCESSED_ITEMS' => 0,
127 'TOTAL_ITEMS' => $this->totalItems,
140 private function runExporting(): array
146 if ($this->appendSamples)
153 $processedItemCount = 0;
155 $filterCodeList = $this->codeList ?: [];
157 foreach ($filterCodeList as $pathCode)
159 [$path, $code] = \explode(
'::', $pathCode);
162 $langFilePath = Translate\IO\Path::replaceLangId($path,
'#LANG_ID#');
163 if (!isset($fileCodeList[$langFilePath]))
165 $fileCodeList[$langFilePath] = [];
167 $fileCodeList[$langFilePath][] = $code;
173 for ($pos = ($this->seekOffset > 0 ? $this->seekOffset : 0), $total = \count($this->pathList); $pos < $total; $pos ++)
175 $exportingPath = $this->pathList[$pos];
178 if (Translate\
IO\Path::isPhpFile($exportingPath))
180 $langFilePath = Translate\IO\Path::replaceLangId($exportingPath,
'#LANG_ID#');
181 if (!empty($this->seekLangFilePath))
183 if ($langFilePath == $this->seekLangFilePath)
185 $this->seekLangFilePath =
'';
194 foreach ($this->languages as $langId)
196 $langRelPath = Translate\IO\Path::replaceLangId($exportingPath, $langId);
197 $langFullPath = Translate\IO\Path::tidy(self::$documentRoot.
'/'.$langRelPath);
199 if (self::$useTranslationRepository && \in_array($langId, self::$translationRepositoryLanguages))
201 $langFullPath = Main\Localization\Translation::convertLangPath($langFullPath, $langId);
204 $fullPaths[$langId] = $langFullPath;
207 $rows = $this->
mergeLangFiles($langFilePath, $fullPaths, $this->collectUntranslated, $fileCodeList[$langFilePath] ?? []);
208 foreach ($rows as $code => $row)
210 if (!empty($this->seekPhraseCode))
212 if ($code == $this->seekPhraseCode)
214 $this->seekPhraseCode =
'';
219 $csvFile->put(\array_values($row));
223 && !empty($row[$currentLangId])
224 && mb_strlen($row[$currentLangId]) < $this->maxSampleSourceLength
227 $samples = $this->findSamples(
228 $row[$currentLangId],
232 $this->samplesRestriction
234 foreach ($samples as $sample)
236 $samplesFile->put(\array_values($sample));
237 $this->exportedSamplesCount ++;
241 $this->exportedPhraseCount ++;
245 $this->seekPhraseCode = $code;
249 $this->seekPhraseCode =
'';
255 $this->seekLangFilePath = $langFilePath;
260 $this->seekLangFilePath =
'';
267 $exportingPath = Translate\IO\Path::tidy($exportingPath.
'/');
268 if (\preg_match(
"#(.+/lang)(/?\w*)#", $exportingPath, $matches))
270 $lookForLangPath = $matches[1];
271 $lookForLangSubPath =
'';
272 if (\preg_match(
"#(.+/lang/[^/]+/?)(.*)$#", $exportingPath, $subMatches))
274 $lookForLangSubPath = $subMatches[2];
279 $lookForLangPath = $exportingPath;
280 $lookForLangSubPath =
'';
282 unset($matches, $subMatches);
287 '=%PATH' => $lookForLangPath.
'%'
289 if ($this->seekPathLangId > 0)
291 $pathFilter[
'>=ID'] = $this->seekPathLangId;
294 $cachePathLangRes = Translate\Index\Internals\PathLangTable::getList([
295 'filter' => $pathFilter,
296 'order' => [
'ID' =>
'ASC'],
297 'select' => [
'ID',
'PATH'],
299 while ($pathLang = $cachePathLangRes->fetch())
301 $lookThroughPath = $pathLang[
'PATH'].
'/#LANG_ID#';
302 if (!empty($lookForLangSubPath))
304 $lookThroughPath .=
'/'. \trim($lookForLangSubPath,
'/');
308 foreach ($filePaths as $langFilePath => $fullPaths)
310 if (!empty($this->seekLangFilePath))
312 if ($langFilePath == $this->seekLangFilePath)
314 $this->seekLangFilePath =
'';
322 $rows = $this->
mergeLangFiles($langFilePath, $fullPaths, $this->collectUntranslated, $fileCodeList[$langFilePath] ?? []);
323 foreach ($rows as $code => $row)
325 if (!empty($this->seekPhraseCode))
327 if ($code == $this->seekPhraseCode)
329 $this->seekPhraseCode =
'';
334 $csvFile->put(\array_values($row));
338 && !empty($row[$currentLangId])
339 && mb_strlen($row[$currentLangId]) < $this->maxSampleSourceLength
342 $samples = $this->findSamples(
343 $row[$currentLangId],
347 $this->samplesRestriction
349 foreach ($samples as $sample)
351 $samplesFile->put(\array_values($sample));
352 $this->exportedSamplesCount ++;
356 $this->exportedPhraseCount ++;
360 $this->seekPhraseCode = $code;
365 $this->seekPhraseCode =
'';
371 $this->seekLangFilePath = $langFilePath;
376 $this->seekLangFilePath =
'';
383 $this->seekPathLangId = (int)$pathLang[
'ID'];
388 $this->seekPathLangId = 0;
393 $processedItemCount ++;
395 if (isset($this->pathList[$pos + 1]))
397 $this->seekOffset = $pos + 1;
401 $this->seekOffset = 0;
412 $this->exportFileSize = $csvFile->getSize();
415 if ($this->appendSamples)
417 $this->samplesFileSize = $samplesFile->getSize();
418 $samplesFile->close();
421 $this->processedItems += $processedItemCount;
436 if ($csvFile->hasErrors())
438 $errors = $csvFile->getErrors();
439 foreach ($errors as $err)
441 if ($err->getCode() == Translate\
IO\CsvFile::ERROR_32K_FIELD_LENGTH)