Completed
Push — master ( 80cf2e...94aa8a )
by
unknown
06:52 queued 14s
created

WikibaseQualityConstraintsHooks::addVariables()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 2
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 ) ) {
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...
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 (
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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' ) &&
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 ) ||
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...
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 ) {
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...
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