Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
fileindexcollection.php
1<?php
2
4
8
9
14 extends Index\Internals\EO_FileIndex_Collection
15{
17 static $verbose = false;
18
20 private static $documentRoot;
21
23 private static $enabledLanguages;
24
26 private $checkLanguages = [];
27
28
34 private static function configure()
35 {
36 self::$documentRoot = rtrim(Translate\IO\Path::tidy(Main\Application::getDocumentRoot()), '/');
37
38 self::$enabledLanguages = Translate\Config::getEnabledLanguages();
39 }
40
41
49 public function countItemsToProcess(?Translate\Filter $filter = null): int
50 {
51 if (isset($filter, $filter->path))
52 {
53 $relPath = '/'. \trim($filter->path, '/');
54 $relPath = Translate\IO\Path::replaceLangId($relPath, '#LANG_ID#');
55
56 $topPathRes = Index\Internals\PathIndexTable::getList([
57 'select' => ['ID'],
58 'filter' => ['=PATH' => $relPath]
59 ]);
60 if (!($topPath = $topPathRes->fetch()))
61 {
62 return 0;
63 }
64
65 $pathFilter = [
66 '=IS_DIR' => 'N',
67 '=IS_LANG' => 'Y',
68 '=%PATH' => $relPath.'%#LANG_ID#%',
69 '=DESCENDANTS.PARENT_ID' => $topPath['ID'],//ancestor
70 //todo: add filter by INDEXED_TIME
71 ];
72 $totalItems = (int)Index\Internals\PathIndexTable::getCount($pathFilter);
73 }
74 else
75 {
76 $totalItems = (int)Index\Internals\PathIndexTable::getCount();
77 }
78
79 return $totalItems;
80 }
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
105 $relPath = '/'. \trim($relPath, '/');
106 $relPath = Translate\IO\Path::replaceLangId($relPath, '#LANG_ID#');
107
108 $this->checkLanguages = self::$enabledLanguages;
109 if (isset($filter, $filter->langId))
110 {
111 $this->checkLanguages = \array_intersect($filter->langId, $this->checkLanguages);
112 }
113
114
115 $topPathRes = Index\Internals\PathIndexTable::getList([
116 'select' => ['ID'],
117 'filter' => ['=PATH' => $relPath]
118 ]);
119 if (!($topPath = $topPathRes->fetch()))
120 {
121 return 0;
122 }
123
124 $pathFilter = [
125 '=IS_DIR' => 'N',
126 '=IS_LANG' => 'Y',
127 '=%PATH' => $relPath.'%#LANG_ID#%',
128 '=DESCENDANTS.PARENT_ID' => $topPath['ID'],//ancestor
129 //todo: add filter by INDEXED_TIME
130 ];
131 if (isset($seek, $seek->pathId))
132 {
133 $pathFilter['>ID'] = $seek->pathId;
134 }
135
136 // path list
137 $pathListRes = Index\Internals\PathIndexTable::getList([
138 'select' => ['ID', 'PATH'],
139 'filter' => $pathFilter,
140 'order' => ['ID' => 'ASC'],
141 //todo: add limit here
142 ]);
143
144 $processedItemCount = 0;
145
146 while (true)
147 {
148 $lastPathId = null;
149 $pathPortion = [];
150 $pathIdPortion = [];
151 while ($pathRow = $pathListRes->fetch())
152 {
153 $pathIdPortion[] = $lastPathId = (int)$pathRow['ID'];
154 $pathPortion[$lastPathId] = $pathRow['PATH'];
155 if (\count($pathIdPortion) >= 100)
156 {
157 break;
158 }
159 }
160 if (empty($pathIdPortion))
161 {
162 break;
163 }
164
165 $indexFileCacheRes = Index\Internals\FileIndexTable::getList([
166 'select' => ['ID', 'PATH_ID', 'LANG_ID'],
167 'filter' => [
168 '=PATH_ID' => $pathIdPortion,
169 '=LANG_ID' => $this->checkLanguages,
170 ]
171 ]);
172 $indexFileCache = [];
173 while ($indexFile = $indexFileCacheRes->fetch())
174 {
175 if (!isset($indexFileCache[(int)$indexFile['PATH_ID']]))
176 {
177 $indexFileCache[(int)$indexFile['PATH_ID']] = [];
178 }
179 $indexFileCache[(int)$indexFile['PATH_ID']][$indexFile['LANG_ID']] = (int)$indexFile['ID'];
180 }
181 unset($indexFileCacheRes, $indexFile);
182
183 $nonexistentFiles = [];
184 $fileData = [];
185
186 foreach ($pathPortion as $pathId => $path)
187 {
188 foreach ($this->checkLanguages as $langId)
189 {
190 $fullPath = self::$documentRoot. \str_replace('#LANG_ID#', $langId, $path);
191 $fullPath = Main\Localization\Translation::convertLangPath($fullPath, $langId);
192
193 if (self::$verbose)
194 {
195 echo "Lang file: {$fullPath}\n";
196 }
197
198 if (!\file_exists($fullPath))
199 {
200 if (isset($indexFileCache[$pathId][$langId]))
201 {
202 // remove file from index
203 $nonexistentFiles[] = $indexFileCache[$pathId][$langId];
204 }
205 continue;
206 }
207
208 if (!isset($indexFileCache[$pathId][$langId]))
209 {
210 $fileData[] = [
211 'PATH_ID' => $pathId,
212 'LANG_ID' => $langId,
213 'FULL_PATH' => $fullPath,
214 ];
215 }
216 }
217 }
218 if (\count($fileData) > 0)
219 {
220 Index\Internals\FileIndexTable::bulkAdd($fileData);
221 }
222
223 if (\count($nonexistentFiles) > 0)
224 {
225 Index\Internals\FileIndexTable::purge(new Translate\Filter(['fileId' => $nonexistentFiles]));
226 }
227
228 $processedItemCount += \count($pathIdPortion);
229
230 if ($timer !== null && $timer->hasTimeLimitReached())
231 {
232 if ($seek !== null)
233 {
234 $seek->nextPathId = $lastPathId;
235 }
236 break;
237 }
238 }
239
240 return $processedItemCount;
241 }
242
243
251 public function purge(?Translate\Filter $filter = null): self
252 {
253 Index\Internals\FileIndexTable::purge($filter);
254
255 return $this;
256 }
257
265 public function unvalidate(?Translate\Filter $filter = null): self
266 {
267 $filterOut = Index\Internals\FileIndexTable::processFilter($filter);
268 Index\Internals\FileIndexTable::bulkUpdate(['INDEXED' => 'N'], $filterOut);
269
270 return $this;
271 }
272}
273
countItemsToProcess(?Translate\Filter $filter=null)
collect(?Translate\Filter $filter=null, ?Translate\Controller\ITimeLimit $timer=null, ?Translate\Filter $seek=null)