Extension talk:Loops/Archive

Feedback

Doesn't only work on MW 1.12 Alpha, works also on 1.12. I hope it also will work on 1.13. This extension is very helpful, for example for building well arranged templates without copy paste and change 100 times for "array" parameters. Found no Bugs as yet. Hope somone will develope on this extension that we can see it with stable Status as soon as possible! --77.191.205.221 10:13, 6 August 2008 (UTC)Reply[reply]

Thanks for the feedback! Though no one has reported any bugs, I know of only three or four people who use this extension (present company included), so I don't feel comfortable updating the status to "stable" yet. —Sledged (talk) 15:59, 6 August 2008 (UTC)Reply[reply]
I use this extension! ([1]). I have a template calling it here. We discuss the necessity of this template all the time on the mailing list. Keep up the great work! --Aquatiki 22:09, 6 August 2008 (UTC)Reply[reply]
Glad to know it works well for you. I'll go ahead and change it to stable, since no bugs have been found with the bread-and-butter functions (while and dowhile). foreachnamedarg, however, is still experimental and subject to change. —Sledged (talk) 18:26, 21 August 2008 (UTC)Reply[reply]
I use this extension, too! ([2]). It's crucial to my [|Random Image Gallery] template as used on the homepage. Awesome work! --Arodicus 22:36, 8 October 2008 (UTC)Reply[reply]

Ok, It also works on MW 1.13.1, tested now! --77.191.216.80 15:03, 30 September 2008 (UTC)Reply[reply]

I use this template at [3] for a lot of templates, the most complex of which is here: [4] Thanks a lot Sledged! --J.nesta 14:08, 2 November 2009 (UTC)Reply[reply]

Foreachnamedarg

Foreachnamedarg is really amazing! But why don't you allow to use it without prefix? I have some templates where I allow the users to use parameter 1, 2, 3 and so on without any limitation, I really could use it there so I would save the increment variable, the condition and the asigning of the parameter value to a variable. If Foreachnamedarg is significant faster than the common loops or it allows to do more loop runs than the other loops before you get a php error message because of memory or something, It would a fortiori make sense to allow it. With many loops in my pages they get really slow sometimes. --Danwe 21:38, 13 February 2009 (UTC)Reply[reply]

I'm glad you find it useful. Foreachnamedarg is a pilot parser function for an idea I'm still fleshing out. Eventually, I plan to replace it with foreacharg which will do exactly what you're describing while still keeping the current functionality. I have no time frame for this though. —Sledged (talk) 20:07, 19 February 2009 (UTC)Reply[reply]
#forargs replaces #foreachnamedarg and works on all template arguments that match the supplied prefix which can be left empty. In which case it will access all template arguments. #fornumargs will access only the numbered arguments, whether they're set explicitly or implicitly. —Sledged (talk) 22:22, 2 May 2009 (UTC)Reply[reply]
Thats amazing. I especially love the #forargs feature which allows to access ALL arguments. #fornumargs Is great as well, Its helpfull to save some code. Can you tell us if #fornumargs brings any performance bonus compared with the old method with {{{ {{#var:i}} |}}}? Thanks for the release! --Danwe 15:22, 4 May 2009 (UTC)Reply[reply]
No clue. I haven't made any benchmarks on the performance. —Sledged (talk) 21:26, 15 May 2009 (UTC)Reply[reply]

Bug #1 - Invisible characters around generated text of the loop

Phew, I spent two evenings now untill I found that bugs source... It seems like the Bug is, that the Loops extension creates some invisible characters arround the text which is created inside the loop. So when you run a loop to create some text for a template the length of the text the template will return is much longer than the text you can see. In some cases this behavior causes problems, for example when you use the return value with string functions or inside a Semantic Media Wiki Property.

Example: The following loop:

