Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
57.14% covered (warning)
57.14%
4 / 7
CRAP
70.27% covered (warning)
70.27%
26 / 37
MetaSaneitizeJobStore
0.00% covered (danger)
0.00%
0 / 1
57.14% covered (warning)
57.14%
4 / 7
11.13
70.27% covered (warning)
70.27%
26 / 37
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 docId
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
4 / 4
 create
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
17 / 17
 get
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
3 / 3
 update
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 8
 delete
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 buildIndexProperties
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
<?php
namespace CirrusSearch\MetaStore;
use Elastica\Index;
use WikiMap;
class MetaSaneitizeJobStore implements MetaStore {
    public const METASTORE_TYPE = "sanitize";
    /** @var Index */
    private $index;
    public function __construct( Index $index ) {
        $this->index = $index;
    }
    /**
     * @param string $jobName
     * @return string the job id
     */
    public static function docId( $jobName ) {
        return implode( '-', [
            self::METASTORE_TYPE,
            WikiMap::getCurrentWikiId(),
            $jobName
        ] );
    }
    /**
     * @param string $jobName
     * @param int $idOffset The starting page id of the job
     * @param string|null $cluster target cluster for this job (null for all writable clusters)
     * @return \Elastica\Document
     */
    public function create( $jobName, $idOffset, $cluster = null ) {
        $doc = new \Elastica\Document(
            self::docId( $jobName ),
            [
                'type' => self::METASTORE_TYPE,
                'wiki' => WikiMap::getCurrentWikiId(),
                'sanitize_job_loop_id' => 0,
                'sanitize_job_wiki' => WikiMap::getCurrentWikiId(), // Deprecated, use common wiki field
                'sanitize_job_created' => time(),
                'sanitize_job_updated' => time(),
                'sanitize_job_last_loop' => null,
                'sanitize_job_cluster' => $cluster,
                'sanitize_job_id_offset' => $idOffset,
                'sanitize_job_ids_sent' => 0,
                'sanitize_job_ids_sent_total' => 0,
                'sanitize_job_jobs_sent' => 0,
                'sanitize_job_jobs_sent_total' => 0
            ],
            '_doc'
        );
        $this->index->addDocuments( [ $doc ] );
        return $doc;
    }
    /**
     * @param string $jobName
     * @return \Elastica\Document|null
     */
    public function get( $jobName ) {
        try {
            // Try to fetch the JobInfo from one of the metastore
            // TODO: remove references to type (T308044)
            return $this->index->getType( '_doc' )->getDocument( self::docId( $jobName ) );
        } catch ( \Elastica\Exception\NotFoundException $e ) {
            return null;
        }
    }
    /**
     * TODO: Might be more comfortable with something that
     * wraps the document and guarantees something sane
     * is provided here.
     *
     * @param \Elastica\Document $jobInfo
     */
    public function update( \Elastica\Document $jobInfo ) {
        if ( $jobInfo->get( 'type' ) != self::METASTORE_TYPE ) {
            throw new \Exception( "Wrong document type" );
        }
        $jobInfo->set( 'sanitize_job_updated', time() );
        // Clear versioning info to prevent issues in the es 6->7 transition
        $params = $jobInfo->getParams();
        unset( $params['version'], $params['_version'] );
        $jobInfo->setParams( $params );
        $this->index->addDocuments( [ $jobInfo ] );
    }
    /**
     * @param string $jobName
     */
    public function delete( $jobName ) {
        // TODO: remove references to type (T308044)
        $this->index->getType( '_doc' )->deleteById( self::docId( $jobName ) );
    }
    /**
     * @return array
     */
    public function buildIndexProperties() {
        return [];
    }
}