File: DefaultOutputPipelineFactory.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (151 lines) | stat: -rw-r--r-- 4,085 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php

namespace MediaWiki\OutputTransform;

use MediaWiki\Config\Config;
use MediaWiki\Config\ServiceOptions;
use MediaWiki\MainConfigNames;
use MediaWiki\OutputTransform\Stages\AddRedirectHeader;
use MediaWiki\OutputTransform\Stages\AddWrapperDivClass;
use MediaWiki\OutputTransform\Stages\DeduplicateStyles;
use MediaWiki\OutputTransform\Stages\ExecutePostCacheTransformHooks;
use MediaWiki\OutputTransform\Stages\ExpandToAbsoluteUrls;
use MediaWiki\OutputTransform\Stages\ExtractBody;
use MediaWiki\OutputTransform\Stages\HandleParsoidSectionLinks;
use MediaWiki\OutputTransform\Stages\HandleSectionLinks;
use MediaWiki\OutputTransform\Stages\HandleTOCMarkers;
use MediaWiki\OutputTransform\Stages\HardenNFC;
use MediaWiki\OutputTransform\Stages\HydrateHeaderPlaceholders;
use MediaWiki\OutputTransform\Stages\ParsoidLocalization;
use MediaWiki\OutputTransform\Stages\RenderDebugInfo;
use Psr\Log\LoggerInterface;
use Wikimedia\ObjectFactory\ObjectFactory;

/**
 * This class contains the default output transformation pipeline factory for wikitext. It is a postprocessor for
 * ParserOutput objects either directly resulting from a parse or fetched from ParserCache.
 * @unstable
 */
class DefaultOutputPipelineFactory {

	private ServiceOptions $options;
	private Config $config;
	private LoggerInterface $logger;
	private ObjectFactory $objectFactory;

	public const CONSTRUCTOR_OPTIONS = [
		MainConfigNames::OutputPipelineStages,
	];

	private const CORE_LIST = [
		'ExtractBody' => [
			'class' => ExtractBody::class,
			'services' => [
				'UrlUtils',
			],
			'optional_services' => [
				'MobileFrontend.Context',
			],
		],
		'AddRedirectHeader' => [
			'class' => AddRedirectHeader::class,
		],
		'RenderDebugInfo' => [
			'class' => RenderDebugInfo::class,
			'services' => [
				'HookContainer',
			],
		],
		'ParsoidLocalization' => [
			'class' => ParsoidLocalization::class,
		],
		'ExecutePostCacheTransformHooks' => [
			'class' => ExecutePostCacheTransformHooks::class,
			'services' => [
				'HookContainer',
			],
		],
		'AddWrapperDivClass' => [
			'class' => AddWrapperDivClass::class,
			'services' => [
				'LanguageFactory',
				'ContentLanguage',
			],
		],
		'HandleSectionLinks' => [
			'class' => HandleSectionLinks::class,
			'services' => [
				'TitleFactory',
			],
		],
		'HandleParsoidSectionLinks' => [
			'class' => HandleParsoidSectionLinks::class,
			'services' => [
				'TitleFactory',
			],
		],
		'HandleTOCMarkers' => [
			'class' => HandleTOCMarkers::class,
			'services' => [
				'Tidy',
			],
		],
		'DeduplicateStyles' => [
			'class' => DeduplicateStyles::class,
		],
		'ExpandToAbsoluteUrls' => [
			'class' => ExpandToAbsoluteUrls::class,
		],
		'HydrateHeaderPlaceholders' => [
			'class' => HydrateHeaderPlaceholders::class,
		],
		# This should be last, in order to ensure final output is hardened
		'HardenNFC' => [
			'class' => HardenNFC::class,
		],
	];

	public function __construct(
		ServiceOptions $options,
		Config $config,
		LoggerInterface $logger,
		ObjectFactory $objectFactory
	) {
		$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
		$this->options = $options;
		$this->config = $config;
		$this->logger = $logger;
		$this->objectFactory = $objectFactory;
	}

	/**
	 * Creates a pipeline of transformations to transform the content of the ParserOutput object from "parsed HTML"
	 * to "output HTML" and returns it.
	 * @internal
	 * @return OutputTransformPipeline
	 */
	public function buildPipeline(): OutputTransformPipeline {
		// Add extension stages
		$list = array_merge(
			self::CORE_LIST,
			$this->options->get( MainConfigNames::OutputPipelineStages )
		);

		$otp = new OutputTransformPipeline();
		foreach ( $list as $spec ) {
			$class = $spec['class'];
			$svcOptions = new ServiceOptions(
				$class::CONSTRUCTOR_OPTIONS, $this->config
			);
			$transform = $this->objectFactory->createObject(
				$spec,
				[
					'assertClass' => OutputTransformStage::class,
					'extraArgs' => [ $svcOptions, $this->logger ],
				]
			);
			$otp->addStage( $transform );
		}
		return $otp;
	}
}