r104315 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r104314‎ | r104315 | r104316 >
Date:21:50, 26 November 2011
Author:danwe
Status:deferred
Tags:
Comment:
new parameter for '#arrayprint' for options. 'print=pretty' as option allows to connect the last two printed items with local ' and ' equivalent. Instead of ', ' as default for arrayprint, the local comma separator will be used.
Modified paths:
  • /trunk/extensions/Arrays/Arrays.php (modified) (history)
  • /trunk/extensions/Arrays/RELEASE-NOTES (modified) (history)

Diff [purge]

Index: trunk/extensions/Arrays/RELEASE-NOTES
@@ -4,7 +4,6 @@
55 * (trunk) -- Version 2.0
66 This release is built upon 1.4 alpha. See changes of 1.4 alpha as well.
77 'ArrayExtension' is now simply called 'Arrays'. Therefore you have to adjust your LocalSettings.php.
8 - - class 'ArrayExtension' renamed to 'ExtArrays'
98 - '#arrayindex' will only expand options/default when required.
109 - '#arraymerge', '#arrayunion', '#arraydiff' and '#arrayintersect' can handle multiple arrays now.
1110 - Compatibility mode variable '$egArrayExtensionCompatbilityMode' is set to false by default. See
@@ -14,12 +13,17 @@
1514 makes the function consistent with Variables '#var' and hash tables '#hashvalue'.
1615 + '#arraymerge', '#arrayunion', '#arraydiff' and '#arrayintersect' with only one array for
1716 the operation will make a copy of that array instead of creating no array at all.
 17+ + Default separator for '#arrayprint' now is the languages default separator instead of ', '.
1818 + See 1.4 alpha for previous changes
1919 - '#arraydefine' option 'print' no longer supports 'print=print' and parameters beyond options.
2020 This functionality never worked reliably anyways. 'print=list' still works.
 21+ - '#arrayprint' now has a new parameter for options. Option 'print=pretty' will print a list where
 22+ the last two strings are chained with an ' and ' or the local languages equivalent. 'print=pretty'
 23+ also can be used as option within '#arraydefine'.
2124 - If 'Regex Fun' extension is installed, '#arraysearcharray' can use Regex Funs 'e' flag feature
2225 for transforming the result and at the same time parsing them after each back-reference inclusion.
2326 - 1.4 alpha bug solved, '#arraysearcharray' with empty string as start index was interpreted as 0
 27+ - class 'ArrayExtension' renamed to 'ExtArrays'
2428
2529
2630 * November 20, 2001 -- Version 1.4 alpha (r103716)
Index: trunk/extensions/Arrays/Arrays.php
@@ -76,6 +76,16 @@
7777 * @private
7878 */
7979 var $mArrays = array();
 80+
 81+ /**
 82+ * Default separator for '#arrayprint'. Might be ', ' in compatibility-mode or
 83+ * by default since Arrays 2.0 the languages comma separator.
 84+ *
 85+ * @since 2.0
 86+ *
 87+ * @var type
 88+ */
 89+ static $mDefaultSep;
8090
8191 /**
8292 * Sets up parser functions
@@ -83,12 +93,25 @@
8494 * @since 2.0
8595 */
8696 public static function init( Parser &$parser ) {
 97+ global $egArrayExtensionCompatbilityMode;
8798 /*
8899 * store for arrays per Parser object. This will solve several bugs related to
89100 * 'ParserClearState' hook clearing all variables early in combination with certain
90101 * other extensions. (since v2.0)
91102 */
92103 $parser->mExtArrays = new self();
 104+
 105+ // initialize default separator for '#arrayprint'
 106+ if( $egArrayExtensionCompatbilityMode ) {
 107+ // COMPATIBILITY-MODE
 108+ self::$mDefaultSep = ', ';
 109+ }
 110+ else {
 111+ // since 2.0 the default separator for arrayprint is set to the languages default
 112+ global $wgLang;
 113+ $wgLang->getMessageFromDB( 'comma-separator' );
 114+ self::$mDefaultSep = $wgLang->getMessageFromDB( 'comma-separator' );
 115+ }
93116
94117 // SFH_OBJECT_ARGS available since MW 1.12
95118 self::initFunction( $parser, 'arraydefine' );
@@ -225,7 +248,7 @@
226249 }
227250 */
228251
229 - // sort array if the option is set
 252+ // sort array if the option is set
230253 if( array_key_exists( 'sort', $arrayOptions ) ) {
231254 $array = self::arraySort( $array, self::array_value( $arrayOptions, 'sort' ) );
232255 }
@@ -234,11 +257,15 @@
235258 switch( self::array_value( $arrayOptions, 'print' ) ) {
236259 case 'list':
237260 // simple list output
238 - $out = implode( ', ', $array );
 261+ $out = implode( self::$mDefaultSep, $array );
239262 break;
 263+ case 'pretty':
 264+ global $wgLang;
 265+ $out = $wgLang->listToText( $array );
 266+ break;
240267 }
241268 }
242 -
 269+
