Page MenuHomePhabricator

info tag messes up form titles when using {{int:any-label}}
Closed, ResolvedPublic

Description

Dear all,

I use int tags to translate my form titles which causes messed up titles like: "Create Page|edit title=Edit Page"

reproduce:

{{{info|create title={{int:any-label}}|edit title={{int:any-label}}|page name=Any page <unique number>}}}

then edit or create a page with this form;

i looked into this issue and assume that the regex pattern ({{.*)\|(.*}}) in PFUTILS::getFormTagComponents() replaces pipes from the info tag itself with \1 in my case; the modified string is then passed on without these necessary pipes to PFUTILS::smartSplitFormTag( ), then replaces the \1 with pipes again; the result confuses the PFFormPrinter in about line 1393 which leads to a messed up form title

the regex matches the outer curly braces and does not account for separate ones --- in my example above the match is: {{int:any-label}}|edit title={{int:any-label}} instead of none;

i am neither familiar with your code base nor fit enough to provide an alternative regex pattern allowing to keep the code as it is; alternatively i can only think of looping through matches of {{[^}]+}} and look for pipes there and then join them back to a single $str to feed PFUTILS::smartSplitFormTag()

happy to provide support with this one, if needed

Event Timeline

@Schtom - thanks for looking into this. I've heard about that problem before, but never really looked into it. If you can come up with alternative parsing code for the "info" tag - whether it uses a regex, or loops through, or anything else, that would be great.

sure you're welcome; i replaced the regex with {{(?>[^{}]|(?R))*}} and replace matches with preg_replace_callback instead. i have a running mediawiki development instance locally and tested the code successfully w/ our project's forms.

notes

  1. the regex does not return nested occurrences of parser functions/magic words, but rather the outermost one (see second example on regex101). I think that it does not matter since we can safely replace pipes knowing we're in the outermost parser function
  2. users cannot use '{' or '}' inside double curly braces; see 4th example; the regex requires perfectly balanced double brackets; this is a little annoying... a workaround could be to wrap such statements in templates

what do you think?

i've never pushed code to any mediawiki repo. where and how can i show/test my code? where should i push to?

If you can just call "git diff" and put the output here, that would be good enough. (Assuming you're using Git.)

there you go; i think using a parsing algorithm rather than regexes could be more robust in the long-run... i could check if i find some code portions in the PageForms extension/somewhere else; let me know what you think

i added another edge case to the regex 101 example (5th example) when triple brackets are used inside a parser function; i cannot judge how critical these cases are

diff --git a/includes/PF_Utils.php b/includes/PF_Utils.php
index 02ea06ce..793c11b9 100644
--- a/includes/PF_Utils.php
+++ b/includes/PF_Utils.php
@@ -375,10 +375,14 @@ END;
                // Turn each pipe within double curly brackets into another,
                // unused character (here, "\1"), then do the explode, then
                // convert them back.
-               $pattern = '/({{.*)\|(.*}})/';
-               while ( preg_match( $pattern, $str, $matches ) ) {
-                       $str = preg_replace( $pattern, "$1" . "\1" . "$2", $str );
-               }
+               // regex adapted from:
+               // https://www.regular-expressions.info/recurse.html
+
+               $pattern = '/{{(?>[^{}]|(?R))*?}}/';
+               $str = preg_replace_callback( $pattern, function($match) {
+                       $hasPipe = strpos($match[0], '|');
+                       return $hasPipe ? str_replace("|", "\1", $match[0]) : $match[0];
+               }, $str);
                return array_map( array( 'PFUtils', 'convertBackToPipes' ), self::smartSplitFormTag( $str ) );
        }
Yaron_Koren claimed this task.

Okay, I just checked in this patch. Thank you! It seems to work fine for me - I hope it works well for everyone else too...