1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SCI; |
4
|
|
|
|
5
|
|
|
use SMW\Store; |
6
|
|
|
use Onoi\Cache\Cache; |
7
|
|
|
use SMW\ApplicationFactory; |
8
|
|
|
use SMW\DIWikiPage; |
9
|
|
|
use SMWDataItem as DataItem; |
10
|
|
|
use Hooks; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @license GNU GPL v2+ |
14
|
|
|
* @since 1.0 |
15
|
|
|
* |
16
|
|
|
* @author mwjames |
17
|
|
|
*/ |
18
|
|
|
class HookRegistry { |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
private $handlers = array(); |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Options |
27
|
|
|
*/ |
28
|
|
|
private $options; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @since 1.0 |
32
|
|
|
* |
33
|
|
|
* @param Store $store |
34
|
|
|
* @param Cache $cache |
35
|
|
|
* @param Options $options |
36
|
|
|
*/ |
37
|
16 |
|
public function __construct( Store $store, Cache $cache, Options $options ) { |
38
|
16 |
|
$this->options = $options; |
39
|
|
|
|
40
|
16 |
|
$this->addCallbackHandlers( |
41
|
16 |
|
$store, |
42
|
16 |
|
$cache, |
43
|
16 |
|
$this->options |
44
|
16 |
|
); |
45
|
16 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @note Usually only used during unit/integration testing |
49
|
|
|
* |
50
|
|
|
* @since 1.0 |
51
|
|
|
* |
52
|
|
|
* @param string $key |
53
|
|
|
* @param mixed $value |
54
|
|
|
*/ |
55
|
14 |
|
public function setOption( $key, $value ) { |
56
|
14 |
|
$this->options->set( $key, $value ); |
57
|
14 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @since 1.0 |
61
|
|
|
* |
62
|
|
|
* @param string $name |
63
|
|
|
* |
64
|
|
|
* @return boolean |
65
|
|
|
*/ |
66
|
1 |
|
public function isRegistered( $name ) { |
67
|
1 |
|
return Hooks::isRegistered( $name ); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @since 1.0 |
72
|
|
|
*/ |
73
|
14 |
|
public function clear() { |
74
|
14 |
|
foreach ( $this->handlers as $name => $callback ) { |
75
|
14 |
|
Hooks::clear( $name ); |
76
|
14 |
|
} |
77
|
14 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @since 1.0 |
81
|
|
|
* |
82
|
|
|
* @param string $name |
83
|
|
|
* |
84
|
|
|
* @return Callable|false |
85
|
|
|
*/ |
86
|
1 |
|
public function getHandlerFor( $name ) { |
87
|
1 |
|
return isset( $this->handlers[$name] ) ? $this->handlers[$name] : false; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @since 1.0 |
92
|
|
|
*/ |
93
|
14 |
|
public function register() { |
94
|
14 |
|
foreach ( $this->handlers as $name => $callback ) { |
95
|
14 |
|
Hooks::register( $name, $callback ); |
96
|
14 |
|
} |
97
|
14 |
|
} |
98
|
|
|
|
99
|
16 |
|
private function addCallbackHandlers( $store, $cache, $options ) { |
100
|
|
|
|
101
|
16 |
|
$propertyRegistry = new PropertyRegistry(); |
102
|
16 |
|
$namespaceExaminer = ApplicationFactory::getInstance()->getNamespaceExaminer(); |
103
|
|
|
|
104
|
16 |
|
$cacheKeyProvider = new CacheKeyProvider(); |
105
|
16 |
|
$cacheKeyProvider->setCachePrefix( $options->get( 'cachePrefix' ) ); |
106
|
|
|
|
107
|
16 |
|
$citationReferencePositionJournal = new CitationReferencePositionJournal( |
108
|
16 |
|
$cache, |
109
|
|
|
$cacheKeyProvider |
110
|
16 |
|
); |
111
|
|
|
|
112
|
16 |
|
$referenceBacklinksLookup = new ReferenceBacklinksLookup( $store ); |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @see https://www.semantic-mediawiki.org/wiki/Hooks/SMW::Property::initProperties |
116
|
|
|
*/ |
117
|
|
|
$this->handlers['SMW::Property::initProperties'] = function ( $corePropertyRegistry ) use ( $propertyRegistry ) { |
118
|
15 |
|
return $propertyRegistry->registerTo( $corePropertyRegistry ); |
119
|
|
|
}; |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @see https://www.semantic-mediawiki.org/wiki/Hooks/SMW::DataType::initTypes |
123
|
|
|
*/ |
124
|
|
|
$this->handlers['SMW::DataType::initTypes'] = function ( $dataTypeRegistry ) use( $citationReferencePositionJournal ) { |
125
|
|
|
|
126
|
1 |
|
$dataTypeRegistry->registerDatatype( |
127
|
1 |
|
'_sci_ref', |
128
|
1 |
|
'\SCI\DataValues\CitationReferenceValue', |
129
|
|
|
DataItem::TYPE_BLOB |
130
|
1 |
|
); |
131
|
|
|
|
132
|
1 |
|
$dataTypeRegistry->registerDatatype( |
133
|
1 |
|
'_sci_rec', |
134
|
1 |
|
'\SCI\DataValues\CitationFrequencyValue', |
135
|
1 |
|
DataItem::TYPE_WIKIPAGE, |
136
|
1 |
|
false, |
137
|
|
|
true |
138
|
1 |
|
); |
139
|
|
|
|
140
|
|
|
$types = array( |
141
|
1 |
|
'_sci_doi', |
142
|
1 |
|
'_sci_pmcid', |
143
|
1 |
|
'_sci_pmid', |
144
|
1 |
|
'_sci_oclc', |
145
|
1 |
|
'_sci_viaf', |
146
|
|
|
'_sci_olid' |
147
|
1 |
|
); |
148
|
|
|
|
149
|
1 |
|
foreach ( $types as $type ) { |
150
|
1 |
|
$dataTypeRegistry->registerDatatype( |
151
|
1 |
|
$type, |
152
|
1 |
|
'\SCI\DataValues\ResourceIdentifierStringValue', |
153
|
|
|
DataItem::TYPE_BLOB |
154
|
1 |
|
); |
155
|
1 |
|
} |
156
|
|
|
|
157
|
1 |
|
$dataTypeRegistry->registerExtraneousFunction( |
158
|
1 |
|
'\SCI\CitationReferencePositionJournal', |
159
|
|
|
function() use( $citationReferencePositionJournal ) { return $citationReferencePositionJournal; } |
160
|
1 |
|
); |
161
|
|
|
|
162
|
1 |
|
return true; |
163
|
|
|
}; |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @see https://www.semantic-mediawiki.org/wiki/Hooks/SMW::Browse::AfterIncomingPropertiesLookupComplete |
167
|
|
|
*/ |
168
|
|
|
$this->handlers['SMW::Browse::AfterIncomingPropertiesLookupComplete'] = function ( $store, $semanticData, $requestOptions ) use ( $referenceBacklinksLookup ) { |
169
|
|
|
|
170
|
1 |
|
$referenceBacklinksLookup->setRequestOptions( $requestOptions ); |
171
|
1 |
|
$referenceBacklinksLookup->setStore( $store ); |
172
|
|
|
|
173
|
1 |
|
$referenceBacklinksLookup->addReferenceBacklinksTo( |
174
|
|
|
$semanticData |
175
|
1 |
|
); |
176
|
|
|
|
177
|
1 |
|
return true; |
178
|
|
|
}; |
179
|
|
|
|
180
|
|
|
$this->handlers['SMW::Browse::BeforeIncomingPropertyValuesFurtherLinkCreate'] = function ( $property, $subject, &$html ) use ( $referenceBacklinksLookup ) { |
181
|
1 |
|
return $referenceBacklinksLookup->getSpecialPropertySearchFurtherLink( $property, $subject, $html ); |
182
|
|
|
}; |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @see https://www.semantic-mediawiki.org/wiki/Hooks/SMW::Parser::BeforeMagicWordsFinder |
186
|
|
|
*/ |
187
|
|
|
$this->handlers['SMW::Parser::BeforeMagicWordsFinder'] = function( array &$magicWords ) { |
188
|
15 |
|
$magicWords = array_merge( $magicWords, array( 'SCI_NOREFERENCELIST' ) ); |
189
|
15 |
|
return true; |
190
|
|
|
}; |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @see https://www.semantic-mediawiki.org/wiki/Hooks/SMW::SQLStore::AddCustomFixedPropertyTables |
194
|
|
|
*/ |
195
|
|
|
$this->handlers['SMW::SQLStore::AddCustomFixedPropertyTables'] = function( array &$customFixedProperties ) use( $propertyRegistry ) { |
196
|
|
|
|
197
|
|
|
$properties = array( |
198
|
14 |
|
$propertyRegistry::SCI_CITE_KEY, |
199
|
14 |
|
$propertyRegistry::SCI_CITE_REFERENCE, |
200
|
14 |
|
$propertyRegistry::SCI_CITE_TEXT, |
201
|
14 |
|
$propertyRegistry::SCI_CITE, |
202
|
14 |
|
$propertyRegistry::SCI_DOI, |
203
|
14 |
|
$propertyRegistry::SCI_PMCID, |
204
|
14 |
|
$propertyRegistry::SCI_PMID, |
205
|
14 |
|
$propertyRegistry::SCI_OCLC, |
206
|
14 |
|
$propertyRegistry::SCI_VIAF, |
207
|
|
|
$propertyRegistry::SCI_OLID |
208
|
14 |
|
); |
209
|
|
|
|
210
|
14 |
|
foreach ( $properties as $property ) { |
211
|
14 |
|
$customFixedProperties[$property] = str_replace( '__', '_', $property ); |
212
|
14 |
|
} |
213
|
|
|
|
214
|
14 |
|
return true; |
215
|
|
|
}; |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/BeforePageDisplay |
219
|
|
|
*/ |
220
|
|
|
$this->handlers['BeforePageDisplay'] = function ( &$outputPage, &$skin ) use( $namespaceExaminer ) { |
|
|
|
|
221
|
|
|
|
222
|
1 |
|
if ( !$namespaceExaminer->isSemanticEnabled( $outputPage->getTitle()->getNamespace() ) ) { |
223
|
|
|
return true; |
224
|
|
|
} |
225
|
|
|
|
226
|
1 |
|
$outputPage->addModuleStyles( 'ext.scite.styles' ); |
227
|
|
|
|
228
|
1 |
|
$outputPage->addModules( |
229
|
|
|
array( |
230
|
1 |
|
'ext.scite.styles', |
231
|
|
|
'ext.scite.tooltip' |
232
|
1 |
|
) |
233
|
1 |
|
); |
234
|
|
|
|
235
|
1 |
|
return true; |
236
|
|
|
}; |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @note This is bit of a hack but there is no other way to get access to |
240
|
|
|
* the ParserOutput so that it can be used in OutputPageBeforeHTML |
241
|
|
|
* |
242
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/OutputPageParserOutput |
243
|
|
|
*/ |
244
|
|
|
$this->handlers['OutputPageParserOutput'] = function( &$outputPage, $parserOutput ) { |
245
|
10 |
|
$outputPage->smwmagicwords = $parserOutput->getExtensionData( 'smwmagicwords' ); |
246
|
10 |
|
return true; |
247
|
|
|
}; |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/OutputPageBeforeHTML |
251
|
|
|
*/ |
252
|
|
|
$this->handlers['OutputPageBeforeHTML'] = function( &$outputPage, &$text ) use ( $store, $namespaceExaminer, $citationReferencePositionJournal, $cache, $cacheKeyProvider, $options ) { |
253
|
|
|
|
254
|
10 |
|
$referenceListFactory = new ReferenceListFactory( |
255
|
10 |
|
$store, |
256
|
10 |
|
$namespaceExaminer, |
257
|
|
|
$citationReferencePositionJournal |
258
|
10 |
|
); |
259
|
|
|
|
260
|
10 |
|
$cachedReferenceListOutputRenderer = $referenceListFactory->newCachedReferenceListOutputRenderer( |
261
|
10 |
|
new MediaWikiContextInteractor( $outputPage->getContext() ), |
262
|
10 |
|
$cache, |
263
|
10 |
|
$cacheKeyProvider, |
264
|
|
|
$options |
265
|
10 |
|
); |
266
|
|
|
|
267
|
10 |
|
$cachedReferenceListOutputRenderer->addReferenceListToText( $text ); |
268
|
|
|
|
269
|
10 |
|
return true; |
270
|
|
|
}; |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* @see https://www.semantic-mediawiki.org/wiki/Hooks#SMWStore::updateDataBefore |
274
|
|
|
*/ |
275
|
|
|
$this->handlers['SMWStore::updateDataBefore'] = function ( $store, $semanticData ) use ( $cache, $cacheKeyProvider, $options, $citationReferencePositionJournal ) { |
276
|
|
|
|
277
|
15 |
|
$citationMeta = new CitationMeta( $citationReferencePositionJournal ); |
278
|
15 |
|
$citationMeta->setEnabledState( $options->get( 'enabledCitationMetaRecord' ) ); |
279
|
15 |
|
$citationMeta->addMetaRecordToSemanticData( $semanticData ); |
280
|
|
|
|
281
|
15 |
|
$hash = $semanticData->getSubject()->getHash(); |
282
|
|
|
|
283
|
|
|
// No remaining reference means it is time to purge the cache once |
284
|
|
|
// more because as long as a CiteRef exists, CitationReferencePositionJournal |
285
|
|
|
// is able recompute and update the entries but with no CiteRef left |
286
|
|
|
// the last entry will remain and needs to be purged at this point |
287
|
15 |
|
if ( !$citationReferencePositionJournal->hasCitationReference( $semanticData ) ) { |
288
|
14 |
|
$cache->delete( |
289
|
14 |
|
$cacheKeyProvider->getCacheKeyForCitationReference( $hash ) |
290
|
14 |
|
); |
291
|
14 |
|
} |
292
|
|
|
|
293
|
15 |
|
$cache->delete( |
294
|
15 |
|
$cacheKeyProvider->getCacheKeyForReferenceList( $hash ) |
295
|
15 |
|
); |
296
|
|
|
|
297
|
15 |
|
return true; |
298
|
|
|
}; |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* @see https://www.semantic-mediawiki.org/wiki/Hooks#SMW::SQLStore::AfterDeleteSubjectComplete |
302
|
|
|
*/ |
303
|
|
|
$this->handlers['SMW::SQLStore::AfterDeleteSubjectComplete'] = function ( $store, $title ) use ( $cache, $cacheKeyProvider ) { |
304
|
|
|
|
305
|
15 |
|
$hash = DIWikiPage::newFromTitle( $title )->getHash(); |
306
|
|
|
|
307
|
15 |
|
$cache->delete( |
308
|
15 |
|
$cacheKeyProvider->getCacheKeyForCitationReference( $hash ) |
309
|
15 |
|
); |
310
|
|
|
|
311
|
15 |
|
$cache->delete( |
312
|
15 |
|
$cacheKeyProvider->getCacheKeyForReferenceList( $hash ) |
313
|
15 |
|
); |
314
|
|
|
|
315
|
15 |
|
return true; |
316
|
|
|
}; |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* @see https://www.semantic-mediawiki.org/wiki/Hooks#SMW::SQLStore::AfterDataUpdateComplete |
320
|
|
|
*/ |
321
|
|
|
$this->handlers['SMW::SQLStore::AfterDataUpdateComplete'] = function ( $store, $semanticData, $compositePropertyTableDiffIterator ) use ( $referenceBacklinksLookup, $options ) { |
322
|
|
|
|
323
|
15 |
|
$referenceBacklinksLookup->setStore( $store ); |
324
|
|
|
|
325
|
15 |
|
$citationTextChangeUpdateJobDispatcher = new CitationTextChangeUpdateJobDispatcher( |
326
|
15 |
|
$store, |
327
|
|
|
$referenceBacklinksLookup |
328
|
15 |
|
); |
329
|
|
|
|
330
|
15 |
|
$citationTextChangeUpdateJobDispatcher->setEnabledUpdateJobState( |
331
|
15 |
|
$options->get( 'enabledCitationTextChangeUpdateJob' ) |
332
|
15 |
|
); |
333
|
|
|
|
334
|
15 |
|
$citationTextChangeUpdateJobDispatcher->dispatchUpdateJobFor( |
335
|
15 |
|
$semanticData->getSubject(), |
336
|
|
|
$compositePropertyTableDiffIterator |
337
|
15 |
|
); |
338
|
|
|
|
339
|
15 |
|
return true; |
340
|
|
|
}; |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/ResourceLoaderGetConfigVars |
344
|
|
|
*/ |
345
|
|
|
$this->handlers['ResourceLoaderGetConfigVars'] = function ( &$vars ) use ( $options ) { |
346
|
|
|
|
347
|
1 |
|
$vars['ext.scite.config'] = array( |
348
|
1 |
|
'showTooltipForCitationReference' => $options->get( 'showTooltipForCitationReference' ), |
349
|
1 |
|
'tooltipRequestCacheTTL' => $options->get( 'tooltipRequestCacheTTL' ), |
350
|
1 |
|
'cachePrefix' => $options->get( 'cachePrefix' ) |
351
|
1 |
|
); |
352
|
|
|
|
353
|
1 |
|
return true; |
354
|
|
|
}; |
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/ParserFirstCallInit |
358
|
|
|
*/ |
359
|
1 |
|
$this->handlers['ParserFirstCallInit'] = function ( &$parser ) use ( $namespaceExaminer, $options ) { |
360
|
|
|
|
361
|
1 |
|
$parserFunctionFactory = new ParserFunctionFactory(); |
362
|
|
|
|
363
|
1 |
|
list( $name, $definition, $flag ) = $parserFunctionFactory->newSciteParserFunctionDefinition( |
364
|
1 |
|
$namespaceExaminer, |
365
|
|
|
$options |
366
|
1 |
|
); |
367
|
|
|
|
368
|
1 |
|
$parser->setFunctionHook( $name, $definition, $flag ); |
369
|
|
|
|
370
|
1 |
|
list( $name, $definition, $flag ) = $parserFunctionFactory->newReferenceListParserFunctionDefinition(); |
371
|
|
|
|
372
|
1 |
|
$parser->setFunctionHook( $name, $definition, $flag ); |
373
|
|
|
|
374
|
1 |
|
return true; |
375
|
|
|
}; |
376
|
16 |
|
} |
377
|
|
|
|
378
|
|
|
} |
379
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.