Completed
Push — master ( 3eff13...afdb92 )
by
unknown
03:28 queued 13s
created

CheckConstraintsJob::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace WikibaseQuality\ConstraintReport\Job;
4
5
use Job;
6
use MediaWiki\MediaWikiServices;
7
use Title;
8
use Wikibase\DataModel\Entity\EntityId;
9
use Wikibase\DataModel\Entity\EntityIdParser;
10
use Wikibase\DataModel\Entity\EntityIdParsingException;
11
use Wikibase\Repo\WikibaseRepo;
12
use WikibaseQuality\ConstraintReport\Api\CachingResultsSource;
13
use WikibaseQuality\ConstraintReport\ConstraintsServices;
14
use Wikimedia\Assert\Assert;
15
16
/**
17
 * A job that runs constraint checks for an item
18
 *
19
 * @author Jonas Kress
20
 * @license GPL-2.0-or-later
21
 */
22
class CheckConstraintsJob extends Job {
23
24
	const COMMAND = 'constraintsRunCheck';
25
26
	/**
27
	 * @var string
28
	 */
29
	private $entityId;
30
31
	/**
32
	 * @var CachingResultsSource
33
	 */
34
	private $resultsSource;
35
36
	/**
37
	 * @var EntityIdParser
38
	 */
39
	private $entityIdParser;
40
41
	/**
42
	 * @param Title $title
43
	 * @param string[] $params should contain 'entityId' => 'Q1234'
44
	 */
45
	public function __construct( Title $title, array $params ) {
46
		parent::__construct( self::COMMAND, $title, $params );
47
		$this->removeDuplicates = true;
48
49
		Assert::parameterType( 'string', $params['entityId'], '$params[\'entityId\']' );
50
		$this->entityId = $params['entityId'];
51
52
		$resultSource = ConstraintsServices::getResultsSource( MediaWikiServices::getInstance() );
53
		// This job should only ever be used when caching result sources are used.
54
		$this->setResultsSource( $resultSource );
55
56
		$this->setEntityIdParser( WikibaseRepo::getDefaultInstance()->getEntityIdParser() );
57
	}
58
59
	public function setResultsSource( CachingResultsSource $resultsSource ) {
60
		$this->resultsSource = $resultsSource;
61
	}
62
63
	public function setEntityIdParser( EntityIdParser $parser ) {
64
		$this->entityIdParser = $parser;
65
	}
66
67
	/**
68
	 * @see Job::run
69
	 *
70
	 * @return bool
71
	 */
72
	public function run() {
73
		try {
74
			$entityId = $this->entityIdParser->parse( $this->entityId );
75
		} catch ( EntityIdParsingException $e ) {
76
			return false;
77
		}
78
79
		$this->checkConstraints( $entityId );
80
81
		return true;
82
	}
83
84
	private function checkConstraints( EntityId $entityId ) {
85
		$this->resultsSource->getResults(
86
			[ $entityId ],
87
			[],
88
			null,
89
			[]
90
		);
91
	}
92
93
}
94