|
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'][] = [ |
|
49
|
1 |
|
'path' => $this->rootDirectory . '/GitHub.php', |
|
50
|
1 |
|
'name' => 'GitHub', |
|
51
|
1 |
|
'version' => GitHub_VERSION, |
|
52
|
|
|
'author' => [ |
|
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
|
|
|
[ |
|
|
|
|
|
|
111
|
|
|
'file' => [ |
|
112
|
|
|
'default' => 'README.md', |
|
113
|
|
|
'aliases' => 'filename', |
|
114
|
|
|
'message' => 'github-par-filename', |
|
115
|
|
|
], |
|
116
|
|
|
'repo' => [ |
|
117
|
|
|
'default' => $this->defaultGitHubRepo, |
|
118
|
|
|
'aliases' => 'reponame', |
|
119
|
|
|
'message' => 'github-par-reponame', |
|
120
|
|
|
], |
|
121
|
|
|
'branch' => [ |
|
122
|
|
|
'default' => 'master', |
|
123
|
|
|
'aliases' => 'branchname', |
|
124
|
|
|
'message' => 'github-par-branchname', |
|
125
|
|
|
], |
|
126
|
|
|
'lang' => [ |
|
127
|
|
|
'default' => '', |
|
128
|
|
|
'message' => 'github-par-lang', |
|
129
|
|
|
], |
|
130
|
|
|
'line' => [ |
|
131
|
|
|
'default' => false, |
|
132
|
|
|
'message' => 'github-par-line', |
|
133
|
|
|
'type' => 'boolean', |
|
134
|
|
|
], |
|
135
|
|
|
'start' => [ |
|
136
|
|
|
'default' => 1, |
|
137
|
|
|
'message' => 'github-par-start', |
|
138
|
|
|
'type' => 'integer', |
|
139
|
|
|
], |
|
140
|
|
|
'highlight' => [ |
|
141
|
|
|
'default' => '', |
|
142
|
|
|
'message' => 'github-par-highlight', |
|
143
|
|
|
], |
|
144
|
|
|
'inline' => [ |
|
145
|
|
|
'default' => false, |
|
146
|
|
|
'message' => 'github-par-inline', |
|
147
|
|
|
'type' => 'boolean', |
|
148
|
|
|
], |
|
149
|
|
|
], |
|
150
|
|
|
[ |
|
151
|
|
|
'file', |
|
152
|
|
|
'repo', |
|
153
|
|
|
'branch', |
|
154
|
|
|
'lang' |
|
155
|
|
|
] |
|
156
|
|
|
); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
1 |
|
public function getGitHubHookHandler(): HookHandler { |
|
160
|
1 |
|
return new GitHubParserHook( |
|
161
|
1 |
|
new GitHubFetcher( |
|
162
|
1 |
|
$this->newFileFetcher(), |
|
163
|
1 |
|
$this->gitHubUrl, |
|
164
|
1 |
|
$this->repositoryWhitelist |
|
165
|
|
|
) |
|
166
|
|
|
); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
1 |
|
private function newFileFetcher(): FileFetcher { |
|
170
|
1 |
|
return $this->newCachingFileFetcher( |
|
171
|
1 |
|
$this->newLoggingFileFetcher( |
|
172
|
1 |
|
$this->gitHubFetcher === 'mediawiki' ? new MediaWikiFileFetcher() : new SimpleFileFetcher() |
|
173
|
|
|
) |
|
174
|
|
|
); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
1 |
|
private function newLoggingFileFetcher( FileFetcher $fileFetcher ): FileFetcher { |
|
178
|
1 |
|
return new ErrorLoggingFileFetcher( |
|
179
|
1 |
|
$fileFetcher, |
|
180
|
1 |
|
$this->newLogger() |
|
181
|
|
|
); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
1 |
|
private function newLogger(): LoggerInterface { |
|
185
|
1 |
|
return new LegacyLogger( 'GitHub-extension' ); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
1 |
|
private function newCachingFileFetcher( FileFetcher $fileFetcher ): FileFetcher { |
|
189
|
1 |
|
if ( $this->gitHubCache === 'full' ) { |
|
190
|
1 |
|
return new CachingFileFetcher( |
|
191
|
1 |
|
$fileFetcher, |
|
192
|
1 |
|
new CombinatoryCache( array( |
|
193
|
1 |
|
new SimpleInMemoryCache(), |
|
194
|
1 |
|
new MediaWikiCache( wfGetMainCache(), $this->cacheTime ) |
|
195
|
|
|
) ) |
|
196
|
|
|
); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
return $fileFetcher; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
} |
|
203
|
|
|
|
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: