Yaron Koren has uploaded a new change for review.
https://gerrit.wikimedia.org/r/61031
Change subject: Fixes to allow parsing, and XML export, of embedded template
calls
......................................................................
Fixes to allow parsing, and XML export, of embedded template calls
Change-Id: I8cbfdfae854f4a4dbabe6d0b8703adc8e7ca3cb1
---
M includes/DT_PageStructure.php
1 file changed, 53 insertions(+), 15 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/DataTransfer
refs/changes/31/61031/1
diff --git a/includes/DT_PageStructure.php b/includes/DT_PageStructure.php
index 8da83b8..1eea0a3 100644
--- a/includes/DT_PageStructure.php
+++ b/includes/DT_PageStructure.php
@@ -53,24 +53,34 @@
$bodyXML = '';
foreach ( $this->mFields as $fieldName => $fieldValue )
{
- if ( $wgDataTransferViewXMLParseFields ) {
+ // If this field itself holds template calls,
+ // get the XML for those calls.
+ if ( is_array( $fieldValue ) ) {
+ $fieldValueXML = '';
+ foreach ( $fieldValue as $subComponent
) {
+ $fieldValueXML .=
$subComponent->toXML( $isSimplified );
+ }
+ } elseif ( $wgDataTransferViewXMLParseFields ) {
$fieldValue = $wgParser->parse(
$fieldValue, $wgTitle, new ParserOptions() )->getText();
}
- if ( is_numeric( $fieldName ) ) {
- if ( $isSimplified ) {
+
+ if ( $isSimplified ) {
+ if ( is_numeric( $fieldName ) ) {
// add "Field" to the beginning
of the file name, since
// XML tags that are simply
numbers aren't allowed
- $bodyXML .= Xml::element(
$field_str . '_' . $fieldName, null, $fieldValue );
+ $fieldName = $field_str . '_' .
$fieldName;
} else {
- $bodyXML .= Xml::element(
$field_str, array( $name_str => $fieldName ), $fieldValue );
- }
- } else {
- if ( $isSimplified ) {
$fieldName = str_replace( ' ',
'_', trim( $fieldName ) );
- $bodyXML .= Xml::element(
$fieldName, null, $fieldValue );
- } else {
- $bodyXML .= Xml::element(
$field_str, array( $name_str => $fieldName ) , $fieldValue );
}
+ $attrs = null;
+ } else {
+ $fieldName = $field_str;
+ $attrs = array( $name_str => $fieldName
);
+ }
+ if ( is_array( $fieldValue ) ) {
+ $bodyXML .= Xml::tags( $fieldName,
$attrs, $fieldValueXML );
+ } else {
+ $bodyXML .= Xml::element( $fieldName,
$attrs, $fieldValue );
}
}
@@ -118,6 +128,27 @@
$page_contents = $article->getContent();
}
+ $pageStructure->parsePageContents( $page_contents );
+
+ // Now, go through the field values and see if any of them
+ // hold template calls - if any of them do, parse the value
+ // as if it's the full contents of a page, and add the
+ // resulting "components" to that field.
+ foreach ( $pageStructure->mComponents as $pageComponent ) {
+ if ( $pageComponent->mIsTemplate ) {
+ foreach ( $pageComponent->mFields as $fieldName
=> $fieldValue ) {
+ if ( strpos( $fieldValue, '{{' ) !==
false ) {
+ $dummyPageStructure = new
DTPageStructure();
+
$dummyPageStructure->parsePageContents( $fieldValue );
+
$pageComponent->mFields[$fieldName] = $dummyPageStructure->mComponents;
+ }
+ }
+ }
+ }
+ return $pageStructure;
+ }
+
+ public function parsePageContents( $page_contents ) {
// escape out variables like "{{PAGENAME}}"
$page_contents = str_replace( '{{PAGENAME}}',
'{{PAGENAME}}', $page_contents );
// escape out parser functions
@@ -148,7 +179,7 @@
$free_text = trim( $free_text );
if ( $free_text != "" ) {
$freeTextComponent =
DTPageComponent::newFreeText( $free_text );
- $pageStructure->addComponent(
$freeTextComponent );
+ $this->addComponent(
$freeTextComponent );
$free_text = "";
}
} elseif ( $c == "{" ) {
@@ -167,16 +198,17 @@
// $field_name = "";
// }
if ( $page_contents[$i - 1] == '}' ) {
- $pageStructure->addComponent(
$curTemplate );
+ $this->addComponent(
$curTemplate );
}
$template_name = "";
}
- } else { // 2 or greater - probably 2
+ } elseif ( $uncompleted_curly_brackets == 2 ) {
if ( $c == "}" ) {
$uncompleted_curly_brackets--;
}
if ( $c == "{" ) {
$uncompleted_curly_brackets++;
+ $field_value .= $c;
} else {
if ( $creating_template_name ) {
if ( $c == "|" || $c == "}" ) {
@@ -216,9 +248,15 @@
}
}
}
+ } else { // greater than 2
+ if ( $c == "}" ) {
+ $uncompleted_curly_brackets--;
+ } elseif ( $c == "{" ) {
+ $uncompleted_curly_brackets++;
+ }
+ $field_value .= $c;
}
}
- return $pageStructure;
}
public function toXML( $isSimplified ) {
--
To view, visit https://gerrit.wikimedia.org/r/61031
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I8cbfdfae854f4a4dbabe6d0b8703adc8e7ca3cb1
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/DataTransfer
Gerrit-Branch: master
Gerrit-Owner: Yaron Koren <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits