MediaWiki master
ContentHolderTransformStage.php
Go to the documentation of this file.
1<?php
2declare( strict_types = 1 );
3
5
9use Psr\Log\LoggerInterface;
10use Wikimedia\Assert\Assert;
11
23 public function __construct(
25 LoggerInterface $logger,
27 private OutputTransformStage&TextTransformStage $textTransform,
29 private OutputTransformStage&DOMTransformStage $domTransform,
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}
A class for passing options to services.
OutputTransformStages that can modify the content either as a DOM tree or an HTML string.
__construct(ServiceOptions $options, LoggerInterface $logger, private OutputTransformStage &TextTransformStage $textTransform, private OutputTransformStage &DOMTransformStage $domTransform, private bool $exclusive=false,)
shouldRun(ParserOutput $po, ParserOptions $popts, array $options=[])
Decides whether or not the stage should be run.
transform(ParserOutput $po, ParserOptions $popts, array &$options)
Transforms the input ParserOutput into the returned ParserOutput.
Classes extending OutputTransformStage are typically added to a pipeline of transformations that tran...
Set options of the Parser.
ParserOutput is a rendering of a Content object or a message.
DOMTransformStage is a simple marker interface to indicate a stage that primary performs transformati...
TextTransformStage is a simple marker interface to indicate a stage that primary performs transformat...