Engine.class.php
12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
<?php
/**
* Created by PhpStorm.
* User: Администратор
* Date: 14.03.14
* Time: 13:47
*/
namespace Cpeople\Classes\Search;
class Engine
{
protected $defaultClassName = '\Cpeople\Classes\Search\Result';
protected $className = '\Cpeople\Classes\Search\Result';
protected $modulesList = array();
protected $tryInvertedLayout = false;
protected $tryYandexSpeller = false;
protected $query = '';
protected $minLenth = 0;
protected $minWordLength = 0;
protected $arNavStartParams = array('nPageSize' => 10, 'iNumPage' => 1);
protected $count = 0;
/**
* @return Engine
*/
static function instance()
{
return new self;
}
/**
* @param null $query
* @param null $offset
* @param null $limit
* @return \Cpeople\Classes\Search\Result[];
*/
public function makeSearch($query = null, $offset = null, $limit = null)
{
$retval = $this->search($query, $offset, $limit);
if(empty($retval) && $this->tryInvertedLayout)
{
$invertedQuery = $this->makeInvertedLayout($query);
$retval = $this->search($invertedQuery, $offset, $limit);
}
if(empty($retval) && $this->tryYandexSpeller)
{
$withoutMistakeQuery = $this->makeYandexSpeller($query);
if($query !== $withoutMistakeQuery)
{
$retval = $this->search($withoutMistakeQuery, $offset, $limit);
}
}
if(empty($retval) && $this->tryInvertedLayout && $this->tryYandexSpeller)
{
$withoutMistakeInvertedQuery = $this->makeYandexSpeller($invertedQuery);
if($query !== $withoutMistakeInvertedQuery)
{
$retval = $this->search($withoutMistakeInvertedQuery, $offset, $limit);
}
}
return $retval;
}
/**
* @param null $query
* @param null $offset
* @param null $limit
* @return \Cpeople\Classes\Search\Result[];
*/
public function search($query = null, $offset = null, $limit = null)
{
if($query !== null) $this->query = $query;
$retval = array();
$whereSQL = $this->makeSQLWhere($query);
$limit = intval($limit === null ? $this->arNavStartParams['nPageSize'] : $limit);
$offset = intval($offset === null ? ($this->arNavStartParams['iNumPage'] - 1) * $limit : $offset);
$sql = "
SELECT SQL_CALC_FOUND_ROWS *
FROM b_search_content bsc
LEFT JOIN b_iblock_site bis
ON bis.IBLOCK_ID = bsc.PARAM2 AND bis.SITE_ID = '" . SITE_ID . "'
$whereSQL
";
if ($limit)
{
$sql .= 'LIMIT ' . ($offset ? "$offset, $limit" : $limit);
}
$res = $this->makeQuery($sql);
while ($row = $res->Fetch())
{
$retval[] = new $this->className($row);
}
if (!empty($retval))
{
$res = $this->makeQuery('SELECT FOUND_ROWS()')->Fetch();
$this->count = (int) reset($res);
}
return $retval;
}
public function getCount()
{
return $this->count;
}
public function makeSQLWhere($query = null)
{
if($query === null) $query = $this->query;
$sqlReadyQuery = $this->prepareQuery($query);
$modulesSQL = $this->makeModulesSQL($this->modulesList);
$retval =
"WHERE
(
bsc.BODY LIKE '%$sqlReadyQuery%' OR bsc.TITLE LIKE '%$sqlReadyQuery%'
)
$modulesSQL
";
return $retval;
}
public function prepareQuery($query)
{
$query = trim($query);
$query = str_replace(preg_split('##', '!@#$%^&*()-+=#{}[]~`,./?<>', 0, PREG_SPLIT_NO_EMPTY), '%', $query);
$query = preg_replace('#%+#', '%', $query);
if (!empty($this->minWordLength))
{
$query = preg_replace('/\b[^\s]{1,' . ($this->minWordLength - 1) . '}\b/u', '', $query);
}
$query = preg_replace('#\s+#', '%', $query);
return $query;
}
public function setClassName($className)
{
$dummy = new $className;
if (!is_subclass_of($dummy, $this->defaultClassName))
{
throw new SearchException('Class ' . $className . ' is not subclass of ' . $this->defaultClassName);
}
$this->className = $className;
return $this;
}
public function addModule($module, $iblockType = null, $iblockId = null)
{
if (is_array($iblockId))
{
foreach ($iblockId as $id)
{
$this->modulesList[$module][$iblockType][$id] = true;
}
}
else
{
$this->modulesList[$module][$iblockType][$iblockId] = true;
}
return $this;
}
public function getFoundRows($query = null)
{
return $this->getCount();
/*
$retval = 0;
$whereSQL = $this->makeSQLWhere($query);
$sql = "
SELECT COUNT(*)
FROM b_search_content bsc
LEFT JOIN b_iblock_site bis
ON bis.IBLOCK_ID = bsc.PARAM2 AND bis.SITE_ID = '" . SITE_ID . "'
$whereSQL
";
$res = $this->makeQuery($sql);
if ($row = $res->Fetch())
{
$retval = $row['COUNT(*)'];
}
$this->total = $retval;
return $retval;*/
}
protected function makeQuery($sql)
{
global $DB;
return $DB->Query($sql, false, __LINE__);
}
protected function makeModulesSQL($modulesList)
{
$modulesSQL = '';
/** если есть модули */
if ($modulesList)
{
$modulesSQL .= " AND (";
}
foreach ($modulesList as $module => $iblockTypes)
{
if ($iblockTypes != reset($modulesList))
{
$modulesSQL .= " OR ";
}
/** запись модуля */
$modulesSQL .= "(bsc.MODULE_ID = '$module'";
/** если есть типы инфоблоков */
if (array_filter(array_keys($iblockTypes)))
{
$modulesSQL .= " AND (";
}
/** для модуля main фильтруем по URL */
if($module === 'main')
{
$urlList = array_keys($iblockTypes);
$urlList = array_filter($urlList);
if(!empty($urlList))
{
$modulesSQL .= "bsc.URL LIKE '" . implode("' OR bsc.URL LIKE '", $urlList) . "'";
}
}
else
{
foreach ($iblockTypes as $iblockType => $iblockIds)
{
if ($iblockIds != reset($iblockTypes))
{
$modulesSQL .= " OR ";
}
if ($iblockType)
{
/** запись типа инфоблока */
$modulesSQL .= "(bsc.PARAM1 = '$iblockType'";
if ($iblocks = implode(",", array_keys($iblockIds)))
{
/** запись инфоблоков */
$modulesSQL .= " AND bsc.PARAM2 IN ($iblocks)";
}
/** закрываем каждый тип инфоблока */
$modulesSQL .= ")";
} elseif ($iblocks = implode(",", array_keys($iblockIds)))
{
/** запись инфоблоков */
if (array_filter(array_keys($iblockTypes)))
{
$modulesSQL .= "bsc.PARAM2 IN ($iblocks)";
/** если у нас только инфоблоки без типов */
} else
{
$modulesSQL .= " AND bsc.PARAM2 IN ($iblocks)";
}
}
}
}
/** если есть типы инфоблоков */
if (array_filter(array_keys($iblockTypes)))
{
$modulesSQL .= ")";
}
/** закрываем каждый модуль */
$modulesSQL .= ")";
}
/** если есть модули */
if ($modulesList)
{
$modulesSQL .= ")";
}
return $modulesSQL;
}
public function setInvertedLayout($bVal)
{
$this->tryInvertedLayout = $bVal;
return $this;
}
protected function makeInvertedLayout($query)
{
if(!$this->tryInvertedLayout) return '';
$invertedQuery = strtr($query, array('q'=>'й','w'=>'ц','e'=>'у','r'=>'к','t'=>'е','y'=>'н','u'=>'г','i'=>'ш','o'=>'щ','p'=>'з','['=>'х',']'=>'ъ','a'=>'ф','s'=>'ы','d'=>'в','f'=>'а','g'=>'п','h'=>'р','j'=>'о','k'=>'л','l'=>'д',';'=>'ж','\''=>'э','z'=>'я','x'=>'ч','c'=>'с','v'=>'м','b'=>'и','n'=>'т','m'=>'ь',','=>'б','.'=>'ю','/'=>'.','Q'=>'Й','W'=>'Ц','E'=>'У','R'=>'К','T'=>'Е','Y'=>'Н','U'=>'Г','I'=>'Ш','O'=>'Щ','P'=>'З','{'=>'Х','}'=>'Ъ','A'=>'Ф','S'=>'Ы','D'=>'В','F'=>'А','G'=>'П','H'=>'Р','J'=>'О','K'=>'Л','L'=>'Д',':'=>'Ж','"'=>'Э','|'=>'/','Z'=>'Я','X'=>'Ч','C'=>'С','V'=>'М','B'=>'И','N'=>'Т','M'=>'Ь','<'=>'Б','>'=>'Ю','?'=>',','й'=>'q','ц'=>'w','у'=>'e','к'=>'r','е'=>'t','н'=>'y','г'=>'u','ш'=>'i','щ'=>'o','з'=>'p','х'=>'[','ъ'=>']','ф'=>'a','ы'=>'s','в'=>'d','а'=>'f','п'=>'g','р'=>'h','о'=>'j','л'=>'k','д'=>'l','ж'=>';','э'=>"\'",'я'=>'z','ч'=>'x','с'=>'c','м'=>'v','и'=>'b','т'=>'n','ь'=>'m','б'=>',','ю'=>'.','.'=>'/','Й'=>'Q','Ц'=>'W','У'=>'E','К'=>'R','Е'=>'T','Н'=>'Y','Г'=>'U','Ш'=>'I','Щ'=>'O','З'=>'P','Х'=>'{','Ъ'=>'}','Ф'=>'A','Ы'=>'S','В'=>'D','А'=>'F','П'=>'G','Р'=>'H','О'=>'J','Л'=>'K','Д'=>'L','Ж'=>':','Э'=>'"','/'=>'|','Я'=>'Z','Ч'=>'X','С'=>'C','М'=>'V','И'=>'B','Т'=>'N','Ь'=>'M','Б'=>'<','Ю'=>'>',','=>'?'));
/*$fromString = 'qwertyuiop[]asdfghjkl;\'\zxcvbnm,./QWERTYUIOP{}ASDFGHJKL:"|ZXCVBNM<>?йцукенгшщзхъфывапролджэ\\ячсмитьбю.ЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭ/ЯЧСМИТЬБЮ,';
$toString = 'йцукенгшщзхъфывапролджэ\\ячсмитьбю.ЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭ/ЯЧСМИТЬБЮ,qwertyuiop[]asdfghjkl;\'\zxcvbnm,./QWERTYUIOP{}ASDFGHJKL:"|ZXCVBNM<>?';
$invertedQuery = '';
for($i = 0, $c = strlen($query); $i < $c; $i++)
{
$curChar = substr($query, $i, 1);
$pos = strpos($fromString, $curChar);
$newChar = $pos !== false ? substr($toString, $pos, 1) : $curChar;
$invertedQuery .= $newChar;
}*/
return $invertedQuery;
}
public function setYandexSpeller($bVal)
{
$this->tryYandexSpeller = $bVal;
return $this;
}
protected function makeYandexSpeller($query)
{
return \Cpeople\Classes\Services\YandexSpeller::correctText($query);
}
public function setPageSize($size)
{
$this->arNavStartParams['nPageSize'] = (int) $size;
return $this;
}
public function setPageNum($pageNum)
{
$this->arNavStartParams['iNumPage'] = (int) $pageNum;
return $this;
}
public function paginate($pagingSize, $pageNum)
{
$this->setPageSize($pagingSize);
$this->setPageNum(intval($pageNum) < 1 ? 1 : intval($pageNum));
return $this;
}
/**
* @param $urlTemplate
* @return \Cpeople\paging
*/
public function getPagingObject($urlTemplate)
{
if (!isset($this->total))
{
$this->total = $this->getFoundRows($this->query);
}
$paging = new \Cpeople\paging($this->arNavStartParams['iNumPage'], $this->total, $this->arNavStartParams['nPageSize']);
$paging->setFormat($urlTemplate);
return $paging;
}
public function setQuery($query)
{
$this->query = $query;
return $this;
}
}