1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Firesphere\SolrSearch\Tasks; |
5
|
|
|
|
6
|
|
|
use Exception; |
7
|
|
|
use Firesphere\SolrSearch\Extensions\DataObjectExtension; |
8
|
|
|
use Firesphere\SolrSearch\Helpers\SolrLogger; |
9
|
|
|
use Firesphere\SolrSearch\Models\DirtyClass; |
10
|
|
|
use Firesphere\SolrSearch\Services\SolrCoreService; |
11
|
|
|
use Firesphere\SolrSearch\Traits\LoggerTrait; |
12
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
13
|
|
|
use SilverStripe\Control\HTTPRequest; |
14
|
|
|
use SilverStripe\Dev\BuildTask; |
15
|
|
|
use SilverStripe\ORM\ArrayList; |
16
|
|
|
use SilverStripe\ORM\DataList; |
17
|
|
|
use SilverStripe\ORM\ValidationException; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class ClearDirtyClasses |
21
|
|
|
* Clear out classes that were not succesfully updated or deleted in Solr |
22
|
|
|
* |
23
|
|
|
* @package Firesphere\SolrSearch\Tasks |
24
|
|
|
*/ |
25
|
|
|
class ClearDirtyClasses extends BuildTask |
26
|
|
|
{ |
27
|
|
|
use LoggerTrait; |
28
|
|
|
/** |
29
|
|
|
* @var string URLSegment |
30
|
|
|
*/ |
31
|
|
|
private static $segment = 'SolrClearDirtyClasses'; |
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string Title |
34
|
|
|
*/ |
35
|
|
|
protected $title = 'Fix broken items in the Solr cores'; |
36
|
|
|
/** |
37
|
|
|
* @var string Description |
38
|
|
|
*/ |
39
|
|
|
protected $description = 'Clear out classes that are marked as dirty on Solr.'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Clean up Dirty Classes in the index |
43
|
|
|
* |
44
|
|
|
* @param HTTPRequest $request |
45
|
|
|
* @return void |
46
|
|
|
* @throws GuzzleException |
47
|
|
|
* @throws \ReflectionException |
48
|
|
|
* @throws ValidationException |
49
|
|
|
*/ |
50
|
|
|
public function run($request) |
51
|
|
|
{ |
52
|
|
|
/** @var DataList|DirtyClass $classes */ |
53
|
|
|
$classes = DirtyClass::get(); |
54
|
|
|
/** @var SolrCoreService $service */ |
55
|
|
|
$service = new SolrCoreService(); |
56
|
|
|
$solrLogger = new SolrLogger(); |
57
|
|
|
foreach ($classes as $class) { |
58
|
|
|
$dirtyClass = $class->Class; |
59
|
|
|
$ids = json_decode($class->IDs); |
60
|
|
|
try { |
61
|
|
|
if ($class->Type === DataObjectExtension::WRITE) { |
62
|
|
|
$dirtyClasses = $dirtyClass::get()->byIDs($ids); |
63
|
|
|
$service->updateItems($dirtyClasses, SolrCoreService::UPDATE_TYPE); |
64
|
|
|
} |
65
|
|
|
if ($class->Type === DataObjectExtension::DELETE) { |
66
|
|
|
$deletions = $this->createDeleteList($ids, $dirtyClass); |
67
|
|
|
$service->updateItems($deletions, SolrCoreService::DELETE_TYPE); |
68
|
|
|
} |
69
|
|
|
$class->delete(); |
70
|
|
|
} catch (Exception $exception) { |
71
|
|
|
$this->getLogger()->error($exception->getMessage()); |
72
|
|
|
continue; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
$solrLogger->saveSolrLog('Index'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Create an ArrayList of the dirty items to be deleted from Solr |
80
|
|
|
* |
81
|
|
|
* @param array $items |
82
|
|
|
* @param string $dirtyClass |
83
|
|
|
* @return ArrayList |
84
|
|
|
*/ |
85
|
|
|
public function createDeleteList($items, $dirtyClass): ArrayList |
86
|
|
|
{ |
87
|
|
|
/** @var ArrayList $deletions */ |
88
|
|
|
$deletions = ArrayList::create(); |
89
|
|
|
foreach ($items as $item) { |
90
|
|
|
$dirtItem = $dirtyClass::create(['ID' => $item]); |
91
|
|
|
$deletions->push($dirtItem); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $deletions; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|