Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
46 / 46 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| DefaultOutputPipelineFactory | |
100.00% |
46 / 46 |
|
100.00% |
3 / 3 |
9 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| buildPipeline | |
100.00% |
35 / 35 |
|
100.00% |
1 / 1 |
5 | |||
| makeExtraArgs | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | declare( strict_types = 1 ); |
| 3 | |
| 4 | namespace MediaWiki\OutputTransform; |
| 5 | |
| 6 | use MediaWiki\Config\Config; |
| 7 | use MediaWiki\Config\ServiceOptions; |
| 8 | use MediaWiki\MainConfigNames; |
| 9 | use MediaWiki\OutputTransform\Stages\AddRedirectHeader; |
| 10 | use MediaWiki\OutputTransform\Stages\AddWrapperDivClass; |
| 11 | use MediaWiki\OutputTransform\Stages\DeduplicateStyles; |
| 12 | use MediaWiki\OutputTransform\Stages\DeduplicateStylesDOM; |
| 13 | use MediaWiki\OutputTransform\Stages\ExecutePostCacheTransformHooks; |
| 14 | use MediaWiki\OutputTransform\Stages\ExpandRelativeAttrs; |
| 15 | use MediaWiki\OutputTransform\Stages\ExpandToAbsoluteUrls; |
| 16 | use MediaWiki\OutputTransform\Stages\ExtractBody; |
| 17 | use MediaWiki\OutputTransform\Stages\HandleParsoidSectionLinks; |
| 18 | use MediaWiki\OutputTransform\Stages\HandleSectionLinks; |
| 19 | use MediaWiki\OutputTransform\Stages\HandleTOCMarkersDOM; |
| 20 | use MediaWiki\OutputTransform\Stages\HandleTOCMarkersText; |
| 21 | use MediaWiki\OutputTransform\Stages\HardenNFC; |
| 22 | use MediaWiki\OutputTransform\Stages\HydrateHeaderPlaceholders; |
| 23 | use MediaWiki\OutputTransform\Stages\ParsoidLocalization; |
| 24 | use MediaWiki\OutputTransform\Stages\RenderDebugInfo; |
| 25 | use Psr\Log\LoggerInterface; |
| 26 | use Wikimedia\ObjectFactory\ObjectFactory; |
| 27 | |
| 28 | /** |
| 29 | * This class contains the default output transformation pipeline factory for wikitext. It is a postprocessor for |
| 30 | * ParserOutput objects either directly resulting from a parse or fetched from ParserCache. |
| 31 | * @unstable |
| 32 | */ |
| 33 | class DefaultOutputPipelineFactory { |
| 34 | |
| 35 | private ServiceOptions $options; |
| 36 | private Config $config; |
| 37 | private LoggerInterface $logger; |
| 38 | private ObjectFactory $objectFactory; |
| 39 | |
| 40 | public const CONSTRUCTOR_OPTIONS = [ |
| 41 | MainConfigNames::OutputPipelineStages, |
| 42 | ]; |
| 43 | |
| 44 | private const CORE_LIST = [ |
| 45 | 'ExtractBody' => [ |
| 46 | 'class' => ExtractBody::class, |
| 47 | 'services' => [ |
| 48 | 'UrlUtils', |
| 49 | ], |
| 50 | ], |
| 51 | 'AddRedirectHeader' => |
| 52 | AddRedirectHeader::class, |
| 53 | |
| 54 | 'RenderDebugInfo' => [ |
| 55 | 'class' => RenderDebugInfo::class, |
| 56 | 'services' => [ |
| 57 | 'HookContainer', |
| 58 | ], |
| 59 | ], |
| 60 | 'ExecutePostCacheTransformHooks' => [ |
| 61 | 'class' => ExecutePostCacheTransformHooks::class, |
| 62 | 'services' => [ |
| 63 | 'HookContainer', |
| 64 | ], |
| 65 | ], |
| 66 | 'AddWrapperDivClass' => [ |
| 67 | 'class' => AddWrapperDivClass::class, |
| 68 | 'services' => [ |
| 69 | 'LanguageFactory', |
| 70 | 'ContentLanguage', |
| 71 | ], |
| 72 | ], |
| 73 | // The next five stages are all DOM-based passes. They are adjacent to each |
| 74 | // other to be able to skip unnecessary intermediate DOM->text->DOM transformations. |
| 75 | 'ExpandRelativeAttrs' => [ |
| 76 | 'class' => ExpandRelativeAttrs::class, |
| 77 | 'services' => [ |
| 78 | 'UrlUtils', |
| 79 | 'ParsoidSiteConfig', |
| 80 | ], |
| 81 | 'optional_services' => [ |
| 82 | 'MobileFrontend.Context', |
| 83 | ], |
| 84 | ], |
| 85 | 'HandleSectionLinks' => [ |
| 86 | 'textStage' => [ |
| 87 | 'class' => HandleSectionLinks::class, |
| 88 | 'services' => [ |
| 89 | 'TitleFactory', |
| 90 | ], |
| 91 | ], |
| 92 | 'domStage' => [ |
| 93 | 'class' => HandleParsoidSectionLinks::class, |
| 94 | 'services' => [ |
| 95 | 'TitleFactory', |
| 96 | ], |
| 97 | ], |
| 98 | 'exclusive' => true |
| 99 | ], |
| 100 | // This should be before DeduplicateStyles because some system messages may use TemplateStyles (so we |
| 101 | // want to expand them before deduplication). |
| 102 | 'ParsoidLocalization' => [ |
| 103 | 'class' => ParsoidLocalization::class, |
| 104 | 'services' => [ |
| 105 | 'TitleFactory', |
| 106 | 'LanguageFactory', |
| 107 | ] |
| 108 | ], |
| 109 | 'HandleTOCMarkers' => [ |
| 110 | 'textStage' => [ |
| 111 | 'class' => HandleTOCMarkersText::class, |
| 112 | 'services' => [ |
| 113 | 'Tidy', |
| 114 | ], |
| 115 | ], |
| 116 | 'domStage' => [ |
| 117 | 'class' => HandleTOCMarkersDOM::class |
| 118 | ], |
| 119 | 'exclusive' => false |
| 120 | ], |
| 121 | 'DeduplicateStyles' => [ |
| 122 | 'textStage' => [ |
| 123 | 'class' => DeduplicateStyles::class, |
| 124 | ], |
| 125 | 'domStage' => [ |
| 126 | 'class' => DeduplicateStylesDOM::class, |
| 127 | ], |
| 128 | 'exclusive' => false |
| 129 | ], |
| 130 | |
| 131 | 'ExpandToAbsoluteUrls' => |
| 132 | ExpandToAbsoluteUrls::class, |
| 133 | |
| 134 | 'HydrateHeaderPlaceholders' => |
| 135 | HydrateHeaderPlaceholders::class, |
| 136 | |
| 137 | # This should be last, in order to ensure final output is hardened |
| 138 | 'HardenNFC' => |
| 139 | HardenNFC::class, |
| 140 | ]; |
| 141 | |
| 142 | public function __construct( |
| 143 | ServiceOptions $options, |
| 144 | Config $config, |
| 145 | LoggerInterface $logger, |
| 146 | ObjectFactory $objectFactory |
| 147 | ) { |
| 148 | $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS ); |
| 149 | $this->options = $options; |
| 150 | $this->config = $config; |
| 151 | $this->logger = $logger; |
| 152 | $this->objectFactory = $objectFactory; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Creates a pipeline of transformations to transform the content of the ParserOutput object from "parsed HTML" |
| 157 | * to "output HTML" and returns it. |
| 158 | * @internal |
| 159 | * @return OutputTransformPipeline |
| 160 | */ |
| 161 | public function buildPipeline(): OutputTransformPipeline { |
| 162 | // Add extension stages |
| 163 | $list = array_merge( |
| 164 | self::CORE_LIST, |
| 165 | $this->options->get( MainConfigNames::OutputPipelineStages ) |
| 166 | ); |
| 167 | |
| 168 | $otp = new OutputTransformPipeline(); |
| 169 | foreach ( $list as $spec ) { |
| 170 | if ( is_array( $spec ) && |
| 171 | array_key_exists( 'domStage', $spec ) && |
| 172 | array_key_exists( 'textStage', $spec ) |
| 173 | ) { |
| 174 | $args = [ |
| 175 | $this->objectFactory->createObject( $spec['textStage'], |
| 176 | [ |
| 177 | 'assertClass' => ContentTextTransformStage::class, |
| 178 | 'allowClassName' => true, |
| 179 | ] + $this->makeExtraArgs( $spec['textStage'] ) ), |
| 180 | $this->objectFactory->createObject( $spec['domStage'], |
| 181 | [ |
| 182 | 'assertClass' => ContentDOMTransformStage::class, |
| 183 | 'allowClassName' => true, |
| 184 | ] + $this->makeExtraArgs( $spec['domStage'] ) ), |
| 185 | $spec['exclusive'] ?? false |
| 186 | ]; |
| 187 | $spec = [ |
| 188 | 'class' => ContentHolderTransformStage::class, |
| 189 | 'args' => $args |
| 190 | ]; |
| 191 | } |
| 192 | |
| 193 | $transform = $this->objectFactory->createObject( |
| 194 | $spec, |
| 195 | [ |
| 196 | 'assertClass' => OutputTransformStage::class, |
| 197 | 'allowClassName' => true, |
| 198 | ] + $this->makeExtraArgs( $spec ) |
| 199 | ); |
| 200 | $otp->addStage( $transform ); |
| 201 | } |
| 202 | return $otp; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Add appropriate ServiceOptions and a logger to the args array. |
| 207 | * @param mixed $spec |
| 208 | * @return array{extraArgs:array{0:ServiceOptions,1:LoggerInterface}} |
| 209 | */ |
| 210 | private function makeExtraArgs( $spec ): array { |
| 211 | // If the handler is specified as a class, use the CONSTRUCTOR_OPTIONS |
| 212 | // for that class. |
| 213 | $class = is_string( $spec ) ? $spec : ( $spec['class'] ?? null ); |
| 214 | $svcOptions = new ServiceOptions( |
| 215 | $class ? $class::CONSTRUCTOR_OPTIONS : [], |
| 216 | $this->config |
| 217 | ); |
| 218 | return [ 'extraArgs' => [ $svcOptions, $this->logger ] ]; |
| 219 | } |
| 220 | } |