-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBuildIndex.php
169 lines (132 loc) · 4.38 KB
/
BuildIndex.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
<?php
namespace Vulcan\Search\Tasks;
use SilverStripe\Control\Director;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Dev\BuildTask;
use SilverStripe\ORM\DataList;
use SilverStripe\ORM\DataObject;
use Vulcan\Search\Extensions\SearchIndexExtension;
use Vulcan\Search\Models\SearchIndexEntry;
use Vulcan\Search\Models\SearchTank;
class BuildIndex extends BuildTask
{
protected $indexedIds = [];
protected $title = "Build Search Index Manifest";
protected $description = "Only needs to be called once on production, or to re-build the manifest";
/**
* Implement this method in the task subclass to
* execute via the TaskRunner
*
* @param HTTPRequest $request
*
* @return void
*/
public function run($request)
{
$allDataObjects = ClassInfo::subclassesFor(DataObject::class);
$classes = [];
/** @var DataObject $className */
foreach ($allDataObjects as $className) {
if ($className::has_extension(SearchIndexExtension::class)) {
$classes[] = $className;
}
}
if (empty($classes)) {
$this->outputText('No models implement Indexable. Nothing to do...');
exit;
}
foreach ($classes as $class) {
$this->runOnClass($class);
}
$this->outputText('');
$this->outputText('');
$this->outputText('#### COMPLETED');
}
/**
* @param string|SearchIndexExtension|DataObject $className
*/
public function runOnClass($className)
{
$this->outputText('');
$this->outputText('');
$this->outputText('# BEGINNING TO INDEX ' . $className);
$searchableColumns = singleton($className)->searchableColumns();
if (!is_array($searchableColumns) || empty($searchableColumns)) {
$this->outputText('Class does not return searchable columns as an array, or the array is empty');
return;
}
/** @var DataList|DataObject[] $records */
$records = $className::get();
if (!$records->exists()) {
$this->outputText('That model has nothing to index...');
return;
}
foreach ($records as $record) {
$this->indexRecord($record, $searchableColumns);
}
}
/**
* @param DataObject|\Page $record
* @param array $searchableColumns
*/
public function indexRecord(DataObject $record, array $searchableColumns)
{
$values = [];
$tank = $record->config()->get('search_tank');
if (!$tank) {
$tank = 'Main';
}
if (!$tank instanceof SearchTank) {
$tank = SearchTank::findOrCreateTank($tank);
}
if ($record instanceof \Page && !$record->ShowInSearch) {
$this->outputText('X', false);
return;
}
foreach ($searchableColumns as $column) {
$values[] = strip_tags(html_entity_decode($record->relObject($column)));
}
$string = strtolower(implode(' ## ', $values));
/** @var SearchIndexEntry $index */
$index = SearchIndexEntry::get()->filter([
'Model' => $record->ClassName,
'RecordID' => $record->ID,
'TankID' => $tank->ID
])->first();
if ($index) {
$this->indexedIds[] = $index->ID;
if ($index->SearchableText == $string) {
$this->outputText('-', false);
return;
}
$index->SearchableText = $string;
$index->write();
$this->outputText('U', false);
return;
}
$index = SearchIndexEntry::create();
$index->Created = $record->Created;
$index->Model = $record->ClassName;
$index->RecordID = $record->ID;
$index->SearchableText = $string;
$index->TankID = $tank->ID;
if ($record instanceof \Page) {
$index->IsPage = true;
}
$this->indexedIds[$tank->ID][] = $index->write();
$this->outputText('.', false);
}
public function outputText($text, $break = true)
{
if ($break) {
$break = "<br>";
if (Director::is_cli()) {
$break = PHP_EOL;
}
echo $text . $break;
return;
}
echo $text;
}
}