1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
exportpath.php
См. документацию.
1<?php
2namespace Bitrix\Translate\Controller\Export;
3
4use Bitrix\Main;
5use Bitrix\Translate;
6use Bitrix\Main\Localization\Loc;
7
12 extends ExportAction
14{
17
19 private array $pathList = [];
20
22 private array $codeList = [];
23
24 private int $seekOffset = 0;
25
26 private int $seekPathLangId = 0;
27
28 private string $seekLangFilePath = '';
29 private string $seekPhraseCode = '';
30
31
40 {
41 $this->keepField(['pathList', 'codeList', 'seekOffset', 'seekPathLangId', 'seekPhraseCode', 'seekLangFilePath']);
42
43 Loc::loadLanguageFile(__DIR__ . '/exportaction.php');
44
45 parent::__construct($name, $controller, $config);
46 }
47
48
57 public function run(string $path = '', bool $runBefore = false): array
58 {
59 if ($runBefore)
60 {
61 $this->onBeforeRun();
62 }
63
64 if ($this->isNewProcess)
65 {
66 $pathList = $this->controller->getRequest()->get('pathList');
67
68 $pathList = \preg_split("/[\r\n]+/", $pathList);
69 \array_walk($pathList, 'trim');
70 $pathList = \array_unique(\array_filter($pathList));
71 if (empty($pathList))
72 {
73 $this->addError(new Main\Error(Loc::getMessage('TR_EXPORT_EMPTY_PATH_LIST')));
74
75 return [
77 ];
78 }
79
80 foreach ($pathList as $testPath)
81 {
82 if (Translate\IO\Path::isPhpFile($testPath))
83 {
84 if (Translate\IO\Path::isLangDir($testPath))
85 {
86 $this->pathList[] = $testPath;
87 }
88 }
89 else
90 {
91 $this->pathList[] = $testPath;
92 }
93 }
94
95 // phrase codes
96 $codeList = $this->controller->getRequest()->get('codeList');
97 if (!empty($codeList))
98 {
99 $codeList = \preg_split("/[\r\n]+/", $codeList);
100 \array_walk($codeList, 'trim');
101 $this->codeList = \array_unique(\array_filter($codeList));
102 }
103
104 $this->totalItems = \count($this->pathList);
105 $this->processedItems = 0;
106
107 if ($this->totalItems > 0)
108 {
109 $this->exportFileName = $this->generateExportFileName($path, $this->languages);
110 $csvFile = $this->createExportTempFile($this->exportFileName);
111 $this->exportFilePath = $csvFile->getPhysicalPath();
112 $this->exportFileSize = $csvFile->getSize();
113 }
114 if ($this->appendSamples)
115 {
116 $this->samplesFileName = $this->generateExportFileName($path.'-samples', $this->languages);
117 $sampleFile = $this->createExportTempFile($this->samplesFileName);
118 $this->samplesFilePath = $sampleFile->getPhysicalPath();
119 $this->samplesFileSize = $sampleFile->getSize();
120 }
121
122 $this->saveProgressParameters();
123
124 return [
126 'PROCESSED_ITEMS' => 0,
127 'TOTAL_ITEMS' => $this->totalItems,
128 ];
129 }
130
131 return $this->performStep('runExporting');
132 }
133
134
140 private function runExporting(): array
141 {
142 $csvFile = new Translate\IO\CsvFile($this->exportFilePath);
143 $this->configureExportCsvFile($csvFile);
144 $csvFile->openWrite( Main\IO\FileStreamOpenMode::APPEND);
145
146 if ($this->appendSamples)
147 {
148 $samplesFile = new Translate\IO\CsvFile($this->samplesFilePath);
149 $this->configureExportCsvFile($samplesFile);
150 $samplesFile->openWrite( Main\IO\FileStreamOpenMode::APPEND);
151 }
152
153 $processedItemCount = 0;
154
155 $filterCodeList = $this->codeList ?: [];
156 $fileCodeList = [];
157 foreach ($filterCodeList as $pathCode)
158 {
159 [$path, $code] = \explode('::', $pathCode);
160 if ($path && $code)
161 {
162 $langFilePath = Translate\IO\Path::replaceLangId($path, '#LANG_ID#');
163 if (!isset($fileCodeList[$langFilePath]))
164 {
165 $fileCodeList[$langFilePath] = [];
166 }
167 $fileCodeList[$langFilePath][] = $code;
168 }
169 }
170
171 $currentLangId = Loc::getCurrentLang();
172
173 for ($pos = ($this->seekOffset > 0 ? $this->seekOffset : 0), $total = \count($this->pathList); $pos < $total; $pos ++)
174 {
175 $exportingPath = $this->pathList[$pos];
176
177 // file
178 if (Translate\IO\Path::isPhpFile($exportingPath))
179 {
180 $langFilePath = Translate\IO\Path::replaceLangId($exportingPath, '#LANG_ID#');
181 if (!empty($this->seekLangFilePath))
182 {
183 if ($langFilePath == $this->seekLangFilePath)
184 {
185 $this->seekLangFilePath = '';
186 }
187 else
188 {
189 continue;
190 }
191 }
192
193 $fullPaths = [];
194 foreach ($this->languages as $langId)
195 {
196 $langRelPath = Translate\IO\Path::replaceLangId($exportingPath, $langId);
197 $langFullPath = Translate\IO\Path::tidy(self::$documentRoot.'/'.$langRelPath);
198
199 if (self::$useTranslationRepository && \in_array($langId, self::$translationRepositoryLanguages))
200 {
201 $langFullPath = Main\Localization\Translation::convertLangPath($langFullPath, $langId);
202 }
203
204 $fullPaths[$langId] = $langFullPath;
205 }
206
207 $rows = $this->mergeLangFiles($langFilePath, $fullPaths, $this->collectUntranslated, $fileCodeList[$langFilePath] ?? []);
208 foreach ($rows as $code => $row)
209 {
210 if (!empty($this->seekPhraseCode))
211 {
212 if ($code == $this->seekPhraseCode)
213 {
214 $this->seekPhraseCode = '';
215 }
216 continue;
217 }
218
219 $csvFile->put(\array_values($row));
220
221 if (
222 $this->appendSamples
223 && !empty($row[$currentLangId])
224 && mb_strlen($row[$currentLangId]) < $this->maxSampleSourceLength
225 )
226 {
227 $samples = $this->findSamples(
228 $row[$currentLangId],
229 $currentLangId,
230 $langFilePath,
231 $this->samplesCount,
232 $this->samplesRestriction
233 );
234 foreach ($samples as $sample)
235 {
236 $samplesFile->put(\array_values($sample));
237 $this->exportedSamplesCount ++;
238 }
239 }
240
241 $this->exportedPhraseCount ++;
242
243 if ($this->instanceTimer()->hasTimeLimitReached())
244 {
245 $this->seekPhraseCode = $code;
246 break;
247 }
248 else
249 {
250 $this->seekPhraseCode = '';
251 }
252 }
253
254 if ($this->instanceTimer()->hasTimeLimitReached())
255 {
256 $this->seekLangFilePath = $langFilePath;
257 break;
258 }
259 else
260 {
261 $this->seekLangFilePath = '';
262 }
263 }
264
265 // folder
266 else
267 {
268 $exportingPath = Translate\IO\Path::tidy($exportingPath. '/');
269 if (\preg_match("#(.+/lang)(/?\w*)#", $exportingPath, $matches))
270 {
271 $lookForLangPath = $matches[1];
272 $lookForLangSubPath = '';
273 if (\preg_match("#(.+/lang/[^/]+/?)(.*)$#", $exportingPath, $subMatches))
274 {
275 $lookForLangSubPath = $subMatches[2];
276 }
277 }
278 else
279 {
280 $lookForLangPath = $exportingPath;
281 $lookForLangSubPath = '';
282 }
283 unset($matches, $subMatches);
284
285
286 // now let's find lang files
287 $pathFilter = [
288 '=%PATH' => $lookForLangPath.'%'
289 ];
290 if ($this->seekPathLangId > 0)
291 {
292 $pathFilter['>=ID'] = $this->seekPathLangId;
293 }
294
295 $cachePathLangRes = Translate\Index\Internals\PathLangTable::getList([
296 'filter' => $pathFilter,
297 'order' => ['ID' => 'ASC'],
298 'select' => ['ID', 'PATH'],
299 ]);
300 while ($pathLang = $cachePathLangRes->fetch())
301 {
302 $lookThroughPath = $pathLang['PATH']. '/#LANG_ID#';
303 if (!empty($lookForLangSubPath))
304 {
305 $lookThroughPath .= '/'. \trim($lookForLangSubPath, '/');
306 }
307 foreach ($this->lookThroughLangFolder($lookThroughPath) as $filePaths)
308 {
309 foreach ($filePaths as $langFilePath => $fullPaths)
310 {
311 if (!empty($this->seekLangFilePath))
312 {
313 if ($langFilePath == $this->seekLangFilePath)
314 {
315 $this->seekLangFilePath = '';
316 }
317 else
318 {
319 continue;
320 }
321 }
322
323 $rows = $this->mergeLangFiles($langFilePath, $fullPaths, $this->collectUntranslated, $fileCodeList[$langFilePath] ?? []);
324 foreach ($rows as $code => $row)
325 {
326 if (!empty($this->seekPhraseCode))
327 {
328 if ($code == $this->seekPhraseCode)
329 {
330 $this->seekPhraseCode = '';
331 }
332 continue;
333 }
334
335 $csvFile->put(\array_values($row));
336
337 if (
338 $this->appendSamples
339 && !empty($row[$currentLangId])
340 && mb_strlen($row[$currentLangId]) < $this->maxSampleSourceLength
341 )
342 {
343 $samples = $this->findSamples(
344 $row[$currentLangId],
345 $currentLangId,
346 $langFilePath,
347 $this->samplesCount,
348 $this->samplesRestriction
349 );
350 foreach ($samples as $sample)
351 {
352 $samplesFile->put(\array_values($sample));
353 $this->exportedSamplesCount ++;
354 }
355 }
356
357 $this->exportedPhraseCount ++;
358
359 if ($this->instanceTimer()->hasTimeLimitReached())
360 {
361 $this->seekPhraseCode = $code;
362 break;
363 }
364 else
365 {
366 $this->seekPhraseCode = '';
367 }
368 }
369
370 if ($this->instanceTimer()->hasTimeLimitReached())
371 {
372 $this->seekLangFilePath = $langFilePath;
373 break;
374 }
375 else
376 {
377 $this->seekLangFilePath = '';
378 }
379 }
380 }
381
382 if ($this->instanceTimer()->hasTimeLimitReached())
383 {
384 $this->seekPathLangId = (int)$pathLang['ID'];
385 break 2;
386 }
387 else
388 {
389 $this->seekPathLangId = 0;
390 }
391 }
392 }
393
394 $processedItemCount ++;
395
396 if (isset($this->pathList[$pos + 1]))
397 {
398 $this->seekOffset = $pos + 1;//next
399 }
400 else
401 {
402 $this->seekOffset = 0;
403 $this->declareAccomplishment();
405 }
406
407 if ($this->instanceTimer()->hasTimeLimitReached())
408 {
409 break;
410 }
411 }
412
413 $this->exportFileSize = $csvFile->getSize();
414
415 $csvFile->close();
416 if ($this->appendSamples)
417 {
418 $this->samplesFileSize = $samplesFile->getSize();
419 $samplesFile->close();
420 }
421
422 $this->processedItems += $processedItemCount;
423
424 if ($this->instanceTimer()->hasTimeLimitReached() !== true)
425 {
426 $this->declareAccomplishment();
428 }
429
430 $result = [
431 'PROCESSED_ITEMS' => $this->processedItems,
432 'TOTAL_ITEMS' => $this->totalItems,
433 'TOTAL_PHRASES' => $this->exportedPhraseCount,
434 'TOTAL_SAMPLES' => $this->exportedSamplesCount,
435 ];
436
437 if ($csvFile->hasErrors())
438 {
439 $errors = $csvFile->getErrors();
440 foreach ($errors as $err)
441 {
442 if ($err->getCode() == Translate\IO\CsvFile::ERROR_32K_FIELD_LENGTH)
443 {
444 $result['WARNING'] = Loc::getMessage('TR_EXPORT_ERROR_32K_LENGTH');
445 }
446 else
447 {
448 $this->addError($err);
449 }
450 }
451 }
452
453 return $result;
454 }
455}
$path
Определения access_edit.php:21
onBeforeRun()
Определения action.php:159
$controller
Определения action.php:23
lookThroughLangFolder(string $langPath)
Определения exportaction.php:378
configureExportCsvFile(Translate\IO\CsvFile $csvFile)
Определения exportaction.php:196
generateExportFileName(string $path, array $languages)
Определения exportaction.php:225
mergeLangFiles(string $langFilePath, array $fullLangFilePaths, bool $collectUntranslated=false, array $filterByCodeList=[])
Определения exportaction.php:270
run(string $path='', bool $runBefore=false)
Определения exportpath.php:57
__construct($name, Main\Engine\Controller $controller, array $config=[])
Определения exportpath.php:39
static replaceLangId(string $path, string $langId)
Определения path.php:98
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$errors
Определения iblock_catalog_edit.php:74
$pathLang
Определения jscore.php:6
if(!is_null($config))($config as $configItem)(! $configItem->isVisible()) $code
Определения options.php:195
Определения action.php:3
Определения directory.php:3
trait ProcessParams
Определения processparams.php:12
const STATUS_COMPLETED
Определения controller.php:6
instanceTimer()
Определения stepper.php:172
int $processedItems
Определения stepper.php:20
int $totalItems
Определения stepper.php:22
performStep($action, array $params=[])
Определения stepper.php:75
declareAccomplishment(bool $flag=true)
Определения stepper.php:136
trait Stepper
Определения stepper.php:13
const STATUS_PROGRESS
Определения controller.php:7
Определения autoload.php:3
addError(Main\Error $error)
Определения error.php:22
trait Error
Определения error.php:11
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
$matches
Определения index.php:22
$rows
Определения options.php:264