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\Control\Director; |
14
|
|
|
use SilverStripe\Dev\BuildTask; |
15
|
|
|
use SilverStripe\Security\Permission; |
16
|
|
|
use SilverStripe\Security\Security; |
17
|
|
|
use Suilven\FreeTextSearch\Helper\BulkIndexingHelper; |
18
|
|
|
|
19
|
|
|
class ReindexTask extends BuildTask |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
protected $title = 'Reindex'; |
23
|
|
|
|
24
|
|
|
protected $description = 'Reindex all dataobjects referred to in indexes'; |
25
|
|
|
|
26
|
|
|
protected $enabled = true; |
27
|
|
|
|
28
|
|
|
/** @var string */ |
29
|
|
|
private static $segment = 'reindex'; |
|
|
|
|
30
|
|
|
|
31
|
|
|
|
32
|
|
|
|
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Implement this method in the task subclass to |
36
|
|
|
* execute via the TaskRunner |
37
|
|
|
* |
38
|
|
|
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint |
39
|
|
|
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingAnyTypeHint |
40
|
|
|
* @param \SilverStripe\Control\HTTPRequest $request |
41
|
|
|
* @return \SilverStripe\Control\HTTPResponse|void |
42
|
|
|
*/ |
43
|
|
|
public function run($request) |
44
|
|
|
{ |
45
|
|
|
$climate = new CLImate(); |
46
|
|
|
|
47
|
|
|
// check this script is being run by admin |
48
|
|
|
// @phpstan-ignore-next-line |
49
|
|
|
$canAccess = (Director::isDev() || Director::is_cli() || Permission::check("ADMIN")); |
50
|
|
|
|
51
|
|
|
// for testing purposes |
52
|
|
|
$fail = $request->getVar('fail'); |
53
|
|
|
if (!\is_null($fail)) { |
54
|
|
|
$canAccess = false; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if (!$canAccess) { |
58
|
|
|
return Security::permissionFailure(null, 'Permission denied'); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
/** @var string $indexName */ |
63
|
|
|
$indexName = $request->getVar('index'); |
64
|
|
|
|
65
|
|
|
$helper = new BulkIndexingHelper(); |
66
|
|
|
$helper->bulkIndex($indexName, false, $climate); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|