r103170 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r103169‎ | r103170 | r103171 >
Date:13:49, 15 November 2011
Author:danwe
Status:deferred
Tags:
Comment:
Compatibility with Variables extension 2.0 ensured
Modified paths:
  • /trunk/extensions/Loops/Loops.php (modified) (history)
  • /trunk/extensions/Loops/RELEASE-NOTES (modified) (history)

Diff [purge]

Index: trunk/extensions/Loops/Loops.php
@@ -161,24 +161,24 @@
162162 // no loops to perform
163163 return '';
164164 }
165 -
166 - global $wgExtVariables;
167 -
 165+
168166 $output = '';
169167 $endVal = $startVal + $loops;
 168+ $i = $startVal;
170169
171 - while( $startVal !== $endVal ) {
 170+ while( $i !== $endVal ) {
172171 // limit check:
173172 if( ! self::incrCounter( $parser ) ) {
174173 return self::msgLoopsLimit( $output );
175174 }
176175
177 - $wgExtVariables->vardefine( $parser, $varName, (string)$startVal );
 176+ // set current position as variable:
 177+ self::setVariable( $parser, $varName, (string)$i );
178178
179179 $output .= trim( $frame->expand( $rawCode ) );
180180
181181 // in-/decrease loop count (count can be negative):
182 - ( $startVal < $endVal ) ? $startVal++ : $startVal--;
 182+ ( $i < $endVal ) ? $i++ : $i--;
183183 }
184184 return $output;
185185 }
@@ -245,8 +245,7 @@
246246 // unexpanded code:
247247 $rawCode = array_shift( $funcArgs );
248248 $rawCode = $rawCode !== null ? $rawCode : '';
249 -
250 - global $wgExtVariables;
 249+
251250 $output = '';
252251
253252 // if prefix contains numbers only or isn't set, get all arguments, otherwise just non-numeric
@@ -259,15 +258,11 @@
260259 continue;
261260 }
262261 if ( $keyVar !== $valVar ) {
263 - // variable with the argument name as value
264 - $wgExtVariables->vardefine(
265 - $parser,
266 - $keyVar,
267 - trim( substr( $argName, strlen( $prefix ) ) )
268 - );
 262+ // variable with the argument name without prefix as value:
 263+ self::setVariable( $parser, $keyVar, substr( $argName, strlen( $prefix ) ) );
269264 }
270 - // variable with the arguments value
271 - $wgExtVariables->vardefine( $parser, $valVar, trim( $argVal ) );
 265+ // variable with the arguments value:
 266+ self::setVariable( $parser, $valVar, $argVal );
272267
273268 // expand current run:
274269 $output .= trim( $frame->expand( $rawCode ) );
@@ -276,7 +271,34 @@
277272 return $output;
278273 }
279274
 275+ /**
 276+ * Connects to 'Variables' extension and sets a variable. Handles different versions of
 277+ * 'Variables' extension since there have changed some things along the way.
 278+ *
 279+ * @param Parser $parser
 280+ * @param string $varName
 281+ * @param string $varValue
 282+ */
 283+ private static function setVariable( Parser &$parser, $varName, $varValue ) {
 284+ global $wgExtVariables;
 285+
 286+ static $newVersion = null;
 287+ if( $newVersion === null ) {
 288+ // find out whether local wiki is using variables extension 2.0 or higher
 289+ $newVersion = ( defined( 'ExtVariables::VERSION' ) && version_compare( ExtVariables::VERSION, '1.9999', '>' ) );
 290+ }
 291+
 292+ if( $newVersion ) {
 293+ // clean way since Variables 2.0:
 294+ ExtVariables::get( $parser )->setVarValue( $varName, $varValue );
 295+ }
 296+ else {
 297+ // make sure to trim values and convert them to string since old versions of Variables extension won't do this.
 298+ $wgExtVariables->vardefine( $parser, trim( $varName ), trim( $varValue ) );
 299+ }
 300+ }
280301
 302+
281303 ###############
282304 # Loops Count #
283305 ###############
Index: trunk/extensions/Loops/RELEASE-NOTES
@@ -4,13 +4,12 @@
55 * (trunk) -- Version 0.4 alpha by Daniel Werner
66 - The loops count now counts per parser instance. Before there were several hacks to reset the
77 counter, for example including an special page in wikitext. This has been solved now.
 8+ - Compatbility to Variables Version 2.0.
89 - Language file clean-up.
910 - Internal structure changed, ExtLoops now works as a static class.
1011 - '#fornumargs' parameter 1 can be empty to make it more consistent with other Loop parser
1112 functions. E.g. '{{#fornumargs: | keyVarName | valVarName | code }}' istead of
1213 '{{#fornumargs: keyVarName | valVarName | code }}' (which still works as well).
13 - @ToDo: Open points for final 0.4 release:
14 - - Compatbility to Variables Version 2.0.
1514
1615 * November 15, 2011 -- Version 0.3.2 by Daniel Werner
1716 - Bug solved where '#loop' didn't create variables with name or starting index '0'.

Status & tagging log