1

This question pertains to the inconsistent behaviour of Mediawiki syntax and its templates and parser functions.

When placed directly in a wiki page, both these snippets work as expected:

{{#ifeq:{{{1|}}}|{{{2}}}|{{{1}}}}}

and

{{#ifeq:{{{1|{{FULLPAGENAME}}}}}|{{NAMESPACE:{{{1|{{FULLPAGENAME}}}}}}}:{{ROOTPAGENAME:{{{1|{{FULLPAGENAME}}}}}}}|{{{1|{{FULLPAGENAME}}}}}}}

The first snippet is simple. The second outputs the full name of the page, but only if it is not a sub-page.

However, when each snippet is placed in a template, let's say the first in Template:IsEqual, and the second in Template:IsRootPage, both of which are then included in some page, such as the output of each is the subject of an #if test, the first continues to behave as expected, while the second always yields the string not empty section of the #if statement.

So:

{{#if:{{IsEqual|A|B}}|equal|unequal}} will output "unequal"

while:

{{#if:{{IsRootPage}}|root|non-root}} will always output "root", even if placed in a subpage or if the name of a non-top-page is passed as a parameter to Template:IsRootPage, driving the template to return nothing.

According to the documentation of #if:

This function evaluates a test string and determines whether or not it is empty. A test string containing only white space is considered to be empty.

Moreover, the inconsistent behaviour persists for Template:IsRootPage even when it is amended to return an empty string when the #ifeq test fails, by introducing a fourth empty parameter to the #ifeq in the template, i.e by adding a third | followed by nothing/space.

It seems that something happens in between the output of the template and the #if parser function.

Is there a known explanation for this discrepancy in behaviour?

Is there some syntax hack to make it work as expected?

Or am I missing something that's otherwise obvious?


Edit: This behaviour is witnessed in Mediawiki 1.35.5

A. Gh.
  • 13
  • 6
  • I am unable to reproduce the issue. Did you test it on a page in a [namespace with subpages enabled](https://www.mediawiki.org/wiki/Manual:$wgNamespacesWithSubpages)? – user15517071 Apr 03 '22 at 07:08
  • Of course, I have tied. This is how the issue came to my attention. But do you mean that the template IsRootPage, when passed as the first parameter to an #if function, returns false, when the page is not at the top level? – A. Gh. Apr 03 '22 at 15:51
  • See: a test page: https://en.wikipedia.org/wiki/User:%D8%A3%D8%AD%D9%85%D8%AF/subpage1 And the template: https://en.wikipedia.org/wiki/User:%D8%A3%D8%AD%D9%85%D8%AF/template1 Subpages are enabled for the User namespace in English Wikipedia. – A. Gh. Apr 03 '22 at 16:06
  • Moreover, if you pass to the template the name of a subpage, regardless of where the code itself is, it should work as expected. – A. Gh. Apr 03 '22 at 16:09
  • `{{#ifeq:{{{1}}}|{{{2}}}|{{{1}}}}}` will always evaluate to showing whatever `{{{2}}}` is because `{{{1}}}` will always have something unless you pass an empty first arg. – Mark A. Hershberger Apr 03 '22 at 17:59
  • I understand this, @MarkA.Hershberger, and I intentionally made it so, because this template has no meaning if no parameter is passed in parameter 1. In all cases, this is besides the point, as the code snippet you refer to is just intended as a control sample where parameter 1 will always be passed. The issue I'm trying to demonstrate is manifested with the other template specifically. But I will edit the question and change it to avoid the confusion. – A. Gh. Apr 03 '22 at 19:07

1 Answers1

1

Your sample template does not return an empty string when the page has sub pages. It returns <nowiki/> for which {{#if:<nowiki/>|yes|no}} properly returns yes.

If you remove the <nowiki/>, you'll get the behavior you want.

  • Thanks! That seems to have been it. Even though I remember having added it in order to mitigate extra new lines output by templates. But don't you think this still points at an issue somewhere, since shouldn't leave traces behind? – A. Gh. Apr 03 '22 at 20:00
  • During the final parsing stage, when wikitext is generating html, it won't leave anything behind. Before then, templates are just substituting their parameters. (Although, someone more knowledgeable about the parser than I am could probably give you a better explanation.) – Mark A. Hershberger Apr 03 '22 at 20:33