Debian Codegraph @

includes/OutputTransform/Stages/HandleParsoidSectionLinks.php

raw symbols SCIP
1 <?php
2 // Suppress UnusedPluginSuppression because Phan on PHP 8.1 needs more
3 // suppressions than PHP 7.x due to tighter types on Element::insertBefore()
4 // and Element::appendChild(): see comments marked PHP81 below. The
5 // Unused*Suppression can be removed once MW moves to >= PHP 8.1.
6 // @phan-file-suppress UnusedPluginSuppression,UnusedPluginFileSuppression
7
8 namespace MediaWiki\OutputTransform\Stages;
9
10 use MediaWiki\Config\ServiceOptions;
11 use MediaWiki\Context\RequestContext;
12 use MediaWiki\OutputTransform\ContentDOMTransformStage;
13 use MediaWiki\Parser\ParserOptions;
14 use MediaWiki\Parser\ParserOutput;
15 use MediaWiki\Parser\ParserOutputFlags;
16 use MediaWiki\Title\TitleFactory;
17 use Psr\Log\LoggerInterface;
18 use Skin;
19 use Wikimedia\Parsoid\DOM\Document;
20 use Wikimedia\Parsoid\Utils\DOMCompat;
21
22 /**
23 * Add anchors and other heading formatting, and replace the section link placeholders.
24 * @internal
25 */
26 class HandleParsoidSectionLinks extends ContentDOMTransformStage {
27
28 private TitleFactory $titleFactory;
29
30 public function __construct(
31 ServiceOptions $options, LoggerInterface $logger, TitleFactory $titleFactory
32 ) {
33 parent::__construct( $options, $logger );
34 $this->titleFactory = $titleFactory;
35 }
36
37 public function shouldRun( ParserOutput $po, ?ParserOptions $popts, array $options = [] ): bool {
38 // Only run this stage if it is parsoid content
39 return ( $options['isParsoidContent'] ?? false );
40 }
41
42 public function transformDOM(
43 Document $dom, ParserOutput $po, ?ParserOptions $popts, array &$options
44 ): Document {
45 $skin = $this->resolveSkin( $options );
46 $titleText = $po->getTitleText();
47 // Transform:
48 // <section data-mw-section-id=...>
49 // <h2 id=...><span id=... typeof="mw:FallbackId"></span> ... </h2>
50 // ...section contents..
51 // To:
52 // <section data-mw-section-id=...>
53 // <div class="mw-heading mw-heading2">
54 // <h2 id=...><span id=... typeof="mw:FallbackId"></span> ... </h2>
55 // <span class="mw-editsection">...section edit link...</span>
56 // </div>
57 // That is, we're wrapping a <div> around the <h2> generated by
58 // Parsoid, and then (assuming section edit links are enabled)
59 // adding a <span> with the section edit link
60 // inside that <div>
61 //
62 // If COLLAPSIBLE_SECTIONS is set, then we also wrap a <div>
63 // around the section *contents*.
64 $toc = $po->getTOCData();
65 $sections = ( $toc !== null ) ? $toc->getSections() : [];
66 // use the TOC data to extract the headings:
67 foreach ( $sections as $section ) {
68 $fromTitle = $section->fromTitle;
69 if ( $fromTitle === null ) {
70 // T353489: don't wrap bare <h> tags
71 continue;
72 }
73 $h = $dom->getElementById( $section->anchor );
74 if ( $h === null ) {
75 $this->logger->error(
76 __METHOD__ . ': Heading missing for anchor',
77 $section->toLegacy()
78 );
79 continue;
80 }
81 $div = $dom->createElement( 'div' );
82 if ( ( $options['enableSectionEditLinks'] ?? true ) &&
83 !$po->getOutputFlag( ParserOutputFlags::NO_SECTION_EDIT_LINKS ) ) {
84 $editPage = $this->titleFactory->newFromTextThrow( $fromTitle );
85 $html = $skin->doEditSectionLink(
86 $editPage, $section->index, $h->textContent,
87 $skin->getLanguage()
88 );
89 DOMCompat::setInnerHTML( $div, $html );
90 }
91
92 // Reuse existing wrapper if present.
93 $maybeWrapper = $h->parentNode;
94 '@phan-var \Wikimedia\Parsoid\DOM\Element $maybeWrapper';
95 if (
96 DOMCompat::nodeName( $maybeWrapper ) === 'div' &&
97 DOMCompat::getClassList( $maybeWrapper )->contains( 'mw-heading' )
98 ) {
99 // Transfer section edit link children to existing wrapper
100 // All contents of the div (the section edit link) will be
101 // inserted immediately following the <h> tag
102 $ref = $h->nextSibling;
103 while ( $div->firstChild !== null ) {
104 // @phan-suppress-next-line PhanTypeMismatchArgumentNullableInternal firstChild is non-null (PHP81)
105 $maybeWrapper->insertBefore( $div->firstChild, $ref );
106 }
107 $div = $maybeWrapper; // for use below
108 } else {
109 // Move <hX> to new wrapper: the div contents are currently
110 // the section edit link. We first replace the h with the
111 // div, then insert the <h> as the first child of the div
112 // so the section edit link is immediately following the <h>.
113 $div->setAttribute(
114 'class', 'mw-heading mw-heading' . $section->hLevel
115 );
116 $h->parentNode->replaceChild( $div, $h );
117 // Work around bug in phan (https://github.com/phan/phan/pull/4837)
118 // by asserting that $div->firstChild is non-null here. Actually,
119 // ::insertBefore will work fine if $div->firstChild is null (if
120 // "doEditSectionLink" returned nothing, for instance), but
121 // phan incorrectly thinks the second argument must be non-null.
122 $divFirstChild = $div->firstChild;
123 '@phan-var \DOMNode $divFirstChild'; // asserting non-null (PHP81)
124 $div->insertBefore( $h, $divFirstChild );
125 }
126 // Create collapsible section wrapper if requested.
127 if ( $po->getOutputFlag( ParserOutputFlags::COLLAPSIBLE_SECTIONS ) ) {
128 $contentsDiv = $dom->createElement( 'div' );
129 while ( $div->nextSibling !== null ) {
130 // @phan-suppress-next-line PhanTypeMismatchArgumentNullableInternal
131 $contentsDiv->appendChild( $div->nextSibling );
132 }
133 $div->parentNode->appendChild( $contentsDiv );
134 }
135 }
136 return $dom;
137 }
138
139 /**
140 * Extracts the skin from the $options array, with a fallback on request context skin
141 * @param array $options
142 * @return Skin
143 */
144 private function resolveSkin( array $options ): Skin {
145 $skin = $options[ 'skin' ] ?? null;
146 if ( !$skin ) {
147 // T348853 passing $skin will be mandatory in the future
148 $skin = RequestContext::getMain()->getSkin();
149 }
150 return $skin;
151 }
152 }