Extension talk:Variables

You can use this board to submit questions and requests concerning the Variables extension. If you want to participate in development, please pick a task on Phabricator or create a new one,

Archive

{{#vardefineecho:n|Expression error: Unrecognized punctuation character "{".}}

The following discussion is closed. Please do not modify it. Subsequent comments should be made on the appropriate discussion page. No further edits should be made to this discussion.


I recently advised someone to use this in a template but I was wrong: multiple instances of such a template will print a series of 1, 1, 1, 1, etc., while use of the same syntax without a template will give you a series of 1, 2, 3, 4, etc. Is this intended behaviour? Cavila 13:26, 24 November 2015 (UTC)

If you repeatedly call a template without parameters, it will reuse the output of the first call. To make sure the template is evaluated each time you need to add an empty parameter: {{template|}}. 92.21.226.2 (talk) 21:19, 20 December 2015 (UTC)
Great, that should do it - thanks for this! Cavila 21:39, 20 December 2015 (UTC)
Ouch! This makes using variables and templates much more difficult. Is there some way to bypass the cache behavior so the template is expanded every time? DavidBiesack (talk) 20:04, 17 June 2016 (UTC)
When I last worked on the extension this didn't seem possible if I remember correctly. Or it was more effort than I thought was justified to solve this.
Just call the template with an empty parameter as pointed out above. Danwe (talk) 13:43, 10 July 2016 (UTC)
This is tracked in T191574 and hopefully fixed soon. MGChecker (talk) 13:44, 6 April 2018 (UTC)
So theres still no easy way to increment the variable to count anything? Gunnar.offel (talk) 08:32, 23 June 2020 (UTC)
There is. This is resolved in Variables 2.4+, which has been released in 2018. MGChecker (talk) 12:31, 23 June 2020 (UTC)
The discussion above is closed. Please do not modify it. No further edits should be made to this discussion.

value starting/finished with spaces

The following discussion is closed. Please do not modify it. Subsequent comments should be made on the appropriate discussion page. No further edits should be made to this discussion.


I'd like to use text values on my page like this:

" any text with preceding spaces"

how can i define it? 145.236.252.35 (talk) 08:42, 28 June 2016 (UTC)

{{#vardefine:text| any text with preceding spaces}} Ioannis Protonotarios 11:32, 26 January 2017 (UTC)
This works… unless you want to put the result of a model inside a {{#tag:syntaxhighlight}} template where the   tag will show (which is the problem, I am having right now…)
To me, there should be a "trim" parameter to the #vardefine template :
  • {{#vardefine:text| text }}/{{#vardefine:text| text |true}}/{{#vardefine:text| text |trim}}
    → stores “text”
  • {{#vardefine:text| text |false}}
    → stores “  text  
Or if the problem comes from the way mediawiki handles template :
  • {{#vardefine:text| my text is "my text" }}
    → stores “my text is "my text"”
  • {{#vardefine:text| ""my text is my text"" }}
    → stores “"my text is my text"”
  • {{#vardefine:text| " my text is "my text" " }}
    → stores “  my text is "my text"  
  • {{#vardefine:text|" my text is "my text" "}}
    → stores “  my text is "my text"  
Or a combination of the above (parameter allowing the use of “"” to delimit text that would otherwise be trimmed). Loizbec (talk) 12:46, 27 February 2020 (UTC)
You can do this by using <nowiki /> tags:
{{#vardefine:text|<nowiki /> any text with preceding spaces}} MGChecker (talk) 12:34, 23 June 2020 (UTC)
The discussion above is closed. Please do not modify it. No further edits should be made to this discussion.

variable value containing extension code

I'd like to define a variable conataining the string <sql2wiki> which is actually an installed extension.
This way the sql2wiki is executed instead of becoming the value of my variable.
PREF1 >{{#vardefineecho:PREF1|<sql2wiki database="db1">}}
how can I escape the excetution of <sql2wiki> 145.236.252.35 (talk) 08:55, 28 June 2016 (UTC)
<nowiki><sql2wiki></nowiki> Ioannis Protonotarios 11:36, 26 January 2017 (UTC)

Is this extension enabled on this site as well as Wikipedia?

The following discussion is closed. Please do not modify it. Subsequent comments should be made on the appropriate discussion page. No further edits should be made to this discussion.


Just asking. I was testing it on this page. Yhynerson1 (talk) 09:27, 18 February 2017 (UTC)

No, see in Special:Version. wargo (talk) 10:59, 18 February 2017 (UTC)
The discussion above is closed. Please do not modify it. No further edits should be made to this discussion.

Can you query the contents of these variables for a given page id using the API?

The following discussion is closed. Please do not modify it. Subsequent comments should be made on the appropriate discussion page. No further edits should be made to this discussion.


Sample page using variables: http://tales-of-link.wikia.com/wiki/%28Artes_Wand_User%29_Agria 174.97.28.4 (talk) 16:07, 6 August 2017 (UTC)

That's not supported by the extension. You'd need to write a new extension on top of Variables to achieve this but it's not impossible. Danwe (talk) 10:14, 1 January 2018 (UTC)
It's an interesting thought to save the distinct var_final values in page_props to make it queryable… Maybe I will put some effort in implementing this additional functionality at some point in the future. MGChecker (talk) 22:30, 15 February 2018 (UTC)
This is tracked in T191575. MGChecker (talk) 13:43, 6 April 2018 (UTC)
The discussion above is closed. Please do not modify it. No further edits should be made to this discussion.

Works with MW 1.30.X ?

The following discussion is closed. Please do not modify it. Subsequent comments should be made on the appropriate discussion page. No further edits should be made to this discussion.


can someone say if this extension works with MW1.30.x? Yukii (talk) 18:27, 11 January 2018 (UTC)

It does, yes. ディノ千?!☎ Dinoguy1000 19:07, 11 January 2018 (UTC)
The discussion above is closed. Please do not modify it. No further edits should be made to this discussion.

Distribution missing "extension.json"

The following discussion is closed. Please do not modify it. Subsequent comments should be made on the appropriate discussion page. No further edits should be made to this discussion.


The bundle available for MW 1.30 via the ExtensionDistributor, i.e REL1_30-c75b9d4, is missing the file "extension.json", causing installation through wfLoadExtension( 'Variables' ); to fail. Ahmad Gharbeia أحمد غربية (talk) 12:59, 17 April 2018 (UTC)

The docu states: "To users running MediaWiki 1.30 or earlier", so for MW 1.30 this extension has to be invoked via require_once .... [[kgh]] (talk) 16:14, 17 April 2018 (UTC)
The discussion above is closed. Please do not modify it. No further edits should be made to this discussion.

How to embed curly braces and quote-marks in variable?

This is breaking my template. How to fix? I want the bolded part to be rendered verbatim with #var, but some characters (probably the curly braces) are getting interpreted as active wikitext characters, instead of just loaded into the variable.

{{#vardefineecho:HtmlLink|<htmltag tagname="a" href="/index.php/Portal:Tag?Tag={{{1}}}">}}

elsewhere:

{{#var:HtmlLink}}

Johnywhy (talk) 10:32, 2 June 2018 (UTC)

The first thing I would try is surrounding the value with a <nowiki></nowiki> tag. ディノ千?!☎ Dinoguy1000 14:26, 2 June 2018 (UTC)
Seems better, but still not rendering as expected.
the following:
{{#vardefineecho:HtmlLink|nowiki <htmltag tagname="a" href="/index.php/Portal:Tag?Tag={{{1}}}"> /nowiki}}
(note, the nowiki tags above ARE surrounded with < and >, but if i include them here then you cannot see the nowiki tags here)
renders, on the template page, as:
<htmltag tagname="a" href="/index.php/Portal:Tag?Tag={{{1}}}">
which is the desired wikitext.
Next, that output should then get parsed as normal wikitext in the template, where #var appears.
But, output of #var is not getting parsed as wikitext in the template, it's just getting rendered on the host-page verbatim, as if it's not wikitext.
It seems the nowiki tag in the #vardefine prevents the output of #var from getting parsed as wikitext.
Any ideas? Johnywhy (talk) 08:19, 3 June 2018 (UTC)
I don't think I understand what you're trying to do. What is the problem you're trying to solve here? ディノ千?!☎ Dinoguy1000 15:38, 3 June 2018 (UTC)
i'm re-using a chunk of complex wikitext repeatedly in a Template. So i want to apply that wikitext to a variable for easy re-use, and easy revising.
Up to now, i tried to store the raw wikitext in the variable. But then, as reported above, the wikitext does not get parsed when retrieved with #var.
Next, i'm going to try parsing the wikitext before i store it in the variable-- if i can figure out how. Tips welcomed. Johnywhy (talk) 15:46, 3 June 2018 (UTC)
Is there a reason you don't use a subtemplate or metatemplate instead? It sounds like you're trying to use variables as a function of sorts, and that doesn't work; the content of a variable is parsed before it's stored in the variable. ディノ千?!☎ Dinoguy1000 17:15, 3 June 2018 (UTC)
As explain in the OP above, it seems the variable can't be parsed correctly, because curlies in the value confuse your extension. That's why had to wrap it in nowiki.
Thx for telling me about subtemplate and metatemplate, i'll have to learn about that! But i get the general idea that i can use a template for re-used wikitext. Great idea. Johnywhy (talk) 17:22, 3 June 2018 (UTC)

How to transclude invisible text?

The following discussion is closed. Please do not modify it. Subsequent comments should be made on the appropriate discussion page. No further edits should be made to this discussion.


If template contains:
<nowiki><!-- this is a comment --></nowiki>
then the whole thing gets rendered as visible text on the host page. If template contains:
<!-- this is a comment -->
then none of it gets transcluded.

Really just want to put some invisible text on the host page-- doesn't have to be an HTML-comment.

This doesn't get transcluded either:
<span style="display:none">Hidden Span</span>
Johnywhy (talk) 17:27, 3 June 2018 (UTC)
The discussion above is closed. Please do not modify it. No further edits should be made to this discussion.

Is there a limit to the quantity of variables you have on one page?

The following discussion is closed. Please do not modify it. Subsequent comments should be made on the appropriate discussion page. No further edits should be made to this discussion.


I've never encountered a problem before when using many variables, but a thought came to me; How many variables can I have at once on a single page?

As an example, if I were to use, say, 100 variables one page for whatever reason, would I be able to do that without encountering a problem?

Asking out of curiosity. Diriector Doc (talk) 00:42, 14 August 2018 (UTC)

There shouldn't be any issue with a large number of variables; I've used well over a hundred variables in a template without issue. ディノ千?!☎ Dinoguy1000 00:54, 14 August 2018 (UTC)
The discussion above is closed. Please do not modify it. No further edits should be made to this discussion.

Calling #var on a different template?

The following discussion is closed. Please do not modify it. Subsequent comments should be made on the appropriate discussion page. No further edits should be made to this discussion.


I'm pretty new to this so it's still confusing to me.

So I have template1 and template 2. Template 1 does usd=12345, template 2 on the other hand has prize=5000. Is it possible to do match between these two tho they're on two different templates? I was trying the following but can't make it work and I'm stuck.

{{#vardefine:{{{result}}}|{{#expr:{{#var:prize}}*{{#var:usd}}}}}} 119.92.13.245 (talk) 21:25, 20 August 2018 (UTC)

If the current template designs allow for it, I would recommend explicitly passing the needed values as template parameters, instead of relying on Variables. Not only does this bypass the problems you're facing now, but it also avoids a whole host of maintenance and code comprehension issues into the future. ディノ千?!☎ Dinoguy1000 13:25, 21 August 2018 (UTC)
The discussion above is closed. Please do not modify it. No further edits should be made to this discussion.

If I define a variable in a template and call the template, is the scope of that variable limited to the template that created it or does the scope extend to the page that called the template?

Broad question: On one page, I call TemplateA and TemplateB. TemplateA defines the variable foo = bar. Can I pass foo as a paramater to TemplateB?


Narrower question: On one page, I call TemplateA multiple times and want to increment a counter every time I call the template. Is that possible?


Specific use case: Users encounter too many acronyms in our wiki. Should we require editors to spell out all acronyms, this would make pages difficult to read. The best practice in any document is to spell out the acronym the first time, place the acronym in parentheses, and then use the acronym for the remainder of the document. Example: "The World Wide Web Consortium (W3C) is the main international standards organization for the World Wide Web. The W3C has 467 members."

I would like to create template pages for common acronyms and direct editors to call those templates whenever they would use the acronym. The template would check whether it had been called on a page already.If so, the template would return the short form. If not, the template would return the long form. Example page source: "<nowiki>The {{W3C}} is the main international standards organization for the World Wide Web. The {{W3C}} has 467 members.</nowiki>"


I'm open to better ways of handling the specific use case. Thank you. 165.225.0.113 (talk) 20:09, 28 May 2019 (UTC)

Broad question: I believe this is possible.
Narrower question: Have a look at the NumerAlpha extension for this.
I guess it is a matter of testing in the end. [[kgh]] (talk) 21:05, 28 May 2019 (UTC)
> Narrower question: On one page, I call TemplateA multiple times and want to increment a counter every time I call the template. Is that possible?
Yes, but because of unwanted template caching issues it was not working in all cases before Variables 2.4. For your specific use case, I would use something like {{#varexists:acr-w3c|Woorld Wide Web Consortium|W3C}}{{#vardefine:acr-w3c|1}}, probably with a generic acr template in the background: {{#varexists:acr-{{{1}}}|{{{2}}}|{{{1}}}}}{{#vardefine:acr-{{{1}}}|1}} MGChecker (talk) 22:03, 28 May 2019 (UTC)

Calling variables

The following discussion is closed. Please do not modify it. Subsequent comments should be made on the appropriate discussion page. No further edits should be made to this discussion.


Simple question, but the article did not answer it to me: if I have defined x as {{#vardefine:x|{{#expr:2*{{#var:a}}+{{#var:b}}}}}} like here – how can I call x?

Thanks – Ciciban (talk) 06:56, 15 September 2019 (UTC)

{{ #var: x }} ディノ千?!☎ Dinoguy1000 09:11, 15 September 2019 (UTC)
The discussion above is closed. Please do not modify it. No further edits should be made to this discussion.

Allowing more than 1000 caracters in Variables

The following discussion is closed. Please do not modify it. Subsequent comments should be made on the appropriate discussion page. No further edits should be made to this discussion.


I encounter bugs when I seek to allocate a variable of more than 1000 characters. The error I get is (in french) :

<strong class="error">Erreur : la chaîne dépasse la limite maximale de 1 000 caractères.</strong>

meaning the string exceedes the maximal size of one thousand characters.

Yet I did not find any configuration parameter to allow for more than one thousand characters (I probably need 5000 or possibly more).

Any idea how I could fix this ?

Frederic.

for web site dufal.fr

e.g. you'll find the problem in page dufal.fr/index.php/Sp%C3%A9cial:Parcourir/:F.AGSO.S0001 2A01:CB15:258:1A00:3C42:E043:373C:AD79 (talk) 21:35, 8 August 2020 (UTC)

This is confusing, as Variables have no such limit. Are you combining them with the Loops extension, by chance?
Can you reproduce this on a normal page, given that this case is kind of weird? I do not know how this special page comes together. MGChecker (talk) 21:42, 8 August 2020 (UTC)
Are you using StringFunctions as well? These have a length limit of 1000 characters for strings. (I tried checking myself, but I don't speak French so I had a hard time telling what was going on.) ディノ千?!☎ Dinoguy1000 00:07, 9 August 2020 (UTC)
Thanks for your prompt help. I'm using ParsingFunctions, Variables, and MyVariables (loaded in this order).
By adding the line
$wgPFStringLengthLimit=9996;
at the end of my LocalSettings.php rather than just after the loading of ParsingFunctions, it solved my problem. (I'm not sure I fully understand why).
Many thanks again.
Frederic. 2A01:CB15:258:1A00:74BC:4610:8CC4:F7CE (talk) 16:15, 9 August 2020 (UTC)
That means you're using StringFunctions somewhere in the templates that page uses. The limit you increased is important for preventing denial-of-service attacks against your wiki, and the better solution here is to redesign your templates so they're not trying to pass extremely long strings through StringFunctions. ディノ千?!☎ Dinoguy1000 10:19, 10 August 2020 (UTC)
It would probably be better (more efficient and more versatile) to use Scribunto for string processing, instead of StringFunctions. MGChecker (talk) 11:54, 10 August 2020 (UTC)
The discussion above is closed. Please do not modify it. No further edits should be made to this discussion.

how to undefine a previously-defined variable?

The following discussion is closed. Please do not modify it. Subsequent comments should be made on the appropriate discussion page. No further edits should be made to this discussion.


I have a template whose behavior changes based on the existence of a variable. It uses {{#varexists...}} for this purpose. I'd like to be able to define/undefine this variable multiple times, e.g.

#vardefine:debug_flag}}
{{My Template}}
{{#varundef:debug_flag}}
{{My Template}}

But currently there is no way that I know of to undefine a variable after defining it. --~ 2600:1700:1B0:BA0:A82F:CAEB:C7F7:8FAF (talk) 02:18, 9 October 2020 (UTC)

To be honest, I've never had any use for #varexists; instead, I just define variables with a dummy value (e.g. {{#vardefine: foo | 1 }}) and then test if the variable has a value with an #if. That way, if I need to later undefine the variable, I can simply clear its value via {{#vardefine: foo }}. ディノ千?!☎ Dinoguy1000 02:28, 9 October 2020 (UTC)
It is a feature that this is not possible, thus a definition of a variable can never be done.
Instead, I recommend Dinoguys approach of checking if a variable is empty. --MGC 194.230.155.242 (talk) 05:40, 9 October 2020 (UTC)
What do you mean by "it is a feature"? What is the advantage of {{#varundef}} not existing? I guess I will have to do it the way Dinoguy1000 suggested. Equivalently, no one ever needs to use {{#varexists}} -- they can always use {{#if}}. But then, why does {{#varexists}} exist? 2600:1700:1B0:BA0:988E:1F3E:9575:814C (talk) 08:13, 9 October 2020 (UTC)
I would have to check this, but at a glance I'd guess that {{#varexists: foo | ... }} is very slightly more efficient than {{ #if: {{ #var: foo }} | ... }} (at the very least, it does make the intention of the code clearer). I wouldn't say to avoid using #varexists entirely, but to use it only for variables that are fully static flags (i.e. the variable is defined - or not - in only one place in the template, and it isn't touched afterwards except to check for it). Certainly, now that I've taken a closer look and thought about it a bit, I'll be going over my own templates and using it where appropriate (after testing to see how it stacks up to #if: #var:, in terms of template limits, at least). ディノ千?!☎ Dinoguy1000 16:48, 9 October 2020 (UTC)
I wouldn't recommend you change all of your templates for an infinitesimal efficiency improvement. You will be giving up the option of doing what I mentioned in my initial post. If you have a thousand references to your template on a page, and you only want the variable to be defined for the first half of them (maybe you're doing a binary search), you won't be able to do it. 2600:1700:1B0:BA0:2D95:883D:C0D7:C5DC (talk) 21:59, 9 October 2020 (UTC)
I'm keenly aware of the gotcha's here, believe me. The changeover would be to benefit code readability (and thus maintainability); any potential efficiency improvement (even an infinitesimal one) would just be a nice bonus. ディノ千?!☎ Dinoguy1000 22:09, 9 October 2020 (UTC)
Revisiting this now that I've looked a bit more into it: careful use of #varexists can save a significant amount on the NewPP parser report metrics (in particular "Preprocessor visited node count", "Post-expand include size", and "Template argument size"), over my go-to {{ #if: {{ #var: construction.
For example, this version of "Template:Chapter" on my wiki (the current version as of writing this, but it won't be for long), as used on the current version of the page "Dark Yugi (manga)" (where the template is transcluded 338 times), results in the following NewPP report:
NewPP limit report
Cached time: 20210907163321
Cache expiry: 1209600
Dynamic content: false
[SMW] In‐text annotation parser time: 0.017 seconds
CPU time usage: 4.748 seconds
Real time usage: 8.358 seconds
Preprocessor visited node count: 64228/1000000
Preprocessor generated node count: 60933/1000000
Post‐expand include size: 418313/2097152 bytes
Template argument size: 35083/2097152 bytes
Highest expansion depth: 17/40
Expensive parser function count: 0/100
Unstrip recursion depth: 1/20
Unstrip post‐expand size: 112756/5000000 bytes
Lua time usage: 2.450/20.000 seconds
Lua virtual size: 14.27 MB/50 MB
Lua estimated memory usage: 0 bytes
However, changing the first line in the template:
<includeonly>{{ #vardefine: $smw | {{ #var: $smw | {{ #if: {{ #show: }} | off | on }} }}<!-- standard implementation -->
to
<includeonly>{{ #varexists: $smw || {{ #vardefine: $smw | {{ #if: {{ #show: }} || 1 }} }}<!-- standard implementation -->
and line 40 from
}}{{ #vardefine: $chapter-name | {{ #ifeq: {{ #var: $chapter-mode }} | number || {{ #ifeq: {{ #var: $smw }} | on | {{ #show: {{ #var: $chapter-pagename }} |?English name }} }} }}
to
}}{{ #vardefine: $chapter-name | {{ #ifeq: {{ #var: $chapter-mode }} | number || {{ #if: {{ #var: $smw }} | {{ #show: {{ #var: $chapter-pagename }} |?English name }} }} }}
...results in the following report:
NewPP limit report
Cached time: 20210907165826
Cache expiry: 1209600
Dynamic content: false
[SMW] In‐text annotation parser time: 0.015 seconds
CPU time usage: 4.776 seconds
Real time usage: 8.623 seconds
Preprocessor visited node count: 63678/1000000
Preprocessor generated node count: 60933/1000000
Post‐expand include size: 417637/2097152 bytes
Template argument size: 35083/2097152 bytes
Highest expansion depth: 17/40
Expensive parser function count: 0/100
Unstrip recursion depth: 1/20
Unstrip post‐expand size: 112756/5000000 bytes
Lua time usage: 2.570/20.000 seconds
Lua virtual size: 14.3 MB/50 MB
Lua estimated memory usage: 0 bytes
Note in particular that "Preprocessor visited node count" dropped from 64228 to 63678, and "Post-expand include size" dropped from 418313 to 417637.
However, as things stand, I am unable to use this method in more places in "Template:Chapter", since other variables I'd like to use it on are always defined, and communicate meaningful state if they are empty (there is no way around this, since subsequent transclusions may store nonempty values to these variables, and then empty values again, and so on). For example, changing line 28 from
}}{{ #vardefine: $chapter-subseries | {{ #if: {{ #var: $chapter-series }}
to
}}{{ #vardefine: $chapter-subseries | {{ #varexists: $chapter-series
...would result in "Preprocessor visited node count" dropping further to 60641, and "Post-expand include size" dropping to 371846, but would not work correctly in some cases of multiple transclusions of the template on the same page, as explained above. However, you don't even need to look past $smw to find suboptimal behavior: because it can be defined but empty, it must be tested on line 40 via {{ #if: {{ #var: $smw }} ..., instead of being able to just use {{ #varexists: $smw ...!
This could be fixed in one of two ways: add a function to undefine a variable entirely, or add a way to test for variable existence that considers empty-but-defined variables to not exist (whether by a modification of #varexists, or the introduction of a new parser function).
(I am, of course, aware of Scribunto and Lua - and we do use modules for a number of things on the wiki already - but we currently only have one editor who is able and willing to develop and maintain modules for us, and they don't have much time to spend on the wiki; I've tried learning myself, but could never get the hang of it.) ディノ千?!☎ Dinoguy1000 18:06, 7 September 2021 (UTC)
The discussion above is closed. Please do not modify it. No further edits should be made to this discussion.

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

The following discussion is closed. Please do not modify it. Subsequent comments should be made on the appropriate discussion page. No further edits should be made to this discussion.


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?

____ i wrote the ask allready in Help_talk:Extension:ParserFunctions but maybe this point is the better choice. Gunnar.offel (talk) 14:14, 22 October 2020 (UTC)

i tried also the given alternatives, but sadly they also doesn't work in my case. :S Gunnar.offel (talk) 14:16, 22 October 2020 (UTC)
Are you sure the refs variable is populated? If this is not the case, you get an empty string out of `refs`, matching your reports. MGChecker (talk) 14:27, 22 October 2020 (UTC)
At least
{{#vardefineecho: refs | Tree }}
{{#ifeq: {{#var: refs }} | Tree | 1 | 0 }}
works properly for me, on the contrary to the reports on the ParserFunctions. Sadly, I have no wiki with StringFunctions and Variables at hand at the moment, so I can not reproduce your claims. MGChecker (talk) 14:31, 22 October 2020 (UTC)
On my own wiki with ParserFunctions, StringFunctions, and Variables, I have never had any issue using the three together as described here. @Gunnar.offel: could you provide what versions of MediaWiki, Ext:ParserFunctions, and Ext:Variables your wiki has installed? ディノ千?!☎ Dinoguy1000 19:12, 22 October 2020 (UTC)
Actually, based on the name of the variable, is it meant to store references? Trying to work with <ref> tags with parser functions is difficult, because of the special parsing that takes place around them. If it's an option for you, you may have better luck using Scribunto/Lua for this instead. ディノ千?!☎ Dinoguy1000 19:15, 22 October 2020 (UTC)
@MGChecker, if i use vardefineecho and also if i call the #var it outputs the wanted things. So i'm pretty sure, that i populate the variable. i just can't get it to work with the parser function.
@Dinoguy1000: sure,
- MediaWiki 1.34.0
- ParserFunctions 1.6.0
- Variables 2.5.1 (d6ce860) 08:16, 4. Sep. 2019
Yes, the variable stores the references and it works, but at the moment not in the parser functions. actually i just want to hide an empty table and only show if content is available. That's a big gun on a little problem, sure but keeps me up for a better understanding how to use/solve the extension. Gunnar.offel (talk) 20:45, 22 October 2020 (UTC)
As Dinoguy said, this is properly a really hard problem. References are a really complicated feature, kind of using variables as internal counter as well, and being evaluated at a different time during parsing. This leads to many features of stock MediaWiki already to behave weird when used together with references – especially advanced syntax features offered by extensions.
What is properly happening here that the references are not populated at the (quite early) point during parsing when variables are evaluated in this logic. I do not know exactly what is happening, but trying to get the references at multiple points, at different parsing stages, is quite typical for problems related to references.
Hence, your references var is always empty for parser functions. On the other hand, variables not encapsulated into the template tree can properly evaluated properly. MGChecker (talk) 00:53, 23 October 2020 (UTC)
The discussion above is closed. Please do not modify it. No further edits should be made to this discussion.

Deprecated hook (InternalParseBeforeSanitize)

This extension needs an update for 1.35.

Use of InternalParseBeforeSanitize hook (used in VariablesHooks::onInternalParseBeforeSanitize) was deprecated in MediaWiki 1.35.

(used for '#var_final' parser function) Cavila 21:12, 5 March 2021 (UTC)

See T276627. MGChecker (talk) 00:19, 6 March 2021 (UTC)
Thanks. Personally, I can live without #var_final, but it's good to have things cleared up. Cavila 10:39, 6 March 2021 (UTC)
The thing is, apparently there are not even plans for hook removal. And complete Parsoid compatibility is out of reach anyway without a complete rewrite, which might very well be impossible and I won't do. MGChecker (talk) 18:56, 6 March 2021 (UTC)
So in the end the code for #var_final will be removed from the extension. This does make sense or is it better to do so once the hook was removed completely from MediaWiki core? I am assuming here that this parser function still works on MW 1.35.x though I may be wrong. [[kgh]] (talk) 16:37, 7 March 2021 (UTC)
The problem lays deeper.
According to the points of the parsing team, this hook might actually continue to work indefinitely even though the deprecation.
On the other hand, I do not feel like listening to these deprecations will help us in the long run, because the whole principle of this extension is uncertain in the context of Parsoid. MGChecker (talk) 13:21, 8 March 2021 (UTC)
Thanks for the information. Still I do not know if the parser function will work or not for 1.35. I guess this is the question people will ask in the short run.
Apparently in the long run the situation appears to not look to bright. [[kgh]] (talk) 19:15, 8 March 2021 (UTC)
It will work, but there will be deprecation notices on upgrade. MGChecker (talk) 01:43, 9 March 2021 (UTC)
It would be possible to make deprecation warnings disappear, I think, by migrating back to InternalParseBeforeLinks and sanitize inside this extension. However, the only reason InternalParseBeforeLinks is not deprecated yet, is its use inside SemanticMediaWiki, which is even more widely used than Variables. I could not achieve a similar exception for Variables, but might have got one if I would have been aware of this process earlier.
It stands to reason that the migration to InternalParseBeforeLinks would bring no fruit anyway, since it will probably be removed at the same point as InternalParseBeforeSanitize. I will try averting a removal of the hook from the old parser if possible, pointing out my discussions with the Parsing-Team regarding this point, but I can not promise anything. MGChecker (talk) 01:51, 9 March 2021 (UTC)
Thank you! I appreciate your effort! [[kgh]] (talk) 07:49, 9 March 2021 (UTC)
Thanks for looking into this! Cavila 09:32, 9 March 2021 (UTC)
We hope to have this fixed very soon, see
https://gerrit.wikimedia.org/r/c/mediawiki/extensions/Variables/+/504488/ MGChecker (talk) 18:08, 4 December 2023 (UTC)

Creating a variable using vardefine adds to extra space in wiki

The following discussion is closed. Please do not modify it. Subsequent comments should be made on the appropriate discussion page. No further edits should be made to this discussion.


I am using the variable extension to us define some variables which I use later in my wiki. But adding these variables adds extra undesirable space at the top of the wiki. Am I missing something? 84.46.53.105 (talk) 08:44, 5 September 2021 (UTC)

Did you use newlines? Avoid them, wrap a div around your vardefines with display:none, or comment out each line break (I just tried to demonstrate this with pre-tags, but MediaWiki messed up my code). Cavila 09:39, 5 September 2021 (UTC)
Cool! Thanks Cavila. display:none did the trick. :) 84.46.52.252 (talk) 10:35, 5 September 2021 (UTC)
Just to add one more option, my preferred method is to place the closing braces for each vardefine on the same line as the opening braces for the next vardefine, e.g.
{{ #vardefine: var1 | foo
}}{{ #vardefine: var2 | bar
}}{{ #vardefine: ...
ディノ千?!☎ Dinoguy1000 17:44, 5 September 2021 (UTC)
The discussion above is closed. Please do not modify it. No further edits should be made to this discussion.

About the worrying warning at the top of the page

Is there a place to vote against the incompatibility of future MW developments with this very useful extension? :-/

Personally, I would rather scuttle Parsoid than Arrays and Variables. ;-) Megajoule (talk) 11:58, 26 April 2022 (UTC)

That's right. I use this extension for a lot of functionality that MediaWiki doesn't have by default. My wiki has really the multilanguage content, I don't use Lua! All is generated by using Extension:Variables. I use it in templates i.e. Image, content, pages & etc. Want (talk) 14:52, 26 April 2022 (UTC)
It might be helpful to raise your concerns or award tokens to phab:T282499. It had already come to the attention of the Parsoid team that this might be worthwile, but as far as I know, there has not been a final decision yet.
Personally, I do not have the resources to keep up with the developments regarding Parsoid in more depth, so I am unable to tell you more. MGChecker (talk) 16:34, 26 April 2022 (UTC)
Thanks for trying. I confess that I have neither the desire nor the time to prove with concrete examples why it is pointless to insist on parallelised content processing at all costs. I understand the motivation of the Parsoid developers. At the same time, it would be enough to add a hook on which to hang the operations to be performed before Parsoid starts chewing wikicode. Want (talk) 18:48, 26 April 2022 (UTC)
A petition to enable this extension on a WMF wiki along with the description of our use case has been included in the Community Wishlist Survey 2023. Peter Bowman (talk) 00:17, 4 February 2023 (UTC)

Deprecated 'InternalParseBeforeSanitize' since 1.35

The following discussion is closed. Please do not modify it. Subsequent comments should be made on the appropriate discussion page. No further edits should be made to this discussion.


This extension does not work with the latest Mediawiki build. 149.167.172.126 (talk) 04:22, 11 September 2022 (UTC)

It works, it just yields deprecation warnings. More information is indicated on the front side. MGChecker (talk) 19:11, 11 September 2022 (UTC)
The discussion above is closed. Please do not modify it. No further edits should be made to this discussion.

InternalParseBeforeSanitize fix?

The following discussion is closed. Please do not modify it. Subsequent comments should be made on the appropriate discussion page. No further edits should be made to this discussion.


Hello

How can i resolve this problem?

PHP message: PHP Deprecated:  Use of InternalParseBeforeSanitize hook (used in VariablesHooks::onInternalParseBeforeSanitize) was deprecated in MediaWiki 1.35.


Thanks for any help Lokipr (talk) 14:33, 21 September 2022 (UTC)

See the various notices at the top of Extension:Variables, and in particular the various Phabricator tasks linked from them. Basically, this notice is known and expected breakage from recent changes to the parser in MediaWiki and the way the extension is currently written, and fixing it would require some more-or-less extensive rewrites of the extension. ディノ千?!☎ Dinoguy1000 18:47, 21 September 2022 (UTC)
The discussion above is closed. Please do not modify it. No further edits should be made to this discussion.

Variables are globally scoped across transclusions. YIKES!

First of all, thanks for the amazing extension. It's really invaluable when you use complex templates. However, we've encountered a head-splitting issue today that seems to be down to the scoping of variables and it has me quite worried.

Imagine the following wikitext:

{{#vardefine: foo | foo :) }}
{{FooBar}}
{{#var: foo }}

And the following definition for the FooBar template used therein:

{{#vardefine: foo | bar >:] }}

What do you suppose will appear on the page? Reading the wikitext, you would assume foo :) but in fact it will be bar >:].

From the sound of some topics on this discussion page, it seems like some people actually rely on this behavior, but it's all kinds of wrong. Variables should be "lexically scoped" (aka "statically scoped") meaning that the value of a variable should always be apparent from the surrounding source code, not from some remote code that you're calling!

Any wiki of sufficient complexity that uses variables is bound to hit some nasty, hard-to-debug problems with the current rules. I'm not sure how people have been managing so far.

It would be amazing if we could add a configuration option to enable lexical scoping of variables. I'm not sure if it would be easy to implement, but it would be invaluable. TaylanKammer (talk) 22:41, 31 August 2023 (UTC)

Given the way templates are evaluated, I would expect precisely that. Templates are often used to avoid endless repetition or to hide complexity that would otherwise distract, or both. Preprocessing and outputting variables can be one of the roles they serve.
Some people use a template for each table row, using variables to make intermediate calculations. Lots of creative uses out there based on the expectation that variables have global scope. Wikitext is not a programming language nor should it be. Not a bug but a feature.
That said, none of this may matter since the future of the extension is at stake because of Parsoid putting the focus on parallelised parsing. If it were to survive with more limited capabilities at all, I wouldn't be surprised if 'foo' in your use case will be unaffected by the Foobar template. Rand(1,2022) (talk) 08:04, 1 September 2023 (UTC)
If you need lexically scoped variables, I have an in-house extension similar to Variables that does just that. It works by setting values inside the template as though they'd been arguments that were passed to it. Because of this, the values are local to each template call. Once defined, they're indistinguishable from any other argument. Thus, {{#local:hello|world}}{{{hello}}} produces world.
That said, there are a few caveats to it as well, namely that it is an in-house extension and I'm it's sole maintainer currently. Further, it's integrated with (and extends some of) the low-level parser objects in MediaWiki. If those change significantly, the extension is toast. That said, Lua is founded on some of those same objects, so it seems unlikely they'd go away any time soon. Plus, of course, you'd have to convert everything over and have your template designers learn a whole new system. If you're interested, you can find documentation for the extension here. Robin Hood  (talk) 08:33, 1 September 2023 (UTC)
I'm also working on a local fork of the extension to see if I can implement this. (In part because I'm curious.) I'm attaching an array to each frame and saving variables therein, but the variables end up all being empty. Not sure what I'm doing wrong. Would be very interested to see your code. TaylanKammer (talk) 12:27, 1 September 2023 (UTC)
You can find my code here: https://github.com/robinhood70
MetaTemplate is the main library, and ParserHelper is a shared library for several of my extensions. Robin Hood  (talk) 13:49, 1 September 2023 (UTC)
This is a long-known issue; T207649 is the long-open request to add a scoping mechanism. My personal workaround is simply to prefix variable names: if a template defines a variable, I'll normally prefix that variable's name with some variant of the template's name (maybe the full name if it's a not-too-long single word that's not likely as a variable name on its own, or an abbreviation of some sort). ディノ千?!☎ Dinoguy1000 09:52, 1 September 2023 (UTC)
I think at the moment, being able to do what you describe as a bug is the central feature of the extension. For example, I have contributed to a wiki where we used Variables as a caching mechanism for repeated talks of parser functions over various templates on a page.
It is true that scoped variables would have much better compatibility with parsoid, as they are closely related to template arguments, albeit not readonly.
@RobinHood: I have thought about this before, although it has been some time ago. Are your locals stored within PPFrame? MGChecker (talk) 10:51, 1 September 2023 (UTC)
Yup, that's it exactly. We also extend the root frame to be able to hold variables, so you can have local variables defined directly on a page. That's useful in conjunction with variable inheritance. As a really primitive example, you could set {{#local:units|lb}} on the page itself, then do {{Weight|20}}{{Weight|30}} etc. and Weight would know that it's operating in pounds. Robin Hood  (talk) 14:00, 1 September 2023 (UTC)
Thanks for the tips & feedback here and also shout out to friendly denizens of the #php IRC channel on Libera.
Given that it seems to be an important feature of this extension that the scope of variables is global/dynamic, and a configuration option to swap between behaviors would probably be quite wonky, and one may actually want to use both behaviors in parallel, and because I wanted to try myself, I decided to make a tiny fork of the extension called LocalVariables that implements the behavior I'm looking for via the alternative parser functions #lvardef and #lvar.
I'm not sure if I'll bother trying to upload it to MediaWiki.org and all that jazz, but following is the entire body of the file ExtLocalVariables.php (analog to ExtVariables.php in the original), and the rest should be obvious.
The only really original/important thing here is the use of the PHP function spl_object_id to be able to store values associated with a PPFrame without needing to extend it in core, and also without relying on "dynamic properties" which are deprecated in PHP 8.2.
class ExtLocalVariables {
    private static $mVarStorage = [];
    /**
     * Implements #lvardef parser function to save variable values.
     *
     * @param Parser  $parser The MediaWiki Parser instance
     * @param PPFrame $frame  The MediaWiki PPFrame instance
     * @param array   $args   The arguments to the parser function
     *
     * @return string '' This parser function has no output.
     */
    public static function pf_lvardef( Parser $parser, PPFrame $frame, array $args ) {
        $varName = isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : '';
        $value = isset( $args[1] ) ? trim( $frame->expand( $args[1] ) ) : '';
        $fid = spl_object_id( $frame );
        self::$mVarStorage[ $fid ][ $varName ] = $value;
        return '';
    }
    /**
     * Implements #lvar parser function to return the value of a variable.
     *
     * @param Parser  $parser The MediaWiki Parser instance
     * @param PPFrame $frame  The MediaWiki PPFrame instance
     * @param array   $args   The arguments to the parser function
     *
     * @return string Value assigned to the variable or empty string if undefined.
     */
    public static function pf_lvar( Parser $parser, PPFrame $frame, array $args ) {
        $varName = isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : '';
        $fid = spl_object_id( $frame );
        $value = self::$mVarStorage[ $fid ][ $varName ] ?? '';
        // Only expand argument when needed.
        if ( $value === '' && isset( $args[1] ) ) {
            return trim( $frame->expand( $args[1] ) );
        }
        return $value;
    }
}
TaylanKammer (talk) 15:59, 1 September 2023 (UTC)
I'll be honest, the spl_object_id seems to be kind of wonky to me, as it may behave unexpectedly if the PPFrame is copied. That being said, dynamic properties are obviously not great either, so it might be the best way forward.
Feel free to upload a patch to Gerrit, I would be inclined to merge it if you attach a parser tests. MGChecker (talk) 17:37, 8 September 2023 (UTC)

Breaks MediaWiki 1.39 (?)

The following discussion is closed. Please do not modify it. Subsequent comments should be made on the appropriate discussion page. No further edits should be made to this discussion.


I noticed that this extension is said to be supported in MediaWiki 1.29+ but I'm quite sure that it breaks MediaWiki 1.39.

https://phabricator.wikimedia.org/T348426#9305340

Premising that:

  • yes, I've seen the red-warning in the extension page
  • yes, I've read the full discussion at phabricator:T2509637

Probably it would be a good idea to evaluate if:

  • explicitly flag this extension as not compatible with MediaWiki 1.39+
  • and/or, mark this extension as "Unmaintained" (it has sense to say it at a certain point if it's not possible to continue the development because of MediaWiki deprecations) Valerio Bozzolan (talk) 16:42, 3 November 2023 (UTC)
I've edited the template to indicate "1.29-1.35" since the deprecation literally talks about 1.35 but feel free to clarify that in a better way. Valerio Bozzolan (talk) 16:49, 3 November 2023 (UTC)
What I did (and forgot about until now) was to remove support for the #var_final parser function, which relies on the deprecated InternalParseBeforeSanitize hook. That way it's working for me fine on MW 1.39 so it should be possible to release a new version. Rand(1,2022) (talk) 17:25, 3 November 2023 (UTC)
Is the only symptom the PHP Deprecated: Use of InternalParseBeforeSanitize hook (used in VariablesHooks::onInternalParseBeforeSanitize) was deprecated in MediaWiki 1.35. [Called from MediaWiki\HookContainer\HookContainer::run] message? Those deprecation notices should be hidden from public view. I mean, it's not an error, just a warning that it will break in the future, it can be silenced and you'll be able to use 1.39 until EOL at least. Ciencia Al Poder (talk) 11:47, 4 November 2023 (UTC)
Thanks for this information. I tried playing with the `extension.json` to remove that hook call, and indeed it seems it was not the cause of our white screen of death. Originally I thought something was escalating the deprecation warning to an exception, but I'm quite sure this is not the case, and it's safe to keep that deprecation warning right there.
Thanks again for your work and I'm sorry that MediaWiki is doing these breaking changes affecting some of your users. But fortunately it seems we are not affected. I will revert my edits in the main page to restore 1.26+ compatibility.
If you are reading this message and if you are affected: enable the MediaWiki logs, and inspect other exception messages, for example the examining any other line marked with "[exception]". Valerio Bozzolan (talk) 14:39, 5 November 2023 (UTC)
The discussion above is closed. Please do not modify it. No further edits should be made to this discussion.

Export pages

Can i retrieve pages var values when exporting pages and importing them to another mediawiki ? 163.62.112.95 (talk) 10:26, 25 January 2024 (UTC)