Completed
Push — master ( 3f5086...28f5b7 )
by mw
02:06
created

Hooks::onChangeFileBeforeIngestProcessComplete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
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
	public function register() {
74 1
		foreach ( $this->handlers as $name => $callback ) {
75 1
			\Hooks::register( $name, $callback );
76
		}
77 1
	}
78
79
	/**
80
	 * @since  1.0
81
	 */
82 1
	public function deregister() {
83 1
		foreach ( array_keys( $this->handlers ) as $name ) {
84
85 1
			\Hooks::clear( $name );
86
87
			// Remove registered `wgHooks` hooks that are not cleared by the
88
			// previous call
89 1
			if ( isset( $GLOBALS['wgHooks'][$name] ) ) {
90 1
				unset( $GLOBALS['wgHooks'][$name] );
91
			}
92
		}
93 1
	}
94
95
	/**
96
	 * @since  1.0
97
	 *
98
	 * @param string $name
99
	 *
100
	 * @return boolean
101
	 */
102 1
	public function isRegistered( $name ) {
103 1
		return \Hooks::isRegistered( $name );
104
	}
105
106
	/**
107
	 * @since  1.0
108
	 *
109
	 * @param string $name
110
	 *
111
	 * @return array
112
	 */
113 1
	public function getHandlers( $name ) {
114 1
		return \Hooks::getHandlers( $name );
115
	}
116
117
	/**
118
	 * @since 1.0
119
	 *
120
	 * @param Title $title
121
	 * @param integer $latestRevID
122
	 */
123 1
	public function onSkipUpdate( $title, $latestRevID ) {
124
125 1
		$approvedRevsHandler =  new ApprovedRevsHandler(
126 1
			new ApprovedRevsFacade()
127
		);
128
129 1
		return $approvedRevsHandler->isApprovedUpdate( $title, $latestRevID );
130
	}
131
132
	/**
133
	 * @since 1.0
134
	 *
135
	 * @param Title $title
136
	 * @param Revision|null &$revision
137
	 */
138 1
	public function onChangeRevision( $title, &$revision ) {
139
140 1
		$approvedRevsHandler =  new ApprovedRevsHandler(
141 1
			new ApprovedRevsFacade()
142
		);
143
144 1
		$approvedRevsHandler->doChangeRevision( $title, $revision );
145
146 1
		return true;
147
	}
148
149
	/**
150
	 * @since 1.0
151
	 *
152
	 * @param Title $title
153
	 * @param integer &$latestRevID
154
	 */
155 1
	public function onOverrideRevisionID( $title, &$latestRevID ) {
156
157 1
		$approvedRevsHandler =  new ApprovedRevsHandler(
158 1
			new ApprovedRevsFacade()
159
		);
160
161 1
		$approvedRevsHandler->doChangeRevisionID( $title, $latestRevID );
162
163 1
		return true;
164
	}
165
166
	/**
167
	 * @see https://www.semantic-mediawiki.org/wiki/Hooks#SMW::Property::initProperties
168
	 *
169
	 * @since 1.0
170
	 *
171
	 * @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...
172
	 * @param integer &$latestRevID
173
	 */
174 1
	public function onInitProperties( $registry ) {
175
176 1
		$propertyRegistry = new PropertyRegistry();
177 1
		$propertyRegistry->register( $registry );
178
179 1
		return true;
180
	}
181
182
	/**
183
	 * @see https://www.semantic-mediawiki.org/wiki/Hooks#SMWStore::updateDataBefore
184
	 *
185
	 * @since 1.0
186
	 *
187
	 * @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...
188
	 * @param integer &$latestRevID
189
	 */
190 1
	public function onUpdateDataBefore( $store, $semanticData ) {
191
192 1
		$propertyAnnotator = new PropertyAnnotator(
193 1
			new ServicesFactory()
194
		);
195
196 1
		$propertyAnnotator->setLogger(
197 1
			ApplicationFactory::getInstance()->getMediaWikiLogger( 'smw-approved-revs' )
198
		);
199
200 1
		$propertyAnnotator->addAnnotation( $semanticData );
201
202 1
		return true;
203
	}
204
205
	/**
206
	 * @see ??
207
	 *
208
	 * @since 1.0
209
	 *
210
	 * @param ParserOutput $output
211
	 * @param Title $title
212
	 * @param integer $rev_id
213
	 * @param string $content
214
	 */
215 1
	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...
216
217 1
		$ttl = 60 * 60; // 1hr
218
219 1
		if ( $this->cache === null ) {
220
			$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...
221
		}
222
223
		// Send an event to ParserAfterTidy and allow it to pass the preliminary
224
		// test even in cases where the content doesn't contain any SMW related
225
		// annotations. It is to ensure that when an agent switches to a blank
226
		// version (no SMW related annotations or categories) the update is carried
227
		// out and the store is able to remove any remaining annotations.
228 1
		$key = smwfCacheKey( 'smw:parseraftertidy', $title->getPrefixedDBKey() );
229 1
		$this->cache->save( $key, $rev_id, $ttl );
230
231 1
		return true;
232
	}
233
234
	/**
235
	 * @see ??
236
	 *
237
	 * @since 1.0
238
	 *
239
	 * @param Parser $parser
240
	 * @param Title $title
241
	 * @param integer $timestamp
242
	 * @param string $sha1
243
	 */
244 1
	public function onApprovedRevsFileRevisionApproved( $parser, $title, $timestamp, $sha1  ) {
245
246 1
		$ttl = 60 * 60; // 1hr
247
248 1
		if ( $this->cache === null ) {
249
			$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...
250
		}
251
252
		// @see onApprovedRevsRevisionApproved for the same reason
253 1
		$key = smwfCacheKey( 'smw:parseraftertidy', $title->getPrefixedDBKey() );
254 1
		$this->cache->save( $key, $sha1, $ttl );
255
256 1
		return true;
257
	}
258
259
	/**
260
	 * @see https://www.semantic-mediawiki.org/wiki/Hooks#SMW::Config::BeforeCompletion
261
	 *
262
	 * @since 1.0
263
	 *
264
	 * @param array &$config
265
	 */
266 1
	public function onConfigBeforeCompletion( &$config ) {
267
268 1
		if ( isset( $config['smwgImportFileDirs'] ) ) {
269 1
			$config['smwgImportFileDirs'] += [ 'sar' => __DIR__ . '/../data/import' ];
270
		}
271
272 1
		return true;
273
	}
274
275
	/**
276
	 * @see https://www.semantic-mediawiki.org/wiki/Hooks#...
277
	 *
278
	 * @since 1.0
279
	 *
280
	 * @param Title $title
281
	 * @param File &$file
282
	 */
283 1
	public function onChangeFileBeforeIngestProcessComplete( $title, &$file ) {
284
285 1
		$approvedRevsHandler =  new ApprovedRevsHandler(
286 1
			new ApprovedRevsFacade()
287
		);
288
289 1
		$approvedRevsHandler->doChangeFile( $title, $file );
290
291 1
		return true;
292
	}
293
294 2
	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...
295 2
		$this->handlers = [
296 2
			'ApprovedRevsRevisionApproved' => [ $this, 'onApprovedRevsRevisionApproved' ],
297 2
			'ApprovedRevsFileRevisionApproved' => [ $this, 'onApprovedRevsFileRevisionApproved' ],
298 2
			'SMW::LinksUpdate::ApprovedUpdate' => [ $this, 'onSkipUpdate' ],
299 2
			'SMW::DataUpdater::SkipUpdate' => [ $this, 'onSkipUpdate' ],
300 2
			'SMW::Parser::ChangeRevision' => [ $this, 'onChangeRevision' ],
301 2
			'SMW::Factbox::OverrideRevisionID' => [ $this, 'onOverrideRevisionID' ],
302 2
			'SMW::Property::initProperties' => [ $this, 'onInitProperties' ],
303 2
			'SMWStore::updateDataBefore' => [ $this, 'onUpdateDataBefore' ],
304 2
			'SMW::Config::BeforeCompletion' => [ $this, 'onConfigBeforeCompletion' ],
305 2
			'SMW::ElasticStore::FileIndexer::ChangeFileBeforeIngestProcessComplete' => [ $this, 'onChangeFileBeforeIngestProcessComplete' ]
306
		];
307 2
	}
308
309
}
310