1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
<?php
namespace MediaWiki\OutputTransform\Stages;
use MediaWiki\Config\ServiceOptions;
use MediaWiki\Html\Html;
use MediaWiki\Language\Language;
use MediaWiki\Languages\LanguageFactory;
use MediaWiki\OutputTransform\ContentTextTransformStage;
use MediaWiki\Parser\ParserOptions;
use MediaWiki\Parser\ParserOutput;
use Psr\Log\LoggerInterface;
/**
* Wrap the output in a div with the provided class name
* @internal
*/
class AddWrapperDivClass extends ContentTextTransformStage {
private LanguageFactory $langFactory;
private Language $contentLang;
public function __construct(
ServiceOptions $options, LoggerInterface $logger, LanguageFactory $langFactory,
Language $contentLang
) {
parent::__construct( $options, $logger );
$this->langFactory = $langFactory;
$this->contentLang = $contentLang;
}
public function shouldRun( ParserOutput $po, ?ParserOptions $popts, array $options = [] ): bool {
return ( $options['wrapperDivClass'] ?? $po->getWrapperDivClass() ) !== '' && !( $options['unwrap'] ?? false );
}
protected function transformText( string $text, ParserOutput $po, ?ParserOptions $popts, array &$options ): string {
$wrapperDivClass = $options['wrapperDivClass'] ?? $po->getWrapperDivClass();
$pageLang = $this->getLanguageWithFallbackGuess( $po );
$extraAttrs = [];
$parsoidVersion = $po->getExtensionData( 'core:parsoid-version' );
$htmlVersion = $po->getExtensionData( 'core:html-version' );
if ( $parsoidVersion !== null ) {
$extraAttrs['data-mw-parsoid-version'] = $parsoidVersion;
}
if ( $htmlVersion !== null ) {
$extraAttrs['data-mw-html-version'] = $htmlVersion;
}
return Html::rawElement( 'div', [
'class' => 'mw-content-' . $pageLang->getDir() . ' ' . $wrapperDivClass,
'lang' => $pageLang->toBcp47Code(),
'dir' => $pageLang->getDir(),
] + $extraAttrs, $text );
}
private function getLanguageWithFallbackGuess( ParserOutput $po ): Language {
$pageLang = $po->getLanguage();
if ( $pageLang ) {
return $this->langFactory->getLanguage( $pageLang );
}
return $this->contentLang;
}
}
|