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, LoggerInterface $logger,
25        /** The HTML transform. */
26        private ContentTextTransformStage $textTransform,
27        /** The DOM tranform. */
28        private ContentDOMTransformStage $domTransform,
29        /**
30         * True if only one of $textTransform and $domTransform are expected to apply;
31         * false if both transforms are equivalent and either can be selected depending
32         * on the ContentHolder preferences.
33         */
34        private bool $exclusive = false
35    ) {
36        parent::__construct( $options, $logger );
37    }
38
39    public function shouldRun( ParserOutput $po, ParserOptions $popts, array $options = [] ): bool {
40        $textShouldRun = $this->textTransform->shouldRun( $po, $popts, $options );
41        $domShouldRun = $this->domTransform->shouldRun( $po, $popts, $options );
42        $textClass = $this->textTransform::class;
43        $domClass = $this->domTransform::class;
44        Assert::invariant( $this->exclusive ?
45                !( $textShouldRun && $domShouldRun ) : $textShouldRun === $domShouldRun,
46            "Bad pipeline stage definition for $textClass / $domClass"
47        );
48        return $textShouldRun || $domShouldRun;
49    }
50
51    public function transform( ParserOutput $po, ParserOptions $popts, array &$options ): ParserOutput {
52        $useDom = $this->exclusive ?
53            $this->domTransform->shouldRun( $po, $popts, $options ) :
54            $po->getContentHolder()->preferDom();
55        return $useDom ? $this->domTransform->transform( $po, $popts, $options ) :
56            $this->textTransform->transform( $po, $popts, $options );
57    }
58}