Completed
Pull Request — master (#182)
by
unknown
01:52
created

Solr_Reindex::runFrom()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 31
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 31
rs 8.8571
cc 3
eloc 16
nc 4
nop 4
1
<?php
2
namespace SilverStripe\FullTextSearch\Solr\Tasks;
3
4
use ReflectionClass;
5
use SilverStripe\Core\ClassInfo;
6
use SilverStripe\Core\Injector\Injector;
7
use SilverStripe\Dev\Debug;
8
use SilverStripe\FullTextSearch\Search\Variants\SearchVariant;
9
use SilverStripe\ORM\DataList;
10
use SilverStripe\FullTextSearch\Solr\Reindex\Handlers\SolrReindexHandler;
11
use SilverStripe\FullTextSearch\Solr\SolrIndex;
12
13
/**
14
 * Task used for both initiating a new reindex, as well as for processing incremental batches
15
 * within a reindex.
16
 *
17
 * When running a complete reindex you can provide any of the following
18
 *  - class (to limit to a single class)
19
 *  - verbose (optional)
20
 *
21
 * When running with a single batch, provide the following querystring arguments:
22
 *  - start
23
 *  - index
24
 *  - class
25
 *  - variantstate
26
 *  - verbose (optional)
27
 */
28
class Solr_Reindex extends Solr_BuildTask
29
{
30
    private static $segment = 'Solr_Reindex';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $segment is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
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;
0 ignored issues
show
Unused Code introduced by
The property $recordsPerRequest is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
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
54
     */
55
    public function run($request)
56
    {
57
        parent::run($request);
58
59
        // Reset state
60
        $originalState = SearchVariant::current_state();
61
        $this->doReindex($request);
62
        SearchVariant::activate_state($originalState);
63
    }
64
65
    /**
66
     * @param SS_HTTPRequest $request
67
     */
68
    protected function doReindex($request)
69
    {
70
        $class = $request->getVar('class');
71
72
        $index = $request->getVar('index');
73
74
        //find the index classname by IndexName
75
        // this is for when index names do not match the class name (this can be done by overloading getIndexName() on
76
        // indexes
77
        if ($index && !ClassInfo::exists($index)) {
78
            foreach (ClassInfo::subclassesFor(SolrIndex::class) as $solrIndexClass) {
79
                $reflection = new ReflectionClass($solrIndexClass);
80
                //skip over abstract classes
81
                if (!$reflection->isInstantiable()) {
82
                    continue;
83
                }
84
                //check the indexname matches the index passed to the request
85
                if (!strcasecmp(singleton($solrIndexClass)->getIndexName(), $index)) {
86
                    //if we match, set the correct index name and move on
87
                    $index = $solrIndexClass;
88
                    break;
89
                }
90
            }
91
        }
92
93
        // Deprecated reindex mechanism
94
        $start = $request->getVar('start');
95
        if ($start !== null) {
96
            // Run single batch directly
97
            $indexInstance = singleton($index);
98
            $state = json_decode($request->getVar('variantstate'), true);
99
            $this->runFrom($indexInstance, $class, $start, $state);
0 ignored issues
show
Documentation Bug introduced by
The method runFrom does not exist on object<SilverStripe\Full...olr\Tasks\Solr_Reindex>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
100
            return;
101
        }
102
103
        // Check if we are re-indexing a single group
104
        // If not using queuedjobs, we need to invoke Solr_Reindex as a separate process
105
        // Otherwise each group is processed via a SolrReindexGroupJob
106
        $groups = $request->getVar('groups');
107
108
        $handler = $this->getHandler();
109
        if ($groups) {
110
            // Run grouped batches (id % groups = group)
111
            $group = $request->getVar('group');
112
            $indexInstance = singleton($index);
113
            $state = json_decode($request->getVar('variantstate'), true);
114
115
            $handler->runGroup($this->getLogger(), $indexInstance, $state, $class, $groups, $group);
116
            return;
117
        }
118
119
        // If run at the top level, delegate to appropriate handler
120
        $taskName = $this->config()->segment ?: get_class($this);
121
        $handler->triggerReindex($this->getLogger(), $this->config()->recordsPerRequest, $taskName, $class);
122
    }
123
124
}
125