Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
77.78% covered (warning)
77.78%
28 / 36
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
ParserFactory
80.00% covered (warning)
80.00%
28 / 35
50.00% covered (danger)
50.00%
2 / 4
6.29
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 create
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
1 / 1
1
 getMainInstance
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 getInstance
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * @license GPL-2.0-or-later
4 * @file
5 * @ingroup Parser
6 */
7
8namespace MediaWiki\Parser;
9
10use MediaWiki\Category\TrackingCategories;
11use MediaWiki\Config\ServiceOptions;
12use MediaWiki\HookContainer\HookContainer;
13use MediaWiki\Http\HttpRequestFactory;
14use MediaWiki\Language\Language;
15use MediaWiki\Language\LanguageConverterFactory;
16use MediaWiki\Language\LanguageNameUtils;
17use MediaWiki\Linker\LinkRendererFactory;
18use MediaWiki\Page\File\BadFileLookup;
19use MediaWiki\Preferences\SignatureValidatorFactory;
20use MediaWiki\SpecialPage\SpecialPageFactory;
21use MediaWiki\Tidy\TidyDriverBase;
22use MediaWiki\Title\NamespaceInfo;
23use MediaWiki\Title\TitleFormatter;
24use MediaWiki\User\Options\UserOptionsLookup;
25use MediaWiki\User\UserFactory;
26use MediaWiki\User\UserNameUtils;
27use MediaWiki\Utils\UrlUtils;
28use Psr\Log\LoggerInterface;
29use Wikimedia\ObjectCache\WANObjectCache;
30
31/**
32 * @since 1.32
33 */
34class ParserFactory {
35    /**
36     * Track calls to Parser constructor to aid in deprecation of direct
37     * Parser invocation.  This is temporary: it will be removed once the
38     * deprecation notice period is over and the underlying method calls
39     * are refactored.
40     * @internal
41     * @var int
42     */
43    public static $inParserFactory = 0;
44
45    /** @var Parser|null */
46    private $mainInstance;
47
48    /**
49     * @since 1.32
50     * @internal
51     */
52    public function __construct(
53        private readonly ServiceOptions $svcOptions,
54        private readonly ParserCoreTagHooks $parserCoreTagHooks,
55        private readonly MagicWordFactory $magicWordFactory,
56        private readonly Language $contLang,
57        private readonly UrlUtils $urlUtils,
58        private readonly SpecialPageFactory $specialPageFactory,
59        private readonly LinkRendererFactory $linkRendererFactory,
60        private readonly NamespaceInfo $nsInfo,
61        private readonly LoggerInterface $logger,
62        private readonly BadFileLookup $badFileLookup,
63        private readonly LanguageConverterFactory $languageConverterFactory,
64        private readonly LanguageNameUtils $languageNameUtils,
65        private readonly HookContainer $hookContainer,
66        private readonly TidyDriverBase $tidy,
67        private readonly WANObjectCache $wanCache,
68        private readonly UserOptionsLookup $userOptionsLookup,
69        private readonly UserFactory $userFactory,
70        private readonly TitleFormatter $titleFormatter,
71        private readonly HttpRequestFactory $httpRequestFactory,
72        private readonly TrackingCategories $trackingCategories,
73        private readonly SignatureValidatorFactory $signatureValidatorFactory,
74        private readonly UserNameUtils $userNameUtils,
75    ) {
76        $svcOptions->assertRequiredOptions( Parser::CONSTRUCTOR_OPTIONS );
77
78        wfDebug( __CLASS__ . ": using default preprocessor" );
79    }
80
81    /**
82     * Creates a new parser
83     *
84     * @note Use this function to get a new Parser instance to store
85     * in a local class property.  Where possible use lazy creation and
86     * create the Parser only when needed, not directly in service wiring.
87     *
88     * @return Parser
89     * @since 1.32
90     */
91    public function create(): Parser {
92        self::$inParserFactory++;
93        try {
94            return new Parser(
95                $this->svcOptions,
96                $this->parserCoreTagHooks,
97                $this->magicWordFactory,
98                $this->contLang,
99                $this->urlUtils,
100                $this->specialPageFactory,
101                $this->linkRendererFactory,
102                $this->nsInfo,
103                $this->logger,
104                $this->badFileLookup,
105                $this->languageConverterFactory,
106                $this->languageNameUtils,
107                $this->hookContainer,
108                $this->tidy,
109                $this->wanCache,
110                $this->userOptionsLookup,
111                $this->userFactory,
112                $this->titleFormatter,
113                $this->httpRequestFactory,
114                $this->trackingCategories,
115                $this->signatureValidatorFactory,
116                $this->userNameUtils
117            );
118        } finally {
119            self::$inParserFactory--;
120        }
121    }
122
123    /**
124     * Get the main shared instance. This is unsafe when the caller is not in
125     * a top-level context, because re-entering the parser will throw an
126     * exception.
127     *
128     * @note This function is used to get metadata from the parser. Avoid
129     * using this function to parse wikitext.  (Generally avoid using this
130     * function at all in new code.)
131     *
132     * @since 1.39
133     * @return Parser
134     */
135    public function getMainInstance() {
136        if ( $this->mainInstance === null ) {
137            $this->mainInstance = $this->create();
138        }
139        return $this->mainInstance;
140    }
141
142    /**
143     * Get the main shared instance, or if it is locked, get a new instance
144     *
145     * @note This function was used to parse wikitext. The instance it
146     * returned should be used only in local scope.  Do not hold a
147     * reference to this parser in class properties.  In general,
148     * avoid using this method and use ::create() instead.
149     *
150     * @since 1.39
151     * @return Parser
152     */
153    public function getInstance() {
154        $instance = $this->getMainInstance();
155        if ( $instance->isLocked() ) {
156            $instance = $this->create();
157        }
158        return $instance;
159    }
160
161}
162
163/** @deprecated class alias since 1.43 */
164class_alias( ParserFactory::class, 'ParserFactory' );