Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ContentHolderTransformStage
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 3
72
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 shouldRun
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
20
 transform
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\OutputTransform;
5
6use MediaWiki\Config\ServiceOptions;
7use MediaWiki\Parser\ParserOptions;
8use MediaWiki\Parser\ParserOutput;
9use Psr\Log\LoggerInterface;
10use Wikimedia\Assert\Assert;
11
12/**
13 * OutputTransformStages that can modify the content either as a DOM tree or an HTML string.
14 *
15 * It contains both a ContentDOMTransformStage for the former, and a ContentTextTransformStage for the latter,
16 * and can be set up to either consider these interchangeable depending on what is useful, or to be exclusive if
17 * they do not provide the same function (e.g. passes that do not behave in the same way for Parsoid and
18 * Legacy could be defined as exclusive Parsoid-DOM and Legacy-Html).
19 *
20 * @internal
21 */
22class ContentHolderTransformStage extends OutputTransformStage {
23    public function __construct(
24        ServiceOptions $options,
25        LoggerInterface $logger,
26        /** The HTML transform. */
27        private OutputTransformStage&TextTransformStage $textTransform,
28        /** The DOM transform. */
29        private OutputTransformStage&DOMTransformStage $domTransform,
30        /**
31         * True if only one of $textTransform and $domTransform are expected to apply;
32         * false if both transforms are equivalent and either can be selected depending
33         * on the ContentHolder preferences.
34         */
35        private bool $exclusive = false,
36    ) {
37        parent::__construct( $options, $logger );
38    }
39
40    public function shouldRun( ParserOutput $po, ParserOptions $popts, array $options = [] ): bool {
41        $textShouldRun = $this->textTransform->shouldRun( $po, $popts, $options );
42        $domShouldRun = $this->domTransform->shouldRun( $po, $popts, $options );
43        $textClass = $this->textTransform::class;
44        $domClass = $this->domTransform::class;
45        Assert::invariant( $this->exclusive ?
46                !( $textShouldRun && $domShouldRun ) : $textShouldRun === $domShouldRun,
47            "Bad pipeline stage definition for $textClass / $domClass"
48        );
49        return $textShouldRun || $domShouldRun;
50    }
51
52    public function transform( ParserOutput $po, ParserOptions $popts, array &$options ): ParserOutput {
53        $useDom = $this->exclusive ?
54            $this->domTransform->shouldRun( $po, $popts, $options ) :
55            $po->getContentHolder()->preferDom();
56        return $useDom ? $this->domTransform->transform( $po, $popts, $options ) :
57            $this->textTransform->transform( $po, $popts, $options );
58    }
59}