Completed
Push — master ( 6a6753...32d7a5 )
by
unknown
02:18
created

onGetBetaFeaturePreferences()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
namespace WikibaseQuality\ConstraintReport;
4
5
use BetaFeatures;
6
use Config;
7
use DatabaseUpdater;
8
use ExtensionRegistry;
9
use JobQueueGroup;
10
use JobSpecification;
11
use User;
12
use MediaWiki\MediaWikiServices;
13
use OutputPage;
14
use Skin;
15
use Title;
16
use Wikibase\Change;
17
use Wikibase\DataModel\Entity\PropertyId;
18
use Wikibase\EntityChange;
19
use Wikibase\Lib\Changes\EntityDiffChangedAspects;
20
use Wikibase\Repo\WikibaseRepo;
21
use WikibaseQuality\ConstraintReport\Api\ResultsCache;
22
use WikiPage;
23
24
/**
25
 * Container for hook callbacks registered in extension.json.
26
 *
27
 * @license GPL-2.0-or-later
28
 */
29
final class WikibaseQualityConstraintsHooks {
30
31
	/**
32
	 * @param DatabaseUpdater $updater
33
	 */
34
	public static function onCreateSchema( DatabaseUpdater $updater ) {
35
		$updater->addExtensionTable(
36
			'wbqc_constraints',
37
			__DIR__ . '/../sql/create_wbqc_constraints.sql'
38
		);
39
		$updater->addExtensionField(
40
			'wbqc_constraints',
41
			'constraint_id',
42
			__DIR__ . '/../sql/patch-wbqc_constraints-constraint_id.sql'
43
		);
44
		$updater->addExtensionIndex(
45
			'wbqc_constraints',
46
			'wbqc_constraints_guid_uniq',
47
			__DIR__ . '/../sql/patch-wbqc_constraints-wbqc_constraints_guid_uniq.sql'
48
		);
49
	}
50
51
	public static function onWikibaseChange( Change $change ) {
52
		$config = MediaWikiServices::getInstance()->getMainConfig();
53
		if ( $config->get( 'WBQualityConstraintsEnableConstraintsImportFromStatements' ) &&
54
			self::isConstraintStatementsChange( $config, $change )
55
		) {
56
			/** @var EntityChange $change */
57
			$title = Title::newMainPage();
58
			$params = [ 'propertyId' => $change->getEntityId()->getSerialization() ];
59
			JobQueueGroup::singleton()->push(
60
				new JobSpecification( 'constraintsTableUpdate', $params, [], $title )
61
			);
62
		}
63
	}
64
65
	public static function isConstraintStatementsChange( Config $config, Change $change ) {
66
		if ( !( $change instanceof EntityChange ) ||
0 ignored issues
show
Bug introduced by
The class Wikibase\EntityChange does not exist. Did you forget a USE statement, or did you not list all dependencies?

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 the composer.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 or require-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 ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
67
			 $change->getAction() !== EntityChange::UPDATE ||
68
			 !( $change->getEntityId() instanceof PropertyId )
69
		) {
70
			return false;
71
		}
72
73
		$info = $change->getInfo();
74
75
		if ( !array_key_exists( 'compactDiff', $info ) ) {
76
			// the non-compact diff ($info['diff']) does not contain statement diffs (T110996),
77
			// so we only know that the change *might* affect the constraint statements
78
			return true;
79
		}
80
81
		/** @var EntityDiffChangedAspects $aspects */
82
		$aspects = $info['compactDiff'];
83
84
		$propertyConstraintId = $config->get( 'WBQualityConstraintsPropertyConstraintId' );
85
		return in_array( $propertyConstraintId, $aspects->getStatementChanges() );
86
	}
87
88
	public static function onArticlePurge( WikiPage $wikiPage ) {
89
		$repo = WikibaseRepo::getDefaultInstance();
90
91
		$entityContentFactory = $repo->getEntityContentFactory();
92
		if ( $entityContentFactory->isEntityContentModel( $wikiPage->getContentModel() ) ) {
93
			$entityId = $entityContentFactory->getEntityIdForTitle( $wikiPage->getTitle() );
94
			if ( $entityId !== null ) {
95
				$resultsCache = ResultsCache::getDefaultInstance();
96
				$resultsCache->delete( $entityId );
97
			}
98
		}
99
	}
100
101
	public static function onBeforePageDisplay( OutputPage $out, Skin $skin ) {
0 ignored issues
show
Unused Code introduced by
The parameter $skin is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
102
		$repo = WikibaseRepo::getDefaultInstance();
103
104
		$lookup = $repo->getEntityNamespaceLookup();
105
		$title = $out->getTitle();
106
		if ( $title === null ) {
107
			return;
108
		}
109
110
		if ( !$lookup->isEntityNamespace( $title->getNamespace() ) ) {
111
			return;
112
		}
113
		if ( empty( $out->getJsConfigVars()['wbIsEditView'] ) ) {
114
			return;
115
		}
116
117
		$out->addModules( 'wikibase.quality.constraints.suggestions' );
118
119
		if ( !$out->getUser()->isLoggedIn() ) {
120
			return;
121
		}
122
123
		$out->addModules( 'wikibase.quality.constraints.gadget' );
124
	}
125
126
	/**
127
	 * @see https://www.mediawiki.org/wiki/Manual:Hooks/GetBetaFeaturePreferences
128
	 *
129
	 * @param User $user
130
	 * @param array[] &$prefs
131
	 */
132
	public static function onGetBetaFeaturePreferences( User $user, array &$prefs ) {
0 ignored issues
show
Unused Code introduced by
The parameter $user is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
133
		$config = MediaWikiServices::getInstance()->getMainConfig();
134
		$extensionAssetsPath = $config->get( 'ExtensionAssetsPath' );
135
		if ( $config->get( 'WBQualityConstraintsSuggestionsBetaFeature' ) ) {
136
			$prefs['constraint-suggestions'] = [
137
					'label-message' => 'wbqc-beta-feature-label-message',
138
					'desc-message' => 'wbqc-beta-feature-description-message',
139
					'screenshot' => [
140
							'ltr' => "$extensionAssetsPath/WikibaseQualityConstraints/resources/ConstraintSuggestions-beta-features-ltr.svg",
141
							'rtl' => "$extensionAssetsPath/WikibaseQualityConstraints/resources/ConstraintSuggestions-beta-features-rtl.svg",
142
					],
143
					'info-link'
144
					=> 'https://www.mediawiki.org/wiki/Special:MyLanguage/Help:Constraints_suggestions',
145
					'discussion-link'
146
					=> 'https://www.mediawiki.org/wiki/Help_talk:Constraints_suggestions',
147
					'requirements' => [
148
							'javascript' => true,
149
					],
150
			];
151
		}
152
	}
153
154
	/**
155
	 * Hook: MakeGlobalVariablesScript
156
	 * @param array &$vars
157
	 * @param OutputPage $out
158
	 */
159
	public static function addVariables( &$vars, OutputPage $out ) {
160
		$config = MediaWikiServices::getInstance()->getMainConfig();
161
162
		$vars['wbQualityConstraintsPropertyConstraintId'] = $config->get( 'WBQualityConstraintsPropertyConstraintId' );
163
		$vars['wbQualityConstraintsOneOfConstraintId'] = $config->get( 'WBQualityConstraintsOneOfConstraintId' );
164
		$vars['wbQualityConstraintsAllowedQualifierConstraintId'] = $config->get( 'WBQualityConstraintsAllowedQualifiersConstraintId' );
165
		$vars['wbQualityConstraintsPropertyId'] = $config->get( 'WBQualityConstraintsPropertyId' );
166
		$vars['wbQualityConstraintsQualifierOfPropertyConstraintId'] = $config->get( 'WBQualityConstraintsQualifierOfPropertyConstraintId' );
167
168
		$vars['wbQualityConstraintsSuggestionsGloballyEnabled'] = false;
169
170
		if ( $config->get( 'WBQualityConstraintsSuggestionsBetaFeature' ) &&
171
			ExtensionRegistry::getInstance()->isLoaded( 'BetaFeatures' ) &&
172
			BetaFeatures::isFeatureEnabled( $out->getUser(), 'constraint-suggestions' )
173
			) {
174
			$vars['wbQualityConstraintsSuggestionsGloballyEnabled'] = true;
175
		}
176
	}
177
178
}
179