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