Completed
Push — master ( 2ba52e...f57ecc )
by
unknown
02:11
created

onBeforePageDisplay()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8977
c 0
b 0
f 0
cc 6
nc 6
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 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
	/**
87
	 * @param string $userName
88
	 * @param int $timestamp UTC timestamp (seconds since the Epoch)
89
	 * @return bool
90
	 */
91
	public static function isGadgetEnabledForUserName( $userName, $timestamp ) {
92
		$initial = $userName[0];
93
94
		if ( $initial === 'Z' ) {
95
			$firstWeek = 0;
96
		} elseif ( $initial >= 'W' && $initial < 'Z' ) {
97
			$firstWeek = 1;
98
		} elseif ( $initial >= 'T' && $initial < 'W' ) {
99
			$firstWeek = 2;
100
		} elseif ( $initial >= 'N' && $initial < 'T' ) {
101
			$firstWeek = 3;
102
		} elseif ( $initial >= 'E' && $initial < 'N' ) {
103
			$firstWeek = 4;
104
		} else {
105
			$firstWeek = 5;
106
		}
107
108
		$threshold = gmmktime(
109
			0, // hour
110
			0, // minute
111
			0, // second
112
			3, // month; overflows to 3 or 4 depending on day
113
			$firstWeek * 7 + 1, // day
114
			2018 // year
115
		);
116
117
		return $timestamp >= $threshold;
118
	}
119
120
	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...
121
		$repo = WikibaseRepo::getDefaultInstance();
122
123
		$lookup = $repo->getEntityNamespaceLookup();
124
		$title = $out->getTitle();
125
		if ( $title === null ) {
126
			return;
127
		}
128
129
		if ( !$lookup->isEntityNamespace( $title->getNamespace() ) ) {
130
			return;
131
		}
132
		if ( empty( $out->getJsConfigVars()['wbIsEditView'] ) ) {
133
			return;
134
		}
135
136
		$out->addModules( 'wikibase.quality.constraints.suggestions' );
137
138
		if ( !$out->getUser()->isLoggedIn() ) {
139
			return;
140
		}
141
		if ( self::isGadgetEnabledForUserName( $out->getUser()->getName(), time() ) ) {
142
			$out->addModules( 'wikibase.quality.constraints.gadget' );
143
		}
144
	}
145
146
	/**
147
	 * Hook: MakeGlobalVariablesScript
148
	 * @param array &$vars
149
	 * @param OutputPage $out
150
	 */
151
	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...
152
		$config = MediaWikiServices::getInstance()->getMainConfig();
153
154
		$vars['wbQualityConstraintsPropertyConstraintId'] = $config->get( 'WBQualityConstraintsPropertyConstraintId' );
155
		$vars['wbQualityConstraintsOneOfConstraintId'] = $config->get( 'WBQualityConstraintsOneOfConstraintId' );
156
		$vars['wbQualityConstraintsQualifierOfPropertyConstraintId'] = $config->get( 'WBQualityConstraintsQualifierOfPropertyConstraintId' );
157
	}
158
159
}
160