Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
OutputTransformPipeline
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 2
30
0.00% covered (danger)
0.00%
0 / 1
 addStage
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 run
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3namespace MediaWiki\OutputTransform;
4
5use MediaWiki\Parser\ParserOutput;
6use ParserOptions;
7
8/**
9 * @unstable
10 */
11class OutputTransformPipeline {
12
13    /** @var OutputTransformStage[] */
14    private array $stages = [];
15
16    public function addStage( OutputTransformStage $stage ): OutputTransformPipeline {
17        $this->stages[] = $stage;
18        return $this;
19    }
20
21    /**
22     * Runs the pipeline on the ParserOutput, yielding a transformed ParserOutput.
23     * @param ParserOutput $in Parser output to which the transformations are
24     *     applied. It is typically copied before applying transformations and is
25     * hence not mutated by this method, but if $options['suppressClone'] is
26     * set it WILL be mutated!
27     * @param ?ParserOptions $popts - will eventually replace options as container
28     *    for transformation options
29     * @param array $options Transformations to apply to the HTML
30     *  - suppressClone: (bool) Whether to clone the ParserOutput before
31     *     applying transformations. Default is false.
32     *  - allowTOC: (bool) Show the TOC, assuming there were enough headings
33     *     to generate one and `__NOTOC__` wasn't used. Default is true,
34     *     but might be statefully overridden.
35     *  - injectTOC: (bool) Replace the TOC_PLACEHOLDER with TOC contents;
36     *     otherwise the marker will be left in the article (and the skin
37     *     will be responsible for replacing or removing it).  Default is
38     *     true.
39     *  - enableSectionEditLinks: (bool) Include section edit links, assuming
40     *     section edit link tokens are present in the HTML. Default is true,
41     *     but might be statefully overridden.
42     *  - userLang: (Language) Language object used for localizing UX messages,
43     *    for example the heading of the table of contents. If omitted, will
44     *    use the language of the main request context.
45     *  - skin: (Skin) Skin object used for transforming section edit links.
46     *  - unwrap: (bool) Return text without a wrapper div. Default is false,
47     *    meaning a wrapper div will be added if getWrapperDivClass() returns
48     *    a non-empty string.
49     *  - wrapperDivClass: (string) Wrap the output in a div and apply the given
50     *    CSS class to that div. This overrides the output of getWrapperDivClass().
51     *    Setting this to an empty string has the same effect as 'unwrap' => true.
52     *  - deduplicateStyles: (bool) When true, which is the default, `<style>`
53     *    tags with the `data-mw-deduplicate` attribute set are deduplicated by
54     *    value of the attribute: all but the first will be replaced by `<link
55     *    rel="mw-deduplicated-inline-style" href="mw-data:..."/>` tags, where
56     *    the scheme-specific-part of the href is the (percent-encoded) value
57     *    of the `data-mw-deduplicate` attribute.
58     *  - absoluteURLs: (bool) use absolute URLs in all links. Default: false
59     *  - includeDebugInfo: (bool) render PP limit report in HTML. Default: false
60     */
61    public function run( ParserOutput $in, ?ParserOptions $popts, array $options ): ParserOutput {
62        if ( $options['suppressClone'] ?? false ) {
63            // T353257: This should be a clone, but we've need to suppress it
64            // for some legacy codepaths.
65            $out = $in;
66        } else {
67            $out = clone $in;
68        }
69        foreach ( $this->stages as $stage ) {
70            if ( $stage->shouldRun( $out, $popts, $options ) ) {
71                // Some stages may (for now) modify $options. See OutputTransformStage documentation for more info.
72                $out = $stage->transform( $out, $popts, $options );
73            }
74        }
75        return $out;
76    }
77}