r112642 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r112641‎ | r112642 | r112643 >
Date:21:12, 28 February 2012
Author:danwe
Status:deferred (Comments)
Tags:
Comment:
new sort mode for #arraysort 'natural' and #arraysort will now respect local sorting settings defined via PHPs setlocale().
Modified paths:
  • /trunk/extensions/Arrays/Arrays.php (modified) (history)
  • /trunk/extensions/Arrays/COPYING (modified) (history)
  • /trunk/extensions/Arrays/RELEASE-NOTES (modified) (history)

Diff [purge]

Index: trunk/extensions/Arrays/RELEASE-NOTES
@@ -7,6 +7,10 @@
88 version 1.3.2. A new option 'singleempty' can be used to avoid this behavior, ',' can be used to
99 create an array with two empty elements.
1010 - Bug fixed where '#arraysearch' would always return '-1' instead of parameter 5 value.
 11+ - '#arraysort' (for 'asce' and 'desc' modes) now respects local sort settings specified via PHPs
 12+ 'setlocale()'. A flag system allows to set 'nolocale' separated by a space behind the sort mode. to
 13+ deactivate this for the sorting.
 14+ - '#arraysort' has a new sort mode 'natural' which will sort numbers within strings more human-like.
1115
1216 * December 5, 2011 -- Version 2.0rc2
1317 - Bug introduced in r105069 fixed where '#arrayprint' was broken in compatibility mode in some cases.
Index: trunk/extensions/Arrays/Arrays.php
@@ -69,7 +69,7 @@
7070 *
7171 * @since 2.0
7272 *
73 - * @var type
 73+ * @var string
7474 */
7575 static $mDefaultSep;
7676
@@ -130,7 +130,7 @@
131131 *
132132 * @since 2.0
133133 *
134 - * @return boolean
 134+ * @return string
135135 */
136136 public static function getDir() {
137137 static $dir = null;
@@ -1091,8 +1091,8 @@
10921092 * This is save and faster for internal usage, just be sure your array doesn't have un-trimmed
10931093 * values or non-numeric or negative array keys and no gaps between keys.
10941094 *
1095 - * @param type $arrayId
1096 - * @param type $array
 1095+ * @param string $arrayId
 1096+ * @param array $array
10971097 */
10981098 protected function setArray( $arrayId, $array = array() ) {
10991099 $this->mArrays[ trim( $arrayId ) ] = $array;
@@ -1192,24 +1192,52 @@
11931193 * @since 2.0
11941194 *
11951195 * @param array $array
1196 - * @param string $sortMode
 1196+ * @param string $sortMode one of the following sort modes:
 1197+ * - random: random array order
 1198+ * - reverse: last entry will be first, first the last.
 1199+ * - asce: sort array in ascending order.
 1200+ * - desc: sort array in descending order.
 1201+ * - natural: sort with a 'natural order' algorithm. See PHPs natsort() function.
 1202+ *
 1203+ * In addition, this function allows to set several flags behind the sort mode. The must be
 1204+ * separated by a space. The following keys are allowed:
 1205+ * - nolocale: will prevent 'asce' and 'desc' mode to considering PHP-defined language rules.
11971206 *
11981207 * @return array
11991208 */
12001209 public static function arraySort( array $array, $sortMode ) {
1201 - // do the requested sorting of the given array:
 1210+ global $egArraysCompatibilityMode;
 1211+
 1212+ $flags = preg_split( '/\s+/s', $sortMode );
 1213+ $sortMode = array_shift( $flags ); // first string is the actual sort mode
 1214+
 1215+ $localeFlag = SORT_LOCALE_STRING; // sort strings accordingly to what was set via setlocale()
 1216+ if(
 1217+ in_array( 'nolocale', $flags )
 1218+ || $egArraysCompatibilityMode // COMPATIBILITY-MODE
 1219+ ) {
 1220+ // 'nolocale' will prevent from using this flag!
 1221+ $localeFlag = null;
 1222+ }
 1223+
 1224+ // do the requested sorting of the given array:
12021225 switch( $sortMode ) {
12031226 case 'asc':
12041227 case 'asce':
12051228 case 'ascending':
1206 - sort( $array );
 1229+ sort( $array, $localeFlag );
12071230 break;
12081231
12091232 case 'desc':
12101233 case 'descending':
1211 - rsort( $array );
 1234+ rsort( $array, $localeFlag );
12121235 break;
1213 -
 1236+
 1237+ case 'nat':
 1238+ case 'natural':
 1239+ natsort( $array );
 1240+ break;
 1241+
12141242 case 'rand':
12151243 case 'random':
12161244 shuffle( $array );
@@ -1218,7 +1246,7 @@
12191247 case 'reverse':
12201248 $array = array_reverse( $array );
12211249 break;
1222 - } ;
 1250+ };
12231251 return $array;
12241252 }
12251253
Index: trunk/extensions/Arrays/COPYING
@@ -1,6 +1,6 @@
22 The MIT License
33
4 - Copyright (c) 2008 - 2011
 4+ Copyright (c) 2008 - 2012
55
66
77 Permission is hereby granted, free of charge, to any person

Comments

#Comment by Danwe (talk | contribs)   21:14, 28 February 2012

'natural' sort though doesn't have a option for respecting the setlocale() settings. At least I found no way yet.

#Comment by Danwe (talk | contribs)   21:15, 28 February 2012

This is a fix for Bug 34358 by the way.

Status & tagging log