| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\OutputTransform\Stages; |
| 4 |
|
| 5 |
use MediaWiki\Message\Message; |
| 6 |
use MediaWiki\OutputTransform\ContentDOMTransformStage; |
| 7 |
use MediaWiki\Page\PageReference; |
| 8 |
use MediaWiki\Page\PageReferenceValue; |
| 9 |
use MediaWiki\Parser\ParserOptions; |
| 10 |
use MediaWiki\Parser\ParserOutput; |
| 11 |
use Wikimedia\Bcp47Code\Bcp47Code; |
| 12 |
use Wikimedia\Bcp47Code\Bcp47CodeValue; |
| 13 |
use Wikimedia\Parsoid\DOM\Document; |
| 14 |
use Wikimedia\Parsoid\DOM\DocumentFragment; |
| 15 |
use Wikimedia\Parsoid\DOM\Element; |
| 16 |
use Wikimedia\Parsoid\NodeData\I18nInfo; |
| 17 |
use Wikimedia\Parsoid\Utils\DOMCompat; |
| 18 |
use Wikimedia\Parsoid\Utils\DOMDataUtils; |
| 19 |
use Wikimedia\Parsoid\Utils\DOMTraverser; |
| 20 |
use Wikimedia\Parsoid\Utils\DOMUtils; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
class ParsoidLocalization extends ContentDOMTransformStage { |
| 27 |
|
| 28 |
public function transformDOM( |
| 29 |
Document $doc, ParserOutput $po, ?ParserOptions $popts, array &$options |
| 30 |
): Document { |
| 31 |
$poLang = $po->getLanguage(); |
| 32 |
if ( $poLang == null ) { |
| 33 |
$this->logger->warning( 'Localization pass started on ParserOutput without defined language', |
| 34 |
[ |
| 35 |
'pass' => 'Localization', |
| 36 |
] ); |
| 37 |
return $doc; |
| 38 |
} |
| 39 |
|
| 40 |
$traverser = new DOMTraverser( false, false ); |
| 41 |
$traverser->addHandler( null, function ( $node ) use ( $po, $doc, $poLang ) { |
| 42 |
if ( $node instanceof Element ) { |
| 43 |
return $this->localizeElement( $node, $poLang, $doc ); |
| 44 |
} |
| 45 |
return true; |
| 46 |
} ); |
| 47 |
$traverser->traverse( null, $doc ); |
| 48 |
return $doc; |
| 49 |
} |
| 50 |
|
| 51 |
public function shouldRun( ParserOutput $po, ?ParserOptions $popts, array $options = [] ): bool { |
| 52 |
return ( $options['isParsoidContent'] ?? false ); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
private function localizeElement( Element $node, Bcp47Code $lang, Document $doc ) { |
| 59 |
if ( DOMUtils::hasTypeOf( $node, 'mw:LocalizedAttrs' ) ) { |
| 60 |
$i18nNames = DOMDataUtils::getDataAttrI18nNames( $node ); |
| 61 |
if ( count( $i18nNames ) === 0 ) { |
| 62 |
$this->logger->warning( 'node with mw:LocalizedAttrs typeof does not contain localisation data', |
| 63 |
[ |
| 64 |
'pass' => 'Localization', |
| 65 |
'node' => $node, |
| 66 |
] ); |
| 67 |
} |
| 68 |
foreach ( $i18nNames as $name ) { |
| 69 |
$i18n = DOMDataUtils::getDataAttrI18n( $node, $name ); |
| 70 |
if ( $i18n === null ) { |
| 71 |
$this->logger->warning( 'null localization element for attribute ' . $name, [ |
| 72 |
'pass' => 'Localization', |
| 73 |
'node' => DOMCompat::getOuterHTML( $node ), |
| 74 |
] ); |
| 75 |
continue; |
| 76 |
} |
| 77 |
$frag = $this->localizeI18n( $i18n, $lang, $doc, true ); |
| 78 |
$node->setAttribute( $name, $frag->textContent ); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
if ( |
| 83 |
( $node->tagName === 'span' || $node->tagName === 'div' ) |
| 84 |
&& DOMUtils::hasTypeOf( $node, 'mw:I18n' ) |
| 85 |
) { |
| 86 |
$i18n = DOMDataUtils::getDataNodeI18n( $node ); |
| 87 |
if ( $i18n !== null ) { |
| 88 |
$frag = $this->localizeI18n( $i18n, $lang, $doc, $node->tagName === 'span' ); |
| 89 |
$node->appendChild( $frag ); |
| 90 |
} else { |
| 91 |
$this->logger->warning( 'element with mw:I18n typeof does not contain i18n data', [ |
| 92 |
'pass' => 'Localization', |
| 93 |
'node' => DOMCompat::getOuterHTML( $node ), |
| 94 |
] ); |
| 95 |
} |
| 96 |
} |
| 97 |
return true; |
| 98 |
} |
| 99 |
|
| 100 |
private function localizeI18n( I18nInfo $i18n, Bcp47Code $poLang, Document $doc, bool $inline ): DocumentFragment { |
| 101 |
$msg = Message::newFromKey( $i18n->key, ...( $i18n->params ?? [] ) ); |
| 102 |
if ( $i18n->lang === I18nInfo::PAGE_LANG ) { |
| 103 |
$msg = $msg->inLanguage( $poLang ); |
| 104 |
} elseif ( $i18n->lang === I18nInfo::USER_LANG ) { |
| 105 |
$msg = $msg->inUserLanguage(); |
| 106 |
} else { |
| 107 |
$msg = $msg->inLanguage( new Bcp47CodeValue( $i18n->lang ) ); |
| 108 |
} |
| 109 |
$txt = $inline ? $msg->parse() : $msg->parseAsBlock(); |
| 110 |
return DOMUtils::parseHTMLToFragment( $doc, $txt ); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
private function getPageReference( ParserOutput $po ): PageReference { |
| 118 |
$title = $po->getTitle(); |
| 119 |
if ( $title === null ) { |
| 120 |
$this->logger->error( __METHOD__ . ": Bad title information in ParserOutput" ); |
| 121 |
return PageReferenceValue::localReference( NS_SPECIAL, 'BadTitle/Localization' ); |
| 122 |
} |
| 123 |
return PageReferenceValue::localReference( |
| 124 |
$title->getNamespace(), $title->getDBkey() |
| 125 |
); |
| 126 |
} |
| 127 |
} |