-
Notifications
You must be signed in to change notification settings - Fork 18
/
SolrReindexTask.php
112 lines (88 loc) · 3.05 KB
/
SolrReindexTask.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
<?php
/**
* Reindex the entire content of the current system in the solr backend
*
* @author Marcus Nyeholt <marcus@silverstripe.com.au>
* @license http://silverstripe.org/bsd-license/
*/
class SolrReindexTask extends BuildTask
{
protected $title = "Reindex all content within Solr";
protected $description = "Iterates through all content within the system, re-indexing it in solr";
protected $types = array();
public function __construct($types = array()) {
parent::__construct();
$this->types = $types;
}
public function run($request) {
increase_time_limit_to();
$type = Convert::raw2sql($request->getVar('type'));
if($type) {
$this->types[] = $type;
} elseif(!$this->types) {
foreach(ClassInfo::subclassesFor('DataObject') as $class) {
if($class::has_extension('SolrIndexable')) {
$this->types[] = $class;
}
}
}
$search = singleton('SolrSearchService');
if (isset($_GET['delete_all'])) {
$search->getSolr()->deleteByQuery('*:*');
$search->getSolr()->commit();
}
$count = 0;
foreach ($this->types as $type) {
$search->getSolr()->deleteByQuery('ClassNameHierarchy_ms:' . $type);
$search->getSolr()->commit();
if (ClassInfo::exists('QueuedJob') && !isset($_GET['direct'])) {
$job = new SolrReindexJob($type);
$svc = singleton('QueuedJobService');
$svc->queueJob($job);
$this->log("Reindexing job for $type has been queued");
} else {
$mode = Versioned::get_reading_mode();
Versioned::reading_stage('Stage');
// get the holders first, see if we have any that AREN'T in the root (ie we've already partitioned everything...)
$pages = $type::get();
$pages = $pages->filter(array('ClassName' => $type));
/* @var $search SolrSearchService */
$this->log("------------------------------");
$this->log("Start reindexing job for $type (Count: ".$pages->count()."):");
$this->log("------------------------------");
foreach ($pages as $page) {
// Make sure the current page is not orphaned.
if($page->ParentID > 0) {
$parent = $page->getParent();
if(is_null($parent) || ($parent === false)) {
continue;
}
}
// Appropriately index the current page, taking versioning into account.
if ($page->hasExtension('Versioned')) {
$search->index($page, 'Stage');
$baseTable = $page->baseTable();
$live = Versioned::get_one_by_stage($page->ClassName, 'Live', "\"$baseTable\".\"ID\" = $page->ID");
if ($live) {
$search->index($live, 'Live');
$this->log("Reindexed Live version of $live->Title");
}
$this->log("Reindexed (#$page->ID) $page->Title");
$count ++;
} else {
$search->index($page);
$this->log("Reindexed $type ID#$page->ID");
$count ++;
}
}
Versioned::set_reading_mode($mode);
}
}
$this->log("------------------------------");
$this->log("Reindex complete, $count objects re-indexed");
$this->log("------------------------------");
}
protected function log($message) {
DB::alteration_message($message);
}
}