Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
76.92% covered (warning)
76.92%
30 / 39
57.14% covered (warning)
57.14%
4 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
MetaSaneitizeJobStore
76.92% covered (warning)
76.92%
30 / 39
57.14% covered (warning)
57.14%
4 / 7
10.00
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 docId
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 create
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
1 / 1
1
 get
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 update
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 delete
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 buildIndexProperties
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace CirrusSearch\MetaStore;
4
5use Elastica\Index;
6use WikiMap;
7
8class MetaSaneitizeJobStore implements MetaStore {
9    public const METASTORE_TYPE = "sanitize";
10
11    /** @var Index */
12    private $index;
13
14    public function __construct( Index $index ) {
15        $this->index = $index;
16    }
17
18    /**
19     * @param string $jobName
20     * @return string the job id
21     */
22    public static function docId( $jobName ) {
23        return implode( '-', [
24            self::METASTORE_TYPE,
25            WikiMap::getCurrentWikiId(),
26            $jobName
27        ] );
28    }
29
30    /**
31     * @param string $jobName
32     * @param int $idOffset The starting page id of the job
33     * @param string|null $cluster target cluster for this job (null for all writable clusters)
34     * @return \Elastica\Document
35     */
36    public function create( $jobName, $idOffset, $cluster = null ) {
37        $doc = new \Elastica\Document(
38            self::docId( $jobName ),
39            [
40                'type' => self::METASTORE_TYPE,
41                'wiki' => WikiMap::getCurrentWikiId(),
42                'sanitize_job_loop_id' => 0,
43                'sanitize_job_wiki' => WikiMap::getCurrentWikiId(), // Deprecated, use common wiki field
44                'sanitize_job_created' => time(),
45                'sanitize_job_updated' => time(),
46                'sanitize_job_last_loop' => null,
47                'sanitize_job_cluster' => $cluster,
48                'sanitize_job_id_offset' => $idOffset,
49                'sanitize_job_ids_sent' => 0,
50                'sanitize_job_ids_sent_total' => 0,
51                'sanitize_job_jobs_sent' => 0,
52                'sanitize_job_jobs_sent_total' => 0
53            ],
54            '_doc'
55        );
56        $this->index->addDocuments( [ $doc ] );
57        return $doc;
58    }
59
60    /**
61     * @param string $jobName
62     * @return \Elastica\Document|null
63     */
64    public function get( $jobName ) {
65        try {
66            return $this->index->getDocument( self::docId( $jobName ) );
67        } catch ( \Elastica\Exception\NotFoundException $e ) {
68            return null;
69        }
70    }
71
72    /**
73     * TODO: Might be more comfortable with something that
74     * wraps the document and guarantees something sane
75     * is provided here.
76     *
77     * @param \Elastica\Document $jobInfo
78     */
79    public function update( \Elastica\Document $jobInfo ) {
80        if ( $jobInfo->get( 'type' ) != self::METASTORE_TYPE ) {
81            throw new \Exception( "Wrong document type" );
82        }
83        $jobInfo->set( 'sanitize_job_updated', time() );
84        $params = $jobInfo->getParams();
85        // Clear versioning info provided by elastica, we don't want
86        // to version these docs (they once were).
87        unset( $params['version'] );
88        $jobInfo->setParams( $params );
89
90        $this->index->addDocuments( [ $jobInfo ] );
91    }
92
93    /**
94     * @param string $jobName
95     */
96    public function delete( $jobName ) {
97        $this->index->deleteById( self::docId( $jobName ) );
98    }
99
100    /**
101     * @return array
102     */
103    public function buildIndexProperties() {
104        return [];
105    }
106}