Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
exportfilelist.php
1<?php
3
8
13 extends ExportAction
15{
16 use Translate\Controller\Stepper;
17 use Translate\Controller\ProcessParams;
18
19 private int $seekPathLangId = 0;
20
21 private string $seekLangFilePath = '';
22 private string $seekPhraseCode = '';
23
31 public function __construct($name, Main\Engine\Controller $controller, array $config = [])
32 {
33 $this->keepField('seekPathLangId', 'seekLangFilePath', 'seekPhraseCode');
34
35 Loc::loadLanguageFile(__DIR__ . '/exportaction.php');
36
37 parent::__construct($name, $controller, $config);
38 }
39
40
49 public function run(string $path = '', bool $runBefore = false): array
50 {
51 if (empty($path))
52 {
53 $path = Translate\Config::getDefaultPath();
54 }
55
56 // part of the path after /lang/
57 $subPath = '';
58 if (\preg_match("#(.+/lang)(/?\w*)#", $path, $matches))
59 {
60 if (\preg_match("#(.+/lang/[^/]+/?)(.*)$#", $path, $subMatches))
61 {
62 $subPath = $subMatches[2];
63 }
64 $path = $matches[1];
65 }
66 unset($matches, $subMatches);
67
68
69 if ($runBefore)
70 {
71 $this->onBeforeRun();
72 }
73
74 if ($this->isNewProcess)
75 {
76 $this->totalItems = (int)Index\Internals\PathLangTable::getCount(['=%PATH' => $path.'%']);
77 $this->processedItems = 0;
78
79 if ($this->totalItems > 0)
80 {
81 $this->exportFileName = $this->generateExportFileName($path, $this->languages);
82 $csvFile = $this->createExportTempFile($this->exportFileName);
83 $this->exportFilePath = $csvFile->getPhysicalPath();
84 $this->exportFileSize = $csvFile->getSize();
85 }
86 if ($this->appendSamples)
87 {
88 $this->samplesFileName = $this->generateExportFileName($path.'-samples', $this->languages);
89 $sampleFile = $this->createExportTempFile($this->samplesFileName);
90 $this->samplesFilePath = $sampleFile->getPhysicalPath();
91 $this->samplesFileSize = $sampleFile->getSize();
92 }
93
95
96 return [
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,
102 ];
103 }
104
105 return $this->performStep('runExporting', ['path' => $path, 'subPath' => $subPath]);
106 }
107
115 private function runExporting(array $params): array
116 {
117 $path = \rtrim($params['path'], '/');
118 $subPath = \trim($params['subPath'], '/');
119
120 $csvFile = new Translate\IO\CsvFile($this->exportFilePath);
121 $this->configureExportCsvFile($csvFile);
122 $csvFile->openWrite( Main\IO\FileStreamOpenMode::APPEND);
123
124 if ($this->appendSamples)
125 {
126 $samplesFile = new Translate\IO\CsvFile($this->samplesFilePath);
127 $this->configureExportCsvFile($samplesFile);
128 $samplesFile->openWrite( Main\IO\FileStreamOpenMode::APPEND);
129 }
130
131 $pathFilter = [];
132 $pathFilter[] = [
133 'LOGIC' => 'OR',
134 '=PATH' => $path,
135 '=%PATH' => $path.'/%',
136 ];
137 if (!empty($this->seekPathLangId))
138 {
139 $pathFilter['>ID'] = $this->seekPathLangId;
140 }
141
142 $currentLangId = Loc::getCurrentLang();
143
144 $cachePathLangRes = Index\Internals\PathLangTable::getList([
145 'filter' => $pathFilter,
146 'order' => ['ID' => 'ASC'],
147 'select' => ['ID', 'PATH'],
148 ]);
149 $processedItemCount = 0;
150 while ($pathLang = $cachePathLangRes->fetch())
151 {
152 $lookThroughPath = $pathLang['PATH']. '/#LANG_ID#';
153 if (!empty($subPath))
154 {
155 $lookThroughPath .= '/'. $subPath;
156 }
157 foreach ($this->lookThroughLangFolder($lookThroughPath) as $filePaths)
158 {
159 foreach ($filePaths as $langFilePath => $fullPaths)
160 {
161 if (!empty($this->seekLangFilePath))
162 {
163 if ($langFilePath == $this->seekLangFilePath)
164 {
165 $this->seekLangFilePath = '';
166 }
167 else
168 {
169 continue;
170 }
171 }
172
173 $rows = $this->mergeLangFiles($langFilePath, $fullPaths, $this->collectUntranslated);
174 foreach ($rows as $code => $row)
175 {
176 if (!empty($this->seekPhraseCode))
177 {
178 if ($code == $this->seekPhraseCode)
179 {
180 $this->seekPhraseCode = '';
181 }
182 continue;
183 }
184
185 $csvFile->put(array_values($row));
186
187 if (
188 $this->appendSamples
189 && !empty($row[$currentLangId])
190 && mb_strlen($row[$currentLangId]) < $this->maxSampleSourceLength
191 )
192 {
193 $samples = $this->findSamples(
194 $row[$currentLangId],
195 $currentLangId,
196 $langFilePath,
197 $this->samplesCount,
198 $this->samplesRestriction
199 );
200 foreach ($samples as $sample)
201 {
202 $samplesFile->put(\array_values($sample));
203 $this->exportedSamplesCount ++;
204 }
205 }
206
207 $this->exportedPhraseCount ++;
208
209 if ($this->instanceTimer()->hasTimeLimitReached())
210 {
211 $this->seekPhraseCode = $code;
212 }
213 else
214 {
215 $this->seekPhraseCode = '';
216 }
217 }
218
219 if ($this->instanceTimer()->hasTimeLimitReached())
220 {
221 $this->seekLangFilePath = $langFilePath;
222 break;
223 }
224 else
225 {
226 $this->seekLangFilePath = '';
227 }
228 }
229 }
230
231 $processedItemCount ++;
232
233 if ($this->instanceTimer()->hasTimeLimitReached())
234 {
235 $this->seekPathLangId = (int)$pathLang['ID'];
236 break;
237 }
238 }
239
240 $this->exportFileSize = $csvFile->getSize();
241 $csvFile->close();
242
243 if ($this->appendSamples)
244 {
245 $this->samplesFileSize = $samplesFile->getSize();
246 $samplesFile->close();
247 }
248
249 $this->processedItems += $processedItemCount;
250
251 if ($this->instanceTimer()->hasTimeLimitReached() !== true)
252 {
253 $this->declareAccomplishment();
255 }
256
257 $result = [
258 'PROCESSED_ITEMS' => $this->processedItems,
259 'TOTAL_ITEMS' => $this->totalItems,
260 'TOTAL_PHRASES' => $this->exportedPhraseCount,
261 'TOTAL_SAMPLES' => $this->exportedSamplesCount,
262 ];
263
264 if ($csvFile->hasErrors())
265 {
266 $errors = $csvFile->getErrors();
267 foreach ($errors as $err)
268 {
269 if ($err->getCode() == Translate\IO\CsvFile::ERROR_32K_FIELD_LENGTH)
270 {
271 $result['WARNING'] = Loc::getMessage('TR_EXPORT_ERROR_32K_LENGTH');
272 }
273 else
274 {
275 $this->addError($err);
276 }
277 }
278 }
279
280 return $result;
281 }
282}
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