Page MenuHomePhabricator

Way to not strip whitespace characters from the beginning and end of a parameter
Open, Needs TriagePublicFeature

Description

See edit at the bottom.

Feature summary (what you would like to be able to do and where):
See https://www.mediawiki.org/wiki/Help:Extension:ParserFunctions#Stripping_whitespace.

I want to be able to have whitespace characters at the beginning/end of a parameter and have a parameter that consists of nothing but whitespace.

Use case(s) (list the steps that you performed to discover that problem, and describe the actual underlying problem which you want to solve. Do not describe only a solution):
There are many, I ran into this before, but today my problem is this: I need to conditionally substitute some newlines. More specifically: I am working on a reply-link/DiscussionTools like script that can add a section. If the page already exists, it has to insert two newlines followed by the section. If the page doesn't exist yet, it should just post the section.

Here are various methods to solve my problem, none of which are ideal:

  1. Litter the place with <nowiki /> tags as instructed by mediawikiwiki. I hate litter and it doesn't work because because <nowiki />==Section title== doesn't work.
  2. Check if the page exists before posting. Increases the risk of failure, reduces responsiveness, complicates my code. I just need some newlines.
  3. Don't use the appendtext parameter from https://en.wikipedia.org/w/api.php?action=help&modules=edit, instead obtain page text (if any) and replace the full page text. See the previous method.
  4. Fuck it and insert the newlines regardless. Not so pretty if the page didn't exist yet.
  5. Use section=new. GREAT IDEA. Too bad it trashes my edit summary. I have no idea what happened but it actually works fine. I figured forcing the default "→Title: new section" summary was a feature, but it isn't forced after all. I'll be using section:new then.

Benefits (why should this be implemented?):
Will cut my Tylenol budget in half.

Edit: okay, my problem today is solved. I already had a function to obtain the wikitext and it was even executed when adding a new section, but the response was discarded. Now I look at the response to determine if the page already exists and add the newlines if it does. That being said: mediawikiwiki describing foo{{#if:||<nowiki /> bar <nowiki />}}foo as a solution is really ugly and makes little sense since it's a workaround. It would be nice if we had {{SPACE}}, {{NEWLINE}} and {{TAB}} so one could use foo{{#if:||{{SPACE}}bar{{SPACE}}}}foo instead, including the ability to successfully subst those so for example a substed template could allow one to insert {{subst:NEWLINE}}{{subst:NEWLINE}}==Message for user=={{subst:NEWLINE}}Hello ~~~~ or '''Keep'''{{subst:SPACE}}.

Event Timeline

Use section=new. GREAT IDEA. Too bad it trashes my edit summary.

On api edit you can distinct the summary and the title of the new section by use of summary and sectionttitle. If that does not work, it is a bug.

If you get the wikitext and append your stuff, the server has to merge the result if someone edit it between the retrieve of the wikitext and the submit of the new wikitext. It could also override by accident the result if the edit conflict parameter baserevid is not used.

if you want a new section, there is a way to do it for index.php and api.php, just use it.

Use section=new. GREAT IDEA. Too bad it trashes my edit summary.

On api edit you can distinct the summary and the title of the new section by use of summary and sectionttitle. If that does not work, it is a bug.

I'm so confused. It does work. But it was also the first thing I tried. I must have messed it up somehow, I thought forcefully using the "→Title: new section" summary was a feature.

So I must have somehow failed to provide the sectiontitle the first time I had tried section:new. No idea how, but it's either that or there was a bug that has been fixed in the meantime. To be frank: if the edit had simply failed instead of using the edit summary as the title (which makes no sense to me, I don't understand in what situation that could ever be useful) or the section title had been left blank I would have probably caught on to my error, so I would have preferred that.

The parser do not strip whitespaces on unnamed template arguments. It strips whitespaces on named template arguments (even numbered named, like |1=) and the parser function typically trim whitespaces in the code to allow better formatting of each of argument and make it easier to read with some space between it.

I am not sure if a new variable to subst a newline in the code is something the parser can provide, because there are some edge cases where the parser already inserts newlines, which can break things, see T14974, it also could make problems with T18700.