Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
exportphrasesearch.php
1<?php
3
8
13 extends ExportAction
15{
16 use Translate\Controller\Stepper;
17 use Translate\Controller\ProcessParams;
18
19 private string $pathList = '';
20
21 private int $seekPathId = 0;
22
30 public function __construct($name, Main\Engine\Controller $controller, array $config = [])
31 {
32 $this->keepField(['pathList', 'seekPathId']);
33
34 Loc::loadLanguageFile(__DIR__ . '/exportaction.php');
35
36 parent::__construct($name, $controller, $config);
37 }
38
39
48 public function run(string $path = '', bool $runBefore = false): array
49 {
50 if (empty($path))
51 {
52 $path = Translate\Config::getDefaultPath();
53 }
54
55 if ($runBefore)
56 {
57 $this->onBeforeRun();
58 }
59
60 if ($this->isNewProcess)
61 {
62 $pathList = $this->controller->getRequest()->get('pathList');
63 if (!empty($pathList))
64 {
65 $this->pathList = $pathList;
66 }
67
68 $this->totalItems = (int)Index\PhraseIndexSearch::getCount($this->processFilter($path));
69 $this->processedItems = 0;
70
71 if ($this->totalItems > 0)
72 {
73 $this->exportFileName = $this->generateExportFileName($path, $this->languages);
74 $csvFile = $this->createExportTempFile($this->exportFileName);
75 $this->exportFilePath = $csvFile->getPhysicalPath();
76 $this->exportFileSize = $csvFile->getSize();
77 }
78 if ($this->appendSamples)
79 {
80 $this->samplesFileName = $this->generateExportFileName($path.'-samples', $this->languages);
81 $sampleFile = $this->createExportTempFile($this->samplesFileName);
82 $this->samplesFilePath = $sampleFile->getPhysicalPath();
83 $this->samplesFileSize = $sampleFile->getSize();
84 }
85
87
88 return [
89 'STATUS' => ($this->totalItems > 0 ? Translate\Controller\STATUS_PROGRESS : Translate\Controller\STATUS_COMPLETED),
90 'PROCESSED_ITEMS' => 0,
91 'TOTAL_ITEMS' => $this->totalItems,
92 'TOTAL_PHRASES' => $this->exportedPhraseCount,
93 'TOTAL_SAMPLES' => $this->exportedSamplesCount,
94 ];
95 }
96
97 return $this->performStep('runExporting', ['path' => $path]);
98 }
99
100
108 private function runExporting(array $params): array
109 {
110 $path = \rtrim($params['path'], '/');
111
112 $csvFile = new Translate\IO\CsvFile($this->exportFilePath);
113 $this->configureExportCsvFile($csvFile);
114 $csvFile->openWrite( Main\IO\FileStreamOpenMode::APPEND);
115
116 if ($this->appendSamples)
117 {
118 $samplesFile = new Translate\IO\CsvFile($this->samplesFilePath);
119 $this->configureExportCsvFile($samplesFile);
120 $samplesFile->openWrite( Main\IO\FileStreamOpenMode::APPEND);
121 }
122
123 $phraseFilter = $this->processFilter($path);
124 if (!empty($this->seekPathId))
125 {
126 $phraseFilter['>PATH_ID'] = $this->seekPathId;
127 }
128
129 $select = ['PATH_ID', 'PHRASE_CODE', 'FILE_PATH'];
130
131 foreach ($this->languages as $langId)
132 {
133 $select[] = \mb_strtoupper($langId)."_LANG";
134 }
135 if (!\in_array($this->filter['LANGUAGE_ID'], $this->languages))
136 {
137 $select[] = \mb_strtoupper($this->filter['LANGUAGE_ID'])."_LANG";
138 }
139
140 $currentLangId = Loc::getCurrentLang();
141
143 $phraseInxRes = Index\PhraseIndexSearch::getList([
144 'filter' => $phraseFilter,
145 'order' => ['PATH_ID' => 'ASC'],
146 'select' => $select,
147 ]);
148
149 $processedItemCount = 0;
150
151 $prevPathId = -1;
152 $fileInxCache = [];
153 while ($phraseInx = $phraseInxRes->fetch())
154 {
155 if ($this->instanceTimer()->hasTimeLimitReached())
156 {
157 if ($prevPathId != (int)$phraseInx['PATH_ID'])
158 {
159 $this->seekPathId = $prevPathId;
160 break;
161 }
162 }
163
164 $pathId = (int)$phraseInx['PATH_ID'];
165 $phraseCode = $phraseInx['PHRASE_CODE'];
166
167 if (!isset($fileInxCache[$pathId]))
168 {
169 $fullPaths = $this->getFullPath($pathId);
170
171 $fileInxCache[$pathId] = $this->mergeLangFiles($phraseInx['FILE_PATH'], $fullPaths, $this->collectUntranslated);
172 }
173
174 if (isset($fileInxCache[$pathId][$phraseCode]))
175 {
176 $row = &$fileInxCache[$pathId][$phraseCode];
177 $csvFile->put(\array_values($row));
178
179 if (
180 $this->appendSamples
181 && !empty($row[$currentLangId])
182 && mb_strlen($row[$currentLangId]) < $this->maxSampleSourceLength
183 )
184 {
185 $samples = $this->findSamples(
186 $row[$currentLangId],
187 $currentLangId,
188 $pathId,
189 $this->samplesCount,
190 $this->samplesRestriction
191 );
192 foreach ($samples as $sample)
193 {
194 $samplesFile->put(\array_values($sample));
195 $this->exportedSamplesCount ++;
196 }
197 }
198
199 $this->exportedPhraseCount ++;
200 }
201
202 $processedItemCount ++;
203
204 $prevPathId = $pathId;
205 }
206
207 $this->exportFileSize = $csvFile->getSize();
208 $csvFile->close();
209
210 if ($this->appendSamples)
211 {
212 $this->samplesFileSize = $samplesFile->getSize();
213 $samplesFile->close();
214 }
215
216 $this->processedItems += $processedItemCount;
217
218 if ($this->instanceTimer()->hasTimeLimitReached() !== true)
219 {
220 $this->declareAccomplishment();
222 }
223
224 return [
225 'PROCESSED_ITEMS' => $this->processedItems,
226 'TOTAL_ITEMS' => $this->totalItems,
227 'TOTAL_PHRASES' => $this->exportedPhraseCount,
228 'TOTAL_SAMPLES' => $this->exportedSamplesCount,
229 ];
230 }
231
232
240 private function processFilter(string $path): array
241 {
242 $filterOut = [];
243
244 if ($this->filter->count() > 0)
245 {
246 foreach ($this->filter as $key => $value)
247 {
248 if (!\in_array($key, ['FILTER_ID', 'PRESET_ID', 'FILTER_APPLIED', 'FIND', 'tabId']))
249 {
250 $filterOut[$key] = $value;
251 }
252 }
253 }
254
255 $filterOut['PATH'] = Translate\IO\Path::replaceLangId($path, '#LANG_ID#');
256
257 if (!empty($this->pathList))
258 {
259 $filterOut['INCLUDE_PATHS'] = $this->pathList;
260 }
261
262 return $filterOut;
263 }
264}
static loadLanguageFile($file, $language=null, $normalize=true)
Definition loc.php:224
configureExportCsvFile(Translate\IO\CsvFile $csvFile)
generateExportFileName(string $path, array $languages)
mergeLangFiles(string $langFilePath, array $fullLangFilePaths, bool $collectUntranslated=false, array $filterByCodeList=[])
__construct($name, Main\Engine\Controller $controller, array $config=[])
performStep($action, array $params=[])
Definition stepper.php:75
declareAccomplishment(bool $flag=true)
Definition stepper.php:136