243270 self::get( $parser )->setArray( $arrayId, $array );
244271
245272 return $out;
@@ -256,7 +283,7 @@
257284 * and each element print-out is deliminated by 'delimiter'
258285 * The subject can embed parser functions; wiki links; and templates.
259286 * usage:
260 - * {{#arrayprint:arrayid|delimiter|search|subject}}
 287+ * {{#arrayprint:arrayid|delimiter|search|subject|options}}
261288 * examples:
262289 * {{#arrayprint:b}} -- simple
263290 * {{#arrayprint:b|<br/>}} -- add change line
@@ -268,15 +295,20 @@
269296 static function pfObj_arrayprint( Parser &$parser, $frame, $args ) {
270297 // Get Parameters
271298 $arrayId = isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : '';
272 - $delimiter = isset( $args[1] ) ? trim( $frame->expand( $args[1] ) ) : ', ';
 299+ $delimiter = isset( $args[1] ) ? trim( $frame->expand( $args[1] ) ) : self::$mDefaultSep;
273300 /*
274301 * PPFrame::NO_ARGS and PPFrame::NO_TEMPLATES for expansion make a lot of sense here since the patterns getting replaced
275302 * in $subject before $subject is being parsed. So any template or argument influence in the patterns wouldn't make any
276303 * sense in any sane scenario.
277304 */
278305 $search = isset( $args[2] ) ? trim( $frame->expand( $args[2], PPFrame::NO_ARGS | PPFrame::NO_TEMPLATES ) ) : '@@@@';
279 - $subject = isset( $args[3] ) ? trim( $frame->expand( $args[3], PPFrame::NO_ARGS | PPFrame::NO_TEMPLATES ) ) : '@@@@';
 306+ $subject = isset( $args[3] ) ? trim( $frame->expand( $args[3], PPFrame::NO_ARGS | PPFrame::NO_TEMPLATES ) ) : '@@@@';
 307+ // options array:
 308+ $options = isset( $args[4] )
 309+ ? self::parse_options( $frame->expand( $args[4] ) )
 310+ : array();
280311
 312+
281313 // get array, null if non-existant:
282314 $array = self::get( $parser )->getArray( $arrayId );
283315
@@ -308,15 +340,28 @@
309341 $rendered_values[] = $rawResult;
310342 }
311343
312 - $output = implode( $delimiter, $rendered_values );
313 - $noparse = false;
 344+ // follow special print options:
 345+ switch( self::array_value( $options, 'print' ) ) {
 346+ case 'pretty':
 347+ // pretty list print with ' and ' connecting the last two items
 348+ if( $delimiter === '' ) {
 349+ // '' as delimiter isn't pretty, so in this case we take the (languages) default
 350+ $output = self::arrayToText( $rendered_values );
 351+ } else {
 352+ $output = self::arrayToText( $rendered_values, $delimiter );
 353+ }
 354+ break;
314355
 356+ default:
 357+ // normal print with one delimiter, might be the languages default
 358+ $output = implode( $delimiter, $rendered_values );
 359+ break;
 360+ }
 361+
315362 /*
316363 * don't leave the final parse to Parser::braceSubstitution() since there are some special cases where it
317364 * would produce unexpected output (it uses a new child frame and ignores whether the frame is a template!)
318365 */
319 - $noparse = true;
320 -
321366 $output = $parser->preprocessToDom( $output, $frame->isTemplate() ? Parser::PTD_FOR_INCLUSION : 0 );
322367 $output = trim( $frame->expand( $output ) );
323368
@@ -916,7 +961,7 @@
917962 }
918963
919964 // now parse the options, and do posterior process on the created array
920 - $options = preg_split( '/\s*,\s*/', strtolower( $options ) );
 965+ $options = preg_split( '/\s*,\s*/', strtolower( trim( $options ) ) );
921966
922967 $ret = array();
923968 foreach( $options as $option ) {
@@ -1148,6 +1193,36 @@
11491194 } ;
11501195 return $array;
11511196 }
 1197+
 1198+ /**
 1199+ * Pretty much the same as Language::listToText() but allows us to set a custom comma separator.
 1200+ *
 1201+ * @since 2.0
 1202+ *
 1203+ * @param Array $array
 1204+ * @param string $commaSep
 1205+ * @return string
 1206+ */
 1207+ public static function arrayToText( $array, $commaSep = null ) {
 1208+ global $wgLang;
 1209+ $commaSep = $commaSep === null ? self::$mDefaultSep : $commaSep;
 1210+ $s = '';
 1211+ $m = count( $array ) - 1;
 1212+ if ( $m == 1 ) {
 1213+ return $array[0] . $wgLang->getMessageFromDB( 'and' ) . $wgLang->getMessageFromDB( 'word-separator' ) . $array[1];
 1214+ } else {
 1215+ for ( $i = $m; $i >= 0; $i-- ) {
 1216+ if ( $i == $m ) {
 1217+ $s = $array[$i];
 1218+ } else if ( $i == $m - 1 ) {
 1219+ $s = $array[$i] . $wgLang->getMessageFromDB( 'and' ) . $wgLang->getMessageFromDB( 'word-separator' ) . $s;
 1220+ } else {
 1221+ $s = $array[$i] . $commaSep . $s;
 1222+ }
 1223+ }
 1224+ return $s;
 1225+ }
 1226+ }
11521227
11531228 /**
11541229 * Decides for the given $pattern whether its a valid regular expression acceptable for

Status & tagging log