Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
fileindexsearch.php
1<?php
2
4
8
10{
17 public static function query($params = [])
18 {
19 [$select, $runtime, $filter] = self::processParams($params);
20
22 $entity = Index\Internals\PathIndexTable::getEntity();
23 foreach ($runtime as $field)
24 {
25 $entity->addField($field);
26 }
27
28 return new Main\ORM\Query\Query($entity);
29 }
30
31
38 public static function getCount($filterIn): int
39 {
40 [$select, $runtime, $filter] = self::processParams(['filter' => $filterIn]);
41
43 $entity = Index\Internals\PathIndexTable::getEntity();
44 foreach ($runtime as $field)
45 {
46 $entity->addField($field);
47 }
48
49 $query = new Main\ORM\Query\Query($entity);
50
51 $query
52 ->addSelect(new Main\ORM\Fields\ExpressionField('CNT', 'COUNT(1)'))
53 ->setFilter($filter);
54
55 $result = $query->exec()->fetch();
56
57 return (int)$result['CNT'];
58 }
59
60
67 public static function getList($params): Main\ORM\Query\Result
68 {
69 [$select, $runtime, $filter] = self::processParams($params);
70
71 $executeParams = [
72 'select' => \array_merge(
73 [
74 'PATH_ID' => 'ID',
75 'PATH' => 'PATH',
76 'IS_LANG' => 'IS_LANG',
77 'IS_DIR' => 'IS_DIR',
78 'TITLE' => 'NAME',
79 ],
80 $select
81 ),
82 'runtime' => $runtime,
83 'filter' => $filter,
84 ];
85
86 if (isset($params['order']))
87 {
88 $executeParams['order'] = $params['order'];
89 }
90 if (isset($params['offset']))
91 {
92 $executeParams['offset'] = $params['offset'];
93 }
94 if (isset($params['limit']))
95 {
96 $executeParams['limit'] = $params['limit'];
97 }
98 if (isset($params['count_total']))
99 {
100 $executeParams['count_total'] = true;
101 }
102
103 return Index\Internals\PathIndexTable::getList($executeParams);
104 }
105
106
113 private static function processParams($params): array
114 {
115 $select = $runtime = $filterIn = $filterOut = [];
116 if (isset($params['filter']))
117 {
118 if (\is_object($params['filter']))
119 {
120 $filterIn = clone $params['filter'];
121 }
122 else
123 {
124 $filterIn = $params['filter'];
125 }
126 }
127
128 $enabledLanguages = Translate\Config::getEnabledLanguages();
129 $languageUpperKeys = \array_combine($enabledLanguages, \array_map('mb_strtoupper', $enabledLanguages));
130
131 $selectedLanguages = [];
132 foreach ($languageUpperKeys as $langId => $langUpper)
133 {
134 $alias = "{$langUpper}_LANG";
135 if (isset($params['select']) && \in_array($alias, $params['select']))
136 {
137 $selectedLanguages[] = $langId;
138 }
139 elseif (isset($params['order'], $params['order'][$alias]))
140 {
141 $selectedLanguages[] = $langId;
142 }
143 elseif (isset($filterIn['LANGUAGE_ID']) && $filterIn['LANGUAGE_ID'] == $langId)
144 {
145 $selectedLanguages[] = $langId;
146 }
147 }
148 if (empty($selectedLanguages))
149 {
150 $selectedLanguages = $enabledLanguages;
151 }
152
153 // top folder
154 if (!empty($filterIn['PATH']))
155 {
156 $topIndexPath = Index\PathIndex::loadByPath($filterIn['PATH']);
157 if ($topIndexPath instanceof Index\PathIndex)
158 {
159 $filterOut['=DESCENDANTS.PARENT_ID'] = $topIndexPath->getId();//ancestor
160 }
161 }
162 unset($filterIn['PATH']);
163
164 foreach ($languageUpperKeys as $langId => $langUpper)
165 {
166 if (
167 !\in_array($langId, $selectedLanguages) &&
168 !Main\Localization\Translation::isDefaultTranslationLang($langId)
169 )
170 {
171 continue;
172 }
173
174 $alias = "{$langUpper}_LANG";
175 $tblAlias = "File{$alias}";
176
177 $searchByLang = false;
178 if (!empty($filterIn['LANGUAGE_ID']) && $langId == $filterIn['LANGUAGE_ID'])
179 {
180 $searchByLang = true;
181 }
182
183 $runtime[] = new Main\ORM\Fields\Relations\Reference(
184 $tblAlias,
185 Index\Internals\FileIndexTable::class,
186 Main\ORM\Query\Join::on('ref.PATH_ID', '=', 'this.ID')->where('ref.LANG_ID', '=', $langId),
187 ['join_type' => $searchByLang ? 'INNER' : 'LEFT']
188 );
189
190 $select[$alias] = "{$tblAlias}.PHRASE_COUNT";
191 }
192 unset($filterIn['LANGUAGE_ID']);
193
194 // is any file exists in main rep
195 if (Main\Localization\Translation::useTranslationRepository())
196 {
197 $statement = '';
198 $fields = [];
199 foreach ($languageUpperKeys as $langId => $langUpper)
200 {
201 if (Main\Localization\Translation::isDefaultTranslationLang($langId))
202 {
203 $alias = "{$langUpper}_LANG";
204 $fields[] = "File{$alias}.ID";
205 $statement .= ' WHEN %s IS NOT NULL THEN 1 ';
206 }
207 }
208 unset($langId, $langUpper, $alias, $tblAlias, $fieldAlias);
209
210 $runtime[] =
211 new Main\ORM\Fields\ExpressionField(
212 'IS_EXIST',
213 "CASE {$statement} ELSE 0 END",
214 $fields
215 );
216 $select[] = 'IS_EXIST';
217 unset($statement, $fields);
218 }
219
220 // folder name
221 /*
222 if (!empty($filterIn['FOLDER_NAME']))
223 {
224 $filterOut['!=NAME'] = '#LANG_ID#';
225 if (empty($filterIn['FILE_NAME']))
226 {
227 $filterOut['%NAME'] = $filterIn['FOLDER_NAME'];
228 $filterOut['=IS_DIR'] = 'Y';
229 }
230 else
231 {
232 $filterOut['%=PATH'] = '%/'. $filterIn['FOLDER_NAME']. '/%';
233 }
234 }
235 */
236
237 $filterOut['=IS_DIR'] = 'N';
238 if (!empty($filterIn['FOLDER_NAME']))
239 {
240 $filterOut['%=PATH'] = '%/'. $filterIn['FOLDER_NAME']. '/%';
241 }
242
243 // only files
244 if (!empty($filterIn['FILE_NAME']))
245 {
246 $filterOut['%NAME'] = $filterIn['FILE_NAME'];
247 }
248 unset($filterIn['FILE_NAME'], $filterIn['FOLDER_NAME']);
249
250 $replaceLangId = static function(&$val)
251 {
252 $val = Translate\IO\Path::replaceLangId($val, '#LANG_ID#');
253 };
254 $trimSlash = static function(&$val)
255 {
256 if (Translate\IO\Path::isPhpFile($val))
257 {
258 $val = '/'. \trim($val, '/');
259 }
260 else
261 {
262 $val = '/'. \trim($val, '/'). '/%';
263 }
264 };
265
266 if (!empty($filterIn['INCLUDE_PATHS']))
267 {
268 $pathIncludes = \preg_split("/[\r\n\t,; ]+/".\BX_UTF_PCRE_MODIFIER, $filterIn['INCLUDE_PATHS']);
269 $pathIncludes = \array_filter($pathIncludes);
270 if (\count($pathIncludes) > 0)
271 {
272 $pathPathIncludes = [];
273 $pathNameIncludes = [];
274 foreach ($pathIncludes as $testPath)
275 {
276 if (!empty($testPath) && \trim($testPath) !== '')
277 {
278 if (\mb_strpos($testPath, '/') === false)
279 {
280 $pathNameIncludes[] = $testPath;
281 }
282 else
283 {
284 $pathPathIncludes[] = $testPath;
285 }
286 }
287 }
288 if (\count($pathNameIncludes) > 0 && \count($pathPathIncludes) > 0)
289 {
290 \array_walk($pathNameIncludes, $replaceLangId);
291 \array_walk($pathPathIncludes, $replaceLangId);
292 \array_walk($pathPathIncludes, $trimSlash);
293 $filterOut[] = [
294 'LOGIC' => 'OR',
295 '%=NAME' => $pathNameIncludes,
296 '%=PATH' => $pathPathIncludes,
297 ];
298 }
299 elseif (\count($pathNameIncludes) > 0)
300 {
301 \array_walk($pathNameIncludes, $replaceLangId);
302 $filterOut[] = [
303 'LOGIC' => 'OR',
304 '%=NAME' => $pathNameIncludes,
305 '%=PATH' => $pathNameIncludes,
306 ];
307 }
308 elseif (\count($pathPathIncludes) > 0)
309 {
310 \array_walk($pathPathIncludes, $replaceLangId);
311 \array_walk($pathPathIncludes, $trimSlash);
312 $filterOut['%=PATH'] = $pathPathIncludes;
313 }
314 }
315 unset($testPath, $pathIncludes, $pathNameIncludes, $pathPathIncludes);
316 }
317 if (!empty($filterIn['EXCLUDE_PATHS']))
318 {
319 $pathExcludes = \preg_split("/[\r\n\t,; ]+/".\BX_UTF_PCRE_MODIFIER, $filterIn['EXCLUDE_PATHS']);
320 $pathExcludes = \array_filter($pathExcludes);
321 if (\count($pathExcludes) > 0)
322 {
323 $pathPathExcludes = [];
324 $pathNameExcludes = [];
325 foreach ($pathExcludes as $testPath)
326 {
327 if (!empty($testPath) && \trim($testPath) !== '')
328 {
329 if (\mb_strpos($testPath, '/') === false)
330 {
331 $pathNameExcludes[] = $testPath;
332 }
333 else
334 {
335 $pathPathExcludes[] = $testPath;
336 }
337 }
338 }
339 if (\count($pathNameExcludes) > 0 && \count($pathPathExcludes) > 0)
340 {
341 \array_walk($pathNameExcludes, $replaceLangId);
342 \array_walk($pathPathExcludes, $replaceLangId);
343 \array_walk($pathPathExcludes, $trimSlash);
344 $filterOut[] = [
345 'LOGIC' => 'AND',
346 '!=%NAME' => $pathNameExcludes,
347 '!=%PATH' => $pathPathExcludes,
348 ];
349 }
350 elseif (\count($pathNameExcludes) > 0)
351 {
352 \array_walk($pathNameExcludes, $replaceLangId);
353 $filterOut[] = [
354 'LOGIC' => 'AND',
355 '!=%NAME' => $pathNameExcludes,
356 '!=%PATH' => $pathNameExcludes,
357 ];
358 }
359 elseif (\count($pathPathExcludes) > 0)
360 {
361 \array_walk($pathPathExcludes, $replaceLangId);
362 \array_walk($pathPathExcludes, $trimSlash);
363 $filterOut["!=%PATH"] = $pathPathExcludes;
364 }
365 }
366 unset($testPath, $pathExcludes, $pathPathExcludes, $pathNameExcludes);
367 }
368 unset($filterIn['INCLUDE_PATHS'], $filterIn['EXCLUDE_PATHS']);
369
370 /*
371 todo: Revert type assignment
372
373 if (!empty($filterIn['ASSIGNMENT']))
374 {
375 $filterOut['=ASSIGNMENT'] = $filterIn['ASSIGNMENT'];
376 }
377 */
378
379 /*
380 todo: Revert module assignment
381
382 if (!empty($filterIn['MODULE_ID']))
383 {
384 $filterOut['=MODULE_ID'] = $filterIn['MODULE_ID'];
385 }
386 */
387
388 foreach ($filterIn as $key => $value)
389 {
390 if (\in_array($key, ['tabId', 'FILTER_ID', 'PRESET_ID', 'FILTER_APPLIED', 'FIND']))
391 {
392 continue;
393 }
394 $filterOut[$key] = $value;
395 }
396
397 return [$select, $runtime, $filterOut];
398 }
399}