r104277 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r104276‎ | r104277 | r104278 >
Date:16:45, 26 November 2011
Author:danwe
Status:deferred
Tags:
Comment:
'#arrayprint' no longer supports option 'print=print' and new function ExtArrays::arraySort()
Modified paths:
  • /trunk/extensions/Arrays/Arrays.php (modified) (history)
  • /trunk/extensions/Arrays/RELEASE-NOTES (modified) (history)

Diff [purge]

Index: trunk/extensions/Arrays/RELEASE-NOTES
@@ -15,6 +15,8 @@
1616 + '#arraymerge', '#arrayunion', '#arraydiff' and '#arrayintersect' with only one array for
1717 the operation will make a copy of that array instead of creating no array at all.
1818 + See 1.4 alpha for previous changes
 19+ - '#arraydefine' option 'print' no longer supports 'print=print' and parameters beyond options.
 20+ This functionality never worked reliably anyways. 'print=list' still works.
1921 - If 'Regex Fun' extension is installed, '#arraysearcharray' can use Regex Funs 'e' flag feature
2022 for transforming the result and at the same time parsing them after each back-reference inclusion.
2123 - 1.4 alpha bug solved, '#arraysearcharray' with empty string as start index was interpreted as 0
Index: trunk/extensions/Arrays/Arrays.php
@@ -154,11 +154,7 @@
155155 $arrayId,
156156 $value = null,
157157 $delimiter = '/\s*,\s*/',
158 - $options = '',
159 - $delimiter2 = ', ',
160 - $search = '@@@@',
161 - $subject = '@@@@',
162 - $frame = null
 158+ $options = ''
163159 ) {
164160 if ( !isset( $arrayId ) ) {
165161 return '';
@@ -230,17 +226,17 @@
231227 */
232228
233229 // sort array if the option is set
234 - self::pf_arraysort( $parser, $arrayId, self::array_value( $arrayOptions, 'sort' ) );
 230+ if( array_key_exists( 'sort', $arrayOptions ) ) {
 231+ $array = self::arraySort( $array, self::array_value( $arrayOptions, 'sort' ) );
 232+ }
235233
236234 // print the array upon request
237235 switch( self::array_value( $arrayOptions, 'print' ) ) {
238236 case 'list':
239 - $out = self::pf_arrayprint( $parser, $arrayId );
 237+ global $wgLang;
 238+ // simple list output
 239+ $out = implode( ', ', $array );
240240 break;
241 -
242 - case 'print':
243 - $out = self::pf_arrayprint( $parser, $arrayId, $delimiter2, $search, $subject, $frame );
244 - break;
245241 }
246242 }
247243
@@ -668,32 +664,16 @@
669665 static function pf_arraysort( Parser &$parser, $arrayId , $sort = 'none' ) {
670666 $store = self::get( $parser );
671667
672 - if( ! $store->arrayExists( $arrayId ) ) {
 668+ $array = $store->getArray( $arrayId );
 669+
 670+ if( $array === null ) {
673671 return '';
674672 }
675673
676 - // do the requested sorting of the given array:
677 - switch( $sort ) {
678 - case 'asc':
679 - case 'asce':
680 - case 'ascending':
681 - sort( $store->mArrays[ $arrayId ] );
682 - break;
683 -
684 - case 'desc':
685 - case 'descending':
686 - rsort( $store->mArrays[ $arrayId ] );
687 - break;
688 -
689 - case 'random':
690 - shuffle( $store->mArrays[ $arrayId ] );
691 - break;
692 -
693 - case 'reverse':
694 - $reverse = array_reverse( $store->getArray( $arrayId ) );
695 - $store->setArray( $arrayId, $reverse );
696 - break;
697 - } ;
 674+ // sort array and store it
 675+ $array = self::arraySort( $array, $sort );
 676+ $store->setArray( $arrayId, $array );
 677+ return '';
698678 }
699679
700680
@@ -921,9 +901,11 @@
922902 /**
923903 * Convenience function to get a value from an array. Returns '' in case the
924904 * value doesn't exist or no array was given
 905+ *
 906+ * @return string
925907 */
926908 protected static function array_value( $array, $field ) {
927 - if ( is_array( $array ) && array_key_exists( $field, $array ) ) {
 909+ if ( is_array( $array ) && array_key_exists( $field, $array ) ) {
928910 return $array[ $field ];
929911 }
930912 return '';
@@ -1124,7 +1106,7 @@
11251107
11261108 /**
11271109 * Removes duplicate values and all empty elements from an array just like the
1128 - * arrayunique parser function would do it. The array will be sanitized internally.
 1110+ * '#arrayunique' parser function would do it. The array will be sanitized internally.
11291111 *
11301112 * @since 2.0
11311113 *
@@ -1136,7 +1118,44 @@
11371119 $arr = $this->sanitizeArray( $arr );
11381120 $array = self::array_unique( $array );
11391121 }
 1122+
 1123+ /**
 1124+ * Sorts an array just like parser function '#arraysort' would do it and allows the
 1125+ * same sort modes.
 1126+ *
 1127+ * @since 2.0
 1128+ *
 1129+ * @param array $array
 1130+ * @param string $sortMode
 1131+ *
 1132+ * @return array
 1133+ */
 1134+ public static function arraySort( array $array, $sortMode ) {
 1135+ // do the requested sorting of the given array:
 1136+ switch( $sortMode ) {
 1137+ case 'asc':
 1138+ case 'asce':
 1139+ case 'ascending':
 1140+ sort( $array );
 1141+ break;
11401142
 1143+ case 'desc':
 1144+ case 'descending':
 1145+ rsort( $array );
 1146+ break;
 1147+
 1148+ case 'rand':
 1149+ case 'random':
 1150+ shuffle( $array );
 1151+ break;
 1152+
 1153+ case 'reverse':
 1154+ $array = array_reverse( $array );
 1155+ break;
 1156+ } ;
 1157+ return $array;
 1158+ }
 1159+
11411160 /**
11421161 * Decides for the given $pattern whether its a valid regular expression acceptable for
11431162 * Arrays parser functions or not.

Status & tagging log