Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
indexcsv.php
1<?php
3
8
9
16{
17 use Translate\Controller\Stepper;
18 use Translate\Controller\ProcessParams;
19
21 private $seekLine;
22
24 private $seekPath;
25
27 protected static $documentRoot;
29 protected static $enabledLanguages;
30
32 private $tabId = 0;
33
35 private $csvFilePath;
36
38 private $csvFile;
39
41 private $languageList;
42
44 private $columnList;
45
46
54 public function __construct($name, Main\Engine\Controller $controller, array $config = [])
55 {
56 $fields = ['tabId', 'csvFilePath', 'seekLine', 'seekPath'];
57
58 $this->keepField($fields);
59
60 foreach ($fields as $key)
61 {
62 if (!empty($config[$key]))
63 {
64 $this->{$key} = $config[$key];
65 }
66 }
67
68 self::$documentRoot = rtrim(Translate\IO\Path::tidy(Main\Application::getDocumentRoot()), '/');
69
70 self::$enabledLanguages = Translate\Config::getEnabledLanguages();
71
72 parent::__construct($name, $controller, $config);
73 }
74
82 public function run($runBefore = false)
83 {
84 if ($runBefore)
85 {
86 $this->onBeforeRun();
87 }
88
89 $this->csvFile = new Translate\IO\CsvFile($this->csvFilePath);
90
91 $this->csvFile
92 ->setFieldsType(Translate\IO\CsvFile::FIELDS_TYPE_WITH_DELIMITER)
93 ->setFirstHeader(false)
94 ;
95
96 if (!$this->csvFile->openLoad())
97 {
98 $this->addError(new Main\Error(Loc::getMessage('TR_IMPORT_EMPTY_FILE_ERROR')));
99
100 return [
101 'STATUS' => Translate\Controller\STATUS_COMPLETED
102 ];
103 }
104 if (!$this->verifyCsvFile())
105 {
106 $this->addError(new Main\Error(Loc::getMessage('TR_IMPORT_FILE_ERROR')));
107 return [
108 'STATUS' => Translate\Controller\STATUS_COMPLETED
109 ];
110 }
111
112 if ($this->isNewProcess)
113 {
115
116 $this->totalItems = 0;
117 $uniquePaths = [];
118 $fileColumn = $this->columnList['file'];
119 while ($csvRow = $this->csvFile->fetch())
120 {
121 $filePath = (isset($csvRow[$fileColumn]) ? $csvRow[$fileColumn] : '');
122 if ($filePath == '')
123 {
124 continue;
125 }
126 if (isset($uniquePaths[$filePath]))
127 {
128 continue;
129 }
130 $uniquePaths[$filePath] = 1;
131 $this->totalItems ++;
132 }
133 $this->csvFile->moveFirst();
134
135 $this->processedItems = 0;
136 $this->seekLine = 0;
137
138 $this->saveProgressParameters();
139
140 $this->isNewProcess = false;
141 }
142 else
143 {
144 $progressParams = $this->getProgressParameters();
145
146 if (isset($progressParams['totalItems']))
147 {
148 $this->totalItems = $progressParams['totalItems'];
149 }
150 if (isset($progressParams['seekLine']))
151 {
152 $this->seekLine = $progressParams['seekLine'];
153 }
154 if (isset($progressParams['seekPath']))
155 {
156 $this->seekPath = $progressParams['seekPath'];
157 }
158 }
159
160 return $this->performStep('runIndexing');
161 }
162
168 private function runIndexing(): array
169 {
170 $fileColumn = $this->columnList['file'];
171
172 $uniquePaths = [];
173
174 if (!empty($this->seekPath))
175 {
176 $uniquePaths[$this->seekPath] = 1;
177 }
178
179 $pathIndexer = new Index\PathIndexCollection();
180
181 $currentLine = 0;
182 while ($csvRow = $this->csvFile->fetch())
183 {
184 $currentLine ++;
185
186 if ($this->seekLine > 0)
187 {
188 if ($currentLine <= $this->seekLine)
189 {
190 continue;
191 }
192 }
193
194 if (
195 !\is_array($csvRow) ||
196 empty($csvRow) ||
197 (\count($csvRow) == 1 && ($csvRow[0] === null || $csvRow[0] === ''))
198 )
199 {
200 continue;
201 }
202
203 $filePath = (isset($csvRow[$fileColumn]) ? $csvRow[$fileColumn] : '');
204 if ($filePath == '')
205 {
206 continue;
207 }
208 if (Translate\IO\Path::isLangDir($filePath, true) !== true)
209 {
210 continue;
211 }
212 $filePath = Translate\IO\Path::normalize('/'.$filePath);
213
214 if (isset($uniquePaths[$filePath]))
215 {
216 continue;
217 }
218
219 foreach ($this->languageList as $languageId)
220 {
221 $langFilePath = Translate\IO\Path::replaceLangId($filePath, $languageId);
222
223 $fullPath = self::$documentRoot. $langFilePath;
224 $fullPath = Main\Localization\Translation::convertLangPath($fullPath, $languageId);
225
226 $langFile = new Translate\File($fullPath);
227 $langFile->setLangId($languageId);
228
229 if (!$langFile->load())
230 {
231 $this->addErrors($langFile->getErrors());
232 continue;
233 }
234
235 if ($langFile->getFileIndex()->getId() <= 0)
236 {
237 $topPath = $pathIndexer->constructAncestorsByPath($filePath);
238
239 if ($topPath['ID'] > 0)
240 {
241 $fileIndex = $langFile->getFileIndex();
242 $fileIndex->setPathId($topPath['ID']);
243 $fileIndex->save();
244 }
245 }
246 $langFile->updatePhraseIndex();
247 }
248
249 $uniquePaths[$filePath] = 1;
250 $this->processedItems ++;
251
252 if ($this->instanceTimer()->hasTimeLimitReached())
253 {
254 $this->seekLine = $currentLine;
255 $this->seekPath = $filePath;
256 break;
257 }
258 }
259
260 $this->csvFile->close();
261
262 if ($this->instanceTimer()->hasTimeLimitReached() !== true)
263 {
264 $this->declareAccomplishment();
266 }
267
268 return [
269 'PROCESSED_ITEMS' => $this->processedItems,
270 'TOTAL_ITEMS' => $this->totalItems,
271 ];
272 }
273
274
280 private function verifyCsvFile(): bool
281 {
282 $testDelimiters = [
283 Translate\IO\CsvFile::DELIMITER_TZP,
284 Translate\IO\CsvFile::DELIMITER_TAB,
285 Translate\IO\CsvFile::DELIMITER_ZPT,
286 ];
287 foreach ($testDelimiters as $delimiter)
288 {
289 $this->csvFile->setFieldDelimiter($delimiter);
290
291 $this->csvFile->moveFirst();
292 $rowHead = $this->csvFile->fetch();
293 if (
294 !\is_array($rowHead) ||
295 empty($rowHead) ||
296 empty($rowHead[0]) ||
297 (\count($rowHead) < 3)
298 )
299 {
300 continue;
301 }
302
303 break;
304 }
305
306 if (
307 !\is_array($rowHead) ||
308 empty($rowHead) ||
309 empty($rowHead[0]) ||
310 (\count($rowHead) < 3)
311 )
312 {
313 return false;
314 }
315
316 $this->languageList = self::$enabledLanguages;
317 $this->columnList = \array_flip($rowHead);
318 foreach ($this->languageList as $keyLang => $langID)
319 {
320 if (!isset($this->columnList[$langID]))
321 {
322 unset($this->languageList[$keyLang]);
323 }
324 }
325 if (!isset($this->columnList['file']))
326 {
327 return false;
328 }
329
330 return true;
331 }
332}
addError(Error $error)
Definition action.php:200
addErrors(array $errors)
Definition action.php:213
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
__construct($name, Main\Engine\Controller $controller, array $config=[])
Definition indexcsv.php:54
performStep($action, array $params=[])
Definition stepper.php:75
declareAccomplishment(bool $flag=true)
Definition stepper.php:136