r40320 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r40319‎ | r40320 | r40321 >
Date:22:49, 1 September 2008
Author:dantman
Status:old
Tags:
Comment:
Implement SFH_OBJ_ARGS versions of #arraymap and #arraymaptemplate.
Modified paths:
  • /trunk/extensions/SemanticForms/includes/SF_ParserFunctions.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticForms/includes/SF_ParserFunctions.php
@@ -103,8 +103,13 @@
104104 function sfgRegisterParser( &$parser ) {
105105 $parser->setFunctionHook('forminput', 'sfRenderFormInput');
106106 $parser->setFunctionHook('formlink', 'sfRenderFormLink');
107 - $parser->setFunctionHook('arraymap', 'sfRenderArrayMap');
108 - $parser->setFunctionHook('arraymaptemplate', 'sfRenderArrayMapTemplate');
 107+ if( defined( get_class( $parser ) . '::SFH_OBJECT_ARGS' ) ) {
 108+ $parser->setFunctionHook('arraymap', 'sfRenderArrayMapObj', SFH_OBJECT_ARGS);
 109+ $parser->setFunctionHook('arraymaptemplate', 'sfRenderArrayMapTemplateObj', SFH_OBJECT_ARGS);
 110+ } else {
 111+ $parser->setFunctionHook('arraymap', 'sfRenderArrayMap');
 112+ $parser->setFunctionHook('arraymaptemplate', 'sfRenderArrayMapTemplate');
 113+ }
109114 return true;
110115 }
111116
@@ -174,7 +179,7 @@
175180 /**
176181 * {{#arraymap:value|delimiter|var|new_value|new_delimiter}}
177182 */
178 -function sfRenderArrayMap ( &$parser, $value = '', $delimiter = ',', $var = 'x', $new_value = '', $new_delimiter = ', ' ) {
 183+function sfRenderArrayMap( &$parser, $value = '', $delimiter = ',', $var = 'x', $new_value = 'x', $new_delimiter = ', ' ) {
179184 $values_array = explode($parser->mStripState->unstripNoWiki($delimiter), $value);
180185 $results = array();
181186 foreach ($values_array as $cur_value) {
@@ -189,9 +194,41 @@
190195 }
191196
192197 /**
193 - * {{#arraymaptemplate:blue;red;yellow|Beautify|;|;}}
 198+ * SFH_OBJ_ARGS
 199+ * {{#arraymap:value|delimiter|var|new_value|new_delimiter}}
194200 */
195 -function sfRenderArrayMapTemplate ( &$parser, $value = '', $template = '', $delimiter = ',', $new_delimiter = ', ' ) {
 201+function sfRenderArrayMapObj( &$parser, $frame, $args ) {
 202+ # Set variables
 203+ $value = isset($args[0]) ? $args[0] : '';
 204+ $delimiter = isset($args[1]) ? $args[1] : ',';
 205+ $var = isset($args[2]) ? $args[2] : 'x';
 206+ $new_value = isset($args[3]) ? $args[3] : 'x';
 207+ $new_delimiter = isset($args[4]) ? $args[4] : ', ';
 208+ # Expand some
 209+ $value = trim($frame->expand($value));
 210+ $delimiter = trim($frame->expand($delimiter));
 211+ $var = trim($frame->expand($var, PPFrame::NO_ARGS | PPFrame::NO_TEMPLATES));
 212+ $new_delimiter = trim($frame->expand($new_delimiter));
 213+ # Unstrip some
 214+ $delimiter = $parser->mStripState->unstripNoWiki($delimiter);
 215+
 216+ $values_array = explode($delimiter, $value);
 217+ $results_array = array();
 218+ foreach( $values_array as $old_value ) {
 219+ $old_value = trim($old_value);
 220+ if( $old_value == '' ) continue;
 221+ $result_value = $frame->expand($new_value, PPFrame::NO_ARGS | PPFrame::NO_TEMPLATES);
 222+ $result_value = str_replace($var, $old_value, $result_value);
 223+ $result_value = $parser->preprocessToDom($result_value, $frame->isTemplate() ? Parser::PTD_FOR_INCLUSION : 0);
 224+ $results_array[] = trim($frame->expand($result_value));
 225+ }
 226+ return implode($new_delimiter, $results_array);
 227+}
 228+
 229+/**
 230+ * {{#arraymaptemplate:value|template|delimiter|new_delimiter}}
 231+ */
 232+function sfRenderArrayMapTemplate( &$parser, $value = '', $template = '', $delimiter = ',', $new_delimiter = ', ' ) {
196233 $values_array = explode($parser->mStripState->unstripNoWiki($delimiter), $value);
197234 $results = array();
198235 $template = trim($template);
@@ -205,3 +242,28 @@
206243 }
207244 return array(implode($new_delimiter, $results), 'noparse' => false, 'isHTML' => false);
208245 }
 246+
 247+/**
 248+ * SFH_OBJ_ARGS
 249+ * {{#arraymaptemplate:value|template|delimiter|new_delimiter}}
 250+ */
 251+function sfRenderArrayMapTemplateObj( &$parser, $frame, $args ) {
 252+ # Set variables
 253+ $value = isset($args[0]) ? trim($frame->expand($args[0])) : '';
 254+ $template = isset($args[1]) ? trim($frame->expand($args[1])) : '';
 255+ $delimiter = isset($args[2]) ? trim($frame->expand($args[2])) : ',';
 256+ $new_delimiter = isset($args[3]) ? trim($frame->expand($args[3])) : ', ';
 257+ # Unstrip some
 258+ $delimiter = $parser->mStripState->unstripNoWiki($delimiter);
 259+
 260+ $values_array = explode($delimiter, $value);
 261+ $results_array = array();
 262+ foreach( $values_array as $old_value ) {
 263+ $old_value = trim($old_value);
 264+ if( $old_value == '' ) continue;
 265+ $results_array[] = $parser->replaceVariables(
 266+ implode('', $frame->virtualBracketedImplode( '{{', '|', '}}',
 267+ $template, '1='.$old_value )), $frame);
 268+ }
 269+ return implode($new_delimiter, $results_array);
 270+}

Status & tagging log