Passed
Push — master ( b4e3f0...d6a119 )
by Robbie
04:16 queued 02:31
created

src/Solr/Tasks/Solr_Reindex.php (1 issue)

1
<?php
2
3
namespace SilverStripe\FullTextSearch\Solr\Tasks;
4
5
use ReflectionClass;
6
use SilverStripe\Core\ClassInfo;
7
use SilverStripe\Core\Injector\Injector;
8
use SilverStripe\Dev\Debug;
9
use SilverStripe\FullTextSearch\Search\Variants\SearchVariant;
10
use SilverStripe\ORM\DataList;
11
use SilverStripe\FullTextSearch\Solr\Reindex\Handlers\SolrReindexHandler;
12
use SilverStripe\FullTextSearch\Solr\SolrIndex;
13
14
/**
15
 * Task used for both initiating a new reindex, as well as for processing incremental batches
16
 * within a reindex.
17
 *
18
 * When running a complete reindex you can provide any of the following
19
 *  - class (to limit to a single class)
20
 *  - verbose (optional)
21
 *
22
 * When running with a single batch, provide the following querystring arguments:
23
 *  - index
24
 *  - class
25
 *  - variantstate
26
 *  - verbose (optional)
27
 */
28
class Solr_Reindex extends Solr_BuildTask
29
{
30
    private static $segment = 'Solr_Reindex';
31
32
    protected $enabled = true;
33
34
    /**
35
     * Number of records to load and index per request
36
     *
37
     * @var int
38
     * @config
39
     */
40
    private static $recordsPerRequest = 200;
41
42
    /**
43
     * Get the reindex handler
44
     *
45
     * @return SolrReindexHandler
46
     */
47
    protected function getHandler()
48
    {
49
        return Injector::inst()->get(SolrReindexHandler::class);
50
    }
51
52
    /**
53
     * @param SS_HTTPRequest $request
0 ignored issues
show
The type SilverStripe\FullTextSea...lr\Tasks\SS_HTTPRequest was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
54
     */
55
    public function run($request)
56
    {
57
        parent::run($request);
58
59
        $this->extend('updateBeforeSolrReindexTask', $request);
60
61
        // Reset state
62
        $originalState = SearchVariant::current_state();
63
        $this->doReindex($request);
64
        SearchVariant::activate_state($originalState);
65
66
        $this->extend('updateAfterSolrReindexTask', $request);
67
    }
68
69
    /**
70
     * @param SS_HTTPRequest $request
71
     */
72
    protected function doReindex($request)
73
    {
74
        $class = $request->getVar('class');
75
76
        $index = $request->getVar('index');
77
78
        //find the index classname by IndexName
79
        // this is for when index names do not match the class name (this can be done by overloading getIndexName() on
80
        // indexes
81
        if ($index && !ClassInfo::exists($index)) {
82
            foreach (ClassInfo::subclassesFor(SolrIndex::class) as $solrIndexClass) {
83
                $reflection = new ReflectionClass($solrIndexClass);
84
                //skip over abstract classes
85
                if (!$reflection->isInstantiable()) {
86
                    continue;
87
                }
88
                //check the indexname matches the index passed to the request
89
                if (!strcasecmp(singleton($solrIndexClass)->getIndexName(), $index)) {
90
                    //if we match, set the correct index name and move on
91
                    $index = $solrIndexClass;
92
                    break;
93
                }
94
            }
95
        }
96
97
        // Check if we are re-indexing a single group
98
        // If not using queuedjobs, we need to invoke Solr_Reindex as a separate process
99
        // Otherwise each group is processed via a SolrReindexGroupJob
100
        $groups = $request->getVar('groups');
101
102
        $handler = $this->getHandler();
103
        if ($groups) {
104
            // Run grouped batches (id % groups = group)
105
            $group = $request->getVar('group');
106
            $indexInstance = singleton($index);
107
            $state = json_decode($request->getVar('variantstate'), true);
108
109
            $handler->runGroup($this->getLogger(), $indexInstance, $state, $class, $groups, $group);
110
            return;
111
        }
112
113
        // If run at the top level, delegate to appropriate handler
114
        $taskName = $this->config()->segment ?: get_class($this);
115
        $handler->triggerReindex($this->getLogger(), $this->config()->recordsPerRequest, $taskName, $class);
116
    }
117
}
118