Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
OutputTransformStage
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 2
6
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 shouldRun
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 transform
n/a
0 / 0
n/a
0 / 0
0
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;
10
11/**
12 * Classes extending OutputTransformStage are typically added to a
13 * pipeline of transformations that transform a ParserOutput. The
14 * argument ParserOutput can explicitly be modified in place; ensuring
15 * that cached objects do not suffer from side effects is the caller's
16 * (typically the pipeline's) responsibility.
17 * @unstable
18 */
19abstract class OutputTransformStage {
20
21    protected ServiceOptions $options;
22    protected LoggerInterface $logger;
23
24    /** @internal */
25    public const CONSTRUCTOR_OPTIONS = [];
26
27    /** @internal */
28    public function __construct(
29        ServiceOptions $options,
30        LoggerInterface $logger,
31    ) {
32        // Note this is static:: not self:: so we use the subclass options
33        $options->assertRequiredOptions( static::CONSTRUCTOR_OPTIONS );
34        $this->options = $options;
35        $this->logger = $logger;
36    }
37
38    /**
39     * Decides whether or not the stage should be run
40     * @param ParserOutput $po
41     * @unstable
42     * @param ParserOptions $popts
43     * @param array $options
44     * @return bool
45     */
46    public function shouldRun( ParserOutput $po, ParserOptions $popts, array $options = [] ): bool {
47        // A simple default implementation.
48        return true;
49    }
50
51    /**
52     * Transforms the input ParserOutput into the returned ParserOutput.
53     * The returned ParserOutput can explicitly be a modified version of the input ParserOutput; if modifications
54     * to that object are unexpected, a copy should be made before passing it to this method.
55     * TODO Some transformations require the possibility of modifying options (this is the case of
56     * ExecutePostCacheTransformHooks in particular). We do NOT want to keep this mechanism for later versions of
57     * this interface - the currently foreseen goal is to not pass $options at all.
58     * Modifying $options during this pass is considered deprecated.
59     * @unstable
60     */
61    abstract public function transform( ParserOutput $po, ParserOptions $popts, array &$options ): ParserOutput;
62}