r81676 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r81675‎ | r81676 | r81677 >
Date:01:14, 8 February 2011
Author:jeroendedauw
Status:deferred (Comments)
Tags:
Comment:
Minor fix and incremented version number
Modified paths:
  • /trunk/extensions/ArrayExtension/ArrayExtension.php (modified) (history)

Diff [purge]

Index: trunk/extensions/ArrayExtension/ArrayExtension.php
@@ -38,7 +38,6 @@
3939 'author' => array ( 'Li Ding', 'Jie Bao', 'Daniel Werner' ),
4040 'description' => 'Store and compute named arrays',
4141 'version' => ArrayExtension::VERSION,
42 -
4342 );
4443
4544 $wgHooks['LanguageGetMagic'][] = 'efArrayExtensionLanguageGetMagic';
@@ -49,7 +48,7 @@
5049 */
5150 class ArrayExtension {
5251
53 - const VERSION = '1.3.2';
 52+ const VERSION = '1.3.3';
5453
5554 var $mArrayExtension = array();
5655
@@ -77,20 +76,22 @@
7877 * see also: http://us2.php.net/manual/en/function.preg-split.php
7978 */
8079 function arraydefine( &$parser, $arrayid, $value = '', $delimiter = '/\s*,\s*/', $options = '', $delimiter2 = ', ', $search = '@@@@', $subject = '@@@@', $frame = null ) {
81 - if ( !isset( $arrayid ) )
82 - return '';
 80+ if ( !isset( $arrayid ) ) {
 81+ return '';
 82+ }
8383
8484 // normalize
8585 $value = trim( $value );
8686 $delimiter = trim( $delimiter );
8787
88 - if ( !$this->is_non_empty ( $value ) ) {
 88+ if ( !$this->is_non_empty( $value ) ) {
8989 $this->mArrayExtension[$arrayid] = array();
9090 } else if ( !$this->is_non_empty( $delimiter ) ) {
9191 $this->mArrayExtension[$arrayid] = array( $value );
9292 } else {
93 - if ( !$this->isValidRegEx( $delimiter ) )
94 - $delimiter = '/\s*' . preg_quote( $delimiter, '/' ) . '\s*/'; // Anpassung von Daniel Werner (preg_quote)
 93+ if ( !$this->isValidRegEx( $delimiter ) ) {
 94+ $delimiter = '/\s*' . preg_quote( $delimiter, '/' ) . '\s*/'; // Anpassung von Daniel Werner (preg_quote)
 95+ }
9596
9697 $this->mArrayExtension[$arrayid] = preg_split( $delimiter, $value );
9798
@@ -103,23 +104,32 @@
104105 // now parse the options, and do posterior process on the created array
105106 $ary_option = $this->parse_options( $options );
106107
 108+ if ( !array_key_exists( 'empty', $ary_option ) ) {
 109+ foreach ( $this->mArrayExtension[$arrayid] as $key => $value ) {
 110+ if ( trim( $value ) == '' ) {
 111+ unset( $this->mArrayExtension[$arrayid][$key] );
 112+ }
 113+ }
 114+ }
 115+
107116 // make it unique if option is set
108 - if ( FALSE !== array_key_exists( 'unique', $ary_option ) ) {
109 - $this->arrayunique( $parser, $arrayid );
 117+ if ( array_key_exists( 'unique', $ary_option ) ) {
 118+ $this->arrayunique( $parser, $arrayid );
110119 }
111120
112121 // sort array if the option is set
113 - $this->arraysort( $parser, $arrayid, $this->get_array_value( $ary_option, "sort" ) );
 122+ $this->arraysort( $parser, $arrayid, $this->get_array_value( $ary_option, 'sort' ) );
114123
115124 // print the array upon request
116 - if ( strcmp( "list", $this->get_array_value( $ary_option, "print" ) ) === 0 ) {
 125+ if ( strcmp( 'list', $this->get_array_value( $ary_option, 'print' ) ) === 0 ) {
117126 return $this->arrayprint( $parser, $arrayid );
118 - } else if ( strcmp( "full", $this->get_array_value( $ary_option, "print" ) ) === 0 ) {
 127+ }
 128+ else if ( strcmp( 'full', $this->get_array_value( $ary_option, 'print' ) ) === 0 ) {
119129 return $this->arrayprint( $parser, $arrayid, $delimiter2, $search, $subject, $frame );
120130 }
121 - }
 131+ }
122132
123 - return '';
 133+ return '';
124134 }
125135
126136

Follow-up revisions

RevisionCommit summaryAuthorDate
r103703Some stuff rewritten to get rid of confusing helper functions. #arraydefine o...danwe19:20, 19 November 2011

Comments

#Comment by Danwe (talk | contribs)   21:03, 18 November 2011

having the 'empty' option as default is not a good idea, this is potentially breaking some stuff. Also after doing this (using unset() on the array), reorganizeArrayKeys() must be executed on the array or you might have errors in a couple of other functions (just had one in #arraysearcharray) because all functions will assume that there are no gaps within array key order!

Is there any good reason for having something like 'empty' or the opposite as an option? By default all values will end up in the array, also empty ones, if you use '#arrayunique' on it it will remove all empty entries as well as duplicates. The design flaw lies within the arrayunique function/option where it is not possible to preserve a single empty item. This is bugging me ever since the first version of arrayextension although I never ran in an actual case where it would have blocked my work, if so I would have written another parser function or a third parameter for arrayunique allowing an option to alter that behavior. Trying to avoiding all this by adding more options to arraydefine which is too complex and buggy as it is doesn't seem like the best solution.

Status & tagging log