1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GitHub; |
4
|
|
|
|
5
|
|
|
use FileFetcher\CachingFileFetcher; |
6
|
|
|
use FileFetcher\ErrorLoggingFileFetcher; |
7
|
|
|
use FileFetcher\FileFetcher; |
8
|
|
|
use FileFetcher\SimpleFileFetcher; |
9
|
|
|
use MediaWiki\Logger\LegacyLogger; |
10
|
|
|
use ParserHooks\FunctionRunner; |
11
|
|
|
use ParserHooks\HookDefinition; |
12
|
|
|
use ParserHooks\HookHandler; |
13
|
|
|
use ParserHooks\HookRegistrant; |
14
|
|
|
use Psr\Log\LoggerInterface; |
15
|
|
|
use Psr\Log\NullLogger; |
16
|
|
|
use SimpleCache\Cache\CombinatoryCache; |
17
|
|
|
use SimpleCache\Cache\MediaWikiCache; |
18
|
|
|
use SimpleCache\Cache\SimpleInMemoryCache; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @licence GNU GPL v2+ |
22
|
|
|
* @author Jeroen De Dauw < [email protected] > |
23
|
|
|
*/ |
24
|
|
|
class Setup { |
25
|
|
|
|
26
|
|
|
private $globals; |
27
|
|
|
private $rootDirectory; |
28
|
|
|
private $defaultGitHubRepo = 'JeroenDeDauw/GitHub'; |
29
|
|
|
private $cacheTime = 600; |
30
|
|
|
private $gitHubUrl = 'https://cdn.rawgit.com'; |
31
|
|
|
private $gitHubFetcher = 'simple'; |
32
|
|
|
private $gitHubCache = 'full'; |
33
|
|
|
private $repositoryWhitelist = []; |
34
|
|
|
|
35
|
1 |
|
public function __construct( &$globals, string $rootDirectory ) { |
36
|
1 |
|
$this->globals =& $globals; |
37
|
1 |
|
$this->rootDirectory = $rootDirectory; |
38
|
1 |
|
} |
39
|
|
|
|
40
|
1 |
|
public function run() { |
41
|
1 |
|
$this->loadSettings(); |
42
|
|
|
|
43
|
1 |
|
$this->registerExtensionCredits(); |
44
|
1 |
|
$this->registerParserHookHandler(); |
45
|
1 |
|
} |
46
|
|
|
|
47
|
1 |
|
private function registerExtensionCredits() { |
48
|
1 |
|
$this->globals['wgExtensionCredits']['other'][] = array( |
49
|
1 |
|
'path' => $this->rootDirectory . '/GitHub.php', |
50
|
1 |
|
'name' => 'GitHub', |
51
|
1 |
|
'version' => GitHub_VERSION, |
52
|
|
|
'author' => array( |
53
|
|
|
'[https://www.mediawiki.org/wiki/User:Jeroen_De_Dauw Jeroen De Dauw]', |
54
|
|
|
), |
55
|
1 |
|
'url' => 'https://github.com/JeroenDeDauw/GitHub', |
56
|
1 |
|
'descriptionmsg' => 'github-desc', |
57
|
1 |
|
'license-name' => 'GPL-2.0+' |
58
|
|
|
); |
59
|
1 |
|
} |
60
|
|
|
|
61
|
1 |
|
private function loadSettings() { |
62
|
1 |
|
if ( array_key_exists( 'egGitHubDefaultRepo', $this->globals ) ) { |
63
|
|
|
$this->defaultGitHubRepo = $this->globals['egGitHubDefaultRepo']; |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
if ( array_key_exists( 'egGitHubCacheTime', $this->globals ) ) { |
67
|
|
|
$this->cacheTime = $this->globals['egGitHubCacheTime']; |
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
if ( array_key_exists( 'egGitHubUrl', $this->globals ) ) { |
71
|
|
|
$this->gitHubUrl = $this->globals['egGitHubUrl']; |
72
|
|
|
} |
73
|
|
|
|
74
|
1 |
|
if ( array_key_exists( 'egGitHubFetcher', $this->globals ) ) { |
75
|
|
|
$this->gitHubFetcher = $this->globals['egGitHubFetcher']; |
76
|
|
|
} |
77
|
|
|
|
78
|
1 |
|
if ( array_key_exists( 'egGitHubCache', $this->globals ) ) { |
79
|
|
|
$this->gitHubCache = $this->globals['egGitHubCache']; |
80
|
|
|
} |
81
|
|
|
|
82
|
1 |
|
if ( array_key_exists( 'egGitHubRepositoryWhitelist', $this->globals ) ) { |
83
|
|
|
$this->repositoryWhitelist = $this->globals['egGitHubRepositoryWhitelist']; |
84
|
|
|
} |
85
|
1 |
|
} |
86
|
|
|
|
87
|
1 |
|
private function registerParserHookHandler() { |
88
|
1 |
|
$self = $this; |
89
|
|
|
|
90
|
|
|
$this->globals['wgHooks']['ParserFirstCallInit'][] = function( \Parser &$parser ) use ( $self ) { |
91
|
|
|
$hookRegistrant = new HookRegistrant( $parser ); |
92
|
|
|
|
93
|
|
|
$hookRegistrant->registerFunction( |
94
|
|
|
new FunctionRunner( |
95
|
|
|
$self->getGitHubHookDefinition(), |
96
|
|
|
$self->getGitHubHookHandler(), |
97
|
|
|
array( |
98
|
|
|
FunctionRunner::OPT_DO_PARSE => false |
99
|
|
|
) |
100
|
|
|
) |
101
|
|
|
); |
102
|
|
|
|
103
|
|
|
return true; |
104
|
|
|
}; |
105
|
1 |
|
} |
106
|
|
|
|
107
|
|
|
public function getGitHubHookDefinition(): HookDefinition { |
108
|
|
|
return new HookDefinition( |
109
|
|
|
'github', |
110
|
|
|
array( |
|
|
|
|
111
|
|
|
'file' => array( |
112
|
|
|
'default' => 'README.md', |
113
|
|
|
'aliases' => 'filename', |
114
|
|
|
'message' => 'github-par-filename', |
115
|
|
|
), |
116
|
|
|
'repo' => array( |
117
|
|
|
'default' => $this->defaultGitHubRepo, |
118
|
|
|
'aliases' => 'reponame', |
119
|
|
|
'message' => 'github-par-reponame', |
120
|
|
|
), |
121
|
|
|
'branch' => array( |
122
|
|
|
'default' => 'master', |
123
|
|
|
'aliases' => 'branchname', |
124
|
|
|
'message' => 'github-par-branchname', |
125
|
|
|
), |
126
|
|
|
'lang' => array( |
127
|
|
|
'default' => '', |
128
|
|
|
'message' => 'github-par-lang', |
129
|
|
|
), |
130
|
|
|
'line' => array( |
131
|
|
|
'default' => false, |
132
|
|
|
'message' => 'github-par-line', |
133
|
|
|
'type' => 'boolean', |
134
|
|
|
), |
135
|
|
|
'start' => array( |
136
|
|
|
'default' => 1, |
137
|
|
|
'message' => 'github-par-start', |
138
|
|
|
'type' => 'integer', |
139
|
|
|
), |
140
|
|
|
'highlight' => array( |
141
|
|
|
'default' => '', |
142
|
|
|
'message' => 'github-par-highlight', |
143
|
|
|
), |
144
|
|
|
'inline' => array( |
145
|
|
|
'default' => false, |
146
|
|
|
'message' => 'github-par-inline', |
147
|
|
|
'type' => 'boolean', |
148
|
|
|
), |
149
|
|
|
), |
150
|
|
|
array( 'file', 'repo', 'branch', 'lang' ) |
151
|
|
|
); |
152
|
|
|
} |
153
|
|
|
|
154
|
1 |
|
public function getGitHubHookHandler(): HookHandler { |
155
|
1 |
|
return new GitHubParserHook( |
156
|
1 |
|
new GitHubFetcher( |
157
|
1 |
|
$this->newFileFetcher(), |
158
|
1 |
|
$this->gitHubUrl, |
159
|
1 |
|
$this->repositoryWhitelist |
160
|
|
|
) |
161
|
|
|
); |
162
|
|
|
} |
163
|
|
|
|
164
|
1 |
|
private function newFileFetcher(): FileFetcher { |
165
|
1 |
|
return $this->newCachingFileFetcher( |
166
|
1 |
|
$this->newLoggingFileFetcher( |
167
|
1 |
|
$this->gitHubFetcher === 'mediawiki' ? new MediaWikiFileFetcher() : new SimpleFileFetcher() |
168
|
|
|
) |
169
|
|
|
); |
170
|
|
|
} |
171
|
|
|
|
172
|
1 |
|
private function newLoggingFileFetcher( FileFetcher $fileFetcher ): FileFetcher { |
173
|
1 |
|
return new ErrorLoggingFileFetcher( |
174
|
1 |
|
$fileFetcher, |
175
|
1 |
|
$this->newLogger() |
176
|
|
|
); |
177
|
|
|
} |
178
|
|
|
|
179
|
1 |
|
private function newLogger(): LoggerInterface { |
180
|
1 |
|
return new LegacyLogger( 'GitHub-extension' ); |
181
|
|
|
} |
182
|
|
|
|
183
|
1 |
|
private function newCachingFileFetcher( FileFetcher $fileFetcher ): FileFetcher { |
184
|
1 |
|
if ( $this->gitHubCache === 'full' ) { |
185
|
1 |
|
return new CachingFileFetcher( |
186
|
1 |
|
$fileFetcher, |
187
|
1 |
|
new CombinatoryCache( array( |
188
|
1 |
|
new SimpleInMemoryCache(), |
189
|
1 |
|
new MediaWikiCache( wfGetMainCache(), $this->cacheTime ) |
190
|
|
|
) ) |
191
|
|
|
); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return $fileFetcher; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
} |
198
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: