summaryrefslogtreecommitdiff
blob: 0d3af65ff56e1a7294b89b63d8235c7681771f3f (plain)
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
<?php
/**
 * Class holding functions for displaying widgets.
 */

class WidgetRenderer {
	// The prefix and suffix for the widget strip marker.
	private static $markerPrefix = "START_WIDGET";
	private static $markerSuffix = "END_WIDGET";

	// Stores the compiled widgets for after the parser has run.
	// Must be public for use in anonymous callback function in PHP 5.3
	public static $widgets = array();

	public static function initRandomString() {
		// Add a random string to the prefix to ensure no conflicts
		// with normal content.
		self::$markerPrefix .= wfRandomString( 16 );
	}

	public static function renderWidget( &$parser, $widgetName ) {
		global $IP, $wgWidgetsCompileDir;

		$smarty = new Smarty;
		$smarty->left_delimiter = '<!--{';
		$smarty->right_delimiter = '}-->';
		$smarty->compile_dir = $wgWidgetsCompileDir;

		// registering custom Smarty plugins
		$smarty->addPluginsDir( __DIR__ . "/smarty_plugins/" );

		$smarty->enableSecurity();
		// These settings were for Smarty v2 - they don't seem to
		// have an equivalent in Smarty v3.
		/*
		$smarty->security_settings = array(
			'IF_FUNCS' => array(
					'is_array',
					'isset',
					'array',
					'list',
					'count',
					'sizeof',
					'in_array',
					'true',
					'false',
					'null'
					),
			'MODIFIER_FUNCS' => array( 'validate' )
		);
		*/

		// Register the Widgets extension functions.
		$smarty->registerResource(
			'wiki',
			array(
				array( 'WidgetRenderer', 'wiki_get_template' ),
				array( 'WidgetRenderer', 'wiki_get_timestamp' ),
				array( 'WidgetRenderer', 'wiki_get_secure' ),
				array( 'WidgetRenderer', 'wiki_get_trusted' )
			)
		);

		$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 = array();

		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] = array();
				}

				// 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] = array( $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() );
			return '<div class="error">' . wfMessage( 'widgets-error', htmlentities( $widgetName ) )->text() . ': ' . $e->getMessage() . '</div>';
		}

		// 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.
		$index = array_push( self::$widgets, $output ) - 1;
		return self::$markerPrefix . '-' . $index . self::$markerSuffix;
	}

	public static function outputCompiledWidget( &$out, &$text ) {
		$text = preg_replace_callback(
			'/' . self::$markerPrefix . '-(\d+)' . self::$markerSuffix . '/S',
			function( $matches ) {
				// Can't use self:: in an anonymous function pre PHP 5.4
				return WidgetRenderer::$widgets[$matches[1]];
			},
			$text
		);

		return true;
	}

	// The following four functions are all registered with Smarty.

	public static function wiki_get_template( $widgetName, &$widgetCode, $smarty_obj ) {
		global $wgWidgetsUseFlaggedRevs;

		$widgetTitle = Title::newFromText( $widgetName, NS_WIDGET );

		if ( $widgetTitle && $widgetTitle->exists() ) {
			if ( $wgWidgetsUseFlaggedRevs ) {
				$flaggedWidgetArticle = FlaggedArticle::getTitleInstance( $widgetTitle );
				$flaggedWidgetArticleRevision = $flaggedWidgetArticle->getStableRev();

				if ( $flaggedWidgetArticleRevision ) {
					$widgetCode = $flaggedWidgetArticleRevision->getRevText();
				} else {
					$widgetCode = '';
				}
			} else {
				$widgetWikiPage = new WikiPage( $widgetTitle );
				$widgetContent = $widgetWikiPage->getContent();
				$widgetCode = ContentHandler::getContentText( $widgetContent );
			}

			// Remove <noinclude> sections and <includeonly> tags from form definition
			$widgetCode = StringUtils::delimiterReplace( '<noinclude>', '</noinclude>', '', $widgetCode );
			$widgetCode = strtr( $widgetCode, array( '<includeonly>' => '', '</includeonly>' => '' ) );

			return true;
		} else {
			return false;
		}
	}

	public static function wiki_get_timestamp( $widgetName, &$widgetTimestamp, $smarty_obj ) {
		$widgetTitle = Title::newFromText( $widgetName, NS_WIDGET );

		if ( $widgetTitle && $widgetTitle->exists() ) {
			$widgetArticle = new Article( $widgetTitle, 0 );
			$widgetTimestamp = $widgetArticle->getTouched();
			return true;
		} else {
			return false;
		}
	}

	public static function wiki_get_secure( $tpl_name, &$smarty_obj ) {
		// assume all templates are secure
		return true;
	}

	public static function wiki_get_trusted( $tpl_name, &$smarty_obj ) {
		// not used for templates
	}

}