1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WikibaseQuality\ConstraintReport; |
4
|
|
|
|
5
|
|
|
use Config; |
6
|
|
|
use DatabaseUpdater; |
7
|
|
|
use JobQueueGroup; |
8
|
|
|
use JobSpecification; |
9
|
|
|
use MediaWiki\MediaWikiServices; |
10
|
|
|
use OutputPage; |
11
|
|
|
use Skin; |
12
|
|
|
use Wikibase\Change; |
13
|
|
|
use Wikibase\DataModel\Entity\PropertyId; |
14
|
|
|
use Wikibase\EntityChange; |
15
|
|
|
use Wikibase\Lib\Changes\EntityDiffChangedAspects; |
16
|
|
|
use Wikibase\Repo\WikibaseRepo; |
17
|
|
|
use WikibaseQuality\ConstraintReport\Api\ResultsCache; |
18
|
|
|
use WikibaseQuality\ConstraintReport\Job\CheckConstraintsJob; |
19
|
|
|
use WikiPage; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Container for hook callbacks registered in extension.json. |
23
|
|
|
* |
24
|
|
|
* @license GPL-2.0-or-later |
25
|
|
|
*/ |
26
|
|
|
final class WikibaseQualityConstraintsHooks { |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param DatabaseUpdater $updater |
30
|
|
|
*/ |
31
|
|
|
public static function onCreateSchema( DatabaseUpdater $updater ) { |
32
|
|
|
$updater->addExtensionTable( |
33
|
|
|
'wbqc_constraints', |
34
|
|
|
__DIR__ . '/../sql/create_wbqc_constraints.sql' |
35
|
|
|
); |
36
|
|
|
$updater->addExtensionField( |
37
|
|
|
'wbqc_constraints', |
38
|
|
|
'constraint_id', |
39
|
|
|
__DIR__ . '/../sql/patch-wbqc_constraints-constraint_id.sql' |
40
|
|
|
); |
41
|
|
|
$updater->addExtensionIndex( |
42
|
|
|
'wbqc_constraints', |
43
|
|
|
'wbqc_constraints_guid_uniq', |
44
|
|
|
__DIR__ . '/../sql/patch-wbqc_constraints-wbqc_constraints_guid_uniq.sql' |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public static function onWikibaseChange( Change $change ) { |
49
|
|
|
if ( !( $change instanceof EntityChange ) ) { |
|
|
|
|
50
|
|
|
return; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** @var EntityChange $change */ |
54
|
|
|
$config = MediaWikiServices::getInstance()->getMainConfig(); |
55
|
|
|
|
56
|
|
|
// If jobs are enabled and the results would be stored in some way run a job. |
57
|
|
View Code Duplication |
if ( |
|
|
|
|
58
|
|
|
$config->get( 'WBQualityConstraintsEnableConstraintsCheckJobs' ) && |
59
|
|
|
$config->get( 'WBQualityConstraintsCacheCheckConstraintsResults' ) && |
60
|
|
|
self::isSelectedForJobRunBasedOnPercentage() |
61
|
|
|
) { |
62
|
|
|
$params = [ 'entityId' => $change->getEntityId()->getSerialization() ]; |
63
|
|
|
JobQueueGroup::singleton()->push( |
64
|
|
|
new JobSpecification( CheckConstraintsJob::COMMAND, $params ) |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
View Code Duplication |
if ( $config->get( 'WBQualityConstraintsEnableConstraintsImportFromStatements' ) && |
|
|
|
|
69
|
|
|
self::isConstraintStatementsChange( $config, $change ) |
70
|
|
|
) { |
71
|
|
|
$params = [ 'propertyId' => $change->getEntityId()->getSerialization() ]; |
72
|
|
|
JobQueueGroup::singleton()->push( |
73
|
|
|
new JobSpecification( 'constraintsTableUpdate', $params ) |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
private static function isSelectedForJobRunBasedOnPercentage() { |
79
|
|
|
$config = MediaWikiServices::getInstance()->getMainConfig(); |
80
|
|
|
$percentage = $config->get( 'WBQualityConstraintsEnableConstraintsCheckJobsRatio' ); |
81
|
|
|
|
82
|
|
|
return mt_rand( 1, 100 ) <= $percentage; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public static function isConstraintStatementsChange( Config $config, Change $change ) { |
86
|
|
|
if ( !( $change instanceof EntityChange ) || |
|
|
|
|
87
|
|
|
$change->getAction() !== EntityChange::UPDATE || |
88
|
|
|
!( $change->getEntityId() instanceof PropertyId ) |
89
|
|
|
) { |
90
|
|
|
return false; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$info = $change->getInfo(); |
94
|
|
|
|
95
|
|
|
if ( !array_key_exists( 'compactDiff', $info ) ) { |
96
|
|
|
// the non-compact diff ($info['diff']) does not contain statement diffs (T110996), |
97
|
|
|
// so we only know that the change *might* affect the constraint statements |
98
|
|
|
return true; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** @var EntityDiffChangedAspects $aspects */ |
102
|
|
|
$aspects = $info['compactDiff']; |
103
|
|
|
|
104
|
|
|
$propertyConstraintId = $config->get( 'WBQualityConstraintsPropertyConstraintId' ); |
105
|
|
|
return in_array( $propertyConstraintId, $aspects->getStatementChanges() ); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public static function onArticlePurge( WikiPage $wikiPage ) { |
109
|
|
|
$repo = WikibaseRepo::getDefaultInstance(); |
110
|
|
|
|
111
|
|
|
$entityContentFactory = $repo->getEntityContentFactory(); |
112
|
|
|
if ( $entityContentFactory->isEntityContentModel( $wikiPage->getContentModel() ) ) { |
113
|
|
|
$entityId = $entityContentFactory->getEntityIdForTitle( $wikiPage->getTitle() ); |
114
|
|
|
if ( $entityId !== null ) { |
115
|
|
|
$resultsCache = ResultsCache::getDefaultInstance(); |
116
|
|
|
$resultsCache->delete( $entityId ); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public static function onBeforePageDisplay( OutputPage $out, Skin $skin ) { |
|
|
|
|
122
|
|
|
$repo = WikibaseRepo::getDefaultInstance(); |
123
|
|
|
|
124
|
|
|
$lookup = $repo->getEntityNamespaceLookup(); |
125
|
|
|
$title = $out->getTitle(); |
126
|
|
|
if ( $title === null ) { |
127
|
|
|
return; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
if ( !$lookup->isEntityNamespace( $title->getNamespace() ) ) { |
131
|
|
|
return; |
132
|
|
|
} |
133
|
|
|
if ( empty( $out->getJsConfigVars()['wbIsEditView'] ) ) { |
134
|
|
|
return; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$out->addModules( 'wikibase.quality.constraints.suggestions' ); |
138
|
|
|
|
139
|
|
|
if ( !$out->getUser()->isLoggedIn() ) { |
140
|
|
|
return; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$out->addModules( 'wikibase.quality.constraints.gadget' ); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Hook: MakeGlobalVariablesScript |
148
|
|
|
* @param array &$vars |
149
|
|
|
* @param OutputPage $out |
150
|
|
|
*/ |
151
|
|
|
public static function addVariables( &$vars, OutputPage $out ) { |
|
|
|
|
152
|
|
|
$config = MediaWikiServices::getInstance()->getMainConfig(); |
153
|
|
|
|
154
|
|
|
$vars['wbQualityConstraintsPropertyConstraintId'] = $config->get( 'WBQualityConstraintsPropertyConstraintId' ); |
155
|
|
|
$vars['wbQualityConstraintsOneOfConstraintId'] = $config->get( 'WBQualityConstraintsOneOfConstraintId' ); |
156
|
|
|
$vars['wbQualityConstraintsAllowedQualifierConstraintId'] = $config->get( 'WBQualityConstraintsAllowedQualifiersConstraintId' ); |
157
|
|
|
$vars['wbQualityConstraintsPropertyId'] = $config->get( 'WBQualityConstraintsPropertyId' ); |
158
|
|
|
$vars['wbQualityConstraintsQualifierOfPropertyConstraintId'] = $config->get( 'WBQualityConstraintsQualifierOfPropertyConstraintId' ); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
} |
162
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.