| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 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 |
|
| 24 |
|
| 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 |
|
| 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 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
$toc = $po->getTOCData(); |
| 65 |
$sections = ( $toc !== null ) ? $toc->getSections() : []; |
| 66 |
|
| 67 |
foreach ( $sections as $section ) { |
| 68 |
$fromTitle = $section->fromTitle; |
| 69 |
if ( $fromTitle === null ) { |
| 70 |
|
| 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 |
|
| 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 |
|
| 100 |
|
| 101 |
|
| 102 |
$ref = $h->nextSibling; |
| 103 |
while ( $div->firstChild !== null ) { |
| 104 |
|
| 105 |
$maybeWrapper->insertBefore( $div->firstChild, $ref ); |
| 106 |
} |
| 107 |
$div = $maybeWrapper; |
| 108 |
} else { |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
$div->setAttribute( |
| 114 |
'class', 'mw-heading mw-heading' . $section->hLevel |
| 115 |
); |
| 116 |
$h->parentNode->replaceChild( $div, $h ); |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
$divFirstChild = $div->firstChild; |
| 123 |
'@phan-var \DOMNode $divFirstChild'; |
| 124 |
$div->insertBefore( $h, $divFirstChild ); |
| 125 |
} |
| 126 |
|
| 127 |
if ( $po->getOutputFlag( ParserOutputFlags::COLLAPSIBLE_SECTIONS ) ) { |
| 128 |
$contentsDiv = $dom->createElement( 'div' ); |
| 129 |
while ( $div->nextSibling !== null ) { |
| 130 |
|
| 131 |
$contentsDiv->appendChild( $div->nextSibling ); |
| 132 |
} |
| 133 |
$div->parentNode->appendChild( $contentsDiv ); |
| 134 |
} |
| 135 |
} |
| 136 |
return $dom; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
private function resolveSkin( array $options ): Skin { |
| 145 |
$skin = $options[ 'skin' ] ?? null; |
| 146 |
if ( !$skin ) { |
| 147 |
|
| 148 |
$skin = RequestContext::getMain()->getSkin(); |
| 149 |
} |
| 150 |
return $skin; |
| 151 |
} |
| 152 |
} |