Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
collectlangpath.php
1<?php
3
7
8
15{
16 use Translate\Controller\Stepper;
17 use Translate\Controller\ProcessParams;
18
20 private $seekPath = '';
21
23 private $seekOffset = 0;
24
26 private $pathList = [];
27
29 private $languages = [];
30
31
39 public function __construct($name, Main\Engine\Controller $controller, array $config = [])
40 {
41 $this->keepField(['seekPath', 'pathList', 'seekOffset', 'languages']);
42
43 parent::__construct($name, $controller, $config);
44 }
45
53 public function run($path = '', $runBefore = false)
54 {
55 if ($runBefore)
56 {
57 $this->onBeforeRun();
58 }
59
60 if (empty($path))
61 {
62 $path = Translate\Config::getDefaultPath();
63 }
64
65 $path = '/'. \trim($path, '/.\\');
66
67 // List of files and folders
68 if ($this->isNewProcess)
69 {
70 $pathList = $this->controller->getRequest()->get('pathList');
71
72 if (!empty($pathList))
73 {
74 $pathList = \preg_split("/[\r\n]+/", $pathList);
75 \array_walk($pathList, 'trim');
76 $pathList = \array_unique(\array_filter($pathList));
77 }
78
79 if (empty($pathList))
80 {
81 $pathList = [$path];
82 }
83
84 $checkIndexExists = $this->controller->getRequest()->get('checkIndexExists') === 'Y';
85
86 foreach ($pathList as $testPath)
87 {
88 if ($checkIndexExists)
89 {
90 $indexPath = Index\PathIndex::loadByPath($testPath);
91 if ($indexPath instanceof Index\PathIndex)
92 {
93 if ($indexPath->getIndexed())
94 {
95 continue;// skip indexing if index exists
96 }
97 }
98 }
99
100 if (Translate\IO\Path::isPhpFile($testPath))
101 {
102 if (!Translate\IO\Path::isLangDir($testPath))
103 {
104 continue;// skip non lang files
105 }
106 }
107
108 $this->pathList[] = $testPath;
109 }
110
111 if (empty($this->pathList))
112 {
113 return [
114 'STATUS' => Translate\Controller\STATUS_COMPLETED,
115 ];
116 }
117
118 $languages = $this->controller->getRequest()->get('languages');
119 if (\is_array($languages) && !\in_array('all', $languages))
120 {
121 $languages = \array_intersect($languages, Translate\Config::getEnabledLanguages());
122 if (!empty($languages))
123 {
124 $this->languages = $languages;
125 }
126 }
127
128 $this->isNewProcess = false;
129 $this->instanceTimer()->setTimeLimit(5);
130 }
131
132 return $this->performStep('runIndexing');
133 }
134
140 private function runIndexing(): array
141 {
142 $indexer = new Index\PathLangCollection();
143
144 $processedItemCount = 0;
145
146 for ($pos = ((int)$this->seekOffset > 0 ? (int)$this->seekOffset : 0), $total = \count($this->pathList); $pos < $total; $pos ++)
147 {
148 $filter = new Translate\Filter();
149
150 if (!empty($this->languages))
151 {
152 $filter->langId = $this->languages;
153 }
154
155 $testPath = $this->pathList[$pos];
156 if (\preg_match("#(.+/lang)(/?\w*)#", $testPath, $matches))
157 {
158 $filter->path = $matches[1];
159 $indexer->purge($filter);
160
161 $processedItemCount += $indexer->collect($filter);//++1
162 }
163 else
164 {
165 $filter->path = $testPath;
166
167 $seek = new Translate\Filter();
168 $seek->lookForSeek = false;
169
170 if (!empty($this->seekPath))
171 {
172 $seek->path = $this->seekPath;
173 $seek->lookForSeek = true;
174 }
175 else
176 {
177 $indexer->purge($filter);
178 }
179
180 $processedItemCount += $indexer->collect($filter, $this->instanceTimer(), $seek);
181
182 if ($this->instanceTimer()->hasTimeLimitReached())
183 {
184 if (isset($seek->nextPath))
185 {
186 $this->seekPath = $seek->nextPath;
187 }
188 break;
189 }
190
191 $this->seekPath = null;
192 }
193
194 // check user abortion
195 if (\connection_status() !== \CONNECTION_NORMAL)
196 {
197 throw new Main\SystemException('Process has been broken course user aborted connection.');
198 }
199
200 if (isset($this->pathList[$pos + 1]))
201 {
202 $this->seekOffset = $pos + 1;//next
203 }
204 else
205 {
206 $this->seekOffset = null;
207 $this->declareAccomplishment();
209 }
210
211 if ($this->instanceTimer()->hasTimeLimitReached())
212 {
213 break;
214 }
215 }
216
217 $this->processedItems += $processedItemCount;
218 $this->totalItems += $processedItemCount;
219
220 if ($this->instanceTimer()->hasTimeLimitReached() !== true)
221 {
222 $this->processedItems = $this->totalItems = (new Index\PathIndexCollection())->countItemsToProcess($filter);
223
224 $this->declareAccomplishment();
226 }
227
228 return [
229 'PROCESSED_ITEMS' => $this->processedItems,
230 'TOTAL_ITEMS' => $this->totalItems,
231 ];
232 }
233}
__construct($name, Main\Engine\Controller $controller, array $config=[])
performStep($action, array $params=[])
Definition stepper.php:75
declareAccomplishment(bool $flag=true)
Definition stepper.php:136