| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\OutputTransform\Stages; |
| 4 |
|
| 5 |
use MediaWiki\Config\ServiceOptions; |
| 6 |
use MediaWiki\Html\HtmlHelper; |
| 7 |
use MediaWiki\MediaWikiServices; |
| 8 |
use MediaWiki\OutputTransform\ContentTextTransformStage; |
| 9 |
use MediaWiki\Parser\Parser; |
| 10 |
use MediaWiki\Parser\ParserOptions; |
| 11 |
use MediaWiki\Parser\ParserOutput; |
| 12 |
use MediaWiki\Title\Title; |
| 13 |
use MediaWiki\Utils\UrlUtils; |
| 14 |
use Psr\Log\LoggerInterface; |
| 15 |
use Wikimedia\RemexHtml\Serializer\SerializerNode; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
class ExtractBody extends ContentTextTransformStage { |
| 22 |
|
| 23 |
private UrlUtils $urlUtils; |
| 24 |
|
| 25 |
|
| 26 |
private ?\MobileContext $mobileContext; |
| 27 |
|
| 28 |
public function __construct( |
| 29 |
ServiceOptions $options, LoggerInterface $logger, UrlUtils $urlUtils, |
| 30 |
|
| 31 |
?\MobileContext $mobileContext |
| 32 |
) { |
| 33 |
parent::__construct( $options, $logger ); |
| 34 |
$this->urlUtils = $urlUtils; |
| 35 |
$this->mobileContext = $mobileContext; |
| 36 |
} |
| 37 |
|
| 38 |
public function shouldRun( ParserOutput $po, ?ParserOptions $popts, array $options = [] ): bool { |
| 39 |
return ( $options['isParsoidContent'] ?? false ); |
| 40 |
} |
| 41 |
|
| 42 |
private const EXPAND_ELEMENTS = [ |
| 43 |
'a' => true, 'img' => true, 'video' => true, 'audio' => true, |
| 44 |
]; |
| 45 |
|
| 46 |
private static function expandRelativeAttrs( |
| 47 |
string $text, |
| 48 |
string $baseHref, |
| 49 |
string $pageFragmentPrefix, |
| 50 |
UrlUtils $urlUtils |
| 51 |
): string { |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
return HtmlHelper::modifyElements( |
| 56 |
$text, |
| 57 |
static function ( SerializerNode $node ): bool { |
| 58 |
if ( !isset( self::EXPAND_ELEMENTS[$node->name] ) ) { |
| 59 |
return false; |
| 60 |
} |
| 61 |
$attr = $node->name === 'a' ? 'href' : 'resource'; |
| 62 |
return str_starts_with( $node->attrs[$attr] ?? '', './' ); |
| 63 |
}, |
| 64 |
static function ( SerializerNode $node ) use ( $baseHref, $pageFragmentPrefix, $urlUtils ): SerializerNode { |
| 65 |
$attr = $node->name === 'a' ? 'href' : 'resource'; |
| 66 |
$href = $node->attrs[$attr]; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
if ( $pageFragmentPrefix && str_starts_with( $href, $pageFragmentPrefix ) ) { |
| 72 |
$node->attrs[$attr] = substr( $href, strlen( $pageFragmentPrefix ) - 1 ); |
| 73 |
} else { |
| 74 |
$href = $baseHref . $href; |
| 75 |
$node->attrs[$attr] = $urlUtils->expand( $href, PROTO_RELATIVE ) ?? false; |
| 76 |
} |
| 77 |
return $node; |
| 78 |
} |
| 79 |
); |
| 80 |
} |
| 81 |
|
| 82 |
protected function transformText( string $text, ParserOutput $po, ?ParserOptions $popts, array &$options ): string { |
| 83 |
|
| 84 |
|
| 85 |
$baseHref = ''; |
| 86 |
if ( preg_match( '{<base href=["\']([^"\']+)["\'][^>]+>}', $text, $matches ) === 1 ) { |
| 87 |
$baseHref = $matches[1]; |
| 88 |
|
| 89 |
if ( $this->mobileContext !== null && $this->mobileContext->usingMobileDomain() ) { |
| 90 |
|
| 91 |
$mobileUrl = $this->mobileContext->getMobileUrl( $baseHref ); |
| 92 |
if ( $mobileUrl !== false ) { |
| 93 |
$baseHref = $mobileUrl; |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
$title = $po->getTitle(); |
| 98 |
if ( $title === null ) { |
| 99 |
|
| 100 |
$this->logger->error( __METHOD__ . ": Missing title information in ParserOutput" ); |
| 101 |
$title = Title::newMainPage(); |
| 102 |
} |
| 103 |
$titleFormatter = MediaWikiServices::getInstance()->getTitleFormatter(); |
| 104 |
$title = $titleFormatter->getPrefixedDBkey( $title ); |
| 105 |
$pageFragmentPrefix = "./" . $title . "#"; |
| 106 |
foreach ( $po->getIndicators() as $name => $html ) { |
| 107 |
$po->setIndicator( |
| 108 |
$name, |
| 109 |
self::expandRelativeAttrs( $html, $baseHref, $pageFragmentPrefix, $this->urlUtils ) |
| 110 |
); |
| 111 |
} |
| 112 |
$text = Parser::extractBody( $text ); |
| 113 |
return self::expandRelativeAttrs( $text, $baseHref, $pageFragmentPrefix, $this->urlUtils ); |
| 114 |
} |
| 115 |
} |