Help talk:Extension:ParserFunctions

Jump to navigation Jump to search

About this board

DavidL (talkcontribs)

The #sub function doesn't work on sites where ParserFunctions is enabled. --  David L  discuter ► 19:02, 23 March 2021 (UTC)

MarMi wiki (talkcontribs)

Only #sub? And on what sites? #sub is from the Extension:StringFunctions.

Edit: Oh, it was integrated into Extension:ParserFunctions. Does those sites have it enabled in config ($wgPFEnableStringFunctions; if other string functions works, then they have)?

DavidL (talkcontribs)
MarMi wiki (talkcontribs)

Integrated functions from StringFunctions needs to be enabled in config in order to work, read the extension pages. I'm guessing that after enabling, they should also appear on the parser functions list (at the bottom of the Special:Version).

Edit: They indeed appear on that function list: https://donate.wikimedia.org/wiki/Special:Version, where that extension is enabled by default (Per-wiki settings for Wikimedia wikis from https://meta.wikimedia.org/wiki/Requesting_wiki_configuration_changes#See_also, search for wmgPFEnableStringFunctions).

Since on your wikis there is no "sub" function, then most likely that extension isn't enabled. And judging by https://phabricator.wikimedia.org/T254782, it most likely wont be enabled.

Dinoguy1000 (talkcontribs)

The mentioned wikis are both WMF wikis, and StringFunctions will never be enabled on WMF wikis. The recommended alternative is Lua via Scribunto.

Pppery (talkcontribs)
DavidL (talkcontribs)

If the module is not enabled, it should not appear in Special:Version, or at least there should be a column indicating the module status (enabled or disabled). I found another solution to use in my template, copied from en.wikipedia.org. Thanks for replies.

Dinoguy1000 (talkcontribs)

