Debian Codegraph @

includes/OutputTransform/Stages/ExtractBody.php

raw symbols SCIP
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 * Applies base href, and strip everything but the <body>
19 * @internal
20 */
21 class ExtractBody extends ContentTextTransformStage {
22
23 private UrlUtils $urlUtils;
24
25 // @phan-suppress-next-line PhanUndeclaredTypeProperty
26 private ?\MobileContext $mobileContext;
27
28 public function __construct(
29 ServiceOptions $options, LoggerInterface $logger, UrlUtils $urlUtils,
30 // @phan-suppress-next-line PhanUndeclaredTypeParameter
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 // T350952: Expand relative links
53 // What we should be doing here is parsing as a title and then
54 // using Title::getLocalURL()
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 // Convert page fragment urls to true fragment urls
68 // This ensures that those fragments include any URL query params
69 // and resolve internally. (Ex: on pages with ?useparsoid=1,
70 // cite link fragments should not take you to a different page).
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 // T350952: temporary fix for subpage paths: use Parsoid's
84 // <base href> to expand relative links
85 $baseHref = '';
86 if ( preg_match( '{<base href=["\']([^"\']+)["\'][^>]+>}', $text, $matches ) === 1 ) {
87 $baseHref = $matches[1];
88 // @phan-suppress-next-line PhanUndeclaredClassMethod
89 if ( $this->mobileContext !== null && $this->mobileContext->usingMobileDomain() ) {
90 // @phan-suppress-next-line PhanUndeclaredClassMethod
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 // We don't think this should ever trigger, but being conservative
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 }