Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
60.00% |
6 / 10 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
| ContentTextTransformStage | |
60.00% |
6 / 10 |
|
33.33% |
1 / 3 |
8.30 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getFragmentsToTransform | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| transform | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| transformText | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| 1 | <?php |
| 2 | declare( strict_types = 1 ); |
| 3 | |
| 4 | namespace MediaWiki\OutputTransform; |
| 5 | |
| 6 | use MediaWiki\Config\ServiceOptions; |
| 7 | use MediaWiki\Parser\ContentHolder; |
| 8 | use MediaWiki\Parser\ParserOptions; |
| 9 | use MediaWiki\Parser\ParserOutput; |
| 10 | use Psr\Log\LoggerInterface; |
| 11 | |
| 12 | /** |
| 13 | * OutputTransformStages that only modify the content. It is expected that all inheriting classes call this class' |
| 14 | * transform() method (either directly by inheritance or by calling them in the overloaded method). |
| 15 | * @internal |
| 16 | */ |
| 17 | abstract class ContentTextTransformStage extends OutputTransformStage |
| 18 | implements TextTransformStage { |
| 19 | |
| 20 | public function __construct( |
| 21 | ServiceOptions $options, |
| 22 | LoggerInterface $logger, |
| 23 | private readonly bool $transformBodyOnly, |
| 24 | ) { |
| 25 | parent::__construct( $options, $logger ); |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Override this method if you need more control over which fragments |
| 30 | * should be transformed. |
| 31 | */ |
| 32 | protected function getFragmentsToTransform( ParserOutput $po, ParserOptions $popts ): array { |
| 33 | return $this->transformBodyOnly ? |
| 34 | [ ContentHolder::BODY_FRAGMENT ] : |
| 35 | $po->getContentHolder()->getFragmentNames(); |
| 36 | } |
| 37 | |
| 38 | public function transform( ParserOutput $po, ParserOptions $popts, array &$options ): ParserOutput { |
| 39 | foreach ( $this->getFragmentsToTransform( $po, $popts ) as $key ) { |
| 40 | $text = $po->getContentHolder()->getAsHtmlString( $key ); |
| 41 | if ( $text !== null ) { |
| 42 | $text = $this->transformText( $text, $po, $popts, $options ); |
| 43 | $po->getContentHolder()->setAsHtmlString( $key, $text ); |
| 44 | } |
| 45 | } |
| 46 | return $po; |
| 47 | } |
| 48 | |
| 49 | abstract protected function transformText( |
| 50 | string $text, ParserOutput $po, ParserOptions $popts, array &$options |
| 51 | ): string; |
| 52 | } |