forked from mathiasertl/CustomNavBlocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomNavBlocks.php
90 lines (77 loc) · 2.99 KB
/
CustomNavBlocks.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
<?php
# Alert the user that this is not a valid entry point to MediaWiki if they try to access the special pages file directly.
if (!defined('MEDIAWIKI')) {
echo <<<EOT
INSTRUCTIONS
EOT;
exit(1);
}
//self executing anonymous function to prevent global scope assumptions
call_user_func( function() {
$GLOBALS['wgHooks']['SkinTemplateOutputPageBeforeExec'][] = 'addCustomNavBlocks';
$GLOBALS['wgExtensionCredits']['other'][] = array (
'path' => __FILE__,
'name' => 'CustomNavBlocks',
'description' => 'Better customization of your sidebar',
'version' => '2.2.2',
'author' => 'Mathias Ertl, [http://www.luukpeters.nl Luuk Peters]',
'url' => 'https://www.mediawiki.org/wiki/Extension:CustomNavBlocks',
);
});
function addCustomNavBlocks($skin, $tpl) {
global $wgParser, $wgCustomNavBlocksEnable;
if (! $wgCustomNavBlocksEnable)
return true;
$parserOptions = new ParserOptions();
$CustomNavBlocksRaw = $tpl->translator->translate('CustomNavBlocks');
$CustomNavBlocksClean = trim(preg_replace(
array('/<!--(.*)-->/s'), array(''), $CustomNavBlocksRaw));
$blocks = explode("\n", $CustomNavBlocksClean);
$sidebar = array();
foreach ($blocks as $block) {
$tmp = explode('|', $block);
# silently ignore lines that have more than one '|':
if (count($tmp) > 2 || count($tmp) < 1) {
continue;
}
if (count($tmp) == 1 && isset($tpl->data['sidebar'][$block])) {
# try to find default sidebar item
$sidebar[$block] = $tpl->data['sidebar'][$block];
} else {
# some shortcuts
$definition = $tmp[0];
$blockTitle = $tmp[1];
# first, we need a title object:
$title = Title::newFromText($definition, NS_MEDIAWIKI);
if (is_null($title)) {
continue;
}
# return false if a page defined by MediaWiki:CustomNavBlocks
# doesn't exist:
if (! $title->exists()) {
if ($title->quickUserCan('edit')) {
/* make edit link */
$html = Linker::link(
$title, $title->getPrefixedText(), array(),
array('action' => 'edit'));
} else {
$html = '';
}
} else {
# get article and content:
$content = $tpl->translator->translate("$definition");
# parse the mediawiki-syntax into html:
$content = $wgParser->preprocess(
$content, $title, $parserOptions);
$parserOutput = $wgParser->parse(
$content, $title, $parserOptions);
$html = $parserOutput->getText();
}
# make a sidebar block:
$sidebar[$blockTitle] = $html;
}
}
# set sidebar to new thing:
$tpl->set('sidebar', $sidebar);
return true;
}