Extension talk:LoopFunctions

From mediawiki.org
Latest comment: 4 months ago by 2002:9030:60D:0:0:0:9030:60D in topic LoopFunctions + Template

can you please provide a foreach function that will take a text, separator, param pattern, output separator, replacement and, will split the text with the first separator, iterate over the resulting array, replacing the param in pattern (so if an item is foo1_bar, and the param pattern is foo$n$_bar, then $n$ is 1), creating another array that is then joined with the output separator?

LoopFunctions + VariablesExtension[edit]

So I'm trying to combine these two extensions with frustrating results. If I have the following:

*{{#vardefine:odd|0}}{{#var:odd}}{{#for: 7 |<nowiki/>
*{{#vardefine:odd|{{#expr: {{#var:odd}} + 1}}}}{{#var:odd}}}}

I get these results:

  • 0
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1

It looks like the #vardefine and #var functions inside the loop are getting resolved before the loop even starts. For the life of me, I can't figure out if it's a problem with the LoopFunctions, the VariablesExtension, or both. —Sledged 22:37, 19 January 2007 (UTC)Reply

I tried to help you out...I guess you can't nest for loops.
 {{#vardefine:odd|0}} 
 {{#var:odd}} 

 {{#for:7 | {{#expr:{{#var:odd}}+1}}
   {{#for:1 | {{#vardefine:odd|{{#expr:{{#var:odd}}+1}}
   }}
 }}
Mcpaige 04:47, 28 February 2007 (UTC)Reply
Fortunately, I've found a solution. —Sledged (talk) 04:14, 25 September 2007 (UTC)Reply

LoopFunctions + Template[edit]

This might not be the best way to do this, but here goes.

On page Table I put {{Template:TheInfoList | n=10}}, the template TheInfoList looks like this {{ #for: {{{n}}} | {{TheInfo$n$}} <br/>}}. There are a bunch of templates named TheInfo1, TheInfo2, TheInfo3, TheInfo... which actually in turn look like this {{Template:TheInfo | parm1=value1 | parm2=value2 | ...}} and of course TheInfo handles all the html and formating and stuff. The loop does not handle templates, what I get on page Table is

Template:TheInfo1
Template:TheInfo2
Template:TheInfo3
Template:TheInfo4
...

with links, the templates are not being included. Any ideas ? Mcpaige 04:26, 28 February 2007 (UTC)Reply

This is along the lines of the mentioned limitation. I expanded that section.--Patrick 12:09, 28 February 2007 (UTC)Reply
这样做很奇怪,不符合预期效果,应该嵌入模版内容。否则直接写 {{ #for: {{{n}}} | {{:TheInfo$n$}} <br/>}} 就可以了啊--2002:9030:60D:0:0:0:9030:60D 11:43, 31 October 2023 (UTC)Reply

Hooks (aka "Custom HTML Tags")[edit]

They don't seem to work inside a looping function.

I have Extension:Colorize installed and when I do this:
NS:Somepage

{{foo | rep1 = 1 | rep2 = 2 }}

Template:foo

{{#foreach: rep$n$ | {{{rep$n$}}} <colorize>{{{rep$n$}}}</colorize><br />}}

I get this:

1 {{{rep$n$}}}
2 {{{rep$n$}}}

As you can see, the "rep$n$" ouside of the "colorize" hook gets replaces as expected, but not the one inside.

Edit: It doesn't matter if I "include" Extension:Colorize before or after Extension:LoopFunctions in LocalSettings.php

Egingell 03:42, 27 April 2007 (UTC)Reply

Version Special Page disappears[edit]

Im using Mediawiki 1.10, and as long as the LoopFunctions extension is installed, my Version-Page is just empty. Any idea? --Die Backe, 91.3.112.102 12:38, 29 May 2007 (UTC)Reply

This is caused by the special u with double dot character in the author's name. Edit LoopFunctions.php and change this character in the version array construct to a simple 'u'. Sorry author. 60.240.113.86 14:23, 1 July 2007 (UTC)Reply

Notice: Undefined variable[edit]

Any idea why I'm getting this notice?

Notice: Undefined variable: return in /foo/wiki/extensions/LoopFunctions/LoopFunctions.php on line 67
  1. How do you disable the Notice messages?
  2. How do you define a "local" variable such as the $return variable in question?

--D0li0 01:27, 12 October 2007 (UTC)Reply

Someone needs to add the following line to the public function foreachHook in the LoopFunctions.php to prevent the error above due to the uninitialized variable $return. This is not unlike the function above foreachHook, but needs to be done again as the variable is not a global.
       $return = '';
And out of curiosity, it is wise to use a variable named return? Why not output, myfooness or something that is not a reserved word? --D0li0 21:45, 12 October 2007 (UTC)Reply
Weird. My wiki had been working fine, and then without changes or reboot, I got this problem in conjunction with the AWC Forum extension. Changing LoopFunctions.php as above fixed the problem. 60.240.122.215 23:28, 10 July 2008 (UTC)Reply

Pass index value as parameter to template[edit]

Trying to create something like:

{{#for: 10 | {{A Template|param=$n$}} }}

...but it doesn't work. Suspect the pipe character '|', which is used to provide the parameter to the template, is confusing the for loop and it's trying to use 'param=$n$' as a replacement parameter.

Is this understaning correct, and if so, can anybody recommend an alternative to achieve the above? Cocjh1 12:49, 21 November 2007 (UTC)Reply

It's the way MediaWiki (or rather when) parses templates, variables, and parser functions. The parameter are parsed before they're passed to the underlying parser function. So you example is the same as:
{{A Template|param=$n$}}{{A Template|param=$n$}}{{A Template|param=$n$}}{{A Template|param=$n$}}{{A Template|param=$n$}}{{A Template|param=$n$}}{{A Template|param=$n$}}{{A Template|param=$n$}}{{A Template|param=$n$}}{{A Template|param=$n$}}
with the exception that after the template is expanded, any instance of '$n$' in the resulting text is replaced by number of that iteration.
Just what, exactly, are you expecting? And what you instead getting? —Sledged (talk) 20:59, 21 November 2007 (UTC)Reply
Thanks for the response Sledged, but sorry, it's my bad. The loop works just fine and it's my testing that was at fault. Have switched over to use the Extension:Control Structure Functions now though, so may just stick with them.
Thanks anyway Cocjh1 13:01, 22 November 2007 (UTC)Reply
You're welcome. Glad you found a solution. Excellent extension choice. —Sledged (talk) 22:04, 26 November 2007 (UTC)Reply

Is it MW 1.12 friendly?[edit]

I was very happy with this extension, until I upgraded MW to 1.12. It doesn't work anymore. Has anybody experienced any problems at all with upgrades? --Cartoro 10:47, 10 April 2008 (UTC)Reply

Yes, I experience problems since my 1.12 upgrade. For the #foreach function, the error messages look like :
Warning: current() [function.current]: Passed variable is not an array or object in /path/to/mediawiki/extensions/LoopFunctions/LoopFunctions.php on line 56
Warning: array_key_exists() [function.array-key-exists]: The second argument should be either an array or an object in /path/to/mediawiki/extensions/LoopFunctions/LoopFunctions.php on line 61
It is due to the type of $parser->mArgStack, which is not an array.
--Xiloynaha 11:37, 13 August 2008 (UTC)Reply
I just adapted LoopFunctions to MW 1.12, see the extension page. It is not fully tested, but it seems to work… --Xiloynaha 14:47, 13 August 2008 (UTC)Reply

Is it MW 1.13 friendly?[edit]

I need this extension under MW 1.13alpha. I get these errors :

Warning: current() [function.current]: Passed variable is not an array or object in ...../LoopFunctions/LoopFunctions.php on line 59
Warning: array_key_exists() [function.array-key-exists]: The second argument should be either an array or an object in ...../LoopFunctions/LoopFunctions.php on line 64

I upgraded MW from 1.10 where everything was working fine. Anyone any ideas ? --217.6.3.114 16:11, 22 July 2008 (UTC)Reply

Maybe you should try my patch (version 1.0.4), since I got the same message than you with MW 1.12, maybe that will work for MW 1.13 too. --Xiloynaha 14:47, 13 August 2008 (UTC)Reply
I downloaded and installed your patched files, but still get the following notice and no result is returned. Any ideas what's still wrong?
Notice: Undefined variable: return in .../LoopFunctions/LoopFunctions.php on line 96

--217.6.3.114 13:01, 19 August 2008 (UTC)Reply

I fixed the Notice by adding a local variable 'return' to the foreach-Function, but still get no result when using the function. Is foreach working for you? --Jailing 09:15, 16 September 2008 (UTC)Reply
As said in my talk page, it seems that you have to name the parameters when you call a template with foreach. I'll try to fix that. See the discussion with Jailing for details. --Xiloynaha 07:28, 19 September 2008 (UTC)Reply

Fixed (see my talkpage). Please let me now if you experience any more problem, I'll try to fix when I'll have time for (that's the most difficult !). --Xiloynaha 19:28, 23 October 2008 (UTC)Reply

Thanks again for your time Xiloynaha. The extension now works like a charm! One more thing: I had to increase the maximum number of loops to 500 for a rather long dynamic list in our wiki (where the loop function is called several times) to display as wanted. You wouldn't think this might lead to problems, would you? --Jailing 16:29, 17 December 2008 (UTC)Reply
500 is quite high, but I don't think it may cause problems other than the server load. But I am not qualified to guarantee anything ;-)
I suppose you modified the $mMaxLoopCount variable in the extension code? Or it is possible to declare it into the LocalSettings.php file? If not, it could be a good feature to add…
--Xiloynaha 21:47, 17 December 2008 (UTC)Reply

Does NOT work with MW 1.15[edit]

I am running MW 1.15.1. Installing this extension takes down my whole wiki. When I comment out the line in LocalSettings.php that calls this extension, everything works. When I uncomment it, my wiki pages are all blank. --Deutne 11:44, 2 June 2010 (UTC)Reply

Hi! I run several MW 1.15.1 with LoopFunctions, and it works fine for me, so I'm not sure I'll be able to help you… What is your configuration? Do you have something in your web server logs? What extra extensions do you have? How do you call LoopFunctions in your LocalSettings.php? --Xiloynaha 12:01, 2 June 2010 (UTC)Reply
My configuration: OS Ubuntu 10.04; MediaWiki 1.15.1; PHP 5.3.2-1ubuntu4.1 (apache2handler); MySQL 5.1.41-3ubuntu12. The relevant line in my LocalSettings.php file is: require_once ( "$IP/extensions/LoopFunctions/LoopFunctions.php" ); The problem occurred when my only installed extension was the LoopFunctions. I also tested LoopFunctions after adding Parser Functions (Version 1.1.1), Variables (Version 1.2.2), and ArrayExtension (Version 1.2.2). I continue to get blank pages. I don't have an error message to convey yet, but I can try turning on more error reporting via my php.ini file. --Deutne 22:39, 2 June 2010 (UTC)Reply
Here is the error message: "Warning: Parameter 1 to Language::getMagic() expected to be a reference, value given in /usr/share/mediawiki/includes/StubObject.php on line 58"--Deutne 22:47, 2 June 2010 (UTC)Reply
I think the warning message is unrelated to the LoopFunctions problem: [1], but until I fixed that problem, I didn't get a useful error message about the problem with LoopFunctions. It seems to me that the problem with LoopFunctions was that LocalSettings.php was looking in the LoopFunctions directory, but the tar file extracted to a LoopFunctions-1.0.5 directory. I changed the name of the directory and am hoping things will work now. --Deutne 09:30, 3 June 2010 (UTC)Reply

Issues with 1.16.1[edit]

The basic for loop functionality works, but when it comes to passing template arguments, it doesn't replace it. The examples that demonstrate how to call the foo template does not seem to work. Is this a known issue? Any work-arounds? --XTsukihime 15:54, 1 March 2011 (UTC)Reply

Second that. I tried to do something like #Pass index value as parameter to template but it doesn't work. Extensions:Control Structure Functions "redirect" people instead to Extension:Loops, while its still experimental. I'm stuck. Bennylin 09:04, 25 April 2011 (UTC)Reply
This is weird because LoopFunctions version 1.0.5 worked for me with MediaWiki 1.16.5. Anyway, Version 1.0.6 works with MW 1.16 and above so you can try it (in the unlikely case you still use it!). --Xiloynaha (talk) 00:07, 6 June 2014 (UTC)Reply

This extension is not working with Mediawiki > 1.17[edit]

The procedure

$wgMessageCache->addMessages( $value, $key );

is depracted.

See Manual:$wgMessageCache

Oops, I had fixed the code back in 2012, but apparently I completely forgot to publish the modification here. I just published the new code (working with MW 1.23 and previous versions as well) in an official Git repo and updated the extension's page. --Xiloynaha (talk) 00:01, 6 June 2014 (UTC)Reply

Broken in 1.30[edit]

Last version I used the extension with was 1.26, so sometime between then and now, this extension broke. It now gives a lovely Parser.php error...

Strict Standards: Non-static method ExtLoopFunctions::foreachHook() should not be called statically in /var/www/html/wiki/includes/parser/Parser.php on line 4805

--140.192.242.150 20:55, 9 April 2018 (UTC)Reply