forked from wikimedia/mediawiki-extensions-Widgets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWidgetRenderer.php
More file actions
166 lines (136 loc) · 4.46 KB
/
Copy pathWidgetRenderer.php
File metadata and controls
166 lines (136 loc) · 4.46 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
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
<?php
use MediaWiki\MediaWikiServices;
/**
* Class holding functions for displaying widgets.
*/
class WidgetRenderer {
/**
* @var string The prefix for the widget strip marker.
*
* \xEF\xBF\xBC = U+FFFC (Object replacement) which is unlikely to be in text.
*/
private static $markerPrefix = "\xEF\xBF\xBCSTART_WIDGET";
/**
* @var string The suffix for the widget strip marker.
*/
private static $markerSuffix = "END_WIDGET";
/**
* @param Parser &$parser
* @param string $widgetName
*
* @return string
*/
public static function renderWidget( &$parser, $widgetName ) {
global $wgWidgetsCompileDir;
$smarty = new Smarty;
$smarty->left_delimiter = '<!--{';
$smarty->right_delimiter = '}-->';
$smarty->compile_dir = $wgWidgetsCompileDir;
// Avoid displaying warnings, which show up with more frequency with PHP 8.
$smarty->error_reporting = E_ERROR;
// registering custom Smarty plugins
$smarty->addPluginsDir( __DIR__ . "/smarty_plugins/" );
$smarty->enableSecurity( 'WidgetSecurity' );
// Register the Widgets extension functions.
$wikiResource = new SmartyResourceWiki( $parser );
$smarty->registerResource(
'wiki',
$wikiResource
);
$params = func_get_args();
// The first and second params are the parser and the widget
// name - we already have both.
array_shift( $params );
array_shift( $params );
$params_tree = [];
foreach ( $params as $param ) {
$pair = explode( '=', $param, 2 );
if ( count( $pair ) == 2 ) {
$key = trim( $pair[0] );
$val = trim( $pair[1] );
} else {
$key = $param;
$val = true;
}
if ( $val == 'false' ) {
$val = false;
}
/* If the name of the parameter has object notation
a.b.c.d
then we assign stuff to hash of hashes, not scalar
*/
$keys = explode( '.', $key );
// $subtree will be moved from top to the bottom and
// at the end will point to the last level.
$subtree =& $params_tree;
// Go through all the keys but the last one.
$last_key = array_pop( $keys );
foreach ( $keys as $subkey ) {
// If next level of subtree doesn't exist yet,
// create an empty one.
if ( !array_key_exists( $subkey, $subtree ) ) {
$subtree[$subkey] = [];
}
// move to the lower level
$subtree =& $subtree[$subkey];
}
// last portion of the key points to itself
if ( isset( $subtree[$last_key] ) ) {
// If this is already an array, push into it;
// otherwise, convert into an array first.
if ( !is_array( $subtree[$last_key] ) ) {
$subtree[$last_key] = [ $subtree[$last_key] ];
}
$subtree[$last_key][] = $val;
} else {
// doesn't exist yet, just setting a value
$subtree[$last_key] = $val;
}
}
$smarty->assign( $params_tree );
try {
$output = $smarty->fetch( "wiki:$widgetName" );
} catch ( Exception $e ) {
wfDebugLog( "Widgets", "Smarty exception while parsing '$widgetName': " . $e->getMessage() );
if ( class_exists( 'MediaWiki\Html\Html' ) ) {
// MW 1.40+
$htmlClass = 'MediaWiki\Html\Html';
} else {
$htmlClass = 'Html';
}
return $htmlClass::element( 'div', [ 'class' => 'error' ],
wfMessage( 'widgets-error', $widgetName )->text() . ': ' . $e->getMessage() );
}
$services = MediaWikiServices::getInstance();
$languageConverter = $services->getLanguageConverterFactory()
->getLanguageConverter( $services->getContentLanguage() );
$output = $languageConverter->convert( $output );
// To prevent the widget output from being tampered with, the
// compiled HTML is stored and a strip marker with an index to
// retrieve it later is returned.
// More reliable replacement. See T149488.
$dash = strpos( $output, '"' ) !== false ? "\"'-" : '-';
$marker = $dash . wfRandomString( 16 );
$widgets = (array)$parser->getOutput()->getExtensionData( 'widgetReplacements' );
$widgets[$marker] = $output;
$parser->getOutput()->setExtensionData( 'widgetReplacements', $widgets );
return self::$markerPrefix . $marker . self::$markerSuffix;
}
/**
* @param Parser $parser
* @param string &$text
*/
public static function outputCompiledWidget( $parser, &$text ) {
$replacements = $parser->getOutput()->getExtensionData( 'widgetReplacements' );
if ( !is_array( $replacements ) ) {
return;
}
$text = preg_replace_callback(
'/' . self::$markerPrefix . "(\"?'?-[a-z0-9]{16})" . self::$markerSuffix . '/S',
static function ( $matches ) use ( $replacements ) {
return $replacements[$matches[1]];
},
$text
);
}
}