Index: trunk/extensions/Arrays/RELEASE-NOTES |
— | — | @@ -4,7 +4,6 @@ |
5 | 5 | * (trunk) -- Version 2.0 |
6 | 6 | This release is built upon 1.4 alpha. See changes of 1.4 alpha as well. |
7 | 7 | 'ArrayExtension' is now simply called 'Arrays'. Therefore you have to adjust your LocalSettings.php. |
8 | | - - class 'ArrayExtension' renamed to 'ExtArrays' |
9 | 8 | - '#arrayindex' will only expand options/default when required. |
10 | 9 | - '#arraymerge', '#arrayunion', '#arraydiff' and '#arrayintersect' can handle multiple arrays now. |
11 | 10 | - Compatibility mode variable '$egArrayExtensionCompatbilityMode' is set to false by default. See |
— | — | @@ -14,12 +13,17 @@ |
15 | 14 | makes the function consistent with Variables '#var' and hash tables '#hashvalue'. |
16 | 15 | + '#arraymerge', '#arrayunion', '#arraydiff' and '#arrayintersect' with only one array for |
17 | 16 | 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 ', '. |
18 | 18 | + See 1.4 alpha for previous changes |
19 | 19 | - '#arraydefine' option 'print' no longer supports 'print=print' and parameters beyond options. |
20 | 20 | 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'. |
21 | 24 | - If 'Regex Fun' extension is installed, '#arraysearcharray' can use Regex Funs 'e' flag feature |
22 | 25 | for transforming the result and at the same time parsing them after each back-reference inclusion. |
23 | 26 | - 1.4 alpha bug solved, '#arraysearcharray' with empty string as start index was interpreted as 0 |
| 27 | + - class 'ArrayExtension' renamed to 'ExtArrays' |
24 | 28 | |
25 | 29 | |
26 | 30 | * November 20, 2001 -- Version 1.4 alpha (r103716) |
Index: trunk/extensions/Arrays/Arrays.php |
— | — | @@ -76,6 +76,16 @@ |
77 | 77 | * @private |
78 | 78 | */ |
79 | 79 | 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; |
80 | 90 | |
81 | 91 | /** |
82 | 92 | * Sets up parser functions |
— | — | @@ -83,12 +93,25 @@ |
84 | 94 | * @since 2.0 |
85 | 95 | */ |
86 | 96 | public static function init( Parser &$parser ) { |
| 97 | + global $egArrayExtensionCompatbilityMode; |
87 | 98 | /* |
88 | 99 | * store for arrays per Parser object. This will solve several bugs related to |
89 | 100 | * 'ParserClearState' hook clearing all variables early in combination with certain |
90 | 101 | * other extensions. (since v2.0) |
91 | 102 | */ |
92 | 103 | $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 | + } |
93 | 116 | |
94 | 117 | // SFH_OBJECT_ARGS available since MW 1.12 |
95 | 118 | self::initFunction( $parser, 'arraydefine' ); |
— | — | @@ -225,7 +248,7 @@ |
226 | 249 | } |
227 | 250 | */ |
228 | 251 | |
229 | | - // sort array if the option is set |
| 252 | + // sort array if the option is set |
230 | 253 | if( array_key_exists( 'sort', $arrayOptions ) ) { |
231 | 254 | $array = self::arraySort( $array, self::array_value( $arrayOptions, 'sort' ) ); |
232 | 255 | } |
— | — | @@ -234,11 +257,15 @@ |
235 | 258 | switch( self::array_value( $arrayOptions, 'print' ) ) { |
236 | 259 | case 'list': |
237 | 260 | // simple list output |
238 | | - $out = implode( ', ', $array ); |
| 261 | + $out = implode( self::$mDefaultSep, $array ); |
239 | 262 | break; |
| 263 | + case 'pretty': |
| 264 | + global $wgLang; |
| 265 | + $out = $wgLang->listToText( $array ); |
| 266 | + break; |
240 | 267 | } |
241 | 268 | } |
242 | | - |
| 269 | + |
243 | 270 | self::get( $parser )->setArray( $arrayId, $array ); |
244 | 271 | |
245 | 272 | return $out; |
— | — | @@ -256,7 +283,7 @@ |
257 | 284 | * and each element print-out is deliminated by 'delimiter' |
258 | 285 | * The subject can embed parser functions; wiki links; and templates. |
259 | 286 | * usage: |
260 | | - * {{#arrayprint:arrayid|delimiter|search|subject}} |
| 287 | + * {{#arrayprint:arrayid|delimiter|search|subject|options}} |
261 | 288 | * examples: |
262 | 289 | * {{#arrayprint:b}} -- simple |
263 | 290 | * {{#arrayprint:b|<br/>}} -- add change line |
— | — | @@ -268,15 +295,20 @@ |
269 | 296 | static function pfObj_arrayprint( Parser &$parser, $frame, $args ) { |
270 | 297 | // Get Parameters |
271 | 298 | $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; |
273 | 300 | /* |
274 | 301 | * PPFrame::NO_ARGS and PPFrame::NO_TEMPLATES for expansion make a lot of sense here since the patterns getting replaced |
275 | 302 | * in $subject before $subject is being parsed. So any template or argument influence in the patterns wouldn't make any |
276 | 303 | * sense in any sane scenario. |
277 | 304 | */ |
278 | 305 | $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(); |
280 | 311 | |
| 312 | + |
281 | 313 | // get array, null if non-existant: |
282 | 314 | $array = self::get( $parser )->getArray( $arrayId ); |
283 | 315 | |
— | — | @@ -308,15 +340,28 @@ |
309 | 341 | $rendered_values[] = $rawResult; |
310 | 342 | } |
311 | 343 | |
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; |
314 | 355 | |
| 356 | + default: |
| 357 | + // normal print with one delimiter, might be the languages default |
| 358 | + $output = implode( $delimiter, $rendered_values ); |
| 359 | + break; |
| 360 | + } |
| 361 | + |
315 | 362 | /* |
316 | 363 | * don't leave the final parse to Parser::braceSubstitution() since there are some special cases where it |
317 | 364 | * would produce unexpected output (it uses a new child frame and ignores whether the frame is a template!) |
318 | 365 | */ |
319 | | - $noparse = true; |
320 | | - |
321 | 366 | $output = $parser->preprocessToDom( $output, $frame->isTemplate() ? Parser::PTD_FOR_INCLUSION : 0 ); |
322 | 367 | $output = trim( $frame->expand( $output ) ); |
323 | 368 | |
— | — | @@ -916,7 +961,7 @@ |
917 | 962 | } |
918 | 963 | |
919 | 964 | // 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 ) ) ); |
921 | 966 | |
922 | 967 | $ret = array(); |
923 | 968 | foreach( $options as $option ) { |
— | — | @@ -1148,6 +1193,36 @@ |
1149 | 1194 | } ; |
1150 | 1195 | return $array; |
1151 | 1196 | } |
| 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 | + } |
1152 | 1227 | |
1153 | 1228 | /** |
1154 | 1229 | * Decides for the given $pattern whether its a valid regular expression acceptable for |