From bb6ad5721b9f1d4398ecf364f0de0ddc0c13832b Mon Sep 17 00:00:00 2001 From: WMDE-Fisch Date: Thu, 5 Dec 2024 11:33:59 +0100 Subject: [PATCH] [refactor] Parsoid: Extract methods when composing a reference Just starting point to give that massive method a bit more structure. Bug: T382232 Change-Id: I050cc32220b0f0faac2470afa757c23ee06b1b62 --- src/Parsoid/References.php | 97 ++++++++++++++++++++++++++------------ 1 file changed, 66 insertions(+), 31 deletions(-) diff --git a/src/Parsoid/References.php b/src/Parsoid/References.php index a4384c228..ae3a6ebf8 100644 --- a/src/Parsoid/References.php +++ b/src/Parsoid/References.php @@ -372,38 +372,17 @@ class References extends ExtensionTagHandler { } } - $class = 'mw-ref reference'; - if ( $hasValidFollow ) { - $class .= ' mw-ref-follow'; - } - - $lastlinkBack = $ref->linkbacks[count( $ref->linkbacks ) - 1] ?? null; - DOMUtils::addAttributes( $linkBackSup, [ - 'about' => $about, - 'class' => $class, - 'id' => ( $referencesData->inEmbeddedContent() || $hasValidFollow ) ? - null : ( $ref->name ? $lastlinkBack : $ref->id ), - 'rel' => 'dc:references', - 'typeof' => DOMCompat::getAttribute( $node, 'typeof' ), - ] - ); - DOMUtils::removeTypeOf( $linkBackSup, 'mw:DOMFragment/sealed/ref' ); - DOMUtils::addTypeOf( $linkBackSup, 'mw:Extension/ref' ); - - $dataParsoid = new DataParsoid; - if ( isset( $nodeDp->src ) ) { - $dataParsoid->src = $nodeDp->src; - } - if ( isset( $nodeDp->dsr ) ) { - $dataParsoid->dsr = $nodeDp->dsr; - } - if ( isset( $nodeDp->pi ) ) { - $dataParsoid->pi = $nodeDp->pi; - } - DOMDataUtils::setDataParsoid( $linkBackSup, $dataParsoid ); - - DOMDataUtils::setDataMw( + $this->addLinkBackAttributes( $linkBackSup, + $this->getLinkbackId( $ref, $referencesData, $hasValidFollow ), + DOMCompat::getAttribute( $node, 'typeof' ), + $about, + $hasValidFollow + ); + + $this->addLinkBackData( + $linkBackSup, + $nodeDp, $isTemplateWrapper ? $templateDataMw : $refDataMw ); @@ -875,4 +854,60 @@ class References extends ExtensionTagHandler { return false; } + + private function addLinkBackData( + Element $linkBackSup, + DataParsoid $nodeDp, + ?DataMw $dataMw + ): void { + $dataParsoid = new DataParsoid(); + if ( isset( $nodeDp->src ) ) { + $dataParsoid->src = $nodeDp->src; + } + if ( isset( $nodeDp->dsr ) ) { + $dataParsoid->dsr = $nodeDp->dsr; + } + if ( isset( $nodeDp->pi ) ) { + $dataParsoid->pi = $nodeDp->pi; + } + DOMDataUtils::setDataParsoid( $linkBackSup, $dataParsoid ); + DOMDataUtils::setDataMw( $linkBackSup, $dataMw ); + } + + private function addLinkBackAttributes( + Element $linkBackSup, + ?string $id, + ?string $typeof, + ?string $about, + bool $hasValidFollow + ): void { + $class = 'mw-ref reference'; + if ( $hasValidFollow ) { + $class .= ' mw-ref-follow'; + } + + DOMUtils::addAttributes( $linkBackSup, [ + 'about' => $about, + 'class' => $class, + 'id' => $id, + 'rel' => 'dc:references', + 'typeof' => $typeof, + ] ); + DOMUtils::removeTypeOf( $linkBackSup, 'mw:DOMFragment/sealed/ref' ); + DOMUtils::addTypeOf( $linkBackSup, 'mw:Extension/ref' ); + } + + private function getLinkbackId( + ?RefGroupItem $ref, + ReferencesData $refsData, + bool $hasValidFollow + ): ?string { + if ( $refsData->inEmbeddedContent() || $hasValidFollow ) { + return null; + } + + $lastLinkBack = $ref->linkbacks[count( $ref->linkbacks ) - 1] ?? null; + return $ref->name ? $lastLinkBack : $ref->id; + } + }