| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\OutputTransform\Stages; |
| 4 |
|
| 5 |
use LogicException; |
| 6 |
use MediaWiki\Config\ServiceOptions; |
| 7 |
use MediaWiki\Context\RequestContext; |
| 8 |
use MediaWiki\Html\Html; |
| 9 |
use MediaWiki\Html\HtmlHelper; |
| 10 |
use MediaWiki\MainConfigNames; |
| 11 |
use MediaWiki\OutputTransform\ContentTextTransformStage; |
| 12 |
use MediaWiki\Parser\ParserOptions; |
| 13 |
use MediaWiki\Parser\ParserOutput; |
| 14 |
use MediaWiki\Parser\ParserOutputFlags; |
| 15 |
use MediaWiki\Parser\Sanitizer; |
| 16 |
use MediaWiki\Title\TitleFactory; |
| 17 |
use Psr\Log\LoggerInterface; |
| 18 |
use Skin; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
class HandleSectionLinks extends ContentTextTransformStage { |
| 25 |
private const EDITSECTION_REGEX = '#<mw:editsection page="(.*?)" section="(.*?)">(.*?)</mw:editsection>#s'; |
| 26 |
private const HEADING_REGEX = |
| 27 |
'/<H(?P<level>[1-6])(?P<attrib>(?:[^\'">]*|"([^"]*)"|\'([^\']*)\')*>)(?P<header>[\s\S]*?)<\/H[1-6] *>/i'; |
| 28 |
|
| 29 |
private TitleFactory $titleFactory; |
| 30 |
|
| 31 |
|
| 32 |
public const CONSTRUCTOR_OPTIONS = [ |
| 33 |
MainConfigNames::ParserEnableLegacyHeadingDOM, |
| 34 |
]; |
| 35 |
|
| 36 |
public function __construct( |
| 37 |
ServiceOptions $options, LoggerInterface $logger, TitleFactory $titleFactory |
| 38 |
) { |
| 39 |
parent::__construct( $options, $logger ); |
| 40 |
$this->titleFactory = $titleFactory; |
| 41 |
} |
| 42 |
|
| 43 |
public function shouldRun( ParserOutput $po, ?ParserOptions $popts, array $options = [] ): bool { |
| 44 |
$isParsoid = $options['isParsoidContent'] ?? false; |
| 45 |
return !$isParsoid; |
| 46 |
} |
| 47 |
|
| 48 |
protected function transformText( string $text, ParserOutput $po, ?ParserOptions $popts, array &$options ): string { |
| 49 |
$text = $this->replaceHeadings( $text, $options ); |
| 50 |
|
| 51 |
if ( |
| 52 |
( $options['enableSectionEditLinks'] ?? true ) && |
| 53 |
!$po->getOutputFlag( ParserOutputFlags::NO_SECTION_EDIT_LINKS ) |
| 54 |
) { |
| 55 |
return $this->addSectionLinks( $text, $po, $options ); |
| 56 |
} else { |
| 57 |
return preg_replace( self::EDITSECTION_REGEX, '', $text ); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
private function replaceHeadings( string $text, array $options ): string { |
| 62 |
$skin = $this->resolveSkin( $options ); |
| 63 |
$useLegacyHeading = $this->options->get( MainConfigNames::ParserEnableLegacyHeadingDOM ); |
| 64 |
if ( $skin && !$skin->getOptions()['supportsMwHeading'] ) { |
| 65 |
$useLegacyHeading = true; |
| 66 |
} |
| 67 |
$needToCheckExistingWrappers = preg_match( '/class="[^"]*\bmw-heading\b[^"]*"/', $text ); |
| 68 |
|
| 69 |
return preg_replace_callback( self::HEADING_REGEX, function ( $m ) use ( |
| 70 |
$useLegacyHeading, $needToCheckExistingWrappers, $text |
| 71 |
) { |
| 72 |
|
| 73 |
|
| 74 |
$attrs = []; |
| 75 |
HtmlHelper::modifyElements( |
| 76 |
$m[0][0], |
| 77 |
static fn ( $node ) => in_array( $node->name, [ 'h1', 'h2', 'h3', 'h4', 'h5', 'h6' ] ), |
| 78 |
static function ( $node ) use ( &$attrs ) { |
| 79 |
$attrs = $node->attrs->getValues(); |
| 80 |
return $node; |
| 81 |
} |
| 82 |
); |
| 83 |
|
| 84 |
if ( !isset( $attrs['data-mw-anchor'] ) ) { |
| 85 |
return $m[0][0]; |
| 86 |
} |
| 87 |
|
| 88 |
$anchor = $attrs['data-mw-anchor']; |
| 89 |
$fallbackAnchor = $attrs['data-mw-fallback-anchor'] ?? false; |
| 90 |
unset( $attrs['data-mw-anchor'] ); |
| 91 |
unset( $attrs['data-mw-fallback-anchor'] ); |
| 92 |
|
| 93 |
|
| 94 |
$editlink = ''; |
| 95 |
$contents = preg_replace_callback( self::EDITSECTION_REGEX, static function ( $mm ) use ( &$editlink ) { |
| 96 |
$editlink = $mm[0]; |
| 97 |
return ''; |
| 98 |
}, $m['header'][0] ); |
| 99 |
|
| 100 |
if ( !$useLegacyHeading ) { |
| 101 |
$wrapperType = 'mwheading'; |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
if ( $needToCheckExistingWrappers ) { |
| 108 |
$textBeforeMatch = substr( $text, 0, $m[0][1] ); |
| 109 |
$openOffset = strrpos( $textBeforeMatch, '<div class="mw-heading' ); |
| 110 |
if ( $openOffset !== false ) { |
| 111 |
$closeOffset = strpos( $textBeforeMatch, '</div>', $openOffset ); |
| 112 |
if ( $closeOffset === false ) { |
| 113 |
|
| 114 |
$wrapperType = 'none'; |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
foreach ( $attrs as $name => $value ) { |
| 122 |
if ( !Sanitizer::isReservedDataAttribute( $name ) ) { |
| 123 |
$wrapperType = 'none'; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
} else { |
| 128 |
$wrapperType = 'legacy'; |
| 129 |
} |
| 130 |
|
| 131 |
return $this->makeHeading( |
| 132 |
(int)$m['level'][0], |
| 133 |
$attrs, |
| 134 |
$anchor, |
| 135 |
$contents, |
| 136 |
$editlink, |
| 137 |
$fallbackAnchor, |
| 138 |
$wrapperType |
| 139 |
); |
| 140 |
}, $text, -1, $count, PREG_OFFSET_CAPTURE ); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
private function makeHeading( $level, $attrs, $anchor, $html, |
| 155 |
$link, $fallbackAnchor, string $wrapperType |
| 156 |
) { |
| 157 |
$anchorEscaped = htmlspecialchars( $anchor, ENT_COMPAT ); |
| 158 |
$idAttr = " id=\"$anchorEscaped\""; |
| 159 |
if ( isset( $attrs['id'] ) ) { |
| 160 |
$idAttr = ''; |
| 161 |
} |
| 162 |
$fallback = ''; |
| 163 |
if ( $fallbackAnchor !== false && $fallbackAnchor !== $anchor ) { |
| 164 |
$fallbackAnchor = htmlspecialchars( $fallbackAnchor, ENT_COMPAT ); |
| 165 |
$fallback = "<span id=\"$fallbackAnchor\"></span>"; |
| 166 |
} |
| 167 |
|
| 168 |
switch ( $wrapperType ) { |
| 169 |
case 'legacy': |
| 170 |
return "<h$level" . Html::expandAttributes( $attrs ) . ">" |
| 171 |
. "$fallback<span class=\"mw-headline\"$idAttr>$html</span>" |
| 172 |
. $link |
| 173 |
. "</h$level>"; |
| 174 |
case 'mwheading': |
| 175 |
return "<div class=\"mw-heading mw-heading$level\">" |
| 176 |
. "<h$level$idAttr" . Html::expandAttributes( $attrs ) . ">$fallback$html</h$level>" |
| 177 |
. $link |
| 178 |
. "</div>"; |
| 179 |
case 'none': |
| 180 |
return "<h$level$idAttr" . Html::expandAttributes( $attrs ) . ">$fallback$html</h$level>" |
| 181 |
. $link; |
| 182 |
default: |
| 183 |
throw new LogicException( "Bad wrapper type: $wrapperType" ); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
private function addSectionLinks( string $text, ParserOutput $po, array $options ): string { |
| 188 |
$skin = $this->resolveSkin( $options ); |
| 189 |
if ( !$skin ) { |
| 190 |
|
| 191 |
return $text; |
| 192 |
} |
| 193 |
$titleText = $po->getTitleText(); |
| 194 |
return preg_replace_callback( self::EDITSECTION_REGEX, function ( $m ) use ( $skin, $titleText ) { |
| 195 |
$editsectionPage = $this->titleFactory->newFromTextThrow( htmlspecialchars_decode( $m[1] ) ); |
| 196 |
$editsectionSection = htmlspecialchars_decode( $m[2] ); |
| 197 |
$editsectionContent = Sanitizer::decodeCharReferences( $m[3] ); |
| 198 |
return $skin->doEditSectionLink( $editsectionPage, $editsectionSection, $editsectionContent, |
| 199 |
$skin->getLanguage() ); |
| 200 |
}, $text ); |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
private function resolveSkin( array $options ): ?Skin { |
| 209 |
$skin = $options[ 'skin' ] ?? null; |
| 210 |
if ( !$skin ) { |
| 211 |
if ( defined( 'MW_NO_SESSION' ) ) { |
| 212 |
return null; |
| 213 |
} |
| 214 |
|
| 215 |
$skin = RequestContext::getMain()->getSkin(); |
| 216 |
} |
| 217 |
return $skin; |
| 218 |
} |
| 219 |
} |