16 use Translate\Controller\Stepper;
17 use Translate\Controller\ProcessParams;
19 private int $seekPathLangId = 0;
21 private string $seekLangFilePath =
'';
22 private string $seekPhraseCode =
'';
33 $this->
keepField(
'seekPathLangId',
'seekLangFilePath',
'seekPhraseCode');
49 public function run(
string $path =
'',
bool $runBefore =
false): array
53 $path = Translate\Config::getDefaultPath();
58 if (\preg_match(
"#(.+/lang)(/?\w*)#", $path, $matches))
60 if (\preg_match(
"#(.+/lang/[^/]+/?)(.*)$#", $path, $subMatches))
62 $subPath = $subMatches[2];
66 unset($matches, $subMatches);
74 if ($this->isNewProcess)
76 $this->totalItems = (int)Index\Internals\PathLangTable::getCount([
'=%PATH' => $path.
'%']);
77 $this->processedItems = 0;
79 if ($this->totalItems > 0)
82 $csvFile = $this->createExportTempFile($this->exportFileName);
83 $this->exportFilePath = $csvFile->getPhysicalPath();
84 $this->exportFileSize = $csvFile->getSize();
86 if ($this->appendSamples)
89 $sampleFile = $this->createExportTempFile($this->samplesFileName);
90 $this->samplesFilePath = $sampleFile->getPhysicalPath();
91 $this->samplesFileSize = $sampleFile->getSize();
97 'STATUS' => ($this->totalItems > 0 ? Translate\Controller\STATUS_PROGRESS : Translate\Controller\STATUS_COMPLETED),
98 'PROCESSED_ITEMS' => 0,
99 'TOTAL_ITEMS' => $this->totalItems,
100 'TOTAL_PHRASES' => $this->exportedPhraseCount,
101 'TOTAL_SAMPLES' => $this->exportedSamplesCount,
105 return $this->
performStep(
'runExporting', [
'path' => $path,
'subPath' => $subPath]);
115 private function runExporting(array $params): array
117 $path = \rtrim($params[
'path'],
'/');
118 $subPath = \trim($params[
'subPath'],
'/');
124 if ($this->appendSamples)
135 '=%PATH' => $path.
'/%',
137 if (!empty($this->seekPathLangId))
139 $pathFilter[
'>ID'] = $this->seekPathLangId;
144 $cachePathLangRes = Index\Internals\PathLangTable::getList([
145 'filter' => $pathFilter,
146 'order' => [
'ID' =>
'ASC'],
147 'select' => [
'ID',
'PATH'],
149 $processedItemCount = 0;
150 while ($pathLang = $cachePathLangRes->fetch())
152 $lookThroughPath = $pathLang[
'PATH'].
'/#LANG_ID#';
153 if (!empty($subPath))
155 $lookThroughPath .=
'/'. $subPath;
159 foreach ($filePaths as $langFilePath => $fullPaths)
161 if (!empty($this->seekLangFilePath))
163 if ($langFilePath == $this->seekLangFilePath)
165 $this->seekLangFilePath =
'';
173 $rows = $this->
mergeLangFiles($langFilePath, $fullPaths, $this->collectUntranslated);
174 foreach ($rows as $code => $row)
176 if (!empty($this->seekPhraseCode))
178 if ($code == $this->seekPhraseCode)
180 $this->seekPhraseCode =
'';
185 $csvFile->put(array_values($row));
189 && !empty($row[$currentLangId])
190 && mb_strlen($row[$currentLangId]) < $this->maxSampleSourceLength
193 $samples = $this->findSamples(
194 $row[$currentLangId],
198 $this->samplesRestriction
200 foreach ($samples as $sample)
202 $samplesFile->put(\array_values($sample));
203 $this->exportedSamplesCount ++;
207 $this->exportedPhraseCount ++;
211 $this->seekPhraseCode = $code;
215 $this->seekPhraseCode =
'';
221 $this->seekLangFilePath = $langFilePath;
226 $this->seekLangFilePath =
'';
231 $processedItemCount ++;
235 $this->seekPathLangId = (int)$pathLang[
'ID'];
240 $this->exportFileSize = $csvFile->getSize();
243 if ($this->appendSamples)
245 $this->samplesFileSize = $samplesFile->getSize();
246 $samplesFile->close();
249 $this->processedItems += $processedItemCount;
264 if ($csvFile->hasErrors())
266 $errors = $csvFile->getErrors();
267 foreach ($errors as $err)
269 if ($err->getCode() == Translate\
IO\CsvFile::ERROR_32K_FIELD_LENGTH)
static loadLanguageFile($file, $language=null, $normalize=true)
static getMessage($code, $replace=null, $language=null)