{{#len:{{#vardefine:i | -1 }}{{
  #while:
  | {{ #vardefine:i | {{ #expr: {{ #var:i }} + 1 }} }}
    {{#ifexpr: {{#var:i}} < 5 |true|}}
  |<nowiki/>{{#var:i}}
}}}}

would return the output "01234", looks like its 5 chars long. But the #len function couns exactly 220 (!) characters!

I found a litte workarround for that: You can use the sub parser function from string functions to avoid this: {{#sub:<The loop>|0|-99999}} but you have to store the generated return value in a variable if it is generated in the loop because the variable has to print their content behind the loop and sub function. So return values of templates dont give back some invisible characters and you can use the string wherever you want without problems.

I hope you will fix this litte bug soon, I dont like my workarround pretty much :-/

--77.191.145.123 19:20, 29 January 2009 (UTC)Reply[reply]

Try it without the <nowiki/> tag, like so:
{{#len:{{#vardefine:i | -1 }}{{
  #while:
  | {{ #vardefine:i | {{ #expr: {{ #var:i }} + 1 }} }}
    {{#ifexpr: {{#var:i}} < 5 |true|}}
  |{{#var:i}}
}}}}
and see how it works. I use the nowiki tags in my examples so that the new line characters immediately after the tags are preserved. —Sledged (talk) 20:39, 29 January 2009 (UTC)Reply[reply]
Thank you, this works like expected! --77.191.253.66 22:33, 1 February 2009 (UTC)Reply[reply]

Help me please

I can't seem to get this to work: In my wiki theres a table at the bottom of many pages that go to different versions of the content on the page, so i want to use a template instead. I want to make a table that has one row (starting with the row heading "Inputs" in bold and the two-input version), and in each cell links to a page called: (first parameter) then "_(" then the i value then " bit)" with a label of the value of i. the variable i increments by 2 until i is less than the second parameter. Below is my attempt but it always seems to time out.


{| class="wikitable" border="1"
|-
| '''Inputs'''
| [[{{1}}|2]]

{{ #vardefine: i | 1 }}
{{
  #while:
  | {{ #ifexpr: {{ #var: i }} <= {{{2}}} | true }}
  | <nowiki/>
| [[{{{1}}} ({{ #var: i }} bit)|{{ #var: i }}]]{{ #vardefine: i | {{ #expr: {{ #var: i }} + 2 }} }}
}}

|}


Example: {{Templatename|Thing|9}} Should produce:

Input 2 3 5 7 9
You need to hide the pipe character after <nowiki/> tag in a template. Otherwise, MW thinks it's the start of another parameter. Standard practice is to create the template Template:!, whose only content that is included is a pipe character. —Sledged (talk) 18:01, 30 December 2008 (UTC)Reply[reply]

#while and header

i'm using MW 1.13.4

A problem with headers. if i try to create header within cycle, then only the 1st header resulted as header and the rest - as plain text. For example,

{{#vardefine: i | 0 }}{{#vardefine: n | 5 }}
{{#while:
  | {{#ifexpr: {{#var: i }} < {{#var: n }} | true }}
  | 
===header===
<br><br>text<br><br>
{{#vardefine: i | {{#expr: {{#var: i }} + 1 }} }} }}

it can be half-solved using var-wrap.

{{#vardefine: i | 0 }}{{#vardefine: n | 5 }}{{#vardefine: rez | }}
{{#while:
  | {{#ifexpr: {{#var: i }} < {{#var: n }} | true }}
  | {{#vardefine: rez | {{#var:rez}}
===header===
<br><br>text<br><br>}}
{{#vardefine: i | {{#expr: {{#var: i }} + 1 }} }} }}
{{#var:rez}}

in my real page i'm using my array extension, so my real code is useful

{{#vardefine: i | 0 }}{{#vardefine: rez | }}
{{#while:
  | {{#ifexpr: {{#var: i }} < {{#var: n }} | true }}
  | {{#vardefine: rez | {{#var:rez}}
==={{#array:pagename|{{#var:i}}}}===
<br><br>{{:Songs:{{#array:pagetitle|{{#var:i}}}}}}<br><br>}}
{{#vardefine: i | {{#expr: {{#var: i }} + 1 }} }} }}
{{#var:rez}}

--Schthaxe 09:12, 5 March 2009 (UTC)Reply[reply]

Whitespace (including newlines) is stripped from the beginning and end of all the arguments of the parser function. Add a non-whitespace character (e.g. the HTML encoding for a whitespace character &#32;, or a <nowiki/> tag) before the newline preceding each heading. —Sledged (talk) 17:35, 6 March 2009 (UTC)Reply[reply]

let's use for-cycle

    public static function forHook( &$parser, $frame, $args ) {
	global $wgExtVariables;
	$iter_var_name = array_shift( $args );
	$iter_var_name = $iter_var_name !== '' ? $iter_var_name : 'i';
	$startval = $frame->expand( array_shift( $args ) );
	$startval = isset( $startval ) ? intval( $startval ) : 0;
        $endval = $frame->expand( array_shift( $args ) );
	$endval = isset( $endval ) ? intval( $endval ) : 0;
        $loopStatement = array_shift( $args );
        $output = '';

	$wgExtVariables->mVariables[ $iter_var_name ] = $startval;
        while ( $startval < $endval ) {
            if ( self::$maxLoops >= 0 &&
                ++$parser->loops_count > self::$maxLoops )
                return wfMsgForContent( 'loops_max' );
 
            $output .= isset( $loopStatement ) ?
                trim( $frame->expand( $loopStatement ) ) : '';

	    $wgExtVariables->mVariables[ $iter_var_name ] = ++$startval;
        }
        return $output;
    }

then we can use

{{#for:i|0|10| {{#var:i}} }}

and get

0123456789

--Schthaxe 10:33, 12 March 2009 (UTC)Reply[reply]

I used #loop instead of #for, because another extension already uses #for. It works like so:
{{#loop: variable name | starting value | number of loops to be performed | wiki markup }}
if a negative value is supplied for the third argument, the value stored in the variable will decrease with each iteration. —Sledged (talk) 22:22, 2 May 2009 (UTC)Reply[reply]

Version 0.3.0 only works with MW 1.14 and later

Since you use functions in forargs and/or fornumargs like getNamedArguments which were added in MW r38981 the Loops plugin isn't full compatible to MW <1.14 anymore. I tested it in 1.13. Perhaps you should use SVN for the project so that MW 1.13 or less users could download an older version of the plugin. --Danwe 12:06, 2 June 2009 (UTC)Reply[reply]

I may do that down the road, but for now later versions can be accessed through the page's history. —Sledged (talk) 18:12, 2 November 2009 (UTC)Reply[reply]

ExtLoops count for -1 loops

In case you config ExtLoops::$maxLoops = -1; the html comment information in the pages source code always shows ExtLoops count: 0/-1. Would be usefull sometimes to know how many loops have been performed on a page. --Danwe 13:52, 11 October 2009 (UTC)Reply[reply]

Sounds reasonable. —Sledged (talk) 18:12, 2 November 2009 (UTC)Reply[reply]

Using #while to pass parameters to a subtemplate (I think I might have found a bug with Extension:Loops)

Has anybody had success in doing this? For example, the following code (with Arg1 being IceCream)

{{#vardefine:i|1}}{{
	#while:
	|{{#ifexpr:{{#var:i}}=1|true}}
	|Arg1={{{Arg1}}}{{#vardefine:i|{{#expr:{{#var:i}}+1}}}}}}
}}

will result in:

Arg1=IceCream

But the following code

{{Subtemplate|{{#vardefine:i|1}}{{
	#while:
	|{{#ifexpr:{{#var:i}}=1|true}}
	|Arg1={{{Arg1}}}{{#vardefine:i|{{#expr:{{#var:i}}+1}}}}}}
}}}}

will not pass the argument to the subtemplate! *throws brick through computer monitor* plz hralp( --J.nesta 14:26, 2 November 2009 (UTC)Reply[reply]

Found a work-around

It seems to work if you just use non-named arguments, like {{{1}}} and so forth. WTF? --J.nesta 14:26, 2 November 2009 (UTC)Reply[reply]

With templates, parser functions, and magic words (i.e. anything that uses the {{...}} syntax), when specifying an argument, MediaWiki only recognizes the equal sign if it's outside a nested template-like call in the argument. It doesn't process equals signs that are the result of a template-like call. For instance, the markup sample cases
{{tplate|arg1=val1}}
{{tplate|arg1={{#if:|| val1 }} }}
{{tplate|{{#if:|| arg1 }}=val1 }}
and
{{tplate|{{#if:|| arg1 }}={{#if:|| val1 }} }}
all set the argument arg1 to the value of val1, because the equal sign is not inside a nested {{#if:}} call. Conversely, the markup
{{tplate|{{#if:|| arg1=val1 }} }}
implicitly sets argument 1 to arg1=val1, because the equal sign is inside the nested {{#if:}} call. —Sledged (talk) 18:12, 2 November 2009 (UTC)Reply[reply]
Interesting. So... is there a work-around that you know of? How would I go about programming this? =) --J.nesta 19:59, 3 November 2009 (UTC)Reply[reply]
I haven't been able to think of any workaround that doesn't involve creating another extension. —Sledged (talk) 22:30, 3 November 2009 (UTC)Reply[reply]
Out of curiosity, do you know of an easy way to pass all template parameters to a subtemplate? ;) Thanks for all your help Sledged. --J.nesta 00:30, 4 November 2009 (UTC)Reply[reply]

Code for passing arguments to a subtemplate

Well, even though Sledged hasn't given me a final answer yet, I'll go ahead and post my workaround. It's not pretty, but I did a little searching on google and AFAIK this is the only code I've seen on the internet that will do this. If someone knows a better way to do this, please let me know.

The premise: since MediaWiki won't process either equals signs or pipes, you can't write up a nice little loop to pass a bunch of arguments to a subtemplate. So, I just used strings to pass them (for the noobs reading this, you have to download the StringFunctions extension for this to work).

Note: The code uses {{!}} (a.k.a. a pipe) as a separator between the arguments, but since it is a string, you can use whatever symbol of string of characters that you want.

The top template code:

{{subtemplate|{{#vardefine:i|1}}{{
	#while:
	|{{{Arg{{#var:i}}|}}}
	|{{{Arg{{#var:i}}|}}}{{!}}{{#vardefine:i|{{#expr:{{#var:i}}+1}}}}
}}}}

The subtemplate code:

{{#vardefine:AllArgs|{{{1}}}}}{{#vardefine:i|1}}{{
	#while:
	|{{#ifexpr:{{#len:{{#var:AllArgs}}}}>1|true}}
	|{{
		#vardefine:Arg{{#var:i}}
		|{{
			#sub:{{#var:AllArgs}}
			|0
			|{{#pos:{{#var:AllArgs}}|{{!}}}}
		}}
	}}{{
		#vardefine:AllArgs
		|{{
			#sub:{{#var:AllArgs}}
			|{{
				#expr:{{#pos:{{#var:AllArgs}}|{{!}}}}+1
			}}
		}}
	}}{{#vardefine:i|{{#expr:{{#var:i}}+1}}}}
}}

Now all the args are stored as vars in the subtemplate, i.e. Arg1, Arg2, etc.

Hopefully this helps out someone out there! And hopefully one day someone over at MediaWiki will implement something like a {{Subtemplate|%PASSALL%}} to automatically pass all parameters to a subtemplate so people don't have to hurt their brains figuring this shit out. =p --J.nesta 01:50, 5 November 2009 (UTC)Reply[reply]

Using the VariablesExtension extension seems like a good workaround. You could use it in conjunction with the {{#forargs}} parser function:
{{
  #forargs:
  | name
  | value
  | {{#vardefine: {{#var: name }} | {{#var: value }} }}
}}
And I'm not just suggesting that because I'm trying to enlist more guine— I mean "testing partners" for a still experimental parser function. Honest! —Sledged (talk) 21:56, 15 December 2009 (UTC)Reply[reply]

Strange Server Errors

Hi,

I really like the functionality of this extension, but I've hit a rather strange problem.

After a random amount of time with the extension enabled on my wiki (generally no more than about 10 minutes), I start getting "500 Internal Server Errors" until I disable the Loops extension. Once I've disabled the extension, the wiki works perfectly. If I try to re-enable the extension, I get the error again.

  • OS: Windows XP
  • Server: Apache 2.2
  • Mysql: 5.1.30-community
  • PHP: 5.2.8
  • MW: 1.15.1
  • Installed extensions: StubManager, HNP, SidebarEx, CategoryTree, Cite, CountDown, CreateBox, DynamicPageList(third party), EasyTimeline, ExtensionFunctions, GroupPortal, ImageMap, LabeledSectionTransclusion, MultiBoilerplate, MultiCategorySearch, ParserFunctions, PasswordReset, RenameUser, SimpleCalendar, StringFunctions, SyntaxHighlight_GeSHi, Tabber, TagAsCategory, Variables.

Any ideas?

AerosAtar 20:25, 13 December 2009 (UTC)

I don't have a 1.15.1 environment running, and no else has made any other comments (favorable or otherwise) about the extension on 1.15.1. Try making Loops the only extension enabled and see if you still get the error. If the error stops, try adding each of the other extensions back one-by-one. —Sledged (talk) 21:56, 15 December 2009 (UTC)Reply[reply]
After poking it with a stick for a while, it appears that it was as simple as my Loops.i18n being corrupted. Seems to be working fine now. Thanks. :) AerosAtar 18:02, 19 December 2009 (UTC)

Using #forargs, the "if ( !( $frame instanceof PPTemplateFrame_DOM ) )" check returns false

... and thus the Wiki prints

{{#forargs:arg| key

 | value
 | 

    * = 

}}

instead of

  • 1 = val1
  • 5 = val5
  • ument = value

when implementing the example.

This is on one MediaWiki instance, while it works on another, nearly identical MediaWiki installation (both MW 1.15.1, PHP 5.2.11, Loops 0.3.0 and an identical set of other extensions). I have no idea what could be causing it. --Patrick Nagel 07:05, 15 January 2010 (UTC)Reply[reply]

After describing the issue in the IRC channel, I know more: The problem is caused by the absence of the DOM PHP extension, which can be enabled by putting 'extension=dom.so' in php.ini (if PHP has been installed with that extension, that is - it is contained in the 'php-xml' package on Red Hat based Linux distributions). TimStarling proposed to change the extension code "to say "instanceof PPTemplateFrame_Hash" instead of DOM, since it probably doesn't make any difference, the interface is virtually the same". --Patrick Nagel 08:01, 15 January 2010 (UTC)Reply[reply]
I'll remember to include it with the next update. —Sledged (talk) 15:11, 1 February 2010 (UTC)Reply[reply]

Syntax

Why is it that the syntax for every parserfunction-like extension is {{#functionname:param1|param2|...|paramN}} except for this one where it's {{#functionname:anything you put here does NOTHING!|param1|param2|...|paramN}}? DeFender1031 20:55, 30 January 2010 (UTC)Reply[reply]

Was it intentional to present this question under the same heading as its answer? —Sledged (talk) 15:11, 1 February 2010 (UTC)Reply[reply]
Yes. Totally intentional. In fact, I just posted here because I'm too stupid to actually READ things under the same header as my talk posts... So... yeah, guess I shoulda looked harder... oh well. DeFender1031 17:36, 1 February 2010 (UTC)Reply[reply]

Update for v1.16beta2 - using the new messages mechanism

Running version 1.16beta2, I was getting warnings that messagescache::addMessages was deprecated, and since I didn't want to turn off warnings in my dev environment, I decided to try and fix that. I admit this is my first attempt at hacking an extension, so I might have made a mistake somewhere along the way (I based my work on other extensions, such as ParserFunctions), but it seems to be working just fine. Basically, I removed the custom function for translation of messages, instead using the now standard wfLoadExtensionMessages. I also moved the Magic Words to their own i18n file.

Get it here: User:Drorsnir/Extension:Loops

Sledged, if you're watching this page: thanks for the extension! It works great! As mentioned above, tested and working with v1.16beta2. Would you update the version here to mine, if it seems OK to you? Also, Wouldn't it be a good idea to move it into SVN? Dror Snir 18:44, 13 April 2010 (UTC)Reply[reply]

0 bug in #loop

When you use something like:

{{
  #loop: i
  | 0
  | 3
  | "{{#var:i}}"<br/>
}}

it will output "" for the first loop. For some reason the 0 doesn't end up in the i variable instead a void string. --Danwe 04:45, 13 July 2010 (UTC)Reply[reply]

solved the problem with $wgExtVariables->vardefine( $parser, (string) $varName, (string) $startVal );. You should cast the values to string in case they are a integer 0. Would be saver to do the same thing for the other $wgExtVariables->vardefine() calls in your extension, too. The $varName could be "0" as well. --Danwe 05:08, 13 July 2010 (UTC)Reply[reply]

Can we add this to the MW SVN?

So we can get it i18n'd properly etc?

You can apply for commit access at Commit_access_requests

Reedy 21:07, 31 July 2010 (UTC)Reply[reply]

See my comment above - I created an i18n-ready version already. I haven't actually tested it, but it should work just fine with another language. --Dror Snir 01:14, 25 August 2010 (UTC)Reply[reply]

Making Templates With While...

See this link for more information. 96.42.46.123 20:29, 2 November 2010 (UTC)Reply[reply]

Where Did I Go Wrong?

Another issue, see this link as well. 96.42.46.123 20:30, 2 November 2010 (UTC)Reply[reply]

Accessing indirect parameters (arrays)

I had a hard time figuring out how to work out an indirect access to parameters (simulating arrays) and inserting them into expressions. Finally I managed to do it. The code:

{{ #vardefine: i | 0 }}
{{ #vardefine: total | 0 }}
{{
  #while:
  | {{{ array/{{#var: i }} |}}}
  |<nowiki/>
  * Adding {{#var: i }}-th array value, {{{ array/{{#var: i }} |}}}
  {{
    #vardefine: total
    | {{ #expr: {{ #var: total }} + {{{array/{{#var: i }} }}} }}
  }}
  {{
    #vardefine: i
    | {{ #expr: {{ #var: i }} + 1 }}
  }}
}}
The total is: {{#var: total}}

Adds the values of the (simulated) array:

{{Loopstest
 | array/0 = 1
 | array/1 = 11
 | array/2 = 10
 | array/3 = 20
}}

Kanor 17:14, 19 January 2011 (UTC)Reply[reply]

#while: Create a table

  • Currently building a template that takes in 4 values and contains two #while loops, outer and inner one. Total iteration is 100 (10x10), and the majority of the while statements are doing nothing more than building a table of information.
  • Any suggestions as to what the best approach would be to get this taken care of?
  • Current Implementation:
{|
|-
!Tier
!Grade
!{{{stat}}}
!Upgrade Cost
|-
|base
|g1/1
|{{#var: bstat}}
| 0
<!-- Iterate 10 times to produce the Tiers and the Grades grouped with them -->
{{#while:
  | {{#ifexpr: {{#var: tier}} <= 10 | true | false }}
  | <nowiki/>
|-
|rowspan="10"|{{#var: tier}}
  <!-- Iterate 10 times to produce the various Grades -->
  {{#while:
    | {{#ifexpr: {{#var: grade}} <= 10 | true | false }}
    | <nowiki/>
{{#vardefine: grade | {{#expr: {{#var: grade}} + 1 }} }} }}
{{#vardefine: txtgrade | g{{#var: tier}}/{{#var: grade}} }}
|{{#var: txtgrade}}
|{{#var: bstat }}
|{{#ifeq: {{#var: txtgrade}}|g2/9|{{#expr: {{#var: bstat}} + ( 2 * {{#var:bcost}} ) }}|{{#expr: {{#var:bstat}} + {{#var:bcost}} }} }}
|-
<!-- Increase values that have accrual values -->
{{#vardefine: bstat | {{#expr: {{#var: bstat}} + {{{plus}}} }} }}
{{#vardefine: ttlcost | {{#expr: {{#var:ttlcost}} + {{#var:cost}} }} }}
  }}
{{#vardefine: tier | {{#expr: {{#var: tier}} + 1 }} }}
<!-- Increase bcost by the amount of the next tier -->
{{#vardefine: bcost | {{#expr: {{#var: tier}} * {{{cost}}} }} }}
{{#vardefine: grade | 1 }} <!-- Reset grade back to 1 for next iteration -->
}}
|colspan="3" align="right"|Total:
|{{#var: ttlcost }}
|}

onParserClearState functions not called

The initialization function onParserClearState isn't ever called, and this produces the result that the "Maximum number of loops have been performed" advice is going to be triggered even if it shouldn't. How? imagine you put the while cycle in a template, and this template is included many times in the page. I suppose the function is to be called every time a loop cycle is begun. Where precisely? 85.20.222.11 21:00, 30 March 2011 (UTC)Reply[reply]

The function is using a hook and is called after each page rendering, after importing a page and finishing its rendering and other occasions. --Danwe 18:12, 31 March 2011 (UTC)Reply[reply]
Shouldn't it be called every time a loop (while, etc.) begins? DonPaolo 20:56, 31 March 2011 (UTC)Reply[reply]
I can't see why. When it gets called it will reset the loop counter and this would mean that the loop maximum would be a per loop limit instead of an per page limit as it's supposed to be. --Danwe 11:24, 6 April 2011 (UTC)Reply[reply]


Problem with forargs and fornumargs in MediaWiki 16.1.0

Although the examples for {{#while}} and {{#loop}} work fine on my MediaWiki installation which has Extension:Loops version 0.3.1, I can't get {{#forargs}} or {{#fornumargs}} to work at all. (I know they are listed as experimental, but so is {{#loop}}.)

I have a page Template:Loop Test that has

{{  #forargs: arg  | key  | value  |  * {{#var: key}} = {{#var: value}} }}

and I test with:

{{Loop Test|one|two|three}}

and all I get when it is rendered is:

{{#forargs:arg| key | value | * = }}

-- WilliamsJD 23:28, 18 May 2011 (UTC)Reply[reply]

instead of {{ # try {{#. Somehow this happened to me, that when having spaces or breaks after the first {{ in a template, it just won't work. It's a weird bug because further down in the source I have similar things and there the parser won't bug around... --Danwe 17:13, 2 June 2011 (UTC)Reply[reply]

#fornumargs Bug

I have a test template like so:

{{#fornumargs: key
| value
| [[Category:{{#var: value}}]]
}}

I have a test use case on a test page, like so:

{{Test|1|2}}

It successfully adds the page to categories "1" and "2". However, if I change it to this:

{{Test|1|2
}}

the page is removed from category "2", and I get this on the screen instead:

[[Category:2 ]]

Then if I change it to this:

{{Test|1
|2
}}

the page is removed from category "1", and I get this on the screen instead:

[[Category:1 ]][[Category:2 ]]

Now, if I go back to this:

{{Test|1|2
}}

and I change it to this:

{{Test|1|2=2
}}

it works. Similarly:

{{Test|1
|2
}}

changed to:

{{Test|1=1
|2=2
}}

also works. This:

{{Test|1
|2=2
}}

puts me in category "2," but I get this on the screen:

[[Category:1 ]]

This:

{{Test|1=1
|2
}}

puts me in no categories at all, and I get this on the screen:

[[Category:2 ]]

This:

{{Test
|1|2}}

somehow works entirely. I even compressed my template code to look like this:

{{#fornumargs:key|value|[[Category:{{#var: value}}]]}}

and also attempted it with a <nowiki /> tag, like so:

{{#fornumargs: key
| value
| <nowiki />
[[Category:{{#var: value}}]]
}}

but I get the exact same set of results.

I tried this same set of experiments with a bulleted list, and did not have the same problems.

So, clearly, #fornumargs hates the combination of implicit parameters, newlines, and categories.
--Cogniac 02:42, 31 August 2011 (UTC)Reply[reply]

Yeah, there are a few bugs already like 0 bug in #loop, your bug I guess is because checking whether the argument is a number or not is not sufficient. It only uses php "is_string" and when you have something like "2\n" where "\n" is a new-line, it will interpret as string instead as number. So we should do something like: if( (string)(int)trim( $argNumber ) === trim( $argNumber ) ) to check whether it is a number. And like in the #0 bug mentioned above, we should see that we use the variables extension in a proper way and deliver strings instead of integers or whatever --Danwe 13:25, 2 September 2011 (UTC)Reply[reply]
I think I have fixed this with Loops 0.4 and Variables 2.0. Please update. There were some proplems with loops - variables interaction before. --Danwe 18:07, 16 November 2011 (UTC)Reply[reply]

Bug: Complex while loops malfunction

{{#vardefine: s | A B, C D, E F}}

{{#vardefine: i | 0 }}
{{#vardefine: s2 | {{#explode:{{#var:s}}|,|{{ #var: i}} }} }}
{{#var: s2 }} → {{#rsplit: {{ #var: s2}}|[ ]|-1}}, {{#rsplit:{{#var: s2}}|([ ][^ ]*)$|0}}

{{#vardefine: i | {{#expr: {{#var: i}} + 1 }} }}
{{#vardefine: s2 | {{#explode:{{#var:s}}|,|{{#var: i}} }} }}
{{#var: s2 }} → {{#rsplit: {{ #var: s2 }}|[ ]|-1}}, {{#rsplit:{{#var: s2}}|([ ][^ ]*)$|0}}

{{#vardefine: i | {{#expr: {{#var: i}} + 1 }} }}
{{#vardefine: s2 | {{#explode:{{#var:s}}|,|{{#var: i}} }} }}
{{#var: s2 }} → {{#rsplit: {{#var: s2}}|[ ]|-1}}, {{#rsplit:{{#var: s2}}|([ ][^ ]*)$|0}}

{{#vardefine: i | 0 }}
{{#while:
  |{{#ifexpr: {{#var: i }} >= 0 | true }}
  |{{#vardefine: s2 | {{#explode:{{#var: s}}|,|{{#var: i}} }}}}
   {{#vardefine: i | {{ #expr: {{#var: i}} + 1 }}}}
   {{#ifeq: {{#len:{{#var: s2 }}}} | 0 | {{#vardefine: i | -1}} | {{#var: s2}} → {{#rsplit: {{#var: s2}}|[ ]|-1}}, {{#rsplit:{{#var: s2}}|([ ][^ ]*)$|0}} ;  }}
}}

The while loop should yield the same result as the three statement groups before. However, it doesn't.

The result is (compare http://glossar.hs-augsburg.de/index.php?title=GlossarWiki:Problems&oldid=19050):

A B → B, A (OK)
C D → D, C (OK)
E F → F, E (OK)

A B → B, A ; C D → D, C ; E F → , ; (here F, E is missing; only the comma is displayed)

Complex while loops in combination with RegexFunction functions seem not work very robust. The while loop behaves differently depending on non-destructive(!) commands stated before that loop.
If, e.g., the command {{#rreplace: {{#rreplace: {{#var:s}}|A|a}}|B|b}} is added before the loop, the result of the loop alters:

B → B, A ; C D → , ; E F → , ; (here F, E and D, C are missing)

(compare: http://glossar.hs-augsburg.de/index.php?title=GlossarWiki:Problems&oldid=19046,
http://glossar.hs-augsburg.de/index.php?title=GlossarWiki:Problems&oldid=19047,
http://glossar.hs-augsburg.de/index.php?title=GlossarWiki:Problems&oldid=19048)

Hey, but sometimes it works!
(http://glossar.hs-augsburg.de/index.php?title=GlossarWiki:Problems&oldid=19049,
http://glossar.hs-augsburg.de/index.php?title=Vorlage:Quelle:Buch&oldid=19034)

BTW: In Vorlage:Quelle:Buch the function #regex had to be used instead of #rregexp in order to make the while loop work correctly.

--Kowarschick 11:17, 9 September 2011 (UTC)Reply[reply]

I first tried to simplify your example since it's really hard to get how it works in detail, wikicode readability... So just try to use the statement of the loop properly, this will make things more clear. Then I get:
{{#vardefine: s | A B, C D, E F}}

{{#while:
  | {{
    #vardefineecho: s2 | {{#explode:{{#var: s}}|,|{{#var: i}} }}}}{{
    #vardefine: i | {{ #expr: {{#var: i}} + 1 }}
    }}
  | {{#var:s2}} → {{#regex: {{#var:s2}} | /(.+?)\s+(.+)/ | $2, $1 }};<br/>
}}
using Extension:Regex Fun instead. Anyway, this way it works. I don't know about the odds of #rsplit but that might be the problem. Also, I have tested this with my improved Loops 0.4 which will be released soon (available in trunk) and Variables 2.0 (also released soon). There were a few bugs in earlier versions, regarding spaces. Your problem seems rather odd though, so if you have further problems after updating to Loops 0.4 and Variables 2.0, please report. --Danwe 18:00, 16 November 2011 (UTC)Reply[reply]

Non-working extension

With all the complaints about bugs, I suppose this extension should be considered non-working? Programming logic like this isn't something where bugs that produce inconsistent results can be tolerated, right? Badon 20:24, 16 September 2011 (UTC)Reply[reply]

works perfectly for me - I just fixed a few minor bugs (those I have reported) myself, can't confirm the newest bugs though. This extension should be in SVN, I really want to fix some code but I am not sure whether to make a fork or just fix the code directly here, can't see any way to contact the author. --Danwe 13:46, 18 September 2011 (UTC)Reply[reply]

Will not work with 1.18 - FIXED

Hi. I made a few tests with 1.18.0beta1: Use of $wgMessageCache removed in 1.18!

"PHP Fatal error:  Call to a member function addMessages() on a non-object 
in /*/m/extensions/Loops/Loops.php on line 91"
I'll take care of this soon as well as of some other bug-fixes and add Loops into mw.org svn if nobody else is going to be faster. --Danwe 13:46, 13 November 2011 (UTC)Reply[reply]
Nice work Danwe for the Version 0.4 and it's intergation in SVN. Works fine for me on my dev site using 1.18.0beta1. Thanks :)) . Akarys. --180.183.25.187 05:15, 17 November 2011 (UTC)Reply[reply]
It is working well for me in MediaWiki 1.18, with no bugs found so far. Great work! Badon 05:44, 2 January 2012 (UTC)Reply[reply]
Return to "Loops/Archive" page.