News:

Wondering if this will always be free?  See why free is better.

Main Menu

How to add exception in a function

Started by spiros, April 27, 2020, 08:53:05 AM

Previous topic - Next topic

spiros

I have a problem when importing some content with Mediawiki's Data Transfer extension (updating existing content). When the text in the template contains a "link=" bit, then it is parsed as an extra import field (the only one I use is "Text") and a line break added before it. This is an example of the text for import:

File:woodhouse_775.jpg | thumb |link={{filepath:woodhouse_775.jpg}}

So when it is imported it is broken like this:

{{TemplateNameHere
    |Text=File:woodhouse_775.jpg|thumb
    |link={{filepath:woodhouse_775.jpg}}
}}


How can I make it so that the output is without a break added after the "|link=" bit like this:

{{TemplateNameHere
   |Text=File:woodhouse_775.jpg|thumb|link={{filepath:woodhouse_775.jpg}}
}}


public function toWikitext() {
if ( $this->mIsTemplate ) {
$wikitext = '{{' . $this->mTemplateName;
foreach ( $this->mFields as $fieldName => $fieldValue ) {
if ( is_numeric( $fieldName ) ) {
$wikitext .= '|' . $fieldValue;
} else {
$wikitext .= "\n|$fieldName=$fieldValue";
}
}
$wikitext .= "\n}}";
return $wikitext;
} else {
return $this->mFreeText;
}
}


The problem appears at this point, but I don't know how to add an exception so that the "|link=" bit is not parsed as a field and a line break added before it:

} else {
$wikitext .= "\n|$fieldName=$fieldValue";


Simply removing the \n bit would result in no line break created for the "|Text=" field either.

Chen Zhen


Did you try removing the line break?

$wikitext .= "|$fieldName=$fieldValue";

My SMF Mods & Plug-Ins

WebDev

"Either you repeat the same conventional doctrines everybody is saying, or else you say something true, and it will sound like it's from Neptune." - Noam Chomsky

Chen Zhen

oops.. I didn't see the bottom of your post until just now.
Try this:


$wikitext .= (substr($fieldname, 0, 4) == 'Text' ? '\n' : '') . "|$fieldName=$fieldValue";

My SMF Mods & Plug-Ins

WebDev

"Either you repeat the same conventional doctrines everybody is saying, or else you say something true, and it will sound like it's from Neptune." - Noam Chomsky

Advertisement: