https://www.mediawiki.org/wiki/Special:Code/MediaWiki/112642
Revision: 112642
Author: danwe
Date: 2012-02-28 21:12:54 +0000 (Tue, 28 Feb 2012)
Log Message:
-----------
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
trunk/extensions/Arrays/COPYING
trunk/extensions/Arrays/RELEASE-NOTES
Modified: trunk/extensions/Arrays/Arrays.php
===================================================================
--- trunk/extensions/Arrays/Arrays.php 2012-02-28 21:12:38 UTC (rev 112641)
+++ trunk/extensions/Arrays/Arrays.php 2012-02-28 21:12:54 UTC (rev 112642)
@@ -69,7 +69,7 @@
*
* @since 2.0
*
- * @var type
+ * @var string
*/
static $mDefaultSep;
@@ -130,7 +130,7 @@
*
* @since 2.0
*
- * @return boolean
+ * @return string
*/
public static function getDir() {
static $dir = null;
@@ -1091,8 +1091,8 @@
* This is save and faster for internal usage, just be sure your array
doesn't have un-trimmed
* values or non-numeric or negative array keys and no gaps between
keys.
*
- * @param type $arrayId
- * @param type $array
+ * @param string $arrayId
+ * @param array $array
*/
protected function setArray( $arrayId, $array = array() ) {
$this->mArrays[ trim( $arrayId ) ] = $array;
@@ -1192,24 +1192,52 @@
* @since 2.0
*
* @param array $array
- * @param string $sortMode
+ * @param string $sortMode one of the following sort modes:
+ * - random: random array order
+ * - reverse: last entry will be first, first the last.
+ * - asce: sort array in ascending order.
+ * - desc: sort array in descending order.
+ * - natural: sort with a 'natural order' algorithm. See PHPs
natsort() function.
+ *
+ * In addition, this function allows to set several flags behind
the sort mode. The must be
+ * separated by a space. The following keys are allowed:
+ * - nolocale: will prevent 'asce' and 'desc' mode to
considering PHP-defined language rules.
*
* @return array
*/
public static function arraySort( array $array, $sortMode ) {
- // do the requested sorting of the given array:
+ global $egArraysCompatibilityMode;
+
+ $flags = preg_split( '/\s+/s', $sortMode );
+ $sortMode = array_shift( $flags ); // first string is the
actual sort mode
+
+ $localeFlag = SORT_LOCALE_STRING; // sort strings accordingly
to what was set via setlocale()
+ if(
+ in_array( 'nolocale', $flags )
+ || $egArraysCompatibilityMode // COMPATIBILITY-MODE
+ ) {
+ // 'nolocale' will prevent from using this flag!
+ $localeFlag = null;
+ }
+
+ // do the requested sorting of the given array:
switch( $sortMode ) {
case 'asc':
case 'asce':
case 'ascending':
- sort( $array );
+ sort( $array, $localeFlag );
break;
case 'desc':
case 'descending':
- rsort( $array );
+ rsort( $array, $localeFlag );
break;
-
+
+ case 'nat':
+ case 'natural':
+ natsort( $array );
+ break;
+
case 'rand':
case 'random':
shuffle( $array );
@@ -1218,7 +1246,7 @@
case 'reverse':
$array = array_reverse( $array );
break;
- } ;
+ };
return $array;
}
Modified: trunk/extensions/Arrays/COPYING
===================================================================
--- trunk/extensions/Arrays/COPYING 2012-02-28 21:12:38 UTC (rev 112641)
+++ trunk/extensions/Arrays/COPYING 2012-02-28 21:12:54 UTC (rev 112642)
@@ -1,6 +1,6 @@
The MIT License
- Copyright (c) 2008 - 2011
+ Copyright (c) 2008 - 2012
Permission is hereby granted, free of charge, to any person
Modified: trunk/extensions/Arrays/RELEASE-NOTES
===================================================================
--- trunk/extensions/Arrays/RELEASE-NOTES 2012-02-28 21:12:38 UTC (rev
112641)
+++ trunk/extensions/Arrays/RELEASE-NOTES 2012-02-28 21:12:54 UTC (rev
112642)
@@ -7,6 +7,10 @@
version 1.3.2. A new option 'singleempty' can be used to avoid this
behavior, ',' can be used to
create an array with two empty elements.
- Bug fixed where '#arraysearch' would always return '-1' instead of
parameter 5 value.
+ - '#arraysort' (for 'asce' and 'desc' modes) now respects local sort
settings specified via PHPs
+ 'setlocale()'. A flag system allows to set 'nolocale' separated by a
space behind the sort mode. to
+ deactivate this for the sorting.
+ - '#arraysort' has a new sort mode 'natural' which will sort numbers within
strings more human-like.
* December 5, 2011 -- Version 2.0rc2
- Bug introduced in r105069 fixed where '#arrayprint' was broken in
compatibility mode in some cases.
_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs