Page MenuHomePhabricator

Add parser function #ifnumber or similar
Closed, DeclinedPublic

Description

There is much demand in templates to check if a given parameter is a number that can be used for calculations, cf. [[:de:Vorlage:IstZahl]]. Most template-only solutions have one or more weaknesses, and in the end, it should not be very performant either to do this check with templates.

So I propose a function #ifnumber that tests whether its parameter is a number. Even more useful would be a bunch of shortcuts to this new function and #expr tests: #ifinteger, #ifposnum, #ifposint, etc.

Version: unspecified
Severity: enhancement
URL: http://de.wikipedia.org/wiki/Vorlage:IstZahl

Details

Reference
bz14034

Event Timeline

bzimport raised the priority of this task from to Low.Nov 21 2014, 10:10 PM
bzimport added a project: ParserFunctions.
bzimport set Reference to bz14034.
bzimport added a subscriber: Unknown Object (MLST).

I'm thinking a line of functions:

{{#ifnumber:}} - Would output the input if true, otherwise output nothing.
{{#ifnumber:|true|false}} - Would output true if true, otherwise false.

#ifinteger: would work in the same way as #ifnumber.

But rather than the if???int, I think an #ifpositive and #ifnegative, which work in the same way as the previous.

herd wrote:

Just a note: you can use something like {{#iferror:{{#expr:{{{1|}}} }}|not number|number}} for ifnumber.

And the rest can be done with clever use of #ifeq, #ifexpr, and #expr, for example, ifinteger might be:

{{#ifeq:1|{{#expr:({{{1}}} round 0) / {{{1}}}}}|true|false}}

There are three considerations to be taken into account:

  1. {{#iferror:{{#expr:{{{1|}}} }}| ... will not only accept numbers, but (any) expression. So, for example, if an editor has to state a (single) height in meters and he enters "100-150" (note the hyphen), your test will succeed but a subsequent conversion to feet will show very interesting results :-). While there may be use cases for editor provided expressions, most times they are simply errors. Your "{{#ifeq:1|{{#expr:({{{1}}} round 0) / {{{1}}}}}|" seems to be more stable, but I have not tested it thoroughly.
  2. The semantics of "{{#iferror:{{#expr:{{{1|}}} }}|" are very hard to understand especially if you are new to template programming and/or it is embedded somewhere in other "{{#if:"s. This is a parser functions problem at large as well.
  3. I haven't run any tests, but I think that a PHP implemented #ifnumber will ease the server load compared to the myriad of template-only solutions.

herd wrote:

  1. well, any valid expression is also a number of a sort(in that it expresses a number). That seems good enough... ish.
  1. True enough, but they can be made into templates (that don't have to return 'true' and 'false' necessarily).
  1. Probably, but there are many tricky implementations of parserfunctions that could be simplified, and yet aren't, because the demand for them is so low.

Have a look at and feel free to edit http://www.mediawiki.org/wiki/User:Tim.Landscheidt/Template:IsNumber. If you can provide a template with no shortcomings, this bug can be closed as far as I'm concerned. But I see no way to solve the problem with templates alone.

Created attachment 4900
Patch for #ifnumber against revision 34765.

Patch against revision 34765.

Attached:

audreyt wrote:

Hi Tim, thank you for the patch!

As you may already know, MediaWiki is currently revamping its PHP-based parser
into a "Parsoid" prototype component, to support the rich-text Visual Editor
project:

https://www.mediawiki.org/wiki/Parsoid
https://www.mediawiki.org/wiki/Visual_editor

Folks interested in enhancing the parser's capabilities are very much welcome
to join the Parsoid project, and contribute patches as Git branches:

https://www.mediawiki.org/wiki/Git/Tutorial#How_to_submit_a_patch

Compared to .diff attachments in Bugzilla tickets, Git branches are much easier
for us to review, refine and merge features together.

Each change set has a distinct URL generated by the "git review" tool, which
can be referenced in Bugzilla by pasting its gerrit.wikimedia.org URL as a
comment.

If you run into any issues with the patch process, please feel free to ask on
irc.freenode.net #wikimedia-dev and the wikitext-l mailing list. Thank you!

As the original intent of the function was to check arguments in complex templates and this is/will easily be possible with Lua (http://test2.wikipedia.org/wiki/Module:IsNumber):

local p = {}
function p.IsNumber(frame)
return not(tonumber(frame.args [1]) == nil)
end
return p

I'm closing this bug as WONTFIX.