Inject LinkRendererFactory into Parser
authorAryeh Gregor <ayg@aryeh.name>
Wed, 8 Aug 2018 14:57:31 +0000 (17:57 +0300)
committerAryeh Gregor <ayg@aryeh.name>
Sun, 7 Apr 2019 12:02:30 +0000 (15:02 +0300)
Change-Id: Idaf5a0f897dc3bd2aa9bf03be280281836bfc645

includes/ServiceWiring.php
includes/parser/Parser.php
includes/parser/ParserFactory.php

index cccb5e7..70a4cd4 100644 (file)
@@ -368,7 +368,8 @@ return [
                        $services->getContentLanguage(),
                        wfUrlProtocols(),
                        $services->getSpecialPageFactory(),
-                       $services->getMainConfig()
+                       $services->getMainConfig(),
+                       $services->getLinkRendererFactory()
                );
        },
 
index 0ced8a1..47e5b40 100644 (file)
@@ -21,6 +21,7 @@
  * @ingroup Parser
  */
 use MediaWiki\Linker\LinkRenderer;
+use MediaWiki\Linker\LinkRendererFactory;
 use MediaWiki\MediaWikiServices;
 use MediaWiki\Special\SpecialPageFactory;
 use Wikimedia\ScopedCallback;
@@ -276,6 +277,9 @@ class Parser {
        /** @var Config */
        private $siteConfig;
 
+       /** @var LinkRendererFactory */
+       private $linkRendererFactory;
+
        /**
         * @param array $parserConf See $wgParserConf documentation
         * @param MagicWordFactory|null $magicWordFactory
@@ -284,11 +288,13 @@ class Parser {
         * @param string|null $urlProtocols As returned from wfUrlProtocols()
         * @param SpecialPageFactory|null $spFactory
         * @param Config|null $siteConfig
+        * @param LinkRendererFactory|null $linkRendererFactory
         */
        public function __construct(
                array $parserConf = [], MagicWordFactory $magicWordFactory = null,
                Language $contLang = null, ParserFactory $factory = null, $urlProtocols = null,
-               SpecialPageFactory $spFactory = null, Config $siteConfig = null
+               SpecialPageFactory $spFactory = null, Config $siteConfig = null,
+               LinkRendererFactory $linkRendererFactory = null
        ) {
                $this->mConf = $parserConf;
                $this->mUrlProtocols = $urlProtocols ?? wfUrlProtocols();
@@ -320,6 +326,9 @@ class Parser {
                $this->factory = $factory ?? $services->getParserFactory();
                $this->specialPageFactory = $spFactory ?? $services->getSpecialPageFactory();
                $this->siteConfig = $siteConfig ?? MediaWikiServices::getInstance()->getMainConfig();
+
+               $this->linkRendererFactory =
+                       $linkRendererFactory ?? MediaWikiServices::getInstance()->getLinkRendererFactory();
        }
 
        /**
@@ -973,9 +982,9 @@ class Parser {
         * @return LinkRenderer
         */
        public function getLinkRenderer() {
+               // XXX We make the LinkRenderer with current options and then cache it forever
                if ( !$this->mLinkRenderer ) {
-                       $this->mLinkRenderer = MediaWikiServices::getInstance()
-                               ->getLinkRendererFactory()->create();
+                       $this->mLinkRenderer = $this->linkRendererFactory->create();
                        $this->mLinkRenderer->setStubThreshold(
                                $this->getOptions()->getStubThreshold()
                        );
index eb05ace..05c0622 100644 (file)
@@ -18,6 +18,7 @@
  * @file
  * @ingroup Parser
  */
+use MediaWiki\Linker\LinkRendererFactory;
 
 use MediaWiki\Special\SpecialPageFactory;
 
@@ -43,6 +44,9 @@ class ParserFactory {
        /** @var Config */
        private $siteConfig;
 
+       /** @var LinkRendererFactory */
+       private $linkRendererFactory;
+
        /**
         * @param array $parserConf See $wgParserConf documentation
         * @param MagicWordFactory $magicWordFactory
@@ -50,11 +54,12 @@ class ParserFactory {
         * @param string $urlProtocols As returned from wfUrlProtocols()
         * @param SpecialPageFactory $spFactory
         * @param Config $siteConfig
+        * @param LinkRendererFactory $linkRendererFactory
         * @since 1.32
         */
        public function __construct(
                array $parserConf, MagicWordFactory $magicWordFactory, Language $contLang, $urlProtocols,
-               SpecialPageFactory $spFactory, Config $siteConfig
+               SpecialPageFactory $spFactory, Config $siteConfig, LinkRendererFactory $linkRendererFactory
        ) {
                $this->parserConf = $parserConf;
                $this->magicWordFactory = $magicWordFactory;
@@ -62,6 +67,7 @@ class ParserFactory {
                $this->urlProtocols = $urlProtocols;
                $this->specialPageFactory = $spFactory;
                $this->siteConfig = $siteConfig;
+               $this->linkRendererFactory = $linkRendererFactory;
        }
 
        /**
@@ -70,6 +76,7 @@ class ParserFactory {
         */
        public function create() : Parser {
                return new Parser( $this->parserConf, $this->magicWordFactory, $this->contLang, $this,
-                       $this->urlProtocols, $this->specialPageFactory, $this->siteConfig );
+                       $this->urlProtocols, $this->specialPageFactory, $this->siteConfig,
+                       $this->linkRendererFactory );
        }
 }