1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Created by PhpStorm. |
5
|
|
|
* User: gordon |
6
|
|
|
* Date: 25/3/2561 |
7
|
|
|
* Time: 17:01 น. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Suilven\FreeTextSearch\Task; |
11
|
|
|
|
12
|
|
|
use League\CLImate\CLImate; |
|
|
|
|
13
|
|
|
use SilverStripe\CMS\Model\SiteTree; |
14
|
|
|
use SilverStripe\Control\Director; |
15
|
|
|
use SilverStripe\Control\HTTPRequest; |
16
|
|
|
use SilverStripe\Dev\BuildTask; |
17
|
|
|
use Suilven\FreeTextSearch\Factory\BulkIndexerFactory; |
18
|
|
|
use Suilven\FreeTextSearch\Indexes; |
19
|
|
|
use Suilven\ManticoreSearch\Service\BulkIndexer; |
|
|
|
|
20
|
|
|
|
21
|
|
|
class ReindexTask extends BuildTask |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
protected $title = 'Reindex'; |
25
|
|
|
|
26
|
|
|
protected $description = 'Reindex all dataobjects referred to in indexes'; |
27
|
|
|
|
28
|
|
|
protected $enabled = true; |
29
|
|
|
|
30
|
|
|
private static $segment = 'reindex'; |
|
|
|
|
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Implement this method in the task subclass to |
34
|
|
|
* execute via the TaskRunner |
35
|
|
|
* |
36
|
|
|
* @param HTTPRequest $request |
37
|
|
|
* @return |
38
|
|
|
*/ |
39
|
|
|
public function run($request) |
40
|
|
|
{ |
41
|
|
|
$climate = new CLImate(); |
42
|
|
|
|
43
|
|
|
// check this script is being run by admin |
44
|
|
|
$canAccess = (Director::isDev() || Director::is_cli() || Permission::check("ADMIN")); |
|
|
|
|
45
|
|
|
if (!$canAccess) { |
46
|
|
|
return Security::permissionFailure($this); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** @var string $indexName */ |
50
|
|
|
$indexName = $request->getVar('index'); |
51
|
|
|
$indexes = new Indexes(); |
52
|
|
|
$index = $indexes->getIndex($indexName); |
53
|
|
|
|
54
|
|
|
/** @var string $clazz */ |
55
|
|
|
$clazz = $index->getClass(); |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
$startTime = microtime(true); |
59
|
|
|
|
60
|
|
|
$climate->border(); |
61
|
|
|
$climate->green()->bold('Indexing sitetree'); |
62
|
|
|
$climate->border(); |
63
|
|
|
|
64
|
|
|
$nDocuments = SiteTree::get()->count(); |
65
|
|
|
$bulkSize = 500; // @todo configurable |
66
|
|
|
$pages = 1+round($nDocuments / $bulkSize); |
67
|
|
|
$climate->green('Pages: ' . $pages); |
68
|
|
|
$climate->green()->info('Indexing ' . $nDocuments .' objects'); |
69
|
|
|
$progress = $climate->progress()->total($nDocuments); |
70
|
|
|
|
71
|
|
|
$factory = new BulkIndexerFactory(); |
72
|
|
|
$bulkIndexer = $factory->getBulkIndexer(); |
73
|
|
|
$bulkIndexer->setIndex($indexName); |
74
|
|
|
|
75
|
|
|
for ($i = 0; $i < $pages; $i++) |
76
|
|
|
{ |
77
|
|
|
$dataObjects = $clazz::get()->limit($bulkSize, $i*$bulkSize)->filter('ShowInSearch', true); |
78
|
|
|
foreach($dataObjects as $do) { |
79
|
|
|
// @hack @todo FIX |
80
|
|
|
if ($do->ID !== 6) { |
81
|
|
|
$bulkIndexer->addDataObject($do); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
} |
85
|
|
|
$bulkIndexer->indexDataObjects(); |
86
|
|
|
$current = $bulkSize * ($i+1); |
87
|
|
|
if ($current > $nDocuments) { |
88
|
|
|
$current = $nDocuments; |
89
|
|
|
} |
90
|
|
|
$progress->current($current); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
|
95
|
|
|
$endTime = microtime(true); |
96
|
|
|
$delta = $endTime-$startTime; |
97
|
|
|
|
98
|
|
|
$rate = round($nDocuments / $delta, 2); |
99
|
|
|
|
100
|
|
|
$elapsedStr = round($delta, 2); |
101
|
|
|
|
102
|
|
|
$climate->bold()->blue()->inline("{$nDocuments}"); |
103
|
|
|
$climate->blue()->inline(' objects indexed in '); |
104
|
|
|
$climate->bold()->blue()->inline("{$elapsedStr}"); |
105
|
|
|
$climate->blue()->inline('s, '); |
106
|
|
|
$climate->bold()->blue()->inline("{$rate}"); |
107
|
|
|
$climate->blue(' per second '); |
108
|
|
|
|
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths