-
Notifications
You must be signed in to change notification settings - Fork 454
/
IndexedSearch.php
200 lines (175 loc) · 6.06 KB
/
IndexedSearch.php
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
<?php
namespace Concrete\Core\Page\Search;
use Concrete\Core\Cache\Cache;
use Concrete\Core\Page\Page;
use Concrete\Core\Search\Index\IndexManagerInterface;
use Concrete\Core\Support\Facade\Application;
use Loader;
use Config;
use PageList;
use Collection;
use Area;
use Concrete\Core\Area\SubArea;
use Block;
use stdClass;
class IndexedSearch
{
public $searchBatchSize;
public $searchReindexTimeout;
private $cPathSections = array();
private $searchableAreaNames;
public function __construct()
{
$this->searchReindexTimeout = Config::get('concrete.misc.page_search_index_lifetime');
$this->searchBatchSize = Config::get('concrete.limits.page_search_index_batch');
}
public function getSearchableAreaAction()
{
$action = Config::get('concrete.misc.search_index_area_method');
if (!strlen($action)) {
$action = 'blacklist';
}
return $action;
}
public function getSavedSearchableAreas()
{
$areas = Config::get('concrete.misc.search_index_area_list');
$areas = unserialize($areas);
if (!is_array($areas)) {
$areas = array();
}
return $areas;
}
public function clearSearchIndex()
{
$db = Loader::db();
$db->Execute('truncate table PageSearchIndex');
}
public function matchesArea($arHandle)
{
if (!isset($this->arHandles)) {
$searchableAreaNamesInitial = $this->getSavedSearchableAreas();
if ($this->getSearchableAreaAction() == 'blacklist') {
$areas = Area::getHandleList();
foreach ($areas as $blArHandle) {
if (!in_array($blArHandle, $searchableAreaNamesInitial)) {
$this->searchableAreaNames[] = $blArHandle;
}
}
} else {
$this->searchableAreaNames = $searchableAreaNamesInitial;
}
}
foreach ($this->searchableAreaNames as $sarHandle) {
if (preg_match('/^' . preg_quote($sarHandle . SubArea::AREA_SUB_DELIMITER, '/') . '.+/i', $arHandle)) {
return true;
} else {
if (in_array($arHandle, $this->searchableAreaNames)) {
return true;
}
}
}
return false;
}
public function reindexPage($page)
{
$db = Loader::db();
if (is_object($page) && ($page instanceof Collection) && ($page->getAttribute('exclude_search_index') != 1)) {
$datetime = Loader::helper('date')->getOverridableNow();
$db->Replace(
'PageSearchIndex',
array(
'cID' => $page->getCollectionID(),
'cName' => $page->getCollectionName(),
'cDescription' => $page->getCollectionDescription(),
'cPath' => $page->getCollectionPath(),
'cDatePublic' => $page->getCollectionDatePublic(),
'content' => $this->getBodyContentFromPage($page),
'cDateLastIndexed' => $datetime,
),
array('cID'),
true
);
} else {
$db->Execute('delete from PageSearchIndex where cID = ?', array($page->getCollectionID()));
}
}
public function getBodyContentFromPage($c)
{
$text = '';
$tagsToSpaces = array(
'<br>',
'<br/>',
'<br />',
'<p>',
'</p>',
'</ p>',
'<div>',
'</div>',
'</ div>',
' ',
);
$blarray = array();
$db = Loader::db();
$r = $db->Execute(
'select bID, arHandle from CollectionVersionBlocks where cID = ? and cvID = ?',
array($c->getCollectionID(), $c->getVersionID())
);
$th = Loader::helper('text');
while ($row = $r->FetchRow()) {
if ($this->matchesArea($row['arHandle'])) {
$b = Block::getByID($row['bID'], $c, $row['arHandle']);
if (!is_object($b)) {
continue;
}
$bi = $b->getInstance();
if (method_exists($bi, 'getSearchableContent')) {
$searchableContent = $bi->getSearchableContent();
if (strlen(trim($searchableContent))) {
$text .= $th->decodeEntities(
strip_tags(str_ireplace($tagsToSpaces, ' ', $searchableContent)),
ENT_QUOTES,
APP_CHARSET
) . ' ';
}
}
unset($b);
unset($bi);
}
}
return $text;
}
/**
* Reindexes the search engine.
*/
public function reindexAll($fullReindex = false)
{
Cache::disableAll();
/** @var IndexManagerInterface $indexStack */
$indexStack = Application::getFacadeApplication()->make(IndexManagerInterface::class);
$db = Loader::db();
if ($fullReindex) {
$db->Execute("truncate table PageSearchIndex");
}
$pl = new PageList();
$pl->ignoreAliases();
$pl->ignorePermissions();
$pl->sortByCollectionIDAscending();
$pl->filter(
false,
'(c.cDateModified > psi.cDateLastIndexed or UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(psi.cDateLastIndexed) > ' . $this->searchReindexTimeout . ' or psi.cID is null or psi.cDateLastIndexed is null)'
);
$pl->filter(false, '(ak_exclude_search_index is null or ak_exclude_search_index = 0)');
$pages = $pl->get($this->searchBatchSize);
$num = 0;
foreach ($pages as $c) {
$indexStack->index(Page::class, $c);
}
$pnum = Collection::reindexPendingPages();
$num = $num + $pnum;
Cache::enableAll();
$result = new stdClass();
$result->count = $num;
return $result;
}
}