ParserFunctions is enabled on these wikis (you'll note that the functions #if and #expr, among others, work just fine); however, the StringFunctions components of the extension (including #sub) are behind a configuration flag, which is not enabled on WMF wikis.

Reply to "#sub doesn't work"

explode, limit parameter, ... ???

4
Summary by MvGulik

Explains and shows how explode works.

MvGulik (talkcontribs)

Doc: "{{#explode:string|delimiter|position|limit}}"

OK. Seems simple enough.

Doc: "The limit parameter is available in ParserFunctions only, not the standalone StringFunctions version,"

OK, checked.
So far so good.

Doc: "..., and allows you to limit the number of parts returned, ..."

Perfect.
That's what I'm looking for.

Doc: "..., with all remaining text included in the final part."

??? Come again.
If its "including all remaining text" in the "final result". What's the point of that limiter value?

O well, lets just see how it actually works.

  • explode: "{{#explode:aaa bbb ccc ddd eee fff| |2|2}}"
  • Looking for: "ccc ddd eee"
  • Returns: "" (ie: empty string)

Wtf ???

MediaWiki: 1.31.0
ParserFunctions: 1.6.0

Clump (talkcontribs)

I interpret the limit parameter to be a count, while the position is an index. So if you say divide it into at most 2 pieces and then ask for the 3rd piece (index 2) then you get an empty string. Try {{#explode:aaa bbb ccc ddd eee fff| |2|3}} instead.

Dinoguy1000 (talkcontribs)

If you have a wiki with string functions enabled and Extension:Arrays handy, try the following code:

{{ #arraydefine: pos | 0,1,2,3,4,5,6,7 }}
{{ #arraydefine: lim | 0,1,2,3,4,5,6,7 }}
<table class="wikitable">
  <tr>
    <th>Pos →<br />Lim ↓</th>
{{ #arrayprint: pos || $pos | <th>$pos</th> }}
  </tr>
{{ #arrayprint: lim || $lim | <nowiki/>
  <tr>
    <th>$lim</th>
{{ #arrayprint: pos || $pos | <td>"{{ #explode: aaa bbb ccc ddd eee fff || $pos | $lim }}"</td> }}
  </tr>
}}
</table>

It results in the following table (provided for those who don't have access to Ext:Arrays):

Pos
Lim
01234567
0 "aaa""bbb""ccc""ddd""eee""fff"""""
1 "aaa bbb ccc ddd eee fff"""""""""""""""
2 "aaa""bbb ccc ddd eee fff"""""""""""""
3 "aaa""bbb""ccc ddd eee fff"""""""""""
4 "aaa""bbb""ccc""ddd eee fff"""""""""
5 "aaa""bbb""ccc""ddd""eee fff"""""""
6 "aaa""bbb""ccc""ddd""eee""fff"""""
7 "aaa""bbb""ccc""ddd""eee""fff"""""

Based on this, I don't really understand what the limit parameter is doing, but it doesn't strike me as particularly useful (and it definitely doesn't behave as I'd expect from documentation that says it "allows you to limit the number of parts returned").

MvGulik (talkcontribs)

Thanks (both).

I see it now. (also vaguely remembering having run into this a long time ago)
The limiter part is uses to control the initial splitting of the source string.
(that also means a direct "ccc ddd eee" output in this example case is not possible, ... with one single #explode that is.)

The doc page definitely seems in need of some adjusting on this. Although it is giving some hints towards how it actual works, its not very clear or none-ambiguous on this part in my view.

Definitely going to look into requesting the Extension:Arrays to be added to the wiki I'm running around in. (very nice table btw)

#switch doesn't work with #while

4
Gunnar.offel (talkcontribs)

i tried to use a #while and deside by #switch how the reply would be.

in meta-code

     count:=3
     i:=0
     {{#while:
       | i < count
       | i++
         {{#switch:
          | 1 = 
          | count = 
          | #default = 
         }}
       }}   

So, not work isn't exactly right, it works for the first run, all other runs are empty. i solved it now by several #ifeq - compares. Is it a wanted behavior?

Dinoguy1000 (talkcontribs)

#while is not added by Extension:ParserFunctions; I don't know what extension adds it. Looking at your code, and knowing how while loops operate in general, though, I wonder if you didn't forget a pipe before the #switch:

     count:=3
     i:=0
     {{#while:
       | i < count
       | i++
       | {{#switch:
          | 1 = 
          | count = 
          | #default = 
         }}
       }} 
Gunnar.offel (talkcontribs)

The code was just to illustrate in which way i wanted do use.

No, the third pipe isn't correct, the #while has just a head and the instructions. i set the increment as first to get sure, that the loop terminates correctly. #while comes with the extension loops. i posted it here, since it works as said with #ifeq-compares.

I just wanted to get more information how it works. i could swear if i divide it in two pages it would works flawless (calling the secound page like a function). To look more in the philosophy may it be, that the #switch is set late in the page generation, so the loop couldn't generate properly?

Dinoguy1000 (talkcontribs)

I don't know any of the specifics of how/when #switch parsing happens, and I don't have any experience at all with the #while parser function (as you might have guessed when I incorrectly suggested you were missing a pipe in it). At this point, all I can offer is to experiment until you find something that works, and maybe file a bug report for constructions that you think should work, but don't currently (you would do this wherever the #while extension's issues are tracked, and if necessary mark your report(s) as belonging to that extension). I can at least say that it's unlikely any changes will be made to #switch or the other Ext:ParserFunctions functions to make them play nicely with #while, unless the cause of the issues is an actual bug in them that needs to be fixed for other reasons (but if you believe the cause is a bug in them, I'd encourage you to file a bug report anyways, especially if you can come up with a demonstration that only uses extensions enabled on WMF wikis).

Reply to "#switch doesn't work with #while"

Any objection to removing section StringFunctions?

4
Mathglot (talkcontribs)

According to this discussion at w:WP:VPT and phab:T8455, String functions were never enabled. Any objection if I delete this section? For the benefit of users coming here from various sister projects, we could replace it with a bullet list of links, for example, starting with:

and so on. Mathglot (talk) 22:06, 2 February 2021 (UTC)

Mathglot (talkcontribs)

Given User:The Earwig's response at WP:VPT, I won't delete this section. However, I may add a caveat near the top for the benefit of visitors from other Wikimedia projects, and a bullet list of links, as I suggested. I wasted a lot of time trying to figure out what was wrong, with nary a hint from this page.

Dinoguy1000 (talkcontribs)

Definitely don't simply remove the documentation of these functions; they are used on third-party wikis. A good solution here would instead be something like Extension:Variables, where a visually-obvious notice gives a summary of the WMF view of the functionality, as well as a quick rundown (or a link to a list) of alternatives.

Gunnar.offel (talkcontribs)

Since Wikipedia is not the only MediaWiki in the world .. no, you cant't just delete?

Reply to "Any objection to removing section StringFunctions?"
Summary by Tacsipacsi

Substitution works if—and only if—there are no unsubstituted variables in the input.

Terasail (talkcontribs)

Is it possible to substitute #time? Because it does not work when "subst:" is used. Thanks

Andrybak (talkcontribs)

For example: {{subst:#time:Y-m-d|now}} produces: 2020-12-08.

Terasail (talkcontribs)

Ah thanks, I was't using "now" and that was not allowing it to be subst.

Tacsipacsi (talkcontribs)

It should work without now as well: {{subst:#time:Y-m-d}} produces 2020-12-11.

Terasail (talkcontribs)

Yeah I was using {{CURRENTMONTHNAME}} instead of "now" and it just gives an error message when you use subst:

Tacsipacsi (talkcontribs)

It doesn’t make sense to subst: a parser function while keeping its input varying—what wikicode would you expect to get? If you subst: the input as well ({{subst:#time:Y-m-d|{{subst:CURRENTMONTHNAME}}}}), it works: 2020-12-11.

Terasail (talkcontribs)

Yeah well I just wasn't thinking much about what I was doing when I first asked the question... I should have been a bit less lazy and thought more about what I was trying to do but it is what it is.

31.147.156.166 (talkcontribs)

I've done a lot, but I can't seem to figure out why can't we just use


{{#explode:{{PAGENAME}}|:}}


It just doesn't work. On the other hand, writing the actual words (not substituting them):


{{#explode:Extension:ParserFunctions|:}}


will work. Why is that and what can be done?

Dinoguy1000 (talkcontribs)

{{PAGENAME}} only returns the name of the current page, without the namespace. For example, (on this wiki) for the page Extension:ParserFunctions, it returns ParserFunctions. Therefore, {{ #explode: {{PAGENAME}} | : }} will only return something different from {{PAGENAME}} if the page name itself contains a colon. If you want to get the namespace of a page, you should instead use {{NAMESPACE}}: for Extension:ParserFunctions (again, on this wiki), it returns Extension. Alternatively, you could use {{FULLPAGENAME}}, which will return the namespace and page name together, but there's no point in doing that just to #explode it to get the namespace.

31.147.156.166 (talkcontribs)

I was thinking without the namespace. It's more about removing extra characters from the title, like getting "car" from "John's car" if the page is named "John's car".

Dinoguy1000 (talkcontribs)

For that specific case, you can just do {{ #explode: {{PAGENAME}} || 1 }}: #explode will operate on spaces if you omit or leave blank the second parameter.

If this still doesn't help for what you're trying to do, please post an actual use case from your wiki and I'll see if I can give you better/more tailored advice.

Reply to "Explode PAGENAME"

would like to use #ifexpr: with #len: and #var:

2
Summary by Dinoguy1000

See Topic:Vwb9ehqnzzbsu32q for a continuation of this discussion.

Gunnar.offel (talkcontribs)

I would like to use a expression to check a length.

{{#ifexpr:{{#len:{{#var:refs|}} }} > 10 | [..]

That provides no exeption, as far as good, but the

{{#len:{{#var:refs|}} }}

provides always 0 so the check doesn't work. Any Idea how to solve the issue or change the behavior?

Gunnar.offel (talkcontribs)
Arlo Barnes (talkcontribs)

Although not stated explicitly, I presume that when calculating at a second level of precision the difference between two dates where a leap second has occurred between them, the leap affects the final count. I was wondering if there is a switch to disregard this and calculate as if the leap second had not happened. This would be handy for math relating to the GPS time system (for instance), which does not observe leap seconds.

Reply to "Leap seconds"

How to output html in an #if function?

2
Lwangaman (talkcontribs)

Is there any way to output HTML in the yes / no sections of an {{#if| parser function? In my case I can't use {{tag| because I need to put a closing span tag </span> followed by an opening span tag <span> (with a specific class). Writing the HTML directly doesn't work, and writing as html entities doesn't work...

>> Update: I didn't realize that {{tag| allows you to specify the type parameter, to determine whether it's an opening tag, or closing tag, or self-closing tag, or opening and closing tag.

>> Update 2: I am quite mixed up. I see that {{tag| is a way of outputting a tag for documentation purposes, so that the tag won't be interpreted literally. How do I actually output a real tag that will be interpreted?

>> Update 3: I finally accomplished what I needed by extending a lua module. The specific case was Template:Quote which I imported into my wiki, I wanted to wrap the author in a <span> with a specific class. I saw that the value for author was being output from within a {{Comma separated list}} template which was directly calling Module:Separated entries. So I added a new parameter "span" to the {{Quote}} template which can be set to true, in which case the lua module separated entries will wrap each element of the list with a span tag. I saw that in lua I can either output an html string directly '<span>' .. doListStuff .. '</span>' or use mw.text.tag('span', {}, CONTENTS). In any case I did succeed, however it seems to me that it is much easier to output html from a lua module than it is from within Template transclusions or template parser functions.

Dinoguy1000 (talkcontribs)

It would help to know what you're actually trying to do here. It may be that, whatever you're trying to achieve, you haven't thought of the best way to go about it yet.

Reply to "How to output html in an #if function?"

How to update parsed datas?

3
Dnsbl1 (talkcontribs)

I installed the Comments and VoteNY extension, retrieving number of comments and votes from a page on another page.

But it doesn't seem to update when comments or votes are added with this

{{NUMBEROFVOTESPAGE:page name}}

{{NUMBEROFCOMMENTSPAGE:page name}}

I tried to put $wgParserCacheType = CACHE_NONE;

but it doesn't seem to change much. The number of votes and comments stay.

Any idea?

Dinoguy1000 (talkcontribs)

Completely disabling caching is generally a bad idea. Do the numbers update if you force the page's cache to be cleared (e.g. by editing the page, or by using action=purge)? If so, then you should add a link to purge the page near where the numbers are displayed, with text like "update these counts" (the code [{{fullurl:{{FULLPAGENAME}}|action=purge}} update these counts] can be used as a base for this).

Dnsbl1 (talkcontribs)

I switched to using cache cause the extensions use it to retrieve datas. I tried after a few hours, ?action=purge update the page correctly and the correct number of comments are displayed, thanks for the idea of adding a link on the page.

Reply to "How to update parsed datas?"