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