Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
pathlangcollection.php
1<?php
2
4
9
10
15 extends Index\Internals\EO_PathLang_Collection
16{
20 static $verbose = false;
21
23 private static $documentRoot;
24
26 private static $enabledLanguages;
27
29 private static $useTranslationRepository;
31 private static $translationRepositoryLanguages;
33 private static $translationEnabledLanguages;
35 private static $translationRepositoryRoot;
36
42 private static function configure()
43 {
44 self::$documentRoot = \rtrim(Translate\IO\Path::tidy(Main\Application::getDocumentRoot()), '/');
45
46 self::$enabledLanguages = Translate\Config::getEnabledLanguages();
47
48 self::$useTranslationRepository = Localization\Translation::useTranslationRepository();
49 if (self::$useTranslationRepository)
50 {
51 self::$translationRepositoryLanguages = Translate\Config::getTranslationRepositoryLanguages();
52 self::$translationRepositoryRoot = \rtrim(Localization\Translation::getTranslationRepositoryPath(), '/');
53
54 // only active languages
55 self::$translationEnabledLanguages = \array_intersect(self::$translationRepositoryLanguages, self::$enabledLanguages);
56 }
57 }
58
66 public function countItemsToProcess(?Translate\Filter $filter = null): int
67 {
68 $relPath = isset($filter, $filter->path) ? $filter->path : '';
69
70 if (!empty($relPath))
71 {
72 $relPath = '/'. \trim($relPath, '/');
73 $totalItems = (int)Index\Internals\PathLangTable::getCount(['=%PATH' => $relPath .'%']);
74 }
75 else
76 {
77 $totalItems = (int)Index\Internals\PathLangTable::getCount();
78 }
79
80 return $totalItems;
81 }
82
92 public function collect(?Translate\Filter $filter = null, ?Translate\Controller\ITimeLimit $timer = null, ?Translate\Filter $seek = null): int
93 {
94 self::configure();
95
96 if (isset($filter, $filter->path))
97 {
98 $relPath = $filter->path;
99 }
100 else
101 {
102 $relPath = Translate\Config::getDefaultPath();
103 }
104 $relPath = '/'. \trim($relPath, '/');
105
106 // If it is lang folder, do nothing
107 if (\basename($relPath) == 'lang')
108 {
109 Index\Internals\PathLangTable::add(['PATH' => $relPath]);
110
111 return 1;
112 }
113
114 $seekAncestors = [];
115 if (isset($seek, $seek->path))
116 {
117 $seekAncestors = \explode('/', \trim($seek->path, '/'));
118 $seek->lookForSeek = true;
119 }
120
121 $checkLanguages = [];
122 if (self::$useTranslationRepository)
123 {
124 $checkLanguages = self::$translationEnabledLanguages;
125 if (isset($filter, $filter->langId))
126 {
127 $checkLanguages = \array_intersect($filter->langId, $checkLanguages);
128 }
129 }
130
131 $pathDepthLevel = \count(\explode('/', \trim($relPath, '/'))) - 1;
132
133 $cache = [];
134 $processedItemCount = 0;
135
142 $lookForLangDirectory = function ($startRoot, $relPath, $depthLevel, $isTop = false)
143 use (&$lookForLangDirectory, &$cache, $checkLanguages, &$seek, $seekAncestors, &$processedItemCount)
144 {
145 $childrenList = [];
146
147 $mergeChildrenList = function($childrenList0, $langId = '') use (&$childrenList)
148 {
149 foreach ($childrenList0 as $childPath)
150 {
151 $name = \basename($childPath);
152 if (\in_array($name, Translate\IGNORE_FS_NAMES))
153 {
154 continue;
155 }
156 if (isset($childrenList[$name]))
157 {
158 continue;
159 }
160
161 if (self::$useTranslationRepository && $langId != '')
162 {
163 $childPath = \str_replace(
164 self::$translationRepositoryRoot. '/'. $langId,
165 self::$documentRoot. '/bitrix/modules',
166 $childPath
167 );
168 }
169
170 $childrenList[$name] = $childPath;
171 }
172 };
173
174 $mergeChildrenList(Translate\IO\FileSystemHelper::getFolderList($startRoot. $relPath));
175
176 if (self::$useTranslationRepository)
177 {
178 foreach ($checkLanguages as $langId)
179 {
180 $path1 = Localization\Translation::convertLangPath($startRoot. $relPath, $langId);
181 if ($path1 != $startRoot)
182 {
183 $mergeChildrenList(Translate\IO\FileSystemHelper::getFolderList($path1), $langId);
184 }
185 }
186 }
187
188 if (!empty($childrenList))
189 {
190 $ignoreDev = \implode('|', Translate\IGNORE_MODULE_NAMES);
191 foreach ($childrenList as $childPath)
192 {
193 $name = \basename($childPath);
194 $relChildPath = \str_replace($startRoot, '', $childPath);
195
196 if (\in_array($name, Translate\IGNORE_FS_NAMES))
197 {
198 continue;
199 }
200 if (\in_array($relChildPath, Translate\IGNORE_BX_NAMES))
201 {
202 continue;
203 }
204 // /bitrix/modules/[smth]/dev/
205 if (\preg_match("#/bitrix/modules/[^/]+/({$ignoreDev})$#", $relChildPath))
206 {
207 continue;
208 }
209
210 $isLang = ($name == 'lang');
211
212 if ($seek !== null && $seek->lookForSeek === true)
213 {
214 if ($seekAncestors[$depthLevel + 1] == $name)
215 {
216 if ($relChildPath == $seek->path)
217 {
218 if (self::$verbose)
219 {
220 echo "Seek folder: {$relChildPath}\n";
221 }
222 $seek->lookForSeek = false;// found
223 continue;
224 }
225
226 if (!$isLang)
227 {
228 foreach ($lookForLangDirectory($startRoot, $relChildPath, $depthLevel + 1) as $subChildPath)// go deeper
229 {
230 }
231 }
232 }
233
234 continue;
235 }
236
237 if ($isLang)
238 {
239 $cache[] = [
240 'PATH' => $relChildPath,
241 ];
242
243 if (\count($cache) >= 50)
244 {
245 Index\Internals\PathLangTable::bulkAdd($cache);
246 $processedItemCount += \count($cache);
247 $cache = [];
248 }
249 }
250 else
251 {
252 foreach ($lookForLangDirectory($startRoot, $relChildPath, $depthLevel + 1) as $subChildPath)// go deeper
253 {
254 yield $subChildPath;
255 }
256 }
257
258 if ($isLang)
259 {
260 yield $relChildPath;
261 }
262 }
263 }
264
265 if ($isTop && \count($cache) > 0)
266 {
267 Index\Internals\PathLangTable::bulkAdd($cache);
268 $processedItemCount += \count($cache);
269 $cache = [];
270 }
271 };
272
273 foreach ($lookForLangDirectory(self::$documentRoot, $relPath, $pathDepthLevel, true) as $langPath)
274 {
275 if (self::$verbose)
276 {
277 if (!$langPath instanceof \Generator)
278 {
279 echo "Lang folder: {$langPath}\n";
280 }
281 }
282 if ($timer !== null)
283 {
284 if ($timer->hasTimeLimitReached())
285 {
286 $seek->nextPath = $langPath;
287 break;
288 }
289 }
290 // check user abortion
291 if (\connection_status() !== \CONNECTION_NORMAL)
292 {
293 throw new Main\SystemException('Process has been broken course user aborted connection.');
294 }
295 }
296
297 if (\count($cache) > 0)
298 {
299 Index\Internals\PathLangTable::bulkAdd($cache);
300 $processedItemCount += \count($cache);
301 }
302
303 return $processedItemCount;
304 }
305
313 public function purge(?Translate\Filter $filter = null): self
314 {
315 Index\Internals\PathLangTable::purge($filter);
316
317 return $this;
318 }
319}
countItemsToProcess(?Translate\Filter $filter=null)
collect(?Translate\Filter $filter=null, ?Translate\Controller\ITimeLimit $timer=null, ?Translate\Filter $seek=null)