Completed
Push — master ( 28f5b7...87cc9d )
by mw
10:20
created

Hooks::onIsApprovedRevision()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
namespace SMW\ApprovedRevs;
4
5
use SMW\ApplicationFactory;
6
use Onoi\Cache\Cache;
7
8
/**
9
 * @license GNU GPL v2+
10
 * @since 1.0
11
 *
12
 * @author mwjames
13
 */
14
class Hooks {
15
16
	/**
17
	 * @var array
18
	 */
19
	private $handlers = [];
20
21
	/**
22
	 * @var Cache
23
	 */
24
	private $cache;
25
26
	/**
27
	 * @since 1.0
28
	 *
29
	 * @param array $config
30
	 */
31 2
	public function __construct( $config = [] ) {
32 2
		$this->registerHandlers( $config );
33 2
	}
34
35
	/**
36
	 * @since 1.0
37
	 *
38
	 * @param Cache $cache
39
	 */
40 1
	public function setCache( Cache $cache ) {
41 1
		$this->cache = $cache;
42 1
	}
43
44
	/**
45
	 * @since  1.0
46
	 */
47
	public static function hasPropertyCollisions( $var ) {
48
49
		if ( !isset( $var['sespgEnabledPropertyList'] ) ) {
50
			return false;
51
		}
52
53
		// SESP properties!
54
		$list = [
55
			'_APPROVED' => true,
56
			'_APPROVEDBY' => true,
57
			'_APPROVEDDATE' => true,
58
			'_APPROVEDSTATUS' => true
59
		];
60
61
		foreach ( $var['sespgEnabledPropertyList'] as $key ) {
62
			if ( isset( $list[$key] ) ) {
63
				return $key;
64
			}
65
		}
66
67
		return false;
68
	}
69
70
	/**
71
	 * @since 1.0
72
	 *
73 1
	 * @param array &$vars
74 1
	 */
75 1
	public static function initExtension( &$vars ) {
76
77 1
		/**
78
		 * @see https://www.semantic-mediawiki.org/wiki/Hooks#SMW::Config::BeforeCompletion
79
		 *
80
		 * @since 1.0
81
		 *
82 1
		 * @param array &$config
83 1
		 */
84
		$vars['wgHooks']['SMW::Config::BeforeCompletion'][] = function( &$config ) {
85 1
86
			if ( isset( $config['smwgImportFileDirs'] ) ) {
87
				$config['smwgImportFileDirs'] += [ 'sar' => __DIR__ . '/../data/import' ];
88
			}
89 1
90 1
			return true;
91
		};
92
	}
93 1
94
	/**
95
	 * @since  1.0
96
	 */
97
	public function register() {
98
		foreach ( $this->handlers as $name => $callback ) {
99
			\Hooks::register( $name, $callback );
100
		}
101
	}
102 1
103 1
	/**
104
	 * @since  1.0
105
	 */
106
	public function deregister() {
107
		foreach ( array_keys( $this->handlers ) as $name ) {
108
109
			\Hooks::clear( $name );
110
111
			// Remove registered `wgHooks` hooks that are not cleared by the
112
			// previous call
113 1
			if ( isset( $GLOBALS['wgHooks'][$name] ) ) {
114 1
				unset( $GLOBALS['wgHooks'][$name] );
115
			}
116
		}
117
	}
118
119
	/**
120
	 * @since  1.0
121
	 *
122
	 * @param string $name
123 1
	 *
124
	 * @return boolean
125 1
	 */
126 1
	public function isRegistered( $name ) {
127
		return \Hooks::isRegistered( $name );
128
	}
129 1
130
	/**
131
	 * @since  1.0
132
	 *
133
	 * @param string $name
134
	 *
135
	 * @return array
136
	 */
137
	public function getHandlers( $name ) {
138 1
		return \Hooks::getHandlers( $name );
139
	}
140 1
141 1
	/**
142
	 * @since 1.0
143
	 *
144 1
	 * @param Title $title
145
	 * @param integer $latestRevID
146 1
	 */
147
	public function onIsApprovedRevision( $title, $latestRevID ) {
148
149
		$approvedRevsHandler =  new ApprovedRevsHandler(
150
			new ApprovedRevsFacade()
151
		);
152
153
		return $approvedRevsHandler->isApprovedUpdate( $title, $latestRevID );
154
	}
155 1
156
	/**
157 1
	 * @since 1.0
158 1
	 *
159
	 * @param Title $title
160
	 * @param Revision|null &$revision
161 1
	 */
162
	public function onChangeRevision( $title, &$revision ) {
163 1
164
		$approvedRevsHandler =  new ApprovedRevsHandler(
165
			new ApprovedRevsFacade()
166
		);
167
168
		$approvedRevsHandler->doChangeRevision( $title, $revision );
169
170
		return true;
171
	}
172
173
	/**
174 1
	 * @since 1.0
175
	 *
176 1
	 * @param Title $title
177 1
	 * @param integer &$latestRevID
178
	 */
179 1
	public function onOverrideRevisionID( $title, &$latestRevID ) {
180
181
		$approvedRevsHandler =  new ApprovedRevsHandler(
182
			new ApprovedRevsFacade()
183
		);
184
185
		$approvedRevsHandler->doChangeRevisionID( $title, $latestRevID );
186
187
		return true;
188
	}
189
190 1
	/**
191
	 * @see https://www.semantic-mediawiki.org/wiki/Hooks#SMW::Property::initProperties
192 1
	 *
193 1
	 * @since 1.0
194
	 *
195
	 * @param ProertyRegistry $$registry
0 ignored issues
show
Documentation introduced by
There is no parameter named $$registry. Did you maybe mean $registry?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
196 1
	 * @param integer &$latestRevID
197 1
	 */
198
	public function onInitProperties( $registry ) {
199
200 1
		$propertyRegistry = new PropertyRegistry();
201
		$propertyRegistry->register( $registry );
202 1
203
		return true;
204
	}
205
206
	/**
207
	 * @see https://www.semantic-mediawiki.org/wiki/Hooks#SMWStore::updateDataBefore
208
	 *
209
	 * @since 1.0
210
	 *
211
	 * @param ProertyRegistry $$registry
0 ignored issues
show
Bug introduced by
There is no parameter named $$registry. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
212
	 * @param integer &$latestRevID
213
	 */
214
	public function onUpdateDataBefore( $store, $semanticData ) {
215 1
216
		$propertyAnnotator = new PropertyAnnotator(
217 1
			new ServicesFactory()
218
		);
219 1
220
		$propertyAnnotator->setLogger(
221
			ApplicationFactory::getInstance()->getMediaWikiLogger( 'smw-approved-revs' )
222
		);
223
224
		$propertyAnnotator->addAnnotation( $semanticData );
225
226
		return true;
227
	}
228 1
229 1
	/**
230
	 * @see ??
231 1
	 *
232
	 * @since 1.0
233
	 *
234
	 * @param ParserOutput $output
235
	 * @param Title $title
236
	 * @param integer $rev_id
237
	 * @param string $content
238
	 */
239
	public function onApprovedRevsRevisionApproved( $output, $title, $rev_id, $content  ) {
0 ignored issues
show
Unused Code introduced by
The parameter $content 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...
240
241
		$ttl = 60 * 60; // 1hr
242
243
		if ( $this->cache === null ) {
244 1
			$this->cache = ApplicationFactory::getInstance()->getCache();
0 ignored issues
show
Documentation Bug introduced by
It seems like \SMW\ApplicationFactory:...tInstance()->getCache() of type object<SMW\Cache> is incompatible with the declared type object<Onoi\Cache\Cache> of property $cache.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
245
		}
246 1
247
		// Send an event to ParserAfterTidy and allow it to pass the preliminary
248 1
		// test even in cases where the content doesn't contain any SMW related
249
		// annotations. It is to ensure that when an agent switches to a blank
250
		// version (no SMW related annotations or categories) the update is carried
251
		// out and the store is able to remove any remaining annotations.
252
		$key = smwfCacheKey( 'smw:parseraftertidy', $title->getPrefixedDBKey() );
253 1
		$this->cache->save( $key, $rev_id, $ttl );
254 1
255
		return true;
256 1
	}
257
258
	/**
259
	 * @see ??
260
	 *
261
	 * @since 1.0
262
	 *
263
	 * @param Parser $parser
264
	 * @param Title $title
265
	 * @param integer $timestamp
266 1
	 * @param string $sha1
267
	 */
268 1
	public function onApprovedRevsFileRevisionApproved( $parser, $title, $timestamp, $sha1  ) {
269 1
270
		$ttl = 60 * 60; // 1hr
271
272 1
		if ( $this->cache === null ) {
273
			$this->cache = ApplicationFactory::getInstance()->getCache();
0 ignored issues
show
Documentation Bug introduced by
It seems like \SMW\ApplicationFactory:...tInstance()->getCache() of type object<SMW\Cache> is incompatible with the declared type object<Onoi\Cache\Cache> of property $cache.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
274
		}
275
276
		// @see onApprovedRevsRevisionApproved for the same reason
277
		$key = smwfCacheKey( 'smw:parseraftertidy', $title->getPrefixedDBKey() );
278
		$this->cache->save( $key, $sha1, $ttl );
279
280
		return true;
281
	}
282
283 1
	/**
284
	 * @see https://www.semantic-mediawiki.org/wiki/Hooks#...
285 1
	 *
286 1
	 * @since 1.0
287
	 *
288
	 * @param Title $title
289 1
	 * @param File &$file
290
	 */
291 1
	public function onChangeFile( $title, &$file ) {
292
293
		$approvedRevsHandler = new ApprovedRevsHandler(
294 2
			new ApprovedRevsFacade()
295 2
		);
296 2
297 2
		$approvedRevsHandler->doChangeFile( $title, $file );
298 2
299 2
		return true;
300 2
	}
301 2
302 2
	/**
303 2
	 * @see https://www.semantic-mediawiki.org/wiki/Hooks#...
304 2
	 *
305 2
	 * @since 1.0
306
	 *
307 2
	 * @param Title $title
308
	 * @param File &$file
309
	 */
310
	public function onChangeFileBeforeIngestProcessComplete( $title, &$file ) {
311
312
		$approvedRevsHandler =  new ApprovedRevsHandler(
313
			new ApprovedRevsFacade()
314
		);
315
316
		$approvedRevsHandler->doChangeFile( $title, $file );
317
318
		return true;
319
	}
320
321
	private function registerHandlers( $config ) {
0 ignored issues
show
Unused Code introduced by
The parameter $config 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...
322
		$this->handlers = [
323
			'ApprovedRevsRevisionApproved' => [ $this, 'onApprovedRevsRevisionApproved' ],
324
			'ApprovedRevsFileRevisionApproved' => [ $this, 'onApprovedRevsFileRevisionApproved' ],
325
			'SMW::RevisionGuard::IsApprovedRevision' => [ $this, 'onIsApprovedRevision' ],
326
			'SMW::RevisionGuard::ChangeRevision' => [ $this, 'onChangeRevision' ],
327
			'SMW::RevisionGuard::ChangeRevisionID' => [ $this, 'onOverrideRevisionID' ],
328
			'SMW::RevisionGuard::ChangeFile' => [ $this, 'onChangeFile' ],
329
			'SMW::Property::initProperties' => [ $this, 'onInitProperties' ],
330
			'SMWStore::updateDataBefore' => [ $this, 'onUpdateDataBefore' ],
331
		];
332
	}
333
334
}
335