Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
89.47% |
17 / 19 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
CheckConstraintsJob | |
89.47% |
17 / 19 |
|
80.00% |
4 / 5 |
6.04 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
setResultsSource | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setEntityIdParser | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
run | |
60.00% |
3 / 5 |
|
0.00% |
0 / 1 |
2.26 | |||
checkConstraints | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace WikibaseQuality\ConstraintReport\Job; |
4 | |
5 | use Job; |
6 | use MediaWiki\MediaWikiServices; |
7 | use MediaWiki\Title\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 | public const COMMAND = 'constraintsRunCheck'; |
25 | |
26 | /** |
27 | * @var CachingResultsSource |
28 | */ |
29 | private $resultsSource; |
30 | |
31 | /** |
32 | * @var EntityIdParser |
33 | */ |
34 | private $entityIdParser; |
35 | |
36 | /** |
37 | * @param Title $title |
38 | * @param string[] $params should contain 'entityId' => 'Q1234' |
39 | */ |
40 | public function __construct( Title $title, array $params ) { |
41 | parent::__construct( self::COMMAND, $title, $params ); |
42 | $this->removeDuplicates = true; |
43 | |
44 | Assert::parameterType( 'string', $params['entityId'], '$params[\'entityId\']' ); |
45 | |
46 | $resultSource = ConstraintsServices::getResultsSource( MediaWikiServices::getInstance() ); |
47 | '@phan-var CachingResultsSource $resultSource'; |
48 | // This job should only ever be used when caching result sources are used. |
49 | $this->setResultsSource( $resultSource ); |
50 | |
51 | $this->setEntityIdParser( WikibaseRepo::getEntityIdParser() ); |
52 | } |
53 | |
54 | public function setResultsSource( CachingResultsSource $resultsSource ) { |
55 | $this->resultsSource = $resultsSource; |
56 | } |
57 | |
58 | public function setEntityIdParser( EntityIdParser $parser ) { |
59 | $this->entityIdParser = $parser; |
60 | } |
61 | |
62 | /** |
63 | * @see Job::run |
64 | * |
65 | * @return bool |
66 | */ |
67 | public function run() { |
68 | try { |
69 | $entityId = $this->entityIdParser->parse( $this->params['entityId'] ); |
70 | } catch ( EntityIdParsingException $e ) { |
71 | return false; |
72 | } |
73 | |
74 | $this->checkConstraints( $entityId ); |
75 | |
76 | return true; |
77 | } |
78 | |
79 | private function checkConstraints( EntityId $entityId ) { |
80 | $this->resultsSource->getResults( |
81 | [ $entityId ], |
82 | [], |
83 | null, |
84 | [] |
85 | ); |
86 | } |
87 | |
88 | } |