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