r103379 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r103378‎ | r103379 | r103380 >
Date:20:44, 16 November 2011
Author:danwe
Status:deferred
Tags:
Comment:
Some cleanup + minor arraysearch bugfix, won't return result with 'noparse' => false anymore and takes care of parsing by itself to handle <noinclude>/<includeonly> the proper way.
Modified paths:
  • /trunk/extensions/ArrayExtension/ArrayExtension.i18n.magic.php (modified) (history)
  • /trunk/extensions/ArrayExtension/ArrayExtension.i18n.php (modified) (history)
  • /trunk/extensions/ArrayExtension/ArrayExtension.php (modified) (history)
  • /trunk/extensions/ArrayExtension/INSTALL (modified) (history)
  • /trunk/extensions/ArrayExtension/RELEASE-NOTES (modified) (history)

Diff [purge]

Index: trunk/extensions/ArrayExtension/ArrayExtension.i18n.php
@@ -1,7 +1,7 @@
22 <?php
33
44 /**
5 - * Internationalization file for the ArrayExtension extension.
 5+ * Internationalization file for the 'ArrayExtension' extension.
66 *
77 * @since 1.3.4
88 *
@@ -81,7 +81,7 @@
8282 * @author Kghbln
8383 */
8484 $messages['de'] = array(
85 - 'arrayext-desc' => 'Ermöglicht das Speichern und Nutzen definierter Datenreihen',
 85+ 'arrayext-desc' => 'Ermöglicht das Speichern und Nutzen definierter Datenreihen (Arrays)',
8686 );
8787
8888 /** French (Français)
Index: trunk/extensions/ArrayExtension/ArrayExtension.php
@@ -3,9 +3,9 @@
44 /**
55 * Initialization file for the ArrayExtension extension.
66 *
7 - * Documentation: http://www.mediawiki.org/wiki/Extension:ArrayExtension
8 - * Support http://www.mediawiki.org/wiki/Extension_talk:ArrayExtension
9 - * Source code: http://svn.wikimedia.org/viewvc/mediawiki/trunk/extensions/ArrayExtension
 7+ * Documentation: http://www.mediawiki.org/wiki/Extension:ArrayExtension
 8+ * Support: http://www.mediawiki.org/wiki/Extension_talk:ArrayExtension
 9+ * Source code: http://svn.wikimedia.org/viewvc/mediawiki/trunk/extensions/ArrayExtension
1010 *
1111 * @file ArrayExtension.php
1212 * @ingroup ArrayExtension
@@ -42,44 +42,58 @@
4343 die( 'This file is a MediaWiki extension, it is not a valid entry point' );
4444 }
4545
46 -$wgHooks['ParserFirstCallInit'][] = 'efArrayExtensionParserFirstCallInit';
47 -
4846 $wgExtensionCredits['parserhook'][] = array(
49 - 'path' => __FILE__,
50 - 'name' => 'ArrayExtension',
51 - 'url' => 'http://www.mediawiki.org/wiki/Extension:ArrayExtension',
52 - 'author' => array ( 'Li Ding', 'Jie Bao', 'Daniel Werner' ),
 47+ 'path' => __FILE__,
 48+ 'name' => 'ArrayExtension',
 49+ 'url' => 'http://www.mediawiki.org/wiki/Extension:ArrayExtension',
 50+ 'author' => array ( 'Li Ding', 'Jie Bao', 'Daniel Werner' ),
5351 'descriptionmsg' => 'arrayext-desc',
54 - 'version' => ArrayExtension::VERSION
 52+ 'version' => ArrayExtension::VERSION
5553 );
5654
 55+$wgHooks['ParserFirstCallInit'][] = 'efArrayExtensionParserFirstCallInit';
 56+
5757 $dir = dirname( __FILE__ );
5858
59 -$wgExtensionMessagesFiles['ArrayExtension'] = $dir . '/ArrayExtension.i18n.php';
 59+$wgExtensionMessagesFiles['ArrayExtension' ] = $dir . '/ArrayExtension.i18n.php';
6060 $wgExtensionMessagesFiles['ArrayExtensionMagic'] = $dir . '/ArrayExtension.i18n.magic.php';
6161
 62+unset( $dir );
 63+
6264 /**
6365 * named arrays - an array has a list of values, and could be set to a SET
6466 */
6567 class ArrayExtension {
6668
 69+ /**
 70+ * Version of the ArrayExtension extension.
 71+ *
 72+ * @since 1.3.2
 73+ */
6774 const VERSION = '1.3.4 alpha';
6875
 76+ /**
 77+ * Store for arrays.
 78+ *
 79+ * @var array
 80+ * @private
 81+ */
6982 var $mArrayExtension = array();
7083
71 - function ArrayExtension() {
 84+ function __construct() {
7285 global $wgHooks;
7386 $wgHooks['ParserClearState'][] = &$this;
7487 }
7588
7689 function onParserClearState( &$parser ) {
77 - $this->mArrayExtension = array(); // remove all arrays to avoid conflicts with job queue or Special:Import or SMW semantic updates
 90+ // remove all arrays to avoid conflicts with job queue or Special:Import or SMW semantic updates
 91+ $this->mArrayExtension = array();
7892 return true;
7993 }
8094
81 - ////////////////////////////////////////////////////////// /
 95+ ///////////////////////////////////////////////////////////
8296 // PART 1. constructor
83 - ////////////////////////////////////////////////////////// /
 97+ ///////////////////////////////////////////////////////////
8498
8599 /**
86100 * Define an array by a list of 'values' deliminated by 'delimiter',
@@ -90,7 +104,17 @@
91105 * http://us2.php.net/manual/en/book.pcre.php
92106 * see also: http://us2.php.net/manual/en/function.preg-split.php
93107 */
94 - function arraydefine( &$parser, $arrayid, $value = '', $delimiter = '/\s*,\s*/', $options = '', $delimiter2 = ', ', $search = '@@@@', $subject = '@@@@', $frame = null ) {
 108+ function arraydefine(
 109+ &$parser,
 110+ $arrayid,
 111+ $value = '',
 112+ $delimiter = '/\s*,\s*/',
 113+ $options = '',
 114+ $delimiter2 = ', ',
 115+ $search = '@@@@',
 116+ $subject = '@@@@',
 117+ $frame = null
 118+ ) {
95119 if ( !isset( $arrayid ) ) {
96120 return '';
97121 }
@@ -148,9 +172,9 @@
149173 }
150174
151175
152 - ////////////////////////////////////////////////////////// /
 176+ ///////////////////////////////////////////////////////////
153177 // PART 2. print
154 - ////////////////////////////////////////////////////////// /
 178+ ///////////////////////////////////////////////////////////
155179
156180
157181 /**
@@ -169,29 +193,59 @@
170194 * {{#arrayprint:b|<br/>|@@@|[[name::@@@]]}} -- make SMW links
171195 */
172196 function arrayprint( &$parser, $arrayid , $delimiter = ', ', $search = '@@@@', $subject = '@@@@', $frame = null ) {
173 - $ret = $this->validate_array_by_arrayid( $arrayid );
174 - if ( $ret !== true ) {
175 - return $ret;
176 - }
 197+ $ret = $this->validate_array_by_arrayid( $arrayid );
 198+ if ( $ret !== true ) {
 199+ return $ret;
 200+ }
177201
178 - $values = $this->mArrayExtension[$arrayid];
179 - $rendered_values = array();
180 - foreach ( $values as $v ) {
181 - $temp_result_value = str_replace( $search, $v, $subject );
182 - if ( isset( $frame ) ) {
183 - $temp_result_value = $parser->preprocessToDom( $temp_result_value, $frame->isTemplate() ? Parser::PTD_FOR_INCLUSION : 0 );
184 - $temp_result_value = trim( $frame->expand( $temp_result_value ) );
185 - }
186 - $rendered_values[] = $temp_result_value ;
187 - }
188 - return array( implode( $delimiter, $rendered_values ) , 'noparse' => false, 'isHTML' => false );
189 - }
 202+ $values = $this->mArrayExtension[$arrayid];
 203+ $rendered_values = array();
 204+ foreach ( $values as $v ) {
 205+ $temp_result_value = str_replace( $search, $v, $subject );
 206+ // frame is only available in newer MW versions
 207+ if ( isset( $frame ) ) {
 208+ /*
 209+ * in case frame is given (new MW versions) the $subjectd still is un-expanded (this allows to use
 210+ * some parser functions like {{FULLPAGENAME:@@@@}} directly without getting parsed before @@@@ is replaced.
 211+ * Expand it so we replace templates like {{!}} which we need for the final parse.
 212+ */
 213+ $temp_result_value = $parser->preprocessToDom( $temp_result_value, $frame->isTemplate() ? Parser::PTD_FOR_INCLUSION : 0 );
 214+ $temp_result_value = trim( $frame->expand( $temp_result_value ) );
 215+ }
 216+ $rendered_values[] = $temp_result_value ;
 217+ }
 218+
 219+ $output = implode( $delimiter, $rendered_values );
 220+ $noparse = false;
 221+
 222+ if ( isset( $frame ) ) {
 223+ /*
 224+ * don't leave the final parse to Parser::braceSubstitution() since there are some special cases where it
 225+ * would produce unexpected output (it uses a new child frame and ignores whether the frame is a template!)
 226+ */
 227+ $noparse = true;
190228
191 - function arrayprintObj( &$parser, $frame, $args ) {
 229+ $output = $parser->preprocessToDom( $output, $frame->isTemplate() ? Parser::PTD_FOR_INCLUSION : 0 );
 230+ $output = trim( $frame->expand( $output ) );
 231+ }
 232+
 233+ return array(
 234+ $output,
 235+ 'noparse' => $noparse,
 236+ 'isHTML' => false
 237+ );
 238+ }
 239+
 240+ function arrayprintObj( &$parser, $frame, $args ) {
192241 // Set variables
193 - $arrayid = isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : '';
 242+ $arrayid = isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : '';
194243 $delimiter = isset( $args[1] ) ? trim( $frame->expand( $args[1] ) ) : ', ';
195 - $search = isset( $args[2] ) ? trim( $frame->expand( $args[2], PPFrame::NO_ARGS | PPFrame::NO_TEMPLATES ) ) : '@@@@';
 244+ /*
 245+ * PPFrame::NO_ARGS and PPFrame::NO_TEMPLATES for expansion make a lot of sense here since the patterns getting replaced
 246+ * in $subject before $subject is being parsed. So any template or argument influence in the patterns wouldn't make any
 247+ * sense in any sane scenario.
 248+ */
 249+ $search = isset( $args[2] ) ? trim( $frame->expand( $args[2], PPFrame::NO_ARGS | PPFrame::NO_TEMPLATES ) ) : '@@@@';
196250 $subject = isset( $args[3] ) ? trim( $frame->expand( $args[3], PPFrame::NO_ARGS | PPFrame::NO_TEMPLATES ) ) : '@@@@';
197251
198252 return $this->arrayprint( $parser, $arrayid, $delimiter, $search, $subject, $frame );
@@ -261,7 +315,7 @@
262316 if ( !$ret )
263317 return $no;
264318
265 - if ( !$this->isValidRegEx( $needle ) )
 319+ if ( ! $this->isValidRegEx( $needle ) )
266320 $needle = '/^\s*' . preg_quote( $needle, '/' ) . '\s*$/';
267321
268322 // search for a match inside the array:
@@ -337,9 +391,9 @@
338392
339393
340394
341 - ////////////////////////////////////////////////////////// /
 395+ ///////////////////////////////////////////////////////////
342396 // PART 3. alter an array
343 - ////////////////////////////////////////////////////////// /
 397+ ///////////////////////////////////////////////////////////
344398
345399 /**
346400 * reset some or all defined arrayes
@@ -425,9 +479,9 @@
426480 }
427481
428482
429 - ////////////////////////////////////////////////////////// /
 483+ ///////////////////////////////////////////////////////////
430484 // PART 4. create an array
431 - ////////////////////////////////////////////////////////// /
 485+ ///////////////////////////////////////////////////////////
432486
433487 /**
434488 * merge two arrays, keep duplicated values
@@ -702,39 +756,51 @@
703757 else
704758 return null;
705759 }
706 -
 760+
 761+ /**
 762+ * Returns whether a certain array is defined within the page scope.
 763+ *
 764+ * @param string $arrayId
 765+ * @return boolean
 766+ */
707767 function arrayExists( $arrayId = '' ) {
708 - if ( array_key_exists( trim( $arrayId ), $this->mArrayExtension ) )
709 -
710 -
711 -
712 - return true;
713 - else
714 - return false;
 768+ return array_key_exists( trim( $arrayId ), $this->mArrayExtension );
715769 }
716770
717 - // add a new array or overwrite existing one. Values delivered as real array.
718 - function createArray( $arrayId = '', $arr = array() ) {
 771+ /**
 772+ * Adds a new array or overwrites an existing one.
 773+ *
 774+ * @param string $arrayId
 775+ * @param array $arr the new array, should contain string values.
 776+ */
 777+ public function createArray( $arrayId = '', $arr = array() ) {
719778 $arr = $this->reorganizeArrayKeys( $arr );
720779 $this->mArrayExtension[ trim( $arrayId ) ] = $arr;
721780 }
722781
723782
724 - // remove an existing array. If array doesn't exist this will return false, otherwise true.
725 - function removeArray( $arrayId = '' ) {
 783+ /**
 784+ * Removes an existing array. If array doesn't exist this will return false, otherwise true.
 785+ *
 786+ * @param string $arrayId
 787+ * @return boolean whether the array existed and has been removed
 788+ */
 789+ public function removeArray( $arrayId = '' ) {
726790 $arrayId = trim( $arrayId );
727791 if ( $this->arrayExists( $arrayId ) ) {
728792 unset( $this->mArrayExtension[ $arrayId ] );
729793 return true;
730 - } else {
731 - return false;
732794 }
733 -
734 -
 795+ return false;
735796 }
736797
737 - // Rebuild the array and reorganize all keys. This means all gaps between array items will be closed.
738 - function reorganizeArrayKeys( $arr = array() ) {
 798+ /**
 799+ * Rebuild the array and reorganize all keys. This means all gaps between array items will be closed.
 800+ *
 801+ * @param array $arr array to be reorganized
 802+ * @return array
 803+ */
 804+ public function reorganizeArrayKeys( $arr = array() ) {
739805 $newArray = array();
740806 foreach ( $arr as $val ) {
741807 $newArray[] = trim( $val );
@@ -742,16 +808,22 @@
743809 return $newArray;
744810 }
745811
746 - // Decide for the given $pattern if its a valid regular expression or not
747 - function isValidRegEx( $pattern ) {
748 - return preg_match( '/^([\\/\\|%]).*\\1[imsSuUx]*$/', $pattern );
 812+ /**
 813+ * Decides for the given $pattern whether its a valid regular expression acceptable for
 814+ * ArrayExtension parser functions or not.
 815+ *
 816+ * @param string $pattern regular expression including delimiters and optional flags
 817+ * @return boolean
 818+ */
 819+ function isValidRegEx( $pattern ) {
 820+ return (bool)preg_match( '/^([\\/\\|%]).*\\1[imsSuUx]*$/', $pattern );
749821 }
750822 }
751823
752 -function efArrayExtensionParserFirstCallInit( $parser ) {
 824+function efArrayExtensionParserFirstCallInit( &$parser ) {
753825 global $wgArrayExtension;
754826
755 - $wgArrayExtension = new ArrayExtension;
 827+ $wgArrayExtension = new ArrayExtension();
756828 $parser->setFunctionHook( 'arraydefine', array( &$wgArrayExtension, 'arraydefine' ) );
757829
758830 if ( defined( get_class( $parser ) . '::SFH_OBJECT_ARGS' ) ) {
Index: trunk/extensions/ArrayExtension/INSTALL
@@ -1,12 +1,12 @@
2 -These is the install file for the ArrayExtension extension.
 2+This is the install file for the 'ArrayExtension' extension.
33
44 Extension page on mediawiki.org: http://www.mediawiki.org/wiki/Extension:ArrayExtension
5 -Latest version of the install file: http://svn.wikimedia.org/viewvc/mediawiki/trunk/extensions/ArrayExtension/INSTALL?view=co
 5+Latest version of this install file: http://svn.wikimedia.org/viewvc/mediawiki/trunk/extensions/ArrayExtension/INSTALL?view=co
66
77
88 == Download ==
99
10 -You can download a snapshot of the ArrayExtension via the Wikimedia extension distributor at
 10+You can download a snapshot of 'ArrayExtension' via the Wikimedia extension distributor at
1111 https://secure.wikimedia.org/wikipedia/mediawiki/wiki/Special:ExtensionDistributor/ArrayExtension
1212
1313 You can also get the code directly from SVN. Tags can be obtained via
@@ -18,10 +18,11 @@
1919
2020 svn checkout <nowiki>http://svn.wikimedia.org/svnroot/mediawiki/trunk/extensions/ArrayExtension/</nowiki>
2121
 22+
2223 == Installation ==
2324
24 -Once you have downloaded the code, place the ''ArrayExtension'' directory within your
 25+Once you have downloaded the code, place the 'ArrayExtension' directory within your
2526 MediaWiki 'extensions' directory. Then add the following code to your [[Manual:LocalSettings.php|LocalSettings.php]] file:
2627
27 -# ArrayExtension
28 -require_once( "$IP/extensions/ArrayExtension/ArrayExtension.php" );
 28+ # ArrayExtension
 29+ require_once( "$IP/extensions/ArrayExtension/ArrayExtension.php" );
Index: trunk/extensions/ArrayExtension/RELEASE-NOTES
@@ -1,5 +1,9 @@
22 Changelog:
33 ==========
 4+ * (trunk) version 1.3.4
 5+ - arrayprint will handle <includeonly>/<noinclude> correct in case its used in a template.
 6+ - moved into mediawiki.org SVN
 7+
48 * January 24, 2011 version 1.3.2
59 - New public class methods for creating and removing arrays. Good for use by other extensions.
610 - VERSION constant added to ArrayExtension class
Index: trunk/extensions/ArrayExtension/ArrayExtension.i18n.magic.php
@@ -1,7 +1,7 @@
22 <?php
33
44 /**
5 - * Internationalization file for magic words in the ArrayExtension extension.
 5+ * Internationalization file for magic words in the 'ArrayExtension' extension.
66 *
77 * @file ArrayExtension.i18n.magic.php
88 * @ingroup ArrayExtension

Status & tagging log