-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIndexMaintenance.php
114 lines (94 loc) · 3.4 KB
/
IndexMaintenance.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
<?php
namespace Vulcan\Search\Tasks;
use SilverStripe\Control\Director;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Dev\BuildTask;
use SilverStripe\ORM\DataList;
use Vulcan\Search\Models\SearchIndexEntry;
use Vulcan\Search\Models\SearchTank;
class IndexMaintenance extends BuildTask
{
protected $indexedIds = [];
protected $title = "Search Index Maintenance";
protected $description = "This will perform any necessary maintenance on the index such as tank swaps and obsolete records";
/**
* Implement this method in the task subclass to
* execute via the TaskRunner
*
* @param HTTPRequest $request
*
* @return void
*/
public function run($request)
{
$classes = SearchIndexEntry::get()->column('Model');
foreach ($classes as $class) {
$this->outputText('');
$this->outputText('# PERFORMING MAINTENANCE ON ' . $class);
/** @var DataList|SearchIndexEntry $record */
$records = SearchIndexEntry::get()->filter('Model', $class);
if (!class_exists($class)) {
// class has been removed, clear all obsolete records
$this->outputText(' - Class no longer exists, deleting all records');
$count = 0;
foreach ($records as $record) {
$record->delete();
$count++;
$this->outputText('X', false);
}
$this->outputText('');
$this->outputText(' - Removed ' . $count);
continue;
}
$singleton = self::singleton($class);
/** @var SearchIndexEntry $first */
$first = $records->first();
$tank = $singleton->config()->get('search_tank');
if ($tank != $first->Tank()->Title) {
// tank name has swapped, update all records
$this->outputText('');
$this->outputText(sprintf('- Tank on %s has changed from %s to %s', $class, $first->Tank()->Title, $tank));
$count = 0;
foreach ($records as $record) {
$record->TankID = SearchTank::findOrCreateTank($tank);
$record->write();
$count++;
$this->outputText('.', false);
}
$this->outputText('');
$this->outputText(sprintf(' - Updated %s records', $count));
}
$this->outputText('');
$this->outputText(' - Searching for obsolete records');
$count = 0;
foreach ($records as $record) {
if (!$record->getRecord()) {
$record->delete();
$this->outputText('X', false);
$count++;
}
}
$this->outputText('');
$this->outputText(sprintf(' - Cleared %s obsolete records', $count));
}
$this->outputText('');
$this->outputText('');
$this->outputText('## COMPLETED');
}
/**
* @param $text
* @param bool $break
*/
public function outputText($text, $break = true)
{
if ($break) {
$break = "<br>";
if (Director::is_cli()) {
$break = PHP_EOL;
}
echo $text . $break;
return;
}
echo $text;
}
}