forked from wikimedia/mediawiki-extensions-Mpdf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMpdfHooks.php
More file actions
129 lines (108 loc) · 2.88 KB
/
Copy pathMpdfHooks.php
File metadata and controls
129 lines (108 loc) · 2.88 KB
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
<?php
class MpdfHooks {
public static function registerExtension() {
global $wgHooks;
if ( class_exists( 'MediaWiki\HookContainer\HookContainer' ) ) {
// MW 1.35+
$wgHooks['SidebarBeforeOutput'][] = "MpdfHooks::onSidebarBeforeOutput";
} else {
// MW < 1.35
$wgHooks['BaseTemplateToolbox'][] = "MpdfHooks::onBaseTemplateToolbox";
}
}
/**
* @param Parser &$parser
*/
public static function onParserFirstCallInit( Parser &$parser ) {
$parser->setFunctionHook( 'mpdftags', 'MpdfHooks::mpdftags_Render' );
}
/**
* Add "PDF Export" link to the toolbox. Called with the
* SidebarBeforeOutput hook, for MW >= 1.35.
*
* @param Skin $skin
* @param array &$sidebar
* @return bool
*/
public static function onSidebarBeforeOutput( Skin $skin, array &$sidebar ) {
global $wgMpdfToolboxLink;
if ( !$wgMpdfToolboxLink ) {
return true;
}
$title = $skin->getTitle();
if ( $title->isSpecialPage() ) {
return true;
}
$sidebar['TOOLBOX']['mpdf'] = [
'msg' => 'mpdf-action',
'href' => $title->getLocalUrl( [ 'action' => 'mpdf' ] ),
'id' => 't-mpdf',
'rel' => 'mpdf'
];
return true;
}
/**
* Add "PDF Export" link to the toolbox. Called with the
* BaseTemplateToolbox hook, for MW < 1.35.
* @param Skin $skin
* @param array &$actions
*
* @return bool true
*/
public static function onSkinTemplateNavigation( $skin, &$actions ) {
global $wgMpdfTab;
if ( $wgMpdfTab ) {
$actions['views']['mpdf'] = [
'class' => false,
'text' => wfMessage( 'mpdf-action' )->text(),
'href' => $skin->getTitle()->getLocalURL( "action=mpdf" ),
];
}
return true;
}
/**
* Add "Export PDF" link to the toolbox.
*
* @param BaseTemplate $skinTemplate
* @param array &$toolbox
* @return bool
*/
public static function onBaseTemplateToolbox( BaseTemplate $skinTemplate, array &$toolbox ) {
global $wgMpdfToolboxLink;
if ( !$wgMpdfToolboxLink ) {
return true;
}
$title = $skinTemplate->getSkin()->getTitle();
// This hook doesn't usually get called for special pages,
// but sometimes it is.
if ( $title->isSpecialPage() ) {
return true;
}
$toolbox['mpdf'] = [
'msg' => 'mpdf-action',
'href' => $title->getLocalUrl( [ 'action' => 'mpdf' ] ),
'id' => 't-mpdf',
'rel' => 'mpdf'
];
return true;
}
/**
* @param Parser &$parser
* @return mixed
*/
public static function mpdftags_Render( &$parser ) {
// Get the parameters that were passed to this function
$params = func_get_args();
array_shift( $params );
// Replace open and close tag for security reason
$values = str_replace( [ '<', '>' ], [ '<', '>' ], $params );
// Insert mpdf tags between <!--mpdf ... mpdf-->
$return = '<!--mpdf';
foreach ( $values as $val ) {
$return .= "<" . $val . " />\n";
}
$return .= "mpdf-->\n";
// Return mpdf tags as raw html
return $parser->insertStripItem( $return );
}
}