Talk:Www.wikipedia.org template and Help:ParserFunctions: Difference between pages
m →'''Requests for updates''': archive requests |
No edit summary |
||
Line 1: | Line 1: | ||
<small>'''Other languages:''' {{ParserFunctions}}</small> |
|||
This page contains HTML for display at http://www.wikipedia.org/. It is protected to prevent the insertion of malicious scripts or objects. Changes to this page will be displayed within one hour. <s>All occurrences of the string $1 in this page will be replaced with the text at [[www.wikipedia.org portal]], converted from wikitext to HTML. </s> Currently [[www.wikipedia.org portal]] is not in use. |
|||
{{shortcut|[[WM:PF]]}} |
|||
This MediaWiki extension is a collection of parser functions. Parser functions typically have the syntax: |
|||
:<nowiki>{{</nowiki>#functionname: argument 1 | argument 2 | argument 3...}} |
|||
==Functions == |
|||
See [[Talk:Www.wikipedia.org portal]] for '''discussion of the content''' of this page. |
|||
This module defines six functions at present: |
|||
<tt>[[#.23expr:|expr]]</tt>, |
|||
<tt>[[#.23if:|if]]</tt>, |
|||
<tt>[[#.23ifeq:|ifeq]]</tt>, |
|||
<tt>[[#.23ifexist:|ifexist]]</tt>, |
|||
<tt>[[#.23ifexpr:|ifexpr]]</tt>, and |
|||
<tt>[[#.23switch:|switch]]</tt>. |
|||
=== #expr: === |
|||
'''Changes can be tested at [[Www.wikipedia.org template/temp]], which is not protected'''. See [[Talk:Www.wikipedia.org template/temp]] for instructions. |
|||
The <tt>expr</tt> function computes mathematical expressions based on permutations of numbers (or [[variable]]s/[[parameter]]s that translate to numbers) and operators. It does ''not'' work with strings; use <tt>ifeq</tt> below instead. The syntax is: |
|||
'''Meta-sysops: Do not be lazy. Use the "temp"-page as well, so that the two pages are in sync.''' |
|||
<nowiki>{{</nowiki> #expr: ''expression'' }} |
|||
<div class="toccolours" style="float: right; width: 50%"> |
|||
A list of supported operators follows. For more details about the operator precedence see [[Help:Calculation]], |
|||
==Archives== |
|||
it's roughly '''(1)''' grouping (parentheses), '''(2)''' unary (+/- signs and NOT), '''(3)''' multiplicative (*, /, div, mod), '''(4)''' additive (+ and -), '''(5)''' round, '''(6)''' comparative (=, !=, <, >, etc.), '''(7)''' logical AND, '''(8)''' logical OR. |
|||
''Old discussions can be found at the following subpages:'' |
|||
Within the same precedence class operators are evaluated left to right. As always some redundant parentheses are better than erroneous terse code. |
|||
{| cellpadding="6px" border=1 style="border:1px solid #C0C0C0; border-collapse:collapse;" |
|||
*[[/Updates|Requests to update the page]] |
|||
! ''Operator'' |
|||
*[[/Language sorting|Discussion on how to sort the language listings]] |
|||
! ''Operation'' |
|||
! ''Example'' |
|||
|- |
|||
|rowspan="2" colspan="2" align="center"| none |
|||
||<nowiki>{{#expr: 123456789012345}}</nowiki> = {{#expr: 123456789012345}} |
|||
|- |
|||
||<nowiki>{{#expr: 0.000001}}</nowiki> = {{#expr: 0.000001}} |
|||
|- |
|||
! ( ) |
|||
|| Grouping operators |
|||
||<nowiki>{{#expr: (30 + 7) * 7 }}</nowiki> = {{#expr: (30 + 7) * 7 }} |
|||
|- |
|||
! + |
|||
|| Unary '''<tt>+</tt>''' sign |
|||
||{{#expr: <tt>+</tt>30 * <tt>+7</tt>}} = {{#expr: +30 * +7}} |
|||
|- |
|||
! - |
|||
|| Unary '''<tt>-</tt>''' sign (negation) |
|||
||{{#expr: <tt>-</tt>30 * <tt>-</tt>7}} = {{#expr: -30 * -7}} |
|||
|- |
|||
! not |
|||
|| Unary NOT, logical NOT |
|||
||<nowiki>{{#expr: not 0 * 7}}</nowiki> = {{#expr: not 0 * 7}}<br /><nowiki>{{#expr: not 30+7}}</nowiki> = {{#expr: not 30+7}} |
|||
|- |
|||
! * |
|||
|| Multiplication |
|||
||<nowiki>{{#expr: 30 * 7}}</nowiki> = {{#expr: 30 * 7}} |
|||
|- |
|||
! / |
|||
|| Division, same as '''div''' |
|||
||<nowiki>{{#expr: 30 / 7}}</nowiki> = {{#expr: 30 / 7}} |
|||
|- |
|||
! div |
|||
|| Division, same as '''/''',<br />no integer division |
|||
||<nowiki>{{#expr: 30 div 7}}</nowiki> = {{#expr: 30 div 7}}<br /><nowiki>{{#expr: 5 div 2 * 2 + 5 mod 2}}</nowiki> = {{#expr: 5 div 2 * 2 + 5 mod 2}} |
|||
|- |
|||
! mod |
|||
|| "Modulo", remainder of division after truncating both operands to an integer.<br />Caveat, '''div''' and '''mod''' are different from all programming languages. |
|||
||<nowiki>{{#expr: 30 mod 7}}</nowiki> = {{#expr: 30 mod 7}}<br />{{#expr: <tt>-</tt>8 mod <tt>-</tt>3}} = {{#expr: -8 mod -3}}<br />{{#expr: <tt>-</tt>8 mod <tt>+</tt>3}} = {{#expr: -8 mod +3}}<br /><nowiki>{{#expr: 8 mod 2.7}}</nowiki> = {{#expr: 8 mod 2.7}}<br /><nowiki>{{#expr: 8 mod 3.2}}</nowiki> = {{#expr: 8 mod 3.2}}<br /><nowiki>{{#expr: 8.9 mod 3}}</nowiki> = {{#expr: 8.9 mod 3}} |
|||
|- |
|||
! <tt>+</tt> |
|||
|| Addition |
|||
|| {{#expr: 30 <tt>+</tt> 7}} = {{#expr: 30 + 7}} |
|||
|- |
|||
! <tt>-</tt> |
|||
|| Subtraction |
|||
|| {{#expr: 30 <tt>-</tt> 7}} = {{#expr: 30 - 7}} |
|||
|- |
|||
! round |
|||
|| Rounds off the number on the left to the power of 1/10 given on the right |
|||
||<nowiki>{{#expr: 30 / 7 round 3}}</nowiki> = {{#expr: 30 / 7 round 3}}<br /><nowiki>{{#expr: 30 / 7 round 0}}</nowiki> = {{#expr: 30 / 7 round 0}}<br /><nowiki>{{#expr: 3456 round -2}}</nowiki> = {{#expr: 3456 round -2}} |
|||
|- |
|||
! = |
|||
|| Equality (numerical incl. logical) |
|||
||<nowiki>{{#expr: 30 = 7}}</nowiki> = {{#expr: 30 = 7}} |
|||
|- |
|||
! <> |
|||
|| Inequality, same as '''!=''' |
|||
||<nowiki>{{#expr: 30 <> 7}}</nowiki> = {{#expr: 30 <> 7}} |
|||
|- |
|||
!| != |
|||
|| Inequality, same as '''<>''', logical ''xor'' |
|||
||<nowiki>{{#expr: 1 != 0}}</nowiki> = {{#expr: 1 != 0}} |
|||
|- |
|||
! < |
|||
|| Less than |
|||
||<nowiki>{{#expr: 30 < 7}}</nowiki> = {{#expr: 30 < 7}} |
|||
|- |
|||
! > |
|||
|| Greater than |
|||
||<nowiki>{{#expr: 30 > 7}}</nowiki> = {{#expr: 30 > 7}} |
|||
|- |
|||
! <= |
|||
|| Less than or equal to |
|||
||<nowiki>{{#expr: 30 <= 7}}</nowiki> = {{#expr: 30 <= 7}} |
|||
|- |
|||
! >= |
|||
|| Greater than or equal to |
|||
||<nowiki>{{#expr: 30 >= 7}}</nowiki> = {{#expr: 30 >= 7}} |
|||
|- |
|||
! and |
|||
|| Logical AND |
|||
||<nowiki>{{#expr: 4<5 and 4 mod 2}}</nowiki> = {{#expr: 4<5 and 4 mod 2}} |
|||
|- |
|||
! or |
|||
|| Logical OR |
|||
||<nowiki>{{#expr: 4<5 or 4 mod 2}}</nowiki> = {{#expr: 4<5 or 4 mod 2}} |
|||
|} |
|||
The boolean operators consider '''0''' to be "false" and any other number to be "true", on output "true" is shown as '''{{#expr: 30 and 7}}'''. |
|||
=='''Requests for updates'''== |
|||
''Old requests moved to [[Talk:Www.wikipedia.org template/Updates#Update|Talk:Www.wikipedia.org template/Updates]]; new requests to synchronize this template with [[/temp]] should still be listed in this section (click <span class="plainlinks">[{{fullurle:{{NAMESPACE}}:{{PAGENAME}}|action=edit}}§ion=2 here]</span>).'' |
|||
Numbers are given in decimal with "." for the decimal point. The <code>[[Help:Magic words#Formatting|formatnum:]]</code> function can be used to change the decimal point to a comma for the appropriate locales. Scientific notation with <tt>E</tt> plus exponent is not yet supported on input for expressions, but used on output, for details see [[Help:Calculation]]. |
|||
<!-- Insert your request right below this line. --> |
|||
=== #if: === |
|||
</div> |
|||
The <tt>if</tt> function is an if-then-else construct. The syntax is: |
|||
<div style="width: 50%"> |
|||
__TOC__ |
|||
</div> |
|||
<br clear="all" /> |
|||
{{ #if: ''<condition>'' | ''<then text>'' | ''<else text>'' }} |
|||
== Language neutrality, search, logos == |
|||
If the condition is an empty string or consists only of whitespace, then it is considered false, and the ''else text'' is returned. Otherwise, the ''then text'' is returned. The ''else text'' may be omitted, in which case the result will be blank if the condition is false. |
|||
1. This page makes NO ATTEMPT to be language-neutral (it even gives its content language as EN at the top). |
|||
2. A search and a logo and sister projects are not needed, they will be provided upon arrival at the appropriate Wikipedia. [[User:Node ue|Node ue]] |
|||
An example: |
|||
:The EN should probably be changed. Search and logo are useful to make the portal more immediately usable. In web page design, one should ALWAYS make a search box prominently available [http://www.cnn.com/2004/TECH/08/09/jakob.nielsen/index.html]; if you want the users to come to you, you can't steer them - [[User:David Gerard|David Gerard]] 19:51, 15 Jan 2005 (UTC) |
|||
<nowiki> {{Template|parameter=something}} {{Template}} {{Template|parameter=}}</nowiki> |
|||
<nowiki> | | |</nowiki> |
|||
<nowiki> | | |</nowiki> |
|||
<nowiki> | | |</nowiki> |
|||
<nowiki>{{ #if: {{{parameter|}}} | Parameter is defined. | Parameter is undefined, or empty }}</nowiki> |
|||
Note that the <tt>if</tt> function does '''not''' support "=" signs or mathematical expressions. <nowiki>{{#if: 1 = 2|yes|no}}</nowiki> will return "yes", because the string "1 = 2" is not blank. It is intended as an "if not empty" structure. |
|||
::Again, this is a <big><big><big>portal</big></big></big>, not a sitewide mainpage. Its sole '''stated''' purpose is to direct people to a different Wikipedia. '''The problem here is that people like you continue to circulate wikipedia.org as the URL for "Wikipedia", rather than using the proper language-specific URLs based on which language medium you are advertising in. The only people who end up at wikipedia.org should be those who guess the URL, or those who pressed "I'm feeling lucky" at Google. The lack of popular distinction among English speakers between Wikipedia and the English Wikipedia is the issue here, NOT whether or not this is a portal or a frontpage.''' http://www.amazon.com/exec/obidos/tg/stores/static/-/gateway/international-gateway/ref=gw_subnav_in/104-2193524-1431902 is a great example of what this page's function actually is. The search box is limited to the local site (which doesn't exist in the case of our portal), and is located AFTER all the languages, not in the middle (as Catherine proposes) or directly to the side (as in the current version). --. |
|||
=== #ifeq: === |
|||
== Image longdesc links don't work right == |
|||
<tt>ifeq</tt> compares two strings or numbers, and returns another string depending on the result of that comparison. The syntax is: |
|||
The longdesc parameters for the images are relative links within meta, and don't work right when it's placed as www.wikipedia.org, as someone pointed out on [[:en:Talk:Main Page]]. They need to be turned into absolute URLs that point to meta. [[User:68.64.220.69|68.64.220.69]] 23:06, 6 May 2005 (UTC) |
|||
:Fixed on the test page, now could someone switch to that. – [[User:ABCD|AB]][[User talk:ABCD|'''''CD''''']] 15:30, 7 May 2005 (UTC) |
|||
<nowiki>{{</nowiki> #ifeq: ''<text 1>'' | ''<text 2>'' | ''<equal text>'' | ''<not equal text>'' }} |
|||
== Wrong name == |
|||
If both strings can be interpreted as numbers the comparison is numerical. To force a string comparison add tokens that can't be interpreted as numbers: |
|||
The name of Friulian language (fur.wiki) it's 'Furlan', not 'Furlane'; please change it thanks [[User:Klenje|Klenje]] 13:26, 23 October 2005 (UTC) |
|||
:<tt>{{ #ifeq: +07 | 007 | 1 | 0 }}</tt> gives {{#ifeq: +07 | 007 | 1 | 0 }} |
|||
:By the way, you can fix any mistakes on this template at the [[/temp|test page]]. – [[User:Mxn|Minh Nguyễn]] <small class="plainlinks">([[User talk:Mxn|talk]], [http://mxn.f2o.org/index.html blog])</small> 03:33, 30 October 2005 (UTC) |
|||
:<tt>{{ #ifeq:"+07"|"007"| 1 | 0 }}</tt> gives {{#ifeq:"+07"|"007"| 1 | 0 }} |
|||
;For compatibility with older templates [[#.23if:|#if:]] cannot directly distinguish defined and undefined parameter values, it's a shorthand for a comparison with the empty string. With [[#.23ifeq:|#ifeq:]] it's directly possible to identify undefined parameters: |
|||
== Unintuitive Text Box == |
|||
:<tt>{{ #if: {{{x| }}}'''|'''not blank'''|'''blank}}</tt> = {{#if: {{{x|}}}|not blank|blank}}, |
|||
:<tt>{{ #ifeq: {{{x| }}}'''| |'''blank'''|'''not blank}}</tt> = {{#ifeq: {{{x|}}}| |blank|not blank}}, |
|||
:<tt>{{ #ifeq: {{{x| }}}'''|'''{{{x|u}}}'''|'''defined'''|'''undefined}}</tt> = {{#ifeq: {{{x|}}}|{{{x|u}}}|defined|undefined}}. |
|||
;An undefined parameter without default counts in the comparison as a string consisting of the tag: |
|||
:<tt>{{ #ifeq: {{{x}}}'''|'''{{ concat'''|'''{'''|'''{'''|'''{x}'''|'''}'''|'''} }}'''|'''1'''|'''0}}</tt> = {{ #ifeq: {{{x}}}|{{ concat|{|{|{x}|}|} }}|1|0}}. |
|||
=== #ifexist: === |
|||
I suggest that the code for this page be changed so that the text box for searching Wikipedia contains the browser's insertion point by default rather than requiring a user to click. This reduces my productivity when I'm trying to search Wikipedia quickly because I must either hit the tab key tons of times or lift my hands from the keyboard to use the mouse. Matt R Hall -- 01 December 2005 02:13 PST GMT -0700 |
|||
<tt>ifexist</tt> returns one of two results based on whether or not a named title exists, e.g. |
|||
:I added a touch of JavaScript to the temporary version of this template that does what you ask. Now we have to wait for an administrator to approve the change. – [[User:Mxn|Minh Nguyễn]] <small class="plainlinks">([[User talk:Mxn|talk]], [http://mxn.f2o.org/index.html blog])</small> 00:34, 15 December 2005 (UTC) |
|||
:<code><nowiki>{{#ifexist:Foo|Bar|RFC 3092}}</nowiki></code> gives {{#ifexist:Foo|Bar|RFC 3092}}, because [[Foo]] {{#ifexist:Foo|exists|doesn't exist}}. |
|||
:By the way, Matt, you can easily attach the date at the end of your comments by typing out five squigglies (<code><nowiki>~~~~~</nowiki></code>). – [[User:Mxn|Minh Nguyễn]] <small class="plainlinks">([[User talk:Mxn|talk]], [http://mxn.f2o.org/index.html blog])</small> 05:59, 15 December 2005 (UTC) |
|||
:<code><nowiki>{{#ifexist:ParserFunctions|Thanks|No}}</nowiki></code> gives {{#ifexist:ParserFunctions|Thanks|No}}. |
|||
:<code><nowiki>{{#ifexist:m:Help:Calculation|Yes|Oops}}</nowiki></code> gives {{#ifexist:m:Help:Calculation|Yes|Oops}} although [[m:Help:Calculation]] exists, because of the interwiki prefix. |
|||
The first parameter is the title to check for, the second is the positive result, and the third, the negative result. If the parameter passed does not produce a valid title object, then the result is negative. |
|||
== удмурт == |
|||
At the moment ''Udmurt'' is spelled with a small letter on the front page - shouldn't it have a capital "У" like all the others? [[User:80.186.33.84|80.186.33.84]] 19:57, 2 December 2005 (UTC) |
|||
{{tim|exists}} gives the same result, except that the result is positive for an interwiki link. {{tim|if interwiki link}} exploits this difference. |
|||
:Sorry, I probably did that not realizing that it wasn't capitalized. The fonts on my computer don't distinguish capital and lowercase Cyrillic letters very well. I've fixed it in the temporary version; now we have to wait for an administrator to approve the changes. – [[User:Mxn|Minh Nguyễn]] <small class="plainlinks">([[User talk:Mxn|talk]], [http://mxn.f2o.org/index.html blog])</small> 00:33, 15 December 2005 (UTC) |
|||
=== #ifexpr: === |
|||
== Reposted from [[en:Talk:Main page]] == |
|||
<tt>ifexpr</tt> evaluates a mathematical expression and returns one of two strings depending on the result. |
|||
I wasn't sure where to post this, but could someone fix the text "Česká" on the wikipedia.org page (not this main page, rather the first page you come to when typing 'wikipedia.org' into your browser). It should read "Česky" , meaning [in] Czech. -- Hexagon1 14:45, 7 January 2006 (UTC) |
|||
{{#ifexpr: ''<expression>'' | ''<then text>'' | ''<else text>'' }} |
|||
:I'm just reposting the comment here, where people may be able to do something about it. [[User:Rspeer|Rspeer]] 22:56, 7 January 2006 (UTC) |
|||
If the expression evaluates to zero, then the ''else text'' is returned, otherwise the ''then text'' is returned. Expression syntax is the same as for <tt>expr</tt>. |
|||
::I have fixt that. If there is anything else you can ask me. --[[User:Walter|Walter]] 23:28, 7 January 2006 (UTC) |
|||
;At the moment the ''else text'' is also returned for an empty expression: |
|||
== Interwiki NUMBEROFARTICLES variable == |
|||
:<code><nowiki>{{#ifexpr: {{ns:0}}|Toast|'''or else'''}}</nowiki></code> gives {{#ifexpr: {{ns:0}}|Toast|'''or else'''}} |
|||
;Omitting both ''then text'' and ''else text'' gives no output except possibly an error message; this can be used to check the correctness of an expression, or to check the wording of the error message (emulated ''assertions'', forced errors): |
|||
:<code>{{#ifexpr: 1/{{#ifeq: {{ns:4}}|Meta|1|0}}}}</code> {{#ifexpr: 1/{{#ifeq: {{ns:4}}|Meta|1|0}}}} |
|||
:<code>{{#ifexpr: 1/{{#ifeq: {{ns:4}}|Meta|0|1}}}}</code> {{#ifexpr: 1/{{#ifeq: {{ns:4}}|Meta|0|1}}}} |
|||
:<code>{{#if:<nowiki>{{#ifexpr: 1=2}}</nowiki>|wrong|correct}}</code> {{#if:{{#ifexpr: 1=2}}|wrong|correct}} |
|||
:<code>{{#if:<nowiki>{{#ifexpr: 1E2}}</nowiki>|wrong|correct}}</code> {{#if:{{#ifexpr: 1E2}}|wrong|correct}} |
|||
:<code>{{#if:<nowiki>{{#ifexpr: 1/0}}</nowiki>|wrong|correct}}</code> {{#if:{{#ifexpr: 1/0}}|wrong|correct}} |
|||
:<code>{{#if:<nowiki>{{#ifexpr: a=b}}</nowiki>|wrong|correct}}</code> {{#if:{{#ifexpr: a=b}}|wrong|correct}} |
|||
For an application, see also {{tim|evalns}}. |
|||
=== #switch: === |
|||
Please see (and perhaps vote for) [[MediaZilla:1534|<span title="NEW: Language-specific article count variables">Bug 1534</span>]], which requests the ability to include the number of articles in another Wikimedia wiki, via the <code><nowiki>{{NUMBEROFARTICLES}}</nowiki></code> variable. This bug would enable us to automate this page, probably with help from the {{tl|foreach}} and {{tl|equal}} templates. – [[User:Mxn|Minh Nguyễn]] <small>([[User talk:Mxn|talk]], [[Special:Contributions/Mxn|contribs]])</small> 23:51, 5 March 2006 (UTC) |
|||
<code>switch</code> compares a single value against multiple others, returning a string if a match is found. The syntax is basically: |
|||
<nowiki>{{</nowiki> #switch: ''<comparison value>'' |
|||
Come to think of it, since these templates consist of plain, unparsed HTML code, we can't automate it using the templates alone, but once this bug is fixed, we could revive [[Www.wikipedia.org portal]] and automate things that way. – [[User:Mxn|Minh Nguyễn]] <small>([[User talk:Mxn|talk]], [[Special:Contributions/Mxn|contribs]])</small> 01:15, 6 March 2006 (UTC) |
|||
| ''<value<sub>1</sub>>'' = ''<result<sub>1</sub>>'' |
|||
:If we don't automate it, can we at least go back to a rounding convention, where we don't bother to update the larger Wikipedia's except for each 5 or 10,000 additional articles? There's no need for updating every time another 1,000 articles is written on some language edition. - [[User:Taxman|Taxman]] 19:03, 9 March 2006 (UTC) |
|||
| ''<value<sub>2</sub>>'' = ''<result<sub>2</sub>>'' |
|||
| ''...'' |
|||
| ''<value<sub>n</sub>>'' = ''<result<sub>n</sub>>'' |
|||
| ''<default result>'' |
|||
}} |
|||
<code>switch</code> will search through each value passed until a match is found with the comparison value. When found, the result for that value is returned (the text string after the equal sign). If no match is found, but the last item has no equal sign in it, it will be returned as the default result. If your default result ''must'' have an equal sign, you may use <code>#default</code>: |
|||
::What we did before wasn't rounding, though: it was just keeping two significant figures rather than truncating at the thousands place. What that meant back then was that we truncated at the ten-thousands place for the 100,000+ editions, and at the thousands place for the 10,000+ editions. I think I'd be alright with truncating at the ten-thousands place from now on, since the top 10 are all above 100,000 now. But note that it's not the same as rounding: 219,000+ can't be rounded to 220,000+, because they don't have more (+) than 220,000 articles yet. – [[User:Mxn|Minh Nguyễn]] <small>([[User talk:Mxn|talk]], [[Special:Contributions/Mxn|contribs]])</small> 22:55, 9 March 2006 (UTC) |
|||
:::Well yes, I meant truncating, I just wasn't being specific. - [[User:Taxman|Taxman]] 23:08, 9 March 2006 (UTC) |
|||
<nowiki>{{</nowiki> #switch: ''<comparison value>'' |
|||
==Norman Wikipedia== |
|||
| ''<value>'' = ''<result>'' |
|||
The [[:nrm:|Norman Wikipedia]] now has 150 articles, and would like being listed on the portal. [[User:Jade Knight|The Jade Knight]] 04:41, 29 March 2006 (UTC) |
|||
| #default = ''<default result>'' |
|||
}} |
|||
Note that it's also possible to have "fall through" for values (reducing the need to duplicate results). For example: |
|||
:I've added the Norman and Baskhir Wikipedias to the [[Talk:Www.wikipedia.org template/temp|temp]] page (which ''you'' can edit too!); we just need an administrator here to synchronize the template with the temp page. – [[User:Mxn|Minh Nguyễn]] <small>([[User talk:Mxn|talk]], [[Special:Contributions/Mxn|contribs]])</small> 07:41, 29 March 2006 (UTC) |
|||
<nowiki>{{</nowiki> #switch: ''<comparison value>'' |
|||
== Forseti formatting == |
|||
| ''<value<sub>1</sub>>'' |
|||
| ''<value<sub>2</sub>>'' |
|||
| ''<value<sub>3</sub>>'' = ''<result<sub>3</sub>>'' |
|||
| ''...'' |
|||
| ''<value<sub>n</sub>>'' = ''<result<sub>n</sub>>'' |
|||
| ''<default result>'' |
|||
}} |
|||
Note how value<sub>1</sub> and value<sub>2</sub> contain no equal sign. If they're matched, they are given the result for value<sub>3</sub> (that is, whatever is in result<sub>3</sub>). |
|||
When/if is the Forseti formatting dicussed back in February coming in? [[User:Andrevan|Andrevan]] 23:44, 7 April 2006 (UTC) |
|||
;As for [[##ifeq:|#ifeq:]] the comparison is numerical where possible: |
|||
:It wasn't exactly agreed upon, if I remember correctly. Maybe it's time to bring that issue back up, but I have a feeling that this design's already kind of established, since it's been up for so long. – [[User:Mxn|Minh Nguyễn]] <small>([[User talk:Mxn|talk]], [[Special:Contributions/Mxn|contribs]])</small> 22:04, 8 April 2006 (UTC) |
|||
:<tt>{{ #switch: +07 | 7 = Yes | 007 = Bond | No }}<tt> gives {{ #switch: +07 | 7 = Yes | 007 = Bond | No }} |
|||
:<tt>{{ #switch:"+07"|"7"= Yes |"007"= Bond | No }}<tt> gives {{ #switch:"+07"|"7"= Yes |"007"= Bond | No }} |
|||
;The matched value can be empty, therefore the following constructs are equivalent: |
|||
:<tt>{{ #if: {{ns:0}} | not empty | empty }}<tt> gives {{ #if: {{ns:0}} | not empty | empty }} |
|||
:<tt>{{ #switch:{{ns:0}}|=empty|not empty }}<tt> gives {{ #switch:{{ns:0}}|=empty|not empty }} |
|||
== |
== #if fix in 1.6.X == |
||
'''A simple fix for #if''' - |
|||
I'm running [[en:SeaMonkey|SeaMonkey]] 1.0, a Gecko browser like Firefox. The current portal page is sprouting a horizontal scrollbar on my 1280x800 screen - the scrollbar can't even be perceptably moved and there's no content out of view, but I just thought I'd note that. -- [[User:Mithent|Mithent]] 11:22, 26 April 2006 (UTC) |
|||
Replace: |
|||
:Yes, I get this in Firefox 1.5 too. It's a CSS issue: the <code>div</code> that contains the sister project links is set to have a width of <code>100%</code>, which probably causes rounding errors. I've attempted to "fix" the issue at the [[Www.wikipedia.org template/temp|temp page]] by setting the width slightly smaller: to <code>99%</code>. Now we need to wait for a Meta administrator to approve the change and incorporate it into [[Www.wikipedia.org template]]. – [[User:Mxn|Minh Nguyễn]] <small>([[User talk:Mxn|talk]], [[Special:Contributions/Mxn|contribs]])</small> 22:31, 27 April 2006 (UTC) |
|||
<pre><nowiki> |
|||
::Looks better now. Thanks! -- [[User:Mithent|Mithent]] 11:36, 28 April 2006 (UTC) |
|||
if ($test !== '') { |
|||
</nowiki></pre> |
|||
on line 47 with: |
|||
== Icelandic -> 10000+ == |
|||
Hi. The [http://is.wikipedia.org/ Icelandic Wikipedia] made it to 10000 articles yesterday. It would be fun to be moved up to the correct catagory. Thanks --[[User:Spm|Smári McCarthy]] 16:07, 7 May 2006 (UTC) |
|||
<pre><nowiki> |
|||
:Hi Smári, I've moved it up at the [[Www.wikipedia.org template/temp|temp page]], which you can edit at any time. Now we just have to wait for an administrator here to update the <ins>whole</ins> portal accordingly. – [[User:Mxn|Minh Nguyễn]] <small>([[User talk:Mxn|talk]], [[Special:Contributions/Mxn|contribs]])</small> 22:41, 7 May 2006 (UTC) |
|||
if ($test) { |
|||
</nowiki></pre> |
|||
That's it. Fix by Andrew Tutt. http://www.duiki.com |
|||
== italian == |
|||
==Caveats== |
|||
Hi folks! The label "''articoli''" (section "Italiano") has to be replaced with "''voci''", the italian word for encyclopedic content ("''articoli''" is used for newspapers), see this [[:it:wikipedia:bar#Articoli su www.wikipedia.org|discussion]]. Thank you. --[[User:Iron Bishop|Iron Bishop]] 14:50, 25 May 2006 (UTC) |
|||
:I've corrected in "voci" the italian entry. Thank you. --[[User:M7|M/]] 16:38, 25 May 2006 (UTC) |
|||
''Most observed problems turned out to be general issues not limited to parser functions.'' |
|||
== nl.wikipedia.org has reached the 200.000 [[:en:Wikipedia:Milestone statistics|milestone]] == |
|||
===Substitution=== |
|||
Applying "subst:" to a ParserFunction works, provided that there is no space between "subst:" and "#". For details see [[Help:Substitution]]. |
|||
Note that unless a technique like [[Help:Substitution#Optional substitution|optional substitution]] is used, substituting a template which uses a ParserFunction does not replace that ParserFunction with its result. This is often undesirable. |
|||
Like other [[Help:colon function|colon function]]s the parser functions are affected by [[mediazilla:5678|5678]] in a predictable way. Summary, undefined parameters can be overwritten by ''corresponding'' parameters, for details see [[/5678]] and [[Help:Substitution#Corrupted_default_value|Substitution]]. Substitution is the only case where this is critical wrt to parameter defaults. It doesn't affect defined parameters. |
|||
The Dutch Wikipedia has reached this milestone some days ago on 24 may. [[User:JePe|JePe]] 10:47, 27 May 2006 (UTC) |
|||
===Tables=== |
|||
:It's been updated. But please note that the [[Www.wikipedia.org template/temp|/temp]] copy of the page was already updated to reflect this; we were just waiting for an administrator here to actually synchronize the two copies. – [[User:Mxn|Minh Nguyễn]] <small>([[User talk:Mxn|talk]], [[Special:Contributions/Mxn|contribs]])</small> 05:27, 28 May 2006 (UTC) |
|||
Currently wiki pipe table syntax doesn't work inside conditionals, there are two main workarounds. |
|||
* Hide the pipe from parser functions by putting it in a template, e.g. {{tim|!}} as on [[w:en:Template:!|w:en:]]. |
|||
* Use html style table syntax instead. |
|||
* See also [[Help:Table]], completely empty rows or columns are not displayed. Empty cells could be also transformed into dummy <tt>&nbsp;</tt> cells on pages not affected by [[mediazilla:5569|5569]]. |
|||
Note that "'''<tt>|</tt>'''" and "'''<tt>=</tt>'''" were always tricky within templates, this is no new issue. |
|||
===Expressions=== |
|||
<!-- ADD YOUR DISCUSSION *ABOVE* THIS LINE --> |
|||
*'''div''' is no integer division and (as is) redundant, use '''/''' (slash) for real divisions. |
|||
[[Category:Wikipedia|Portal www.wikipedia.org]] |
|||
*'''mod''' uses PHP's % operator, which is different from modulo-operators in all other programming languages, see also {{tim|mod}} and [[mediazilla:6068|6068]]. |
|||
<!-- REMEMBER: YOU can edit [[Www.wikipedia.org template/temp]]! --> |
|||
*'''mod''' sometimes returns wrong results for the same input values, see [[mediazilla:6356|6356]] and [[/MOD10000]]. ''Update: values less than 1E+12 are apparently not affected.'' |
|||
*Valid [[#.23expr:|#expr:]] results like {{#expr: 0.0000001}} are not yet supported as '''#expr:''' input: |
|||
*:<nowiki>{{#expr:</nowiki>{{#expr:0.0000001}}<nowiki>}}</nowiki> yields ''{{#expr:{{#expr:0.0000001}}}}''. |
|||
*Under certain conditions '''round 0''' results in '''-0''' instead of '''0'''. For an expression '''x''' using '''0+(x)''' fixes this oddity. |
|||
== Installation == |
|||
Download both of these files and put them in a new directory called ''ParserFunctions'' in your [[Mediawiki extensions|extensions]] directory. |
|||
* [http://svn.wikimedia.org/svnroot/mediawiki/trunk/extensions/ParserFunctions/Expr.php Expr.php] |
|||
* [http://svn.wikimedia.org/svnroot/mediawiki/trunk/extensions/ParserFunctions/ParserFunctions.php ParserFunctions.php] |
|||
Then put the following at the end of your [[LocalSettings.php]]: |
|||
require_once( "$IP/extensions/ParserFunctions/ParserFunctions.php" ); |
|||
You can also browse the code tree here: |
|||
* [http://svn.wikimedia.org/viewvc/mediawiki/trunk/extensions/ParserFunctions/ ParserFunctions in MediaWiki SVN] |
|||
=== 1.7alpha === |
|||
The ParserFunctions, including #if, work under 1.7alpha, assuming you're not using an extremely early build. |
|||
=== 1.6 === |
|||
Most ParserFunctions (except #if, which does not work at all) work just as well on MediaWiki 1.6, but the syntax of ParserFunctions is without the '#' character. If you want to use the '#' character, find this section of [http://svn.wikimedia.org/viewvc/mediawiki/trunk/extensions/ParserFunctions/ParserFunctions.php?revision=14375&view=markup ParserFunctions.php]: |
|||
<pre><nowiki> |
|||
$wgParser->setFunctionHook( 'expr', array( &$wgExtParserFunctions, 'expr' ) ); |
|||
$wgParser->setFunctionHook( 'if', array( &$wgExtParserFunctions, 'ifHook' ) ); |
|||
$wgParser->setFunctionHook( 'ifeq', array( &$wgExtParserFunctions, 'ifeq' ) ); |
|||
$wgParser->setFunctionHook( 'ifexpr', array( &$wgExtParserFunctions, 'ifexpr' ) ); |
|||
$wgParser->setFunctionHook( 'switch', array( &$wgExtParserFunctions, 'switchHook' ) ); |
|||
$wgParser->setFunctionHook( 'ifexist', array( &$wgExtParserFunctions, 'ifexist' ) ); |
|||
</nowiki></pre> |
|||
Then, replace it with this: |
|||
<pre><nowiki> |
|||
$wgParser->setFunctionHook( '#expr', array( &$wgExtParserFunctions, 'expr' ) ); |
|||
$wgParser->setFunctionHook( '#if', array( &$wgExtParserFunctions, 'ifHook' ) ); |
|||
$wgParser->setFunctionHook( '#ifeq', array( &$wgExtParserFunctions, 'ifeq' ) ); |
|||
$wgParser->setFunctionHook( '#ifexpr', array( &$wgExtParserFunctions, 'ifexpr' ) ); |
|||
$wgParser->setFunctionHook( '#switch', array( &$wgExtParserFunctions, 'switchHook' ) ); |
|||
$wgParser->setFunctionHook( '#ifexist', array( &$wgExtParserFunctions, 'ifexist' ) ); |
|||
</nowiki></pre> |
|||
==See also== |
|||
*[[Help:Calculation]] |
|||
*[[Help:Magic words]] |
|||
*[[StringFunctions]] |
|||
*[[DynamicFunctions]] |
|||
*[[:m:Category:Templates using ParserFunctions]] |
|||
*[[:en:Category:Templates using ParserFunctions]] |
|||
== External links == |
|||
* [http://mail.wikipedia.org/pipermail/wikitech-l/2006-April/thread.html#34685 The discussion about the ParserFunctions in the Wikitech-l list archive] |
|||
* [http://mail.wikipedia.org/pipermail/wikitech-l/2006-April/034892.html ParserFunctions trial] |
|||
[[Category:MediaWiki extensions]] |
Revision as of 02:06, 22 July 2006
Other languages: Template:ParserFunctions
This MediaWiki extension is a collection of parser functions. Parser functions typically have the syntax:
- {{#functionname: argument 1 | argument 2 | argument 3...}}
Functions
This module defines six functions at present: expr, if, ifeq, ifexist, ifexpr, and switch.
#expr:
The expr function computes mathematical expressions based on permutations of numbers (or variables/parameters that translate to numbers) and operators. It does not work with strings; use ifeq below instead. The syntax is:
{{ #expr: expression }}
A list of supported operators follows. For more details about the operator precedence see Help:Calculation, it's roughly (1) grouping (parentheses), (2) unary (+/- signs and NOT), (3) multiplicative (*, /, div, mod), (4) additive (+ and -), (5) round, (6) comparative (=, !=, <, >, etc.), (7) logical AND, (8) logical OR. Within the same precedence class operators are evaluated left to right. As always some redundant parentheses are better than erroneous terse code.
Operator | Operation | Example |
---|---|---|
none | {{#expr: 123456789012345}} = 1.2345678901234E+14 | |
{{#expr: 0.000001}} = 1.0E-6 | ||
( ) | Grouping operators | {{#expr: (30 + 7) * 7 }} = 259 |
+ | Unary + sign | {{#expr: +30 * +7}} = 210 |
- | Unary - sign (negation) | {{#expr: -30 * -7}} = 210 |
not | Unary NOT, logical NOT | {{#expr: not 0 * 7}} = 7 {{#expr: not 30+7}} = 7 |
* | Multiplication | {{#expr: 30 * 7}} = 210 |
/ | Division, same as div | {{#expr: 30 / 7}} = 4.2857142857143 |
div | Division, same as /, no integer division |
{{#expr: 30 div 7}} = 4.2857142857143 {{#expr: 5 div 2 * 2 + 5 mod 2}} = 6 |
mod | "Modulo", remainder of division after truncating both operands to an integer. Caveat, div and mod are different from all programming languages. |
{{#expr: 30 mod 7}} = 2 {{#expr: -8 mod -3}} = -2 {{#expr: -8 mod +3}} = -2 {{#expr: 8 mod 2.7}} = 0 {{#expr: 8 mod 3.2}} = 2 {{#expr: 8.9 mod 3}} = 2 |
+ | Addition | {{#expr: 30 + 7}} = 37 |
- | Subtraction | {{#expr: 30 - 7}} = 23 |
round | Rounds off the number on the left to the power of 1/10 given on the right | {{#expr: 30 / 7 round 3}} = 4.286 {{#expr: 30 / 7 round 0}} = 4 {{#expr: 3456 round -2}} = 3500 |
= | Equality (numerical incl. logical) | {{#expr: 30 = 7}} = 0 |
<> | Inequality, same as != | {{#expr: 30 <> 7}} = 1 |
!= | Inequality, same as <>, logical xor | {{#expr: 1 != 0}} = 1 |
< | Less than | {{#expr: 30 < 7}} = 0 |
> | Greater than | {{#expr: 30 > 7}} = 1 |
<= | Less than or equal to | {{#expr: 30 <= 7}} = 0 |
>= | Greater than or equal to | {{#expr: 30 >= 7}} = 1 |
and | Logical AND | {{#expr: 4<5 and 4 mod 2}} = 0 |
or | Logical OR | {{#expr: 4<5 or 4 mod 2}} = 1 |
The boolean operators consider 0 to be "false" and any other number to be "true", on output "true" is shown as 1.
Numbers are given in decimal with "." for the decimal point. The formatnum:
function can be used to change the decimal point to a comma for the appropriate locales. Scientific notation with E plus exponent is not yet supported on input for expressions, but used on output, for details see Help:Calculation.
#if:
The if function is an if-then-else construct. The syntax is:
{{ #if: <condition> | <then text> | <else text> }}
If the condition is an empty string or consists only of whitespace, then it is considered false, and the else text is returned. Otherwise, the then text is returned. The else text may be omitted, in which case the result will be blank if the condition is false.
An example:
{{Template|parameter=something}} {{Template}} {{Template|parameter=}} | | | | | | | | | {{ #if: {{{parameter|}}} | Parameter is defined. | Parameter is undefined, or empty }}
Note that the if function does not support "=" signs or mathematical expressions. {{#if: 1 = 2|yes|no}} will return "yes", because the string "1 = 2" is not blank. It is intended as an "if not empty" structure.
#ifeq:
ifeq compares two strings or numbers, and returns another string depending on the result of that comparison. The syntax is:
{{ #ifeq: <text 1> | <text 2> | <equal text> | <not equal text> }}
If both strings can be interpreted as numbers the comparison is numerical. To force a string comparison add tokens that can't be interpreted as numbers:
- {{ #ifeq: +07 | 007 | 1 | 0 }} gives 1
- {{ #ifeq:"+07"|"007"| 1 | 0 }} gives 0
- For compatibility with older templates #if: cannot directly distinguish defined and undefined parameter values, it's a shorthand for a comparison with the empty string. With #ifeq: it's directly possible to identify undefined parameters:
- {{ #if: {{{x| }}}|not blank|blank}} = blank,
- {{ #ifeq: {{{x| }}}| |blank|not blank}} = blank,
- {{ #ifeq: {{{x| }}}|{{{x|u}}}|defined|undefined}} = undefined.
- An undefined parameter without default counts in the comparison as a string consisting of the tag:
- {{ #ifeq: {{{x}}}|{{ concat|{|{|{x}|}|} }}|1|0}} = 1.
#ifexist:
ifexist returns one of two results based on whether or not a named title exists, e.g.
{{#ifexist:Foo|Bar|RFC 3092}}
gives RFC 3092, because Foo doesn't exist.{{#ifexist:ParserFunctions|Thanks|No}}
gives Thanks.{{#ifexist:m:Help:Calculation|Yes|Oops}}
gives Oops although m:Help:Calculation exists, because of the interwiki prefix.
The first parameter is the title to check for, the second is the positive result, and the third, the negative result. If the parameter passed does not produce a valid title object, then the result is negative.
Template:Exists gives the same result, except that the result is positive for an interwiki link. Template:If interwiki link exploits this difference.
#ifexpr:
ifexpr evaluates a mathematical expression and returns one of two strings depending on the result.
{{#ifexpr: <expression> | <then text> | <else text> }}
If the expression evaluates to zero, then the else text is returned, otherwise the then text is returned. Expression syntax is the same as for expr.
- At the moment the else text is also returned for an empty expression:
{{#ifexpr: {{ns:0}}|Toast|'''or else'''}}
gives or else- Omitting both then text and else text gives no output except possibly an error message; this can be used to check the correctness of an expression, or to check the wording of the error message (emulated assertions, forced errors):
{{#ifexpr: 1/{{#ifeq: {{ns:4}}|Meta|1|0}}}}
{{#ifexpr: 1/{{#ifeq: {{ns:4}}|Meta|0|1}}}}
Expression error: Division by zero{{#if:{{#ifexpr: 1=2}}|wrong|correct}}
correct{{#if:{{#ifexpr: 1E2}}|wrong|correct}}
correct{{#if:{{#ifexpr: 1/0}}|wrong|correct}}
wrong{{#if:{{#ifexpr: a=b}}|wrong|correct}}
wrong
For an application, see also Template:Evalns.
#switch:
switch
compares a single value against multiple others, returning a string if a match is found. The syntax is basically:
{{ #switch: <comparison value> | <value1> = <result1> | <value2> = <result2> | ... | <valuen> = <resultn> | <default result> }}
switch
will search through each value passed until a match is found with the comparison value. When found, the result for that value is returned (the text string after the equal sign). If no match is found, but the last item has no equal sign in it, it will be returned as the default result. If your default result must have an equal sign, you may use #default
:
{{ #switch: <comparison value> | <value> = <result> | #default = <default result> }}
Note that it's also possible to have "fall through" for values (reducing the need to duplicate results). For example:
{{ #switch: <comparison value> | <value1> | <value2> | <value3> = <result3> | ... | <valuen> = <resultn> | <default result> }}
Note how value1 and value2 contain no equal sign. If they're matched, they are given the result for value3 (that is, whatever is in result3).
- As for #ifeq: the comparison is numerical where possible:
- {{ #switch: +07 | 7 = Yes | 007 = Bond | No }} gives Yes
- {{ #switch:"+07"|"7"= Yes |"007"= Bond | No }} gives No
- The matched value can be empty, therefore the following constructs are equivalent:
- {{ #if: {{ns:0}} | not empty | empty }} gives empty
- {{ #switch:{{ns:0}}|=empty|not empty }} gives empty
#if fix in 1.6.X
A simple fix for #if -
Replace:
if ($test !== '') {
on line 47 with:
if ($test) {
That's it. Fix by Andrew Tutt. http://www.duiki.com
Caveats
Most observed problems turned out to be general issues not limited to parser functions.
Substitution
Applying "subst:" to a ParserFunction works, provided that there is no space between "subst:" and "#". For details see Help:Substitution. Note that unless a technique like optional substitution is used, substituting a template which uses a ParserFunction does not replace that ParserFunction with its result. This is often undesirable.
Like other colon functions the parser functions are affected by 5678 in a predictable way. Summary, undefined parameters can be overwritten by corresponding parameters, for details see /5678 and Substitution. Substitution is the only case where this is critical wrt to parameter defaults. It doesn't affect defined parameters.
Tables
Currently wiki pipe table syntax doesn't work inside conditionals, there are two main workarounds.
- Hide the pipe from parser functions by putting it in a template, e.g. Template:! as on w:en:.
- Use html style table syntax instead.
- See also Help:Table, completely empty rows or columns are not displayed. Empty cells could be also transformed into dummy cells on pages not affected by 5569.
Note that "|" and "=" were always tricky within templates, this is no new issue.
Expressions
- div is no integer division and (as is) redundant, use / (slash) for real divisions.
- mod uses PHP's % operator, which is different from modulo-operators in all other programming languages, see also Template:Mod and 6068.
- mod sometimes returns wrong results for the same input values, see 6356 and /MOD10000. Update: values less than 1E+12 are apparently not affected.
- Valid #expr: results like 1.0E-7 are not yet supported as #expr: input:
- {{#expr:1.0E-7}} yields 1.0E-7.
- Under certain conditions round 0 results in -0 instead of 0. For an expression x using 0+(x) fixes this oddity.
Installation
Download both of these files and put them in a new directory called ParserFunctions in your extensions directory.
Then put the following at the end of your LocalSettings.php:
require_once( "$IP/extensions/ParserFunctions/ParserFunctions.php" );
You can also browse the code tree here:
1.7alpha
The ParserFunctions, including #if, work under 1.7alpha, assuming you're not using an extremely early build.
1.6
Most ParserFunctions (except #if, which does not work at all) work just as well on MediaWiki 1.6, but the syntax of ParserFunctions is without the '#' character. If you want to use the '#' character, find this section of ParserFunctions.php:
$wgParser->setFunctionHook( 'expr', array( &$wgExtParserFunctions, 'expr' ) ); $wgParser->setFunctionHook( 'if', array( &$wgExtParserFunctions, 'ifHook' ) ); $wgParser->setFunctionHook( 'ifeq', array( &$wgExtParserFunctions, 'ifeq' ) ); $wgParser->setFunctionHook( 'ifexpr', array( &$wgExtParserFunctions, 'ifexpr' ) ); $wgParser->setFunctionHook( 'switch', array( &$wgExtParserFunctions, 'switchHook' ) ); $wgParser->setFunctionHook( 'ifexist', array( &$wgExtParserFunctions, 'ifexist' ) );
Then, replace it with this:
$wgParser->setFunctionHook( '#expr', array( &$wgExtParserFunctions, 'expr' ) ); $wgParser->setFunctionHook( '#if', array( &$wgExtParserFunctions, 'ifHook' ) ); $wgParser->setFunctionHook( '#ifeq', array( &$wgExtParserFunctions, 'ifeq' ) ); $wgParser->setFunctionHook( '#ifexpr', array( &$wgExtParserFunctions, 'ifexpr' ) ); $wgParser->setFunctionHook( '#switch', array( &$wgExtParserFunctions, 'switchHook' ) ); $wgParser->setFunctionHook( '#ifexist', array( &$wgExtParserFunctions, 'ifexist' ) );
See also
- Help:Calculation
- Help:Magic words
- StringFunctions
- DynamicFunctions
- m:Category:Templates using ParserFunctions
- en:Category:Templates using ParserFunctions