aboutsummaryrefslogtreecommitdiffstats
path: root/includes/OutputTransform
diff options
context:
space:
mode:
authorC. Scott Ananian <cscott@cscott.net>2024-06-10 20:51:09 -0400
committerC. Scott Ananian <cscott@cscott.net>2024-06-10 20:53:21 -0400
commit292709cc13f1256b6dd0a5c23732fc3981abebb1 (patch)
tree0f75b8715a2225c22c3974ec3b5f10cd2aa67950 /includes/OutputTransform
parent6011792afa70791b741aad92a4fbc717232d5047 (diff)
downloadmediawikicore-292709cc13f1256b6dd0a5c23732fc3981abebb1.tar.gz
mediawikicore-292709cc13f1256b6dd0a5c23732fc3981abebb1.zip
Use $stage::CONSTRUCTOR_OPTIONS in DefaultOutputPipelineFactory
Rather than have DefaultOutputPipelineFactory::CONSTRUCTOR_OPTIONS be a union of all the options needed by all the stages, allow each stage to define its own CONSTRUCTOR_OPTIONS and pass a Config object to the DefaultOutputPipelineFactory service. In the process, move the $options and $logger properties into the abstract superclass, since they are passed to every stage. Bug: T363764 Followup-To: I64aeb81b395ba84e1d839dfbd31decf16c337cd0 Change-Id: I7d386b22c7d8e99b6dfe4cf798069914ac9af373
Diffstat (limited to 'includes/OutputTransform')
-rw-r--r--includes/OutputTransform/ContentDOMTransformStage.php2
-rw-r--r--includes/OutputTransform/ContentTextTransformStage.php2
-rw-r--r--includes/OutputTransform/DefaultOutputPipelineFactory.php19
-rw-r--r--includes/OutputTransform/OutputTransformStage.php22
-rw-r--r--includes/OutputTransform/Stages/AddWrapperDivClass.php1
-rw-r--r--includes/OutputTransform/Stages/ExecutePostCacheTransformHooks.php1
-rw-r--r--includes/OutputTransform/Stages/ExtractBody.php3
-rw-r--r--includes/OutputTransform/Stages/HandleParsoidSectionLinks.php3
-rw-r--r--includes/OutputTransform/Stages/HandleSectionLinks.php8
-rw-r--r--includes/OutputTransform/Stages/HandleTOCMarkers.php1
-rw-r--r--includes/OutputTransform/Stages/ParsoidLocalization.php8
-rw-r--r--includes/OutputTransform/Stages/RenderDebugInfo.php1
12 files changed, 42 insertions, 29 deletions
diff --git a/includes/OutputTransform/ContentDOMTransformStage.php b/includes/OutputTransform/ContentDOMTransformStage.php
index 53c38640f380..23bf77c4bc14 100644
--- a/includes/OutputTransform/ContentDOMTransformStage.php
+++ b/includes/OutputTransform/ContentDOMTransformStage.php
@@ -23,7 +23,7 @@ use Wikimedia\Parsoid\Utils\DOMUtils;
*
* @internal
*/
-abstract class ContentDOMTransformStage implements OutputTransformStage {
+abstract class ContentDOMTransformStage extends OutputTransformStage {
/**
* @inheritDoc
diff --git a/includes/OutputTransform/ContentTextTransformStage.php b/includes/OutputTransform/ContentTextTransformStage.php
index 351b520e8882..342f2ad86b30 100644
--- a/includes/OutputTransform/ContentTextTransformStage.php
+++ b/includes/OutputTransform/ContentTextTransformStage.php
@@ -10,7 +10,7 @@ use ParserOptions;
* transform() method (either directly by inheritance or by calling them in the overloaded method).
* @internal
*/
-abstract class ContentTextTransformStage implements OutputTransformStage {
+abstract class ContentTextTransformStage extends OutputTransformStage {
public function transform( ParserOutput $po, ?ParserOptions $popts, array &$options ): ParserOutput {
$text = $po->getContentHolderText();
diff --git a/includes/OutputTransform/DefaultOutputPipelineFactory.php b/includes/OutputTransform/DefaultOutputPipelineFactory.php
index 305add29cfeb..c66375739c83 100644
--- a/includes/OutputTransform/DefaultOutputPipelineFactory.php
+++ b/includes/OutputTransform/DefaultOutputPipelineFactory.php
@@ -2,8 +2,8 @@
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;
@@ -26,12 +26,7 @@ use Wikimedia\ObjectFactory\ObjectFactory;
*/
class DefaultOutputPipelineFactory {
- /** @internal */
- public const CONSTRUCTOR_OPTIONS = [
- MainConfigNames::ParserEnableLegacyHeadingDOM, // For HandleSectionLinks
- ];
-
- private ServiceOptions $options;
+ private Config $config;
private LoggerInterface $logger;
private ObjectFactory $objectFactory;
@@ -97,11 +92,11 @@ class DefaultOutputPipelineFactory {
];
public function __construct(
- ServiceOptions $options,
+ Config $config,
LoggerInterface $logger,
ObjectFactory $objectFactory
) {
- $this->options = $options;
+ $this->config = $config;
$this->logger = $logger;
$this->objectFactory = $objectFactory;
}
@@ -115,12 +110,16 @@ class DefaultOutputPipelineFactory {
public function buildPipeline(): OutputTransformPipeline {
$otp = new OutputTransformPipeline();
foreach ( self::CORE_LIST as $spec ) {
+ $class = $spec['class'];
+ $svcOptions = new ServiceOptions(
+ $class::CONSTRUCTOR_OPTIONS, $this->config
+ );
// @phan-suppress-next-line PhanTypeInvalidCallableArraySize
$transform = $this->objectFactory->createObject(
$spec,
[
'assertClass' => OutputTransformStage::class,
- 'extraArgs' => [ $this->options, $this->logger ],
+ 'extraArgs' => [ $svcOptions, $this->logger ],
]
);
$otp->addStage( $transform );
diff --git a/includes/OutputTransform/OutputTransformStage.php b/includes/OutputTransform/OutputTransformStage.php
index e99b47cfee16..2a9722a8aaad 100644
--- a/includes/OutputTransform/OutputTransformStage.php
+++ b/includes/OutputTransform/OutputTransformStage.php
@@ -2,8 +2,10 @@
namespace MediaWiki\OutputTransform;
+use MediaWiki\Config\ServiceOptions;
use MediaWiki\Parser\ParserOutput;
use ParserOptions;
+use Psr\Log\LoggerInterface;
/**
* Classes implementing the OutputTransformStage aim at being added to a pipeline of transformations that transform
@@ -11,7 +13,21 @@ use ParserOptions;
* do not suffer from side effects is the caller's (typically the pipeline's) responsibility.
* @unstable
*/
-interface OutputTransformStage {
+abstract class OutputTransformStage {
+ protected ServiceOptions $options;
+ protected LoggerInterface $logger;
+
+ /** @internal */
+ public const CONSTRUCTOR_OPTIONS = [];
+
+ /** @internal */
+ public function __construct( ServiceOptions $options, LoggerInterface $logger ) {
+ // Note this is static:: not self:: so we use the subclass options
+ $options->assertRequiredOptions( static::CONSTRUCTOR_OPTIONS );
+ $this->options = $options;
+ $this->logger = $logger;
+ }
+
/**
* Decides whether or not the stage should be run
* @param ParserOutput $po
@@ -20,7 +36,7 @@ interface OutputTransformStage {
* @param array $options
* @return bool
*/
- public function shouldRun( ParserOutput $po, ?ParserOptions $popts, array $options = [] ): bool;
+ abstract public function shouldRun( ParserOutput $po, ?ParserOptions $popts, array $options = [] ): bool;
/**
* Transforms the input ParserOutput into the returned ParserOutput.
@@ -32,5 +48,5 @@ interface OutputTransformStage {
* Modifying $options during this pass is considered deprecated.
* @unstable
*/
- public function transform( ParserOutput $po, ?ParserOptions $popts, array &$options ): ParserOutput;
+ abstract public function transform( ParserOutput $po, ?ParserOptions $popts, array &$options ): ParserOutput;
}
diff --git a/includes/OutputTransform/Stages/AddWrapperDivClass.php b/includes/OutputTransform/Stages/AddWrapperDivClass.php
index 3628d0e8728e..7872c248cf13 100644
--- a/includes/OutputTransform/Stages/AddWrapperDivClass.php
+++ b/includes/OutputTransform/Stages/AddWrapperDivClass.php
@@ -24,6 +24,7 @@ class AddWrapperDivClass extends ContentTextTransformStage {
ServiceOptions $options, LoggerInterface $logger, LanguageFactory $langFactory,
Language $contentLang
) {
+ parent::__construct( $options, $logger );
$this->langFactory = $langFactory;
$this->contentLang = $contentLang;
}
diff --git a/includes/OutputTransform/Stages/ExecutePostCacheTransformHooks.php b/includes/OutputTransform/Stages/ExecutePostCacheTransformHooks.php
index 24464bf932e4..a0c5ce2f5087 100644
--- a/includes/OutputTransform/Stages/ExecutePostCacheTransformHooks.php
+++ b/includes/OutputTransform/Stages/ExecutePostCacheTransformHooks.php
@@ -22,6 +22,7 @@ class ExecutePostCacheTransformHooks extends ContentTextTransformStage {
public function __construct(
ServiceOptions $options, LoggerInterface $logger, HookContainer $hookContainer
) {
+ parent::__construct( $options, $logger );
$this->hookRunner = new HookRunner( $hookContainer );
$this->hookContainer = $hookContainer;
}
diff --git a/includes/OutputTransform/Stages/ExtractBody.php b/includes/OutputTransform/Stages/ExtractBody.php
index 9bd984a573f1..2ae7bd66e819 100644
--- a/includes/OutputTransform/Stages/ExtractBody.php
+++ b/includes/OutputTransform/Stages/ExtractBody.php
@@ -18,7 +18,6 @@ use Wikimedia\RemexHtml\Serializer\SerializerNode;
*/
class ExtractBody extends ContentTextTransformStage {
- private LoggerInterface $logger;
// @phan-suppress-next-line PhanUndeclaredTypeProperty
private ?\MobileContext $mobileContext;
@@ -27,7 +26,7 @@ class ExtractBody extends ContentTextTransformStage {
// @phan-suppress-next-line PhanUndeclaredTypeParameter
?\MobileContext $mobileContext
) {
- $this->logger = $logger;
+ parent::__construct( $options, $logger );
$this->mobileContext = $mobileContext;
}
diff --git a/includes/OutputTransform/Stages/HandleParsoidSectionLinks.php b/includes/OutputTransform/Stages/HandleParsoidSectionLinks.php
index 4f439fe0d6a6..63f5df91ea87 100644
--- a/includes/OutputTransform/Stages/HandleParsoidSectionLinks.php
+++ b/includes/OutputTransform/Stages/HandleParsoidSectionLinks.php
@@ -25,13 +25,12 @@ use Wikimedia\Parsoid\Utils\DOMCompat;
*/
class HandleParsoidSectionLinks extends ContentDOMTransformStage {
- private LoggerInterface $logger;
private TitleFactory $titleFactory;
public function __construct(
ServiceOptions $options, LoggerInterface $logger, TitleFactory $titleFactory
) {
- $this->logger = $logger;
+ parent::__construct( $options, $logger );
$this->titleFactory = $titleFactory;
}
diff --git a/includes/OutputTransform/Stages/HandleSectionLinks.php b/includes/OutputTransform/Stages/HandleSectionLinks.php
index d44589c8b12e..9d7fd78ee697 100644
--- a/includes/OutputTransform/Stages/HandleSectionLinks.php
+++ b/includes/OutputTransform/Stages/HandleSectionLinks.php
@@ -26,13 +26,17 @@ class HandleSectionLinks extends ContentTextTransformStage {
private const HEADING_REGEX =
'/<H(?P<level>[1-6])(?P<attrib>(?:[^\'">]*|"([^"]*)"|\'([^\']*)\')*>)(?P<header>[\s\S]*?)<\/H[1-6] *>/i';
- private ServiceOptions $options;
private TitleFactory $titleFactory;
+ /** @internal */
+ public const CONSTRUCTOR_OPTIONS = [
+ MainConfigNames::ParserEnableLegacyHeadingDOM, // For HandleSectionLinks
+ ];
+
public function __construct(
ServiceOptions $options, LoggerInterface $logger, TitleFactory $titleFactory
) {
- $this->options = $options;
+ parent::__construct( $options, $logger );
$this->titleFactory = $titleFactory;
}
diff --git a/includes/OutputTransform/Stages/HandleTOCMarkers.php b/includes/OutputTransform/Stages/HandleTOCMarkers.php
index 63ec928c46fa..68493e308694 100644
--- a/includes/OutputTransform/Stages/HandleTOCMarkers.php
+++ b/includes/OutputTransform/Stages/HandleTOCMarkers.php
@@ -28,6 +28,7 @@ class HandleTOCMarkers extends ContentTextTransformStage {
public function __construct(
ServiceOptions $options, LoggerInterface $logger, TidyDriverBase $tidy
) {
+ parent::__construct( $options, $logger );
$this->tidy = $tidy;
}
diff --git a/includes/OutputTransform/Stages/ParsoidLocalization.php b/includes/OutputTransform/Stages/ParsoidLocalization.php
index 46123c6aa825..721fbcf3715b 100644
--- a/includes/OutputTransform/Stages/ParsoidLocalization.php
+++ b/includes/OutputTransform/Stages/ParsoidLocalization.php
@@ -2,12 +2,10 @@
namespace MediaWiki\OutputTransform\Stages;
-use MediaWiki\Config\ServiceOptions;
use MediaWiki\OutputTransform\ContentDOMTransformStage;
use Message;
use ParserOptions;
use ParserOutput;
-use Psr\Log\LoggerInterface;
use Wikimedia\Bcp47Code\Bcp47Code;
use Wikimedia\Bcp47Code\Bcp47CodeValue;
use Wikimedia\Parsoid\DOM\Document;
@@ -25,12 +23,6 @@ use Wikimedia\Parsoid\Utils\DOMUtils;
*/
class ParsoidLocalization extends ContentDOMTransformStage {
- private LoggerInterface $logger;
-
- public function __construct( ServiceOptions $options, LoggerInterface $logger ) {
- $this->logger = $logger;
- }
-
public function transformDOM(
Document $doc, ParserOutput $po, ?ParserOptions $popts, array &$options
): Document {
diff --git a/includes/OutputTransform/Stages/RenderDebugInfo.php b/includes/OutputTransform/Stages/RenderDebugInfo.php
index e1a68617c555..41f3da69da6b 100644
--- a/includes/OutputTransform/Stages/RenderDebugInfo.php
+++ b/includes/OutputTransform/Stages/RenderDebugInfo.php
@@ -23,6 +23,7 @@ class RenderDebugInfo extends ContentTextTransformStage {
public function __construct(
ServiceOptions $options, LoggerInterface $logger, HookContainer $hookContainer
) {
+ parent::__construct( $options, $logger );
$this->hookRunner = new HookRunner( $hookContainer );
}