r93030 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r93029‎ | r93030 | r93031 >
Date:23:36, 24 July 2011
Author:jeroendedauw
Status:deferred
Tags:lamecommitsummary 
Comment:
Modified paths:
  • /trunk/extensions/Widgets/WidgetRenderer.php (modified) (history)
  • /trunk/extensions/Widgets/Widgets.php (modified) (history)
  • /trunk/extensions/Widgets/smarty_plugins/modifier.validate.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Widgets/WidgetRenderer.php
@@ -3,158 +3,151 @@
44 * Class holding functions for displaying widgets.
55 */
66
7 -if ( !defined( 'MEDIAWIKI' ) ) {
8 - echo "This file is not a valid entry point.";
9 - exit( 1 );
10 -}
11 -
127 class WidgetRenderer {
138
14 -public static function renderWidget ( &$parser, $widgetName ) {
15 - global $IP;
 9+ public static function renderWidget ( &$parser, $widgetName ) {
 10+ global $IP;
1611
17 - $smarty = new Smarty;
18 - $smarty->left_delimiter = '<!--{';
19 - $smarty->right_delimiter = '}-->';
20 - $smarty->compile_dir = "$IP/extensions/Widgets/compiled_templates/";
 12+ $smarty = new Smarty;
 13+ $smarty->left_delimiter = '<!--{';
 14+ $smarty->right_delimiter = '}-->';
 15+ $smarty->compile_dir = "$IP/extensions/Widgets/compiled_templates/";
2116
22 - // registering custom Smarty plugins
23 - $smarty->plugins_dir[] = "$IP/extensions/Widgets/smarty_plugins/";
 17+ // registering custom Smarty plugins
 18+ $smarty->plugins_dir[] = "$IP/extensions/Widgets/smarty_plugins/";
2419
25 - $smarty->security = true;
26 - $smarty->security_settings = array(
27 - 'IF_FUNCS' => array(
28 - 'is_array',
29 - 'isset',
30 - 'array',
31 - 'list',
32 - 'count',
33 - 'sizeof',
34 - 'in_array',
35 - 'true',
36 - 'false',
37 - 'null'
38 - ),
39 - 'MODIFIER_FUNCS' => array( 'validate' )
40 - );
 20+ $smarty->security = true;
 21+ $smarty->security_settings = array(
 22+ 'IF_FUNCS' => array(
 23+ 'is_array',
 24+ 'isset',
 25+ 'array',
 26+ 'list',
 27+ 'count',
 28+ 'sizeof',
 29+ 'in_array',
 30+ 'true',
 31+ 'false',
 32+ 'null'
 33+ ),
 34+ 'MODIFIER_FUNCS' => array( 'validate' )
 35+ );
4136
42 - // register the resource name "db"
43 - $smarty->register_resource(
44 - 'wiki',
45 - array(
46 - array('WidgetRenderer', 'wiki_get_template'),
47 - array('WidgetRenderer', 'wiki_get_timestamp'),
48 - array('WidgetRenderer', 'wiki_get_secure'),
49 - array('WidgetRenderer', 'wiki_get_trusted')
50 - )
51 - );
 37+ // register the resource name "db"
 38+ $smarty->register_resource(
 39+ 'wiki',
 40+ array(
 41+ array( 'WidgetRenderer', 'wiki_get_template' ),
 42+ array( 'WidgetRenderer', 'wiki_get_timestamp' ),
 43+ array( 'WidgetRenderer', 'wiki_get_secure' ),
 44+ array( 'WidgetRenderer', 'wiki_get_trusted' )
 45+ )
 46+ );
5247
53 - $params = func_get_args();
54 - array_shift( $params ); # first one is parser - we don't need it
55 - array_shift( $params ); # second one is widget name
 48+ $params = func_get_args();
 49+ array_shift( $params ); # first one is parser - we don't need it
 50+ array_shift( $params ); # second one is widget name
5651
57 - $params_tree = array();
 52+ $params_tree = array();
5853
59 - foreach ( $params as $param ) {
60 - $pair = explode('=', $param, 2);
 54+ foreach ( $params as $param ) {
 55+ $pair = explode('=', $param, 2);
6156
62 - if ( count( $pair ) == 2 ) {
63 - $key = trim($pair[0]);
64 - $val = trim($pair[1]);
65 - } else {
66 - $key = $param;
67 - $val = true;
68 - }
 57+ if ( count( $pair ) == 2 ) {
 58+ $key = trim( $pair[0] );
 59+ $val = trim( $pair[1] );
 60+ } else {
 61+ $key = $param;
 62+ $val = true;
 63+ }
6964
70 - if ( $val == 'false' ) {
71 - $val = false;
72 - }
 65+ if ( $val == 'false' ) {
 66+ $val = false;
 67+ }
7368
74 - /* If the name of the parameter has object notation
 69+ /* If the name of the parameter has object notation
7570
76 - a.b.c.d
 71+ a.b.c.d
7772
78 - then we assign stuff to hash of hashes, not scalar
 73+ then we assign stuff to hash of hashes, not scalar
7974
80 - */
81 - $keys = explode( '.', $key );
 75+ */
 76+ $keys = explode( '.', $key );
8277
83 - // $subtree will be moved from top to the bottom and at the end will point to the last level
84 - $subtree =& $params_tree;
 78+ // $subtree will be moved from top to the bottom and at the end will point to the last level
 79+ $subtree =& $params_tree;
8580
86 - // go throught all the keys but last one
87 - $last_key = array_pop( $keys );
 81+ // go throught all the keys but last one
 82+ $last_key = array_pop( $keys );
8883
89 - foreach ( $keys as $subkey ) {
90 - // if next level of subtree doesn't exist yet, create an empty one
91 - if ( !array_key_exists( $subkey, $subtree ) ) {
92 - $subtree[$subkey] = array();
93 - }
 84+ foreach ( $keys as $subkey ) {
 85+ // if next level of subtree doesn't exist yet, create an empty one
 86+ if ( !array_key_exists( $subkey, $subtree ) ) {
 87+ $subtree[$subkey] = array();
 88+ }
9489
95 - // move to the lower level
96 - $subtree =& $subtree[$subkey];
97 - }
 90+ // move to the lower level
 91+ $subtree =& $subtree[$subkey];
 92+ }
9893
99 - // last portion of the key points to itself
100 - if ( isset( $subtree[$last_key] ) ) {
101 - // if already an array, push into it, otherwise, convert into array first
102 - if ( !is_array( $subtree[$last_key] ) ) {
103 - $subtree[$last_key] = array( $subtree[$last_key] );
104 - }
 94+ // last portion of the key points to itself
 95+ if ( isset( $subtree[$last_key] ) ) {
 96+ // if already an array, push into it, otherwise, convert into array first
 97+ if ( !is_array( $subtree[$last_key] ) ) {
 98+ $subtree[$last_key] = array( $subtree[$last_key] );
 99+ }
105100
106 - $subtree[$last_key][] = $val;
107 - } else {
108 - // doesn't exist yet, just setting a value
109 - $subtree[$last_key] = $val;
110 - }
111 - }
 101+ $subtree[$last_key][] = $val;
 102+ } else {
 103+ // doesn't exist yet, just setting a value
 104+ $subtree[$last_key] = $val;
 105+ }
 106+ }
112107
113 - $smarty->assign( $params_tree );
 108+ $smarty->assign( $params_tree );
114109
115 - try {
116 - $output = $smarty->fetch( "wiki:$widgetName" );
117 - } catch ( Exception $e ) {
118 -
119 - return '<div class=\"error\">' . wfMsgExt( 'widgets-desc', array( 'parsemag' ), htmlentities($widgetName) ) . '</div>';
120 - }
 110+ try {
 111+ $output = $smarty->fetch( "wiki:$widgetName" );
 112+ } catch ( Exception $e ) {
121113
122 - // Hide the widget from the parser
123 - $output = 'ENCODED_CONTENT '.base64_encode($output).' END_ENCODED_CONTENT';
124 - return $output;
125 -}
 114+ return '<div class=\"error\">' . wfMsgExt( 'widgets-desc', array( 'parsemag' ), htmlentities($widgetName) ) . '</div>';
 115+ }
126116
127 -public static function processEncodedWidgetOutput( &$out, &$text ) {
128 - // Find all hidden content and restore to normal
129 - $text = preg_replace(
130 - '/ENCODED_CONTENT ([0-9a-zA-Z\/+]+=*)* END_ENCODED_CONTENT/esm',
131 - 'base64_decode("$1")',
132 - $text
133 - );
 117+ // Hide the widget from the parser
 118+ $output = 'ENCODED_CONTENT '.base64_encode($output).' END_ENCODED_CONTENT';
 119+ return $output;
 120+ }
134121
135 - return true;
136 -}
 122+ public static function processEncodedWidgetOutput( &$out, &$text ) {
 123+ // Find all hidden content and restore to normal
 124+ $text = preg_replace(
 125+ '/ENCODED_CONTENT ([0-9a-zA-Z\/+]+=*)* END_ENCODED_CONTENT/esm',
 126+ 'base64_decode("$1")',
 127+ $text
 128+ );
137129
 130+ return true;
 131+ }
 132+
138133 // the following four functions are all registered with Smarty
139134 public static function wiki_get_template( $widgetName, &$widgetCode, &$smarty_obj ) {
140135 global $wgWidgetsUseFlaggedRevs;
141136
142 - $widgetTitle = Title::newFromText($widgetName, NS_WIDGET);
 137+ $widgetTitle = Title::newFromText( $widgetName, NS_WIDGET );
 138+
143139 if ( $widgetTitle && $widgetTitle->exists() ) {
144 - if ($wgWidgetsUseFlaggedRevs)
145 - {
 140+ if ( $wgWidgetsUseFlaggedRevs ) {
146141 $flaggedWidgetArticle = FlaggedArticle::getTitleInstance( $widgetTitle );
147142 $flaggedWidgetArticleRevision = $flaggedWidgetArticle->getStableRev();
148 - if ($flaggedWidgetArticleRevision)
149 - {
 143+
 144+ if ( $flaggedWidgetArticleRevision ) {
150145 $widgetCode = $flaggedWidgetArticleRevision->getRevText();
151146 }
152 - else
153 - {
 147+ else {
154148 $widgetCode = '';
155149 }
156150 }
157 - else
158 - {
 151+ else {
159152 $widgetArticle = new Article( $widgetTitle, 0 );
160153 $widgetCode = $widgetArticle->getContent();
161154 }
@@ -171,10 +164,10 @@
172165
173166 public static function wiki_get_timestamp( $widgetName, &$widgetTimestamp, &$smarty_obj ) {
174167 $widgetTitle = Title::newFromText( $widgetName, NS_WIDGET );
175 - if ($widgetTitle && $widgetTitle->exists()) {
 168+
 169+ if ( $widgetTitle && $widgetTitle->exists() ) {
176170 $widgetArticle = new Article( $widgetTitle, 0 );
177171 $widgetTimestamp = $widgetArticle->getTouched();
178 -
179172 return true;
180173 } else {
181174 return false;
Index: trunk/extensions/Widgets/smarty_plugins/modifier.validate.php
@@ -9,8 +9,7 @@
1010 * Useful when you need to validate but not escape.
1111 * -------------------------------------------------------------
1212 */
13 -function smarty_modifier_validate($string, $type='url')
14 -{
 13+function smarty_modifier_validate( $string, $type='url' ) {
1514 // mapping for PHP filters (http://us2.php.net/manual/en/filter.constants.php)
1615 $filters = array(
1716 'url' => FILTER_VALIDATE_URL,
@@ -21,8 +20,7 @@
2221 'ip' => FILTER_VALIDATE_IP
2322 );
2423
25 - if (array_key_exists($type, $filters) && filter_var($string, $filters[$type]) !== FALSE)
26 - {
 24+ if ( array_key_exists($type, $filters) && filter_var($string, $filters[$type]) !== FALSE ) {
2725 return $string;
2826 }
2927
Index: trunk/extensions/Widgets/Widgets.php
@@ -42,6 +42,10 @@
4343
4444 // Define new right
4545 $wgAvailableRights[] = 'editwidgets';
 46+
 47+// Assign editing to widgeteditor and sysop groups only (widgets can be dangerous so we do it here, not in LocalSettings)
 48+$wgGroupPermissions['*']['editwidgets'] = false;
 49+$wgGroupPermissions['widgeteditor']['editwidgets'] = true;
4650 $wgGroupPermissions['sysop']['editwidgets'] = true;
4751
4852 // Set this to true to use FlaggedRevs extension's stable version for widget security
@@ -99,10 +103,6 @@
100104 global $wgGroupPermissions, $wgNamespaceProtection, $wgWidgetsUseFlaggedRevs;
101105
102106 if ( !$wgWidgetsUseFlaggedRevs ) {
103 - // Assign editing to widgeteditor group only (widgets can be dangerous so we do it here, not in LocalSettings)
104 - $wgGroupPermissions['*']['editwidgets'] = false;
105 - $wgGroupPermissions['widgeteditor']['editwidgets'] = true;
106 -
107107 // Setting required namespace permission rights
108108 $wgNamespaceProtection[NS_WIDGET] = array( 'editwidgets' );
109109 }

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r93023let sysops have the right to edit widgets by default; really doesn't make sen...jeroendedauw22:18, 24 July 2011

Status & tagging log