Completed
Push — master ( 185772...b3210e )
by
unknown
02:28
created

WikibaseQualityConstraintsHooks::onCreateSchema()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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 Title;
13
use Wikibase\Change;
14
use Wikibase\DataModel\Entity\PropertyId;
15
use Wikibase\EntityChange;
16
use Wikibase\Lib\Changes\EntityDiffChangedAspects;
17
use Wikibase\Repo\WikibaseRepo;
18
use WikibaseQuality\ConstraintReport\Api\ResultsCache;
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( 'wbqc_constraints', __DIR__ . '/../sql/create_wbqc_constraints.sql' );
33
		$updater->addExtensionField( 'wbqc_constraints', 'constraint_id', __DIR__ . '/../sql/patch-wbqc_constraints-constraint_id.sql' );
34
	}
35
36
	public static function onWikibaseChange( Change $change ) {
37
		$config = MediaWikiServices::getInstance()->getMainConfig();
38
		if ( $config->get( 'WBQualityConstraintsEnableConstraintsImportFromStatements' ) &&
39
			self::isConstraintStatementsChange( $config, $change )
40
		) {
41
			/** @var EntityChange $change */
42
			$title = Title::newMainPage();
43
			$params = [ 'propertyId' => $change->getEntityId()->getSerialization() ];
44
			JobQueueGroup::singleton()->push(
45
				new JobSpecification( 'constraintsTableUpdate', $params, [], $title )
46
			);
47
		}
48
	}
49
50
	public static function isConstraintStatementsChange( Config $config, Change $change ) {
51
		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...
52
			 $change->getAction() !== EntityChange::UPDATE ||
53
			 !( $change->getEntityId() instanceof PropertyId )
54
		) {
55
			return false;
56
		}
57
58
		$info = $change->getInfo();
59
60
		if ( !array_key_exists( 'compactDiff', $info ) ) {
61
			// the non-compact diff ($info['diff']) does not contain statement diffs (T110996),
62
			// so we only know that the change *might* affect the constraint statements
63
			return true;
64
		}
65
66
		/** @var EntityDiffChangedAspects $aspects */
67
		$aspects = $info['compactDiff'];
68
69
		$propertyConstraintId = $config->get( 'WBQualityConstraintsPropertyConstraintId' );
70
		return in_array( $propertyConstraintId, $aspects->getStatementChanges() );
71
	}
72
73
	public static function onArticlePurge( WikiPage $wikiPage ) {
74
		$repo = WikibaseRepo::getDefaultInstance();
75
76
		$entityContentFactory = $repo->getEntityContentFactory();
77
		if ( $entityContentFactory->isEntityContentModel( $wikiPage->getContentModel() ) ) {
78
			$entityId = $entityContentFactory->getEntityIdForTitle( $wikiPage->getTitle() );
79
			if ( $entityId !== null ) {
80
				$resultsCache = ResultsCache::getDefaultInstance();
81
				$resultsCache->delete( $entityId );
82
			}
83
		}
84
	}
85
86
	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...
87
		$repo = WikibaseRepo::getDefaultInstance();
88
89
		$lookup = $repo->getEntityNamespaceLookup();
90
		$title = $out->getTitle();
91
		if ( $title === null ) {
92
			return;
93
		}
94
95
		if ( !$lookup->isEntityNamespace( $title->getNamespace() ) ) {
96
			return;
97
		}
98
		if ( empty( $out->getJsConfigVars()['wbIsEditView'] ) ) {
99
			return;
100
		}
101
102
		$out->addModules( 'wikibase.quality.constraints.suggestions' );
103
104
		if ( !$out->getUser()->isLoggedIn() ) {
105
			return;
106
		}
107
108
		$out->addModules( 'wikibase.quality.constraints.gadget' );
109
	}
110
111
	/**
112
	 * Hook: MakeGlobalVariablesScript
113
	 * @param array &$vars
114
	 * @param OutputPage $out
115
	 */
116
	public static function addVariables( &$vars, OutputPage $out ) {
0 ignored issues
show
Unused Code introduced by
The parameter $out 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...
117
		$config = MediaWikiServices::getInstance()->getMainConfig();
118
119
		$vars['wbQualityConstraintsPropertyConstraintId'] = $config->get( 'WBQualityConstraintsPropertyConstraintId' );
120
		$vars['wbQualityConstraintsOneOfConstraintId'] = $config->get( 'WBQualityConstraintsOneOfConstraintId' );
121
		$vars['wbQualityConstraintsQualifierOfPropertyConstraintId'] = $config->get( 'WBQualityConstraintsQualifierOfPropertyConstraintId' );
122
	}
123
124
}
125