| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\OutputTransform; |
| 4 |
|
| 5 |
use MediaWiki\Config\Config; |
| 6 |
use MediaWiki\Config\ServiceOptions; |
| 7 |
use MediaWiki\MainConfigNames; |
| 8 |
use MediaWiki\OutputTransform\Stages\AddRedirectHeader; |
| 9 |
use MediaWiki\OutputTransform\Stages\AddWrapperDivClass; |
| 10 |
use MediaWiki\OutputTransform\Stages\DeduplicateStyles; |
| 11 |
use MediaWiki\OutputTransform\Stages\ExecutePostCacheTransformHooks; |
| 12 |
use MediaWiki\OutputTransform\Stages\ExpandToAbsoluteUrls; |
| 13 |
use MediaWiki\OutputTransform\Stages\ExtractBody; |
| 14 |
use MediaWiki\OutputTransform\Stages\HandleParsoidSectionLinks; |
| 15 |
use MediaWiki\OutputTransform\Stages\HandleSectionLinks; |
| 16 |
use MediaWiki\OutputTransform\Stages\HandleTOCMarkers; |
| 17 |
use MediaWiki\OutputTransform\Stages\HardenNFC; |
| 18 |
use MediaWiki\OutputTransform\Stages\HydrateHeaderPlaceholders; |
| 19 |
use MediaWiki\OutputTransform\Stages\ParsoidLocalization; |
| 20 |
use MediaWiki\OutputTransform\Stages\RenderDebugInfo; |
| 21 |
use Psr\Log\LoggerInterface; |
| 22 |
use Wikimedia\ObjectFactory\ObjectFactory; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
class DefaultOutputPipelineFactory { |
| 30 |
|
| 31 |
private ServiceOptions $options; |
| 32 |
private Config $config; |
| 33 |
private LoggerInterface $logger; |
| 34 |
private ObjectFactory $objectFactory; |
| 35 |
|
| 36 |
public const CONSTRUCTOR_OPTIONS = [ |
| 37 |
MainConfigNames::OutputPipelineStages, |
| 38 |
]; |
| 39 |
|
| 40 |
private const CORE_LIST = [ |
| 41 |
'ExtractBody' => [ |
| 42 |
'class' => ExtractBody::class, |
| 43 |
'services' => [ |
| 44 |
'UrlUtils', |
| 45 |
], |
| 46 |
'optional_services' => [ |
| 47 |
'MobileFrontend.Context', |
| 48 |
], |
| 49 |
], |
| 50 |
'AddRedirectHeader' => [ |
| 51 |
'class' => AddRedirectHeader::class, |
| 52 |
], |
| 53 |
'RenderDebugInfo' => [ |
| 54 |
'class' => RenderDebugInfo::class, |
| 55 |
'services' => [ |
| 56 |
'HookContainer', |
| 57 |
], |
| 58 |
], |
| 59 |
'ParsoidLocalization' => [ |
| 60 |
'class' => ParsoidLocalization::class, |
| 61 |
], |
| 62 |
'ExecutePostCacheTransformHooks' => [ |
| 63 |
'class' => ExecutePostCacheTransformHooks::class, |
| 64 |
'services' => [ |
| 65 |
'HookContainer', |
| 66 |
], |
| 67 |
], |
| 68 |
'AddWrapperDivClass' => [ |
| 69 |
'class' => AddWrapperDivClass::class, |
| 70 |
'services' => [ |
| 71 |
'LanguageFactory', |
| 72 |
'ContentLanguage', |
| 73 |
], |
| 74 |
], |
| 75 |
'HandleSectionLinks' => [ |
| 76 |
'class' => HandleSectionLinks::class, |
| 77 |
'services' => [ |
| 78 |
'TitleFactory', |
| 79 |
], |
| 80 |
], |
| 81 |
'HandleParsoidSectionLinks' => [ |
| 82 |
'class' => HandleParsoidSectionLinks::class, |
| 83 |
'services' => [ |
| 84 |
'TitleFactory', |
| 85 |
], |
| 86 |
], |
| 87 |
'HandleTOCMarkers' => [ |
| 88 |
'class' => HandleTOCMarkers::class, |
| 89 |
'services' => [ |
| 90 |
'Tidy', |
| 91 |
], |
| 92 |
], |
| 93 |
'DeduplicateStyles' => [ |
| 94 |
'class' => DeduplicateStyles::class, |
| 95 |
], |
| 96 |
'ExpandToAbsoluteUrls' => [ |
| 97 |
'class' => ExpandToAbsoluteUrls::class, |
| 98 |
], |
| 99 |
'HydrateHeaderPlaceholders' => [ |
| 100 |
'class' => HydrateHeaderPlaceholders::class, |
| 101 |
], |
| 102 |
|
| 103 |
'HardenNFC' => [ |
| 104 |
'class' => HardenNFC::class, |
| 105 |
], |
| 106 |
]; |
| 107 |
|
| 108 |
public function __construct( |
| 109 |
ServiceOptions $options, |
| 110 |
Config $config, |
| 111 |
LoggerInterface $logger, |
| 112 |
ObjectFactory $objectFactory |
| 113 |
) { |
| 114 |
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS ); |
| 115 |
$this->options = $options; |
| 116 |
$this->config = $config; |
| 117 |
$this->logger = $logger; |
| 118 |
$this->objectFactory = $objectFactory; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
public function buildPipeline(): OutputTransformPipeline { |
| 128 |
|
| 129 |
$list = array_merge( |
| 130 |
self::CORE_LIST, |
| 131 |
$this->options->get( MainConfigNames::OutputPipelineStages ) |
| 132 |
); |
| 133 |
|
| 134 |
$otp = new OutputTransformPipeline(); |
| 135 |
foreach ( $list as $spec ) { |
| 136 |
$class = $spec['class']; |
| 137 |
$svcOptions = new ServiceOptions( |
| 138 |
$class::CONSTRUCTOR_OPTIONS, $this->config |
| 139 |
); |
| 140 |
$transform = $this->objectFactory->createObject( |
| 141 |
$spec, |
| 142 |
[ |
| 143 |
'assertClass' => OutputTransformStage::class, |
| 144 |
'extraArgs' => [ $svcOptions, $this->logger ], |
| 145 |
] |
| 146 |
); |
| 147 |
$otp->addStage( $transform ); |
| 148 |
} |
| 149 |
return $otp; |
| 150 |
} |
| 151 |
} |