From c5ff6c70df27706a494eb9b091e673f8a8e22cf2 Mon Sep 17 00:00:00 2001 From: Igor Rogatty Date: Mon, 9 May 2016 12:46:23 +0200 Subject: [PATCH] XW-1429 | move the first infobox to the top of article content --- ...rtableInfoboxParserTagController.class.php | 37 +++++++++++++++---- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/controllers/PortableInfoboxParserTagController.class.php b/controllers/PortableInfoboxParserTagController.class.php index b994ff9..e5ed4f4 100644 --- a/controllers/PortableInfoboxParserTagController.class.php +++ b/controllers/PortableInfoboxParserTagController.class.php @@ -100,7 +100,6 @@ class PortableInfoboxParserTagController extends WikiaController { * @returns String $html */ public function renderInfobox( $text, $params, $parser, $frame ) { - global $wgArticleAsJson; $this->markerNumber++; $markup = '<' . self::PARSER_TAG_NAME . '>' . $text . ''; @@ -114,11 +113,6 @@ class PortableInfoboxParserTagController extends WikiaController { return $this->handleError( wfMessage( 'portable-infobox-xml-parse-error-infobox-tag-attribute-unsupported', [ $e->getMessage() ] )->escaped() ); } - if ( $wgArticleAsJson ) { - // (wgArticleAsJson == true) it means that we need to encode output for use inside JSON - $renderedValue = trim( json_encode( $renderedValue ), '"' ); - } - $marker = $parser->uniqPrefix() . "-" . self::PARSER_TAG_NAME . "-{$this->markerNumber}\x7f-QINU"; $this->markers[ $marker ] = $renderedValue; @@ -126,7 +120,19 @@ class PortableInfoboxParserTagController extends WikiaController { } public function replaceMarkers( $text ) { - return strtr( $text, $this->markers ); + wfProfileIn( __METHOD__ ); + + global $wgArticleAsJson; + + if ( !empty( $wgArticleAsJson ) && !empty( $this->markers ) ) { + $text = $this->moveFirstInfoboxToTop( $text ); + } + + $articleWithMarkersReplaced = strtr( $text, $this->markers ); + + wfProfileOut( __METHOD__ ); + + return $articleWithMarkersReplaced; } protected function saveToParserOutput( \ParserOutput $parserOutput, Nodes\NodeInfobox $raw ) { @@ -179,4 +185,21 @@ class PortableInfoboxParserTagController extends WikiaController { return self::INFOBOX_LAYOUT_PREFIX . self::DEFAULT_LAYOUT_NAME; } + + private function moveFirstInfoboxToTop( $article ) { + $articleDecoded = json_decode( $article ); + + if ( !empty( $articleDecoded->content ) ) { + $firstMarker = array_keys( $this->markers )[0]; + $firstInfobox = $this->markers[$firstMarker]; + + // Remove the first marker + $this->markers[$firstMarker] = ''; + + // Put the first infobox in the beginning of article content + $articleDecoded->content = $firstInfobox . $articleDecoded->content; + } + + return json_encode( $articleDecoded ); + } }