|
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 |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
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 ) { |
|
|
|
|
|
|
216
|
|
|
|
|
217
|
1 |
|
$ttl = 60 * 60; // 1hr |
|
218
|
|
|
|
|
219
|
1 |
|
if ( $this->cache === null ) { |
|
220
|
|
|
$this->cache = ApplicationFactory::getInstance()->getCache(); |
|
|
|
|
|
|
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(); |
|
|
|
|
|
|
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
|
2 |
|
private function registerHandlers( $config ) { |
|
|
|
|
|
|
276
|
2 |
|
$this->handlers = [ |
|
277
|
2 |
|
'ApprovedRevsRevisionApproved' => [ $this, 'onApprovedRevsRevisionApproved' ], |
|
278
|
2 |
|
'ApprovedRevsFileRevisionApproved' => [ $this, 'onApprovedRevsFileRevisionApproved' ], |
|
279
|
2 |
|
'SMW::LinksUpdate::ApprovedUpdate' => [ $this, 'onSkipUpdate' ], |
|
280
|
2 |
|
'SMW::DataUpdater::SkipUpdate' => [ $this, 'onSkipUpdate' ], |
|
281
|
2 |
|
'SMW::Parser::ChangeRevision' => [ $this, 'onChangeRevision' ], |
|
282
|
2 |
|
'SMW::Factbox::OverrideRevisionID' => [ $this, 'onOverrideRevisionID' ], |
|
283
|
2 |
|
'SMW::Property::initProperties' => [ $this, 'onInitProperties' ], |
|
284
|
2 |
|
'SMWStore::updateDataBefore' => [ $this, 'onUpdateDataBefore' ], |
|
285
|
2 |
|
'SMW::Config::BeforeCompletion' => [ $this, 'onConfigBeforeCompletion' ] |
|
286
|
|
|
]; |
|
287
|
2 |
|
} |
|
288
|
|
|
|
|
289
|
|
|
} |
|
290
|
|
|
|
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
$irelandis not defined by the methodfinale(...).The most likely cause is that the parameter was changed, but the annotation was not.