-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNamespaceLinks.body.php
252 lines (228 loc) · 5.73 KB
/
NamespaceLinks.body.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
$wgNLConfigMap = array();
$wgNLEnabled = true;
$wgNLMainAliases = array();
function NLReplaceLinks ($text, $nsText) {
/*
* Assign all links in the given text with no namespace the namespace
* given by $nsText
*/
$links = array();
preg_match_all('/\[\[[^]]+\]\]/', $text, $links);
$links = $links[0];
foreach ($links as $linkText) {
if ($linkText[2] == '#') {
// Skip section links within current article (e.g. [[#section]])
continue;
}
if ($linkText[2] == '/') {
continue;
}
// Replace multiple leaading :'s with a single :
$linkText = preg_replace('/^\[\[:+/', '[[:', $linkText);
if (preg_match('/::/', $linkText)) {
// Skip links that have double colons, like MediaWiki
continue;
}
$link = new NLLink($linkText);
if (!$link->valid) {
continue;
}
if ($link->ns == NS_FILE || $link->ns == NS_CATEGORY) {
continue;
}
if (!$link->hasNS) {
$link->nsText = $nsText;
}
$text = str_replace($linkText, $link->render(), $text);
}
return $text;
}
function NLNSNameToID ($nsName) {
$t = Title::newFromText("$nsName:Dummy text");
return $t->mNamespace;
}
function NLParseConfig ($text) {
/*
* Parses configuration text, in the following format:
*
* *ns=defaultns
* *ns2=defaultns2
* ...
*
* Returns an array of old_ns_id => new_ns_name pairs (for convenience)
*/
$map = array();
$text = preg_replace('/\n+/', "\n", $text);
$text = str_replace("\r", "", $text);
$lines = preg_split('/\n/', $text);
foreach ($lines as $line) {
$line = preg_replace('/^\*/', '', $line);
$parts = preg_split('/=/', $line, 2);
if (count($parts) == 1) { // bad syntax
continue;
}
$map[NLNSNameToID($parts[0])] = $parts[1];
}
return $map;
}
class NLLink {
/*
* A basic link
*
* Extracts page (with namespace) and text from a wiki link, but not
* the suffix (if it exists)
*/
public $contents;
public $hasNS;
public $title;
public $text;
public $ns;
public $nsText;
public function __construct ($text) {
/*
* Takes a string of text in wiki link format
*/
// Safety check
$this->valid = true;
$matches = array();
$isLink = preg_match('/\[\[[^]]+\]\]/', $text, $matches);
if (!$isLink) {
trigger_error("`$text` does not contain a valid link.");
}
$linkText = $matches[0];
// Remove leading [[ and trailing ]]
$linkContents = substr($linkText, 2, strlen($linkText) - 4);
$this->contents = $linkContents;
$parts = preg_split('/\|/', $linkContents, 2);
$title = '';
$text = '';
if (count($parts) == 2) {
$title = $parts[0];
$text = $parts[1];
}
else {
$title = $text = $parts[0];
}
if ($text == '') {
$this->valid = false;
return;
}
if ($text[0] == ':') {
/* For links like [[:Page]] (as in MediaWiki:Searchmenu-new)
* or [[:Category:Name]] (for escaping category links),
* the colon can safely be left off the text displayed
* in the link.
*/
$text = substr($text, 1);
}
$wtitle = Title::newFromText($title);
$this->wtitle = $wtitle;
if ($wtitle == null || $wtitle->getDBkey() == '') {
$this->valid = false;
return;
}
$hasNS = false; // Whether the link has an *explicit* namespace
$nsText = '';
// check for main aliases
$hasExplicitMainAlias = false;
global $wgNLMainAliases;
if ($wtitle->mNamespace === NS_MAIN && strpos($linkContents, ':') !== false) {
foreach ($wgNLMainAliases as $alias) {
if (strpos(strtolower(trim($linkContents)), strtolower($alias) . ':') === 0) {
$hasExplicitMainAlias = true;
break;
}
}
}
if ($wtitle->mInterwiki || $wtitle->mNamespace || $hasExplicitMainAlias) {
$hasNS = true;
$title = preg_split('/:/', $title, 2);
$nsText = $title[0];
$title = $title[1];
}
$this->hasNS = $hasNS;
$this->ns = $wtitle->mNamespace;
$this->nsText = $nsText;
$this->title = $title;
$this->text = $text;
}
public function render () {
/*
* Render the link
*
* Returns [[ns:title|text]]
*/
if (!$this->hasNS && !$this->nsText) {
$str = "[[{$this->title}|{$this->text}]]";
}
else {
$str = "[[{$this->nsText}:{$this->title}|{$this->text}]]";
}
return preg_replace('/([^:]+):+([^|]+)/', '$1:$2', $str);
}
}
class NLHooks {
static public function init (&$parser) {
global $wgNLConfigMap, $wgLanguageCode;
$wgNLConfigMap = NLParseConfig(wfMessage('namespace-links')->inLanguage($wgLanguageCode)->plain());
$parser->setFunctionHook('nlenable', 'NLHooks::enable');
$parser->setFunctionHook('nlenabled', 'NLHooks::enabled');
global $wgNLMainAliases, $wgNamespaceAliases;
foreach ($wgNamespaceAliases as $key => $ns) {
if ($ns === NS_MAIN) {
$wgNLMainAliases[] = strtolower($key);
}
}
return true;
}
static public function parseLinks (&$parser, &$text) {
global $wgNLEnabled;
if (!$wgNLEnabled) {
return true;
}
global $wgNLConfigMap;
$currentNSID = $parser->mTitle->getNamespace();
$defaultNSName = NS_MAIN;
if (array_key_exists($currentNSID, $wgNLConfigMap)) {
$defaultNSName = $wgNLConfigMap[$currentNSID];
}
$oldText = $text;
$newText = NLReplaceLinks($text, $defaultNSName);
$text = $newText;
return true;
}
static public function enable (&$parser, $set_enabled='1') {
global $wgNLEnabled;
$set_enabled = strtolower($set_enabled);
switch ($set_enabled) {
case '0':
case 'no':
case 'false':
case 'n':
$enabled = false;
break;
default:
$enabled = true;
break;
}
$wgNLEnabled = $enabled;
}
static public function enabled (&$parser) {
global $wgNLEnabled;
$args = func_get_args();
if (count($args) <= 2) {
// {{#nlenabled:}} or {{#nlenabled:1}}
return $wgNLEnabled ? ($args[1] ? $args[1] : '1') : '';
}
else {
// {{#nlenabled:1|0}}
if ($wgNLEnabled) {
return $args[1];
}
else {
return $args[2];
}
}
}
}