Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
exportfile.php
1<?php
3
7
12 extends ExportAction
14{
15 use Translate\Controller\Stepper;
16 use Translate\Controller\ProcessParams;
17
18 private string $seekPhraseCode = '';
19
20 private array $data = [];
21
22 private string $langFilePath = '';
23
31 public function __construct($name, Main\Engine\Controller $controller, array $config = [])
32 {
33 Loc::loadLanguageFile(__DIR__ . '/exportaction.php');
34
35 $this->keepField('seekPhraseCode');
36
37 parent::__construct($name, $controller, $config);
38 }
39
48 public function run(string $path = '', bool $runBefore = false): array
49 {
50 if (empty($path) || !\preg_match("#(.+\/lang)(\/?\w*)#", $path, $matches))
51 {
52 $this->addError(new Main\Error(Loc::getMessage('TR_EXPORT_EMPTY_PATH_LIST')));
53
54 return [
55 'STATUS' => Translate\Controller\STATUS_COMPLETED
56 ];
57 }
58 if (!Translate\IO\Path::isLangDir($path))
59 {
60 $this->addError(new Main\Error(Loc::getMessage('TR_EXPORT_FILE_NOT_LANG', ['#FILE#' => $path])));
61
62 return [
63 'STATUS' => Translate\Controller\STATUS_COMPLETED
64 ];
65 }
66
67 if ($runBefore)
68 {
69 $this->onBeforeRun();
70 }
71
72 $this->langFilePath = Translate\IO\Path::replaceLangId($path, '#LANG_ID#');
73
74 $fullPaths = [];
75 foreach ($this->languages as $langId)
76 {
77 $langRelPath = Translate\IO\Path::replaceLangId($path, $langId);
78 $langFullPath = Translate\IO\Path::tidy(self::$documentRoot . '/' . $langRelPath);
79
80 if (self::$useTranslationRepository && \in_array($langId, self::$translationRepositoryLanguages))
81 {
82 $langFullPath = Main\Localization\Translation::convertLangPath($langFullPath, $langId);
83 }
84
85 $fullPaths[$langId] = $langFullPath;
86 }
87
88 $this->data = $this->mergeLangFiles($this->langFilePath, $fullPaths, $this->collectUntranslated);
89
90 if ($this->isNewProcess)
91 {
92 $this->totalItems = (int)count($this->data);
93 $this->processedItems = 0;
94
95 if ($this->totalItems > 0)
96 {
97 $this->exportFileName = $this->generateExportFileName($path, $this->languages);
98 $csvFile = $this->createExportTempFile($this->exportFileName);
99 $this->exportFilePath = $csvFile->getPhysicalPath();
100 $this->exportFileSize = $csvFile->getSize();
101 }
102 if ($this->appendSamples)
103 {
104 $this->samplesFileName = $this->generateExportFileName('samples-'.$path, $this->languages);
105 $sampleFile = $this->createExportTempFile($this->samplesFileName);
106 $this->samplesFilePath = $sampleFile->getPhysicalPath();
107 $this->samplesFileSize = $sampleFile->getSize();
108 }
109
110 $this->saveProgressParameters();
111
112 return [
113 'STATUS' => $this->totalItems > 0
114 ? Translate\Controller\STATUS_PROGRESS
115 : Translate\Controller\STATUS_COMPLETED,
116 'PROCESSED_ITEMS' => 0,
117 'TOTAL_ITEMS' => $this->totalItems,
118 'TOTAL_PHRASES' => $this->exportedPhraseCount,
119 'TOTAL_SAMPLES' => $this->exportedSamplesCount,
120 ];
121 }
122
123 return $this->performStep('runExporting', ['path' => $path]);
124 }
125
133 private function runExporting(array $params): array
134 {
135 $csvFile = new Translate\IO\CsvFile($this->exportFilePath);
136 $this->configureExportCsvFile($csvFile);
137 $csvFile->openWrite( Main\IO\FileStreamOpenMode::APPEND);
138
139 if ($this->appendSamples)
140 {
141 $samplesFile = new Translate\IO\CsvFile($this->samplesFilePath);
142 $this->configureExportCsvFile($samplesFile);
143 $samplesFile->openWrite( Main\IO\FileStreamOpenMode::APPEND);
144 }
145
146 $currentLangId = Loc::getCurrentLang();
147
148 $processedItemCount = 0;
149 foreach ($this->data as $code => $row)
150 {
151 if (!empty($this->seekPhraseCode))
152 {
153 if ($code == $this->seekPhraseCode)
154 {
155 $this->seekPhraseCode = '';
156 }
157 continue;
158 }
159
160 $csvFile->put(\array_values($row));
161
162 if (
163 $this->appendSamples
164 && !empty($row[$currentLangId])
165 && mb_strlen($row[$currentLangId]) < $this->maxSampleSourceLength
166 )
167 {
168 $samples = $this->findSamples(
169 $row[$currentLangId],
170 $currentLangId,
171 $this->langFilePath,
172 $this->samplesCount,
173 $this->samplesRestriction
174 );
175 foreach ($samples as $sample)
176 {
177 $samplesFile->put(\array_values($sample));
178 $this->exportedSamplesCount ++;
179 }
180 }
181
182 $this->exportedPhraseCount ++;
183 $processedItemCount ++;
184
185 if ($this->instanceTimer()->hasTimeLimitReached())
186 {
187 $this->seekPhraseCode = $code;
188 break;
189 }
190 }
191
192 $this->processedItems += $processedItemCount;
193
194 $this->exportFileSize = $csvFile->getSize();
195 $csvFile->close();
196
197 if ($this->appendSamples)
198 {
199 $this->samplesFileSize = $samplesFile->getSize();
200 $samplesFile->close();
201 }
202
203 if ($this->instanceTimer()->hasTimeLimitReached() !== true)
204 {
205 $this->declareAccomplishment();
207 }
208
209 $result = [
210 'PROCESSED_ITEMS' => $this->processedItems,
211 'TOTAL_ITEMS' => $this->totalItems,
212 'TOTAL_PHRASES' => $this->exportedPhraseCount,
213 'TOTAL_SAMPLES' => $this->exportedSamplesCount,
214 ];
215
216 if ($csvFile->hasErrors())
217 {
218 $errors = $csvFile->getErrors();
219 foreach ($errors as $err)
220 {
221 if ($err->getCode() == Translate\IO\CsvFile::ERROR_32K_FIELD_LENGTH)
222 {
223 $result['WARNING'] = Loc::getMessage('TR_EXPORT_ERROR_32K_LENGTH');
224 }
225 else
226 {
227 $this->addError($err);
228 }
229 }
230 }
231
232 return $result;
233 }
234}
static loadLanguageFile($file, $language=null, $normalize=true)
Definition loc.php:224
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
configureExportCsvFile(Translate\IO\CsvFile $csvFile)
generateExportFileName(string $path, array $languages)
mergeLangFiles(string $langFilePath, array $fullLangFilePaths, bool $collectUntranslated=false, array $filterByCodeList=[])
run(string $path='', bool $runBefore=false)
__construct($name, Main\Engine\Controller $controller, array $config=[])
performStep($action, array $params=[])
Definition stepper.php:75
declareAccomplishment(bool $flag=true)
Definition stepper.php:136
addError(Main\Error $error)
Definition error.php:22