|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SUC; |
|
4
|
|
|
|
|
5
|
|
|
use Hooks; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* @license GNU GPL v2+ |
|
9
|
|
|
* @since 1.0 |
|
10
|
|
|
* |
|
11
|
|
|
* @author mwjames |
|
12
|
|
|
*/ |
|
13
|
|
|
class HookRegistry { |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var array |
|
17
|
|
|
*/ |
|
18
|
|
|
private $handlers = array(); |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var Options |
|
22
|
|
|
*/ |
|
23
|
|
|
private $options; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @since 1.0 |
|
27
|
|
|
* |
|
28
|
|
|
* @param Options $options |
|
29
|
|
|
*/ |
|
30
|
2 |
|
public function __construct( Options $options ) { |
|
31
|
2 |
|
$this->options = $options; |
|
32
|
|
|
|
|
33
|
2 |
|
$this->addCallbackHandlers( |
|
34
|
2 |
|
$this->options |
|
35
|
2 |
|
); |
|
36
|
2 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @since 1.0 |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $name |
|
42
|
|
|
* |
|
43
|
|
|
* @return boolean |
|
44
|
|
|
*/ |
|
45
|
1 |
|
public function isRegistered( $name ) { |
|
46
|
1 |
|
return Hooks::isRegistered( $name ); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @since 1.0 |
|
51
|
|
|
*/ |
|
52
|
|
|
public function clear() { |
|
53
|
|
|
foreach ( $this->handlers as $name => $callback ) { |
|
54
|
|
|
Hooks::clear( $name ); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @since 1.0 |
|
60
|
|
|
* |
|
61
|
|
|
* @param string $name |
|
62
|
|
|
* |
|
63
|
|
|
* @return Callable|false |
|
64
|
|
|
*/ |
|
65
|
1 |
|
public function getHandlerFor( $name ) { |
|
66
|
1 |
|
return isset( $this->handlers[$name] ) ? $this->handlers[$name] : false; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @since 1.0 |
|
71
|
|
|
*/ |
|
72
|
|
|
public function register() { |
|
73
|
|
|
foreach ( $this->handlers as $name => $callback ) { |
|
74
|
|
|
Hooks::register( $name, $callback ); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
2 |
|
private function addCallbackHandlers( $options ) { |
|
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/BeforePageDisplay |
|
82
|
|
|
*/ |
|
83
|
2 |
|
$this->handlers['BeforePageDisplay'] = function ( &$outputPage, &$skin ) { |
|
|
|
|
|
|
84
|
|
|
|
|
85
|
1 |
|
$outputPage->addModuleStyles( 'ext.summary.cards.styles' ); |
|
86
|
|
|
|
|
87
|
1 |
|
$outputPage->addModules( |
|
88
|
|
|
array( |
|
89
|
|
|
'ext.summary.cards' |
|
90
|
1 |
|
) |
|
91
|
1 |
|
); |
|
92
|
|
|
|
|
93
|
1 |
|
return true; |
|
94
|
|
|
}; |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Hook: NewRevisionFromEditComplete called when a revision was inserted |
|
98
|
|
|
* due to an edit |
|
99
|
|
|
* |
|
100
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/NewRevisionFromEditComplete |
|
101
|
|
|
*/ |
|
102
|
2 |
|
$this->handlers['NewRevisionFromEditComplete'] = function ( $wikiPage, $revision, $baseId, $user ) { |
|
|
|
|
|
|
103
|
1 |
|
return BackendCache::getInstance()->invalidateCache( $wikiPage->getTitle() ); |
|
104
|
|
|
}; |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/GetPreferences |
|
108
|
|
|
*/ |
|
109
|
2 |
|
$this->handlers['GetPreferences'] = function ( $user, &$preferences ) { |
|
110
|
|
|
|
|
111
|
|
|
// Option to enable tooltip info |
|
112
|
1 |
|
$preferences['suc-tooltip-disabled'] = array( |
|
113
|
1 |
|
'type' => 'toggle', |
|
114
|
1 |
|
'label-message' => 'suc-tooltip-disabled', |
|
115
|
1 |
|
'section' => 'rendering/suc-options', |
|
116
|
|
|
); |
|
117
|
|
|
|
|
118
|
1 |
|
return true; |
|
119
|
|
|
}; |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/ResourceLoaderGetConfigVars |
|
123
|
|
|
*/ |
|
124
|
|
|
$this->handlers['ResourceLoaderGetConfigVars'] = function ( &$vars ) use ( $options ) { |
|
125
|
|
|
|
|
126
|
1 |
|
$contentLanguage = $GLOBALS['wgContLang']; |
|
127
|
1 |
|
$enabledNamespaceWithTemplate = array(); |
|
128
|
1 |
|
$namespacesByContentLanguage = array(); |
|
129
|
|
|
|
|
130
|
1 |
|
foreach ( $options->get( 'enabledNamespaceWithTemplate' ) as $ns => $template ) { |
|
131
|
|
|
$enabledNamespaceWithTemplate[$contentLanguage->getNsText( $ns )] = $template; |
|
132
|
1 |
|
} |
|
133
|
|
|
|
|
134
|
|
|
// Get literals to match and split when comparing href's during the |
|
135
|
|
|
// JS parse process |
|
136
|
1 |
|
foreach ( \MWNamespace::getCanonicalNamespaces() as $ns => $name ) { |
|
137
|
1 |
|
$namespacesByContentLanguage[$contentLanguage->getNsText( $ns )] = $name; |
|
138
|
1 |
|
} |
|
139
|
|
|
|
|
140
|
1 |
|
$vars['ext.suc.config'] = array( |
|
141
|
1 |
|
'tooltipRequestCacheTTL' => $options->get( 'tooltipRequestCacheTTL' ), |
|
142
|
1 |
|
'cachePrefix' => $options->get( 'cachePrefix' ), |
|
143
|
1 |
|
'enabledForAnonUsers' => $options->get( 'enabledForAnonUsers' ), |
|
144
|
1 |
|
'enabledNamespaceWithTemplate' => $enabledNamespaceWithTemplate, |
|
145
|
1 |
|
'namespacesByContentLanguage' => $namespacesByContentLanguage, |
|
146
|
1 |
|
'namespacesByCanonicalIdentifier' => array_flip( $namespacesByContentLanguage ) |
|
147
|
1 |
|
); |
|
148
|
|
|
|
|
149
|
1 |
|
return true; |
|
150
|
|
|
}; |
|
151
|
2 |
|
} |
|
152
|
|
|
|
|
153
|
|
|
} |
|
154
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: