Help talk:Extension:ParserFunctions/2011

Latest comment: 12 years ago by Kiril Simeonovski in topic Multiple #if

#ifexist problem

I'm creating a template to redirect red links to wikipedia, here's the content of the template :

{{#ifexist:{{{1}}}|[[{{{1}}}]]|[[w:en:{{{1}}}|{{{1}}}]]}}

theorically if {{{1}}} exists output [[{{{1}}}]], if doesn't exist output [[w:en:{{{1}}}|{{{1}}}]], function seems right isn't it?

practically, it works with one word, for instance it works with

{{w|Example}}

, w being the name of the template but when it comes with spaced words such as

{{w|Test Page}}

, output is Page Test Page and link is w:en:Test

why is spacing causing issues? I don't get it, if anyone can help me, thanks.

Use {{fullurl:{{{1}}}}} rather than hardcoding the enwiki domain. The actual problem you're experiencing is that the division between the link target and the link text is placed at the first space in the expanded string, so you need to replace spaces with underscores in the first parameter; fullurl will do that for you, and also make the template portable to other wikis. Happymelon 16:20, 28 August 2011 (UTC)Reply
No, it doesn't seem right. External links must not contain spaces. Try using {{urlencode:{{{1}}}|WIKI}} as part of the url.
{{#ifexist:{{{1}}}|[[{{{1}}}]]|[http://en.wikipedia.org/wiki/{{urlencode:{{{1}}}|WIKI}} {{{1}}}]}}

Gustronico 20:08, 28 August 2011 (UTC)Reply

Fullurl didn't make it. So I tried the solution of Gustronico, the title works but the link doesn't, the space is replaced by a plus sign. It gives w:en:Test+Page

Edit : anchorencode works, but it won't work with special characters while urlencode can! If there's any solution it would be great.

You can use interwiki linking. I think it is standard to have them to the English Wikipedia, otherwise see Manual:Guide to setting up interwiki linking. On this site: w:Test page or Wikipedia:Test page. If for some reason you want external link style: {{fullurl:w:Test page}} gives https://en.wikipedia.org/wiki/Test_page .--Patrick1 12:06, 3 September 2011 (UTC)Reply

Did you put the |WIKI parameter in urlencode? It would return underscores for spaces:

  • {{urlencode:Test Page}} → Test+Page
  • {{urlencode:Test Page|WIKI}} → Test_Page
  • {{urlencode:Test Page|QUERY}} → Test+Page
  • {{urlencode:Test Page|PATH}} → Test%20Page

Gustronico 20:38, 3 September 2011 (UTC)Reply

Logical OR failing?? (Nope, I just fail.)

Hello. This expression should return "EXPANSION IS TRUE", but returns "EXPANSION IS FALSE":

  • BOOLEAN ALGEBRA: {{#ifeq: {{#ifeq: 0 | 1 | 0}} OR {{#ifeq: 1 | 1 | 0}} OR {{#ifeq: 0 | 1 | 0}} | 1 | EXPANSION IS TRUE | EXPANSION IS FALSE }}:
    BOOLEAN ALGEBRA: EXPANSION IS FALSE;

Likewise, the below code should return TRUE [(1 || 0 || 0 = 1); should be TRUE] but returns FALSE:

  • SIMPLE BOOLEAN ALGEBRA: {{#ifeq: 1 OR 0 OR 0 | 1 | TRUE | FALSE }}:
    SIMPLE BOOLEAN ALGEBRA: FALSE

I'm running MediaWiki 1.16.3 with ParserFunctions (Version 1.4.0). Help! 17:39, 4 August 2011 (UTC)

I think you will need to use #ifexpr, as in {{#ifexpr: 1 OR 0 OR 0 | TRUE | FALSE }}, which returns:
TRUE
Analogously, {{#ifexpr: {{#ifeq: 0 | 1 | 1 | 0}} OR {{#ifeq: 1 | 1 | 1 | 0}} OR {{#ifeq: 0 | 1 | 1 | 0}} | TRUE | FALSE }} returns
TRUE
Helder 18:12, 4 August 2011 (UTC)
Erm. You're quite right here. How embarrassing for me; I've been looking at this for too long and completely missed this. Sorry about that, and thanks for the help! 18:34, 4 August 2011 (UTC)

#time

Why is it that the time function y only works for years before 2000? For example, {{#time: y | 2002 }} yields 08 (currently, when the year changes to 2009 presumably it will yield 09 at that point) when it should yield 02. This is very frustrating when trying to construct things utilizing dates with 2 digits. It doesn't seem to matter for 1999 and before, though. Examples:
{{#time: y | 2020 }} yields 20
{{#time: y | 2002 }} yields 02
{{#time: y | 1998 }} yields 98
{{#time: y | 1965 }} yields 65
Thanks for any help that anyone may be able to provide. I had no idea where to post this question, but hopefully this is the right spot. 24.236.101.233

Also, I edit mainly on wikipedia (sorry for not having a username here). If/when this is fixed, would it automatically update on wikipedia or not? Sorry, I didn't know how that worked. Thanks again for any info anyone might be able to provide. 24.236.101.233 09:52, 30 December 2008 (UTC)Reply
The problem is that if you use {{#time: j F Y H:i | 2003 }}, you get 16 November 2003 00:00, like for {{#time: j F Y H:i | 1923 }} (16 November 1923 00:00). iAlex 12:56, 30 December 2008 (UTC)Reply
You just need to make the date look more like a date. A 4 digit number staring with a 2 isn't enough. Try: {{#time: y | {{#time: F }} 2002 }} or {{#time: y | December 2002 }}. See strtotime. Splarka 21:35, 30 December 2008 (UTC)Reply
It is possible with some minor limitations to use #time for specifying dates from -6999BCE to 6999CE. It requires some template hackery to bypass normal limitations of #time, but it is possible to do. One limitation is that there is no way of recognizing the precision the editor intended for certain dates and times. EG: if seconds were specified as 00, there is no way (that I have come up with yet) to tell if the user simply omitted the seconds because they didn't measure with that precision. For example of the lengths I had to go to in order to get this functionality, see code at {{:en:Template:start-date}}. (Note- you won't be able to see the results of the calculations from preview- the easiest way to see the UTC value emitted is to turn off CSS, eg, Opera View.Style.User mode. Look for the UTZ value in parens following the visible date-time.) -J JMesserly 20:05, 17 February 2009 (UTC)Reply

I am wondering why the format code "z" delivers the day of the year starting with 0, deviating from the ISO-8601 standard, which counts from 1 to 365 (or 366). Someone recently used this code in the en:ISO-8601 article, giving wrong results. Question: was the definition here intentionally deviating? If so, how can the correct ISO ordinal date be displayed; if not please revise the magic word. −Woodstone 07:33, 11 June 2010 (UTC)Reply

Uses

Each section would benefit from a short blurb on what the parser function can accomplish.

I retained this, one of many uses for switch:

#switch allows an editor to add information in one template and this information will be visible in several other templates which all have different formatting. Ref: Embedded template, Wikipedia:Village Pump (technical), (24 June 2010).

Adamtheclown 15:17, 2 July 2010 (UTC)Reply


Adam, thanks for this. Answering your points in turn:
  • Personally, I find the use of "foo", "bar", "baz" and "quok" rather distracting. My brain reads "foo" and "bar" and proceeds to shuts off, requiring a reset. But they seem to be deeeply ingrained in the minds of programmers, coming from early C times, possibly earlier. The /pt version of the PD help uses examples like:
{{#switch: 1 | 1 = one | 2 = two | other }} → one
{{#switch: 2 | 1 = one | 2 = two | other }} → two
{{#switch: 3 | 1 = one | 2 = two | other }} → other
Don't know how you feel about those, but to me they're as simple as one can get, while fully illustrating what #switch does without distracting the user with anything else.
  • I apologise for failing to qualify "bottom". I meant the end of the #switch section. If you go there, you'll notice a note that already directs the reader to two real life examples, the first meant to be simple and the second more complex. How do you feel about those?
  • This is an interesting challenge, which I cannot raise up to for lack of availability. But the challenge rests on you. I can, however, provide a couple of suggestions.
  1. Being real, they should point to real implementations, as is done in the two just mentioned.
  2. Because they may change in future, we could use permanent links. Building real life examples in this page will cause clutter and is prone to natural errors (your examples contained some syntax innacuracies such as {{{{PAGENAME|out=type}}, which would be avoided).
  3. Because this is an opportunity for plugs, we could restrict them to wikimedia wikis. Perhaps just to mediawiki.org, since we already have template {{Mediawiki }} to connect here from the PD help pages.
  4. We could perhaps not just point users to them (as is done in the section), but also explain what #switch is being used for, in them.
  5. There may be even better or simpler examples out there. The challenge is in finding them.
  6. Rather than a note, we could perhaps agree on a bottom section for every function with up to 4-5 examples, or something like that.
Hamilton Abreu 13:27, 4 July 2010 (UTC)Reply
really excellent points! Errectstapler 21:38, 13 May 2011 (UTC)Reply

more examples and slight rewrite

Added and clarified with the following:

This parser function compares two strings and determines whether they are identical.

{{#ifeq: string 1 | string 2 | value if identical | value if different }}
text

The comparison is made as text; this comparison is case sensitive:

{{#ifeq: foo | bar | yes | no}} → Is foo and bar the same text? Answer: no
{{#ifeq: foo | Foo | yes | no}} → Is foo and Foo the same text? Answer: no
{{#ifeq: "01" | "1" | yes | no}} → Is "01" and "1" the same text? Answer: no (compare to same example below, without quotes)
{{#ifeq: 10^3 | 1000 | yes | no}} → Is 10^3 and 1000 the same text? Answer: no (compare to same example below)
number

If both strings are valid numerical values, the strings are compared numerically:

{{#ifeq: 01 | 1 | yes | no}}yes
{{#ifeq: 0 | -0 | yes | no}}yes
{{#ifeq: 1e3 | 1000 | yes | no}}yes
{{#ifeq: {{#expr:10^3}} | 1000 | yes | no}}yes
parameter

A sting can be a parameter, which is defined somewhere else:

{{#ifeq:{{{1}}}|foo| yes | no }} → If the parameter {{{1}}} equals foo, then the #ifeq will display yes, otherwise if the text is different or there is no text, #ifeq will display no.
example
This is useful in a template. For example the template:size, used to control the size of images on several pages.
template:size has the following on its page:
{{#ifeq:{{{1}}}|1| 20 | 40}}
If {{size|1}} then 20.
{{size}} alone displays 40.

Errectstapler 21:37, 13 May 2011 (UTC)Reply

Originally it said "If .. Otherwise .." Now you say something only partly true ("The comparison is made as text"), then formulate an exception. The first seems better.--Patrick1 13:41, 15 May 2011 (UTC)Reply
Hi Errectstapler, I second Patrick1's comment above, and find the addition of "text", "number", "parameter", "example" and "warnings" sub-headers visually unhelpful. I took the liberty of undoing your edits, but retained two useful things: the calls for the reader to notice the similar numerical examples, and the practical example. I hope I've managed to retain the spirit of your edits, while keeping the section congruent with the remaining sections of the page. Hamilton Abreu 02:06, 16 May 2011 (UTC)Reply

Operators, again

It's taken me a long time to understand some of the operators listed in the table at the top-right of the page. In my opinion there could be much better documentation either inside the table or in the text of the article. I'm I retired "programmer" with a BS in computer science but most of my experience was in c, FORTRAN, and various database languages. I expected the div operator to perform integer division (as in c). I doesn't of course. I seems to be identical to the / operator It treats numbers as reals. Mod is a quasi integer operator because the output is an integer although the input can be a real. The comparison operators only work on numerical data. It would be nice some worked on alpha data. Of course #ifeq: can be used for equality. The warning about floating-point errors is hard to condone but at least its documented although the circumstances under which failure can be predicted are not well defined (it says it depends on the server?). You can get away with almost anything if its a well documented constant behavior. I'm far from throwing up my hands and walking away but I find the current state of the documentation unforgivable. I know that coders are notorious for the failing to document but, in my experience its essential to have a go to documentation person. On one job we had a women who could not code to save her life but she was a wizard at documentation and so was a valued employee (almost indispensable). There should be someone around here like that. People like that are worth their wait in gold. (Rant over and I feel much better.)droll [chat] 09:04, 5 July 2010 (UTC)Reply

I guess clicking the links to Help:Calculation, the floating-point bug description, etc was just too much work for you?
131.216.14.1 17:37, 25 August 2010 (UTC)Reply
Dickish responses like 131.216.14.1 is the part of the reason this article is so shitty. Errectstapler 20:14, 12 May 2011 (UTC)Reply

Numbers and magic words

I face some problems when using #expr with some number formats like Arabic-Hindi (٠,١,٢,٣,٤,٥,٦,٧,٨,٩). This also arises when using some magic words such as CURRENTWEEK, which I believe produces numbers in the previous format. Is there a solution, or how can I change my default numbering format?--Email4mobile 17:05, 15 July 2010 (UTC)Reply

Checking #tag for errors with #iferror doesn't work

I have a tag extension which returns an valid error in some cases. So I tried to use it with #iferror like {{#iferror: {{#tag:mytag |error producing input}} | ERROR |no error }} but it always returns no error. When I copy the output of {{#tag:mytag |error producing input}} into the #iferror function it works fine. I use MW 1.15 --Danwe 09:53, 25 July 2010 (UTC)Reply

#expr

How can I get a decimal number produced here to a certain amount of decimal points?--Launchballer 20:09, 1 August 2010 (UTC)Reply

With round.--Patrick 12:41, 2 August 2010 (UTC)Reply
{{#expr:23.45678 round 2}} → 23.46
Told you the operators could use some documentation.  –droll [chat] 20:12, 2 August 2010 (UTC)Reply

#ifeq Problem

When I try something like this:

{| border=1 colspan=0 colspacing=0 style="margin: 1em auto;text-align:center; font-size:90%;" 
|-
|In this box here.
{{#ifeq:{{FULLPAGENAME}}|Help talk:Extension:ParserFunctions||-
|Example.}}
|}

Then this is what shows up:

In this box here.

-

When it should read:

In this box here.
Example.

And the reasonwhy this is happening is because of the two pipe characters that are next to each other but are for different kinds of formatting but MediaWiki gets mixed up with the formatting and causes a problem. So is there a solution to this? --Ultra Dot 15:00, 30 August 2010 (UTC)Reply

Yes. You need to escape the pipes that relate to the table, so that they are not interpreted by the parser. In most wikis, the template {{! }} exists for precisely this purpose. So:
{{#ifeq:{{FULLPAGENAME}}|Help talk:Extension:ParserFunctions|
{{!}}-
{{!}} Example.}}
should work. Hamilton Abreu 16:00, 30 August 2010 (UTC)Reply
Hamilton Abreu beat me to it. Note that the "pipes" (template) should appear at the beginning of a row, as where you'd put them in a regular wikitable. -DePiep 16:44, 30 August 2010 (UTC)Reply
Doesn't seem to work the same way on wikia wikis, so I am asking this question: Do wikia wikis use MediaWiki? (NOTE: I am trying to use it in a wikia wiki) --Ultra Dot 10:11, 31 August 2010 (UTC)Reply
Yes, they do use MW. I guess Template:! doesn't exist there. --Danwe 11:00, 31 August 2010 (UTC)Reply
They do use it. But when I tried doing the suggestion it started showing up on all pages even if the page title (string 1) is different from string 2. --Ultra Dot 13:25, 31 August 2010 (UTC)Reply

Expensive Parser Function: What_if_logically_not_executed?

I put a question at a Manual_talk, but it seems to be out of the loop. Could someone take a look overthere? -DePiep 14:06, 4 September 2010 (UTC)Reply

Why doesn't negative time work?

If I use the {{#time}} function and I write

{{#time:j F|8 June}}

the output becomes 8 June naturally. And if I write

{{#time:j F|8 June + 1 day}}

the output becomes 9 June. However if I write

{{#time:j F|8 June - 1 day}}

the output doesn't become 7 June as expected, but a big Error: Invalid time. Why on earth is that? On "my" Wikipedia (Swedish language version) it seems to work for some months (for instance, June works, but January doesn't) but on the English, it doesn't seem to work for any month at all. Could somebody shed som light on this please? /Ludde23 18:14, 13 September 2010 (UTC)Reply

The help page states "The date/time object can be in any format accepted by PHP's strtotime() function." This link states that "The function expects to be given a string containing a US English date format [...]." So,
{{#time:j F|June 8}}8 June
{{#time:j F|June 8 + 1 day}}9 June
{{#time:j F|June 8 - 1 day}}7 June
seems to work. Could this be it? Hamilton Abreu 02:39, 15 September 2010 (UTC)Reply

#time a day out with dates in certain months!

Hi.

If I feed 25 September 2010 in via

{{#time:l j F Y|{{{1|}}}}}

it returns Friday 24 September 2010. Inputting 4 October 2010 returns Sunday 3 October 2010. So both are one day out!

But if I feed 3 November 2010 in it returns Wednesday 3 November 2010, which is the correct day.

Any idea what is going on here? I'm running MW 1.15.3 and PF 1.1.1
Mitchelln 15:44, 27 September 2010 (UTC)Reply

Any way to get the username?

Is there any way to detect the username of the person looking at a given page, so you could have the code respond "Greetings, <username>" for example? This is a basic programming exercise, so I was surprised that there doesn't seem to be any documented way to do it with Mediawiki, unless I just haven't been looking in the right place.

Search CURRENTUSER and find the variables extension. --Subfader 16:41, 27 September 2010 (UTC)Reply
There's a variable "currentusers" but that generates a list of all users who are currently logged in, not the ones who are looking at a given page. On the English Wikipedia, there's a variable "currentuser" but that seems to return the last person who edited the page, not the person looking at it.

How to refer to / check on Semantic Properties?

I'm currently having problems to display a HintBox-Template based on the Semantic Property Values set for a page. How do i refer to the Property and do a comparison in a IF-Function.

Example: The Property: PageStatus
The possible Values: Draft, Review, Release

Result: Depending on the Property state a small hintbox should appear on top of my article. I'm not sure if i should use if, ifexpr or switch here (tendency going to "switch"). --Fennyface 09:12, 27 October 2010 (UTC)Reply

I saw this example, and it doesn't make sense to me:

Original:

{{#ifeq: <nowiki />foo<nowiki /> | <nowiki />foo<nowiki /> | equal | not equal }}
not equal
{{#ifeq: <nowiki />foo<nowiki /> | <nowiki />foo<nowiki /> | equal | not equal }}
- not equal - Manually retyped to make sure the spaces were real


Isn't this what it looks like after the nowiki is removed?

{{#ifeq: foo | foo | equal | not equal }}
equal

Can someone explain why these #ifeq expressions above are not equivalent, and why the last one returns the expected result, and the first two don't return equal?

User:173.116.103.237, 17:19, 8 December 2010 (UTC)Reply

I think the <nowiki/> tags are temporarily expanded during parsing, using unique identifiers, with the effect that they are each treated as non-identical. — Richardguk 19:54, 8 December 2010 (UTC)Reply

Where is #tag documentation

I couldn't find #tag in the list with #if, #ifeq, etc. Where is it documented?

Help:Magic words#Miscellaneous.--Patrick 09:49, 19 December 2010 (UTC)Reply

Declaring variables?

Is there a possibility to declare variables with the Parser Functions (or maybe an extension)? An example:

  • if {{{param}}} is "foo", {{{color}}} should be red and {{{width}}} 50
  • if {{{param}}} is "bar", {{{color}}} should be green and {{{width}}} 100
  • at the end of the code, there is one code for all cases, like this:

<div style="color: {{{color}}}; width: {{{width}}}px">Text</div>

At the moment, I have to write a code like this:

{{#switch: {{{param}}}
|foo = <div style="color: red; width: 50px">Text</div>
|bar = <div style="color: green; width: 100px">Text</div>
}}

I hope you understand what I want to say.--Harmageddon 13:41, 20 February 2011 (UTC)Reply

#vardefine is way easier: {{#ifeq: {{{param|}}} | foo | {{#vardefine:width|50}} {{#vardefine:color|green}} | (else) }}. Then use {{#var:width}} and {{#var:color}}. --Subfader 16:20, 21 February 2011 (UTC)Reply
I think questions to the extension are better off in the support desk, so I transcluded this and replied there, please check: Project:Support Desk#Declaring_variables.3F_2580. --Theaitetos 21:15, 25 February 2011 (UTC)Reply

Rounding to 1000

I want to round to even thousand units, e.g. 12,345 should become 12,000 or 123,456.9 > 123,000. I thought something like this would work: "Over {{#expr:{{NUMBEROFARTICLES:R}}|round -3}} articles" (as here). --Subfader 13:06, 6 March 2011 (UTC)Reply

Fix: {{#rreplace:{{NUMBEROFARTICLES}}|[0-9][0-9][0-9]$|000}} --Subfader 17:48, 6 March 2011 (UTC)Reply

iftrue

Isn't there also a {{#iftrue: function? Mglovesfun 13:50, 2 May 2011 (UTC)Reply

No, although there was a template by that name on the English Wikipedia several years ago. —Emufarmers(T|C) 22:14, 2 May 2011 (UTC)Reply

Fully (recursively?) substituting within a switch

Hi, I'm trying to write a template that will randomly substitute one of four messages. I've got the random part down by using a mod function with time, but I can't get just the text to substitute. When I call it with a {{subst:randomtemplate}}, it grabs a new time and therefore a new message. It seems like a recursive template as in the docs, and I've tried every combination of <includeonly>subst:</includeonly> I can find anywhere in the template, and I still can't get it to work. I'd even settle for leaving the switch function in place and substituting the mod time value, but I can't even get that to work.

Template so far:

subst:</includeonly>{{#expr:{{#time: s}} mod 4}}}}
|0=message 1
|1=message 2
|2=message 3
|3=message 4
}}

Thanks! Staeiou 03:44, 11 July 2011 (UTC)Reply

Figured it out, turns out you have to subst everything and put includeonly tags around the entire template. So behold, a random template generator:
<includeonly>{{subst:#switch: {{subst:#expr:{{subst:#time: s}} mod 4}}
|0=message0
|1=message1
|2=message2
|3=message3
</includeonly>
Staeiou 19:16, 11 July 2011 (UTC)Reply

Trimming

I wish the parser functions did not trim the leading/trailing spaces and other characters. I am having trouble adding spaces and paragraph marks on these functions. Ganeshk 15:51, 24 September 2011 (UTC)Reply

#switch

How many parameters can I put into a single #switch?--90.217.236.107 09:59, 22 October 2011 (UTC)Reply

Multiple #if

Is it possible to use the #if function for multiple articles? For example, IF this article AND that article exist, THEN 1; IF this article exists and that NOT, THEN 2; IF this article does NOT exist and that exists, THEN 3; IF both articles do NOT exist, THEN 4. I tried it many times using IF in IF, but it was effectless. Thanks.--Kiril Simeonovski 10:47, 27 December 2011 (UTC)Reply

I would use {{#expr: {{#ifexist: <ARTICLE1> | 1 | 0 }} + {{#ifexist: <ARTICLE2> | 2 | 0 }} }}, which will give you values 3, 2, 1 and 0, respectively, for the four cases you described. Or if the numbers are particularly important, by playing around a bit you should be able to get the exact numbers you want out. Happymelon 12:16, 27 December 2011 (UTC)Reply
Thanks once again. I didn't even imagine it's possible to use "+" sign.--Kiril Simeonovski 13:34, 27 December 2011 (UTC)Reply
Return to "Extension:ParserFunctions/2011" page.