General Community > Scripting Help

How to add exception in a function

(1/1)

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:


--- Code: ---File:woodhouse_775.jpg | thumb |link={{filepath:woodhouse_775.jpg}}
--- End code ---

So when it is imported it is broken like this:


--- Code: ---{{TemplateNameHere
    |Text=File:woodhouse_775.jpg|thumb
    |link={{filepath:woodhouse_775.jpg}}
}}
--- End code ---

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


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

--- End code ---


--- Code: ---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;
}
}

--- End code ---

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:


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

--- End code ---

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?


--- Code: ---$wikitext .= "|$fieldName=$fieldValue";
--- End code ---

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


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

--- End code ---

Navigation

[0] Message Index

Go to full version