1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @ingroup Parser
20 */
21
22use MediaWiki\BadFileLookup;
23use MediaWiki\Config\ServiceOptions;
24use MediaWiki\HookContainer\HookContainer;
25use MediaWiki\Http\HttpRequestFactory;
26use MediaWiki\Languages\LanguageConverterFactory;
27use MediaWiki\Linker\LinkRendererFactory;
28use MediaWiki\SpecialPage\SpecialPageFactory;
29use MediaWiki\Tidy\TidyDriverBase;
30use MediaWiki\User\UserFactory;
31use MediaWiki\User\UserOptionsLookup;
32use Psr\Log\LoggerInterface;
33
34/**
35 * @since 1.32
36 */
37class ParserFactory {
38	/** @var ServiceOptions */
39	private $svcOptions;
40
41	/** @var MagicWordFactory */
42	private $magicWordFactory;
43
44	/** @var Language */
45	private $contLang;
46
47	/** @var string */
48	private $urlProtocols;
49
50	/** @var SpecialPageFactory */
51	private $specialPageFactory;
52
53	/** @var LinkRendererFactory */
54	private $linkRendererFactory;
55
56	/** @var NamespaceInfo */
57	private $nsInfo;
58
59	/** @var LoggerInterface */
60	private $logger;
61
62	/** @var BadFileLookup */
63	private $badFileLookup;
64
65	/** @var LanguageConverterFactory */
66	private $languageConverterFactory;
67
68	/** @var UserOptionsLookup */
69	private $userOptionsLookup;
70
71	/** @var UserFactory */
72	private $userFactory;
73
74	/** @var TitleFormatter */
75	private $titleFormatter;
76
77	/** @var HttpRequestFactory */
78	private $httpRequestFactory;
79
80	/**
81	 * Track calls to Parser constructor to aid in deprecation of direct
82	 * Parser invocation.  This is temporary: it will be removed once the
83	 * deprecation notice period is over and the underlying method calls
84	 * are refactored.
85	 * @internal
86	 * @var int
87	 */
88	public static $inParserFactory = 0;
89
90	/** @var HookContainer */
91	private $hookContainer;
92
93	/** @var TidyDriverBase */
94	private $tidy;
95
96	/** @var WANObjectCache */
97	private $wanCache;
98
99	/**
100	 * @param ServiceOptions $svcOptions
101	 * @param MagicWordFactory $magicWordFactory
102	 * @param Language $contLang Content language
103	 * @param string $urlProtocols As returned from wfUrlProtocols()
104	 * @param SpecialPageFactory $spFactory
105	 * @param LinkRendererFactory $linkRendererFactory
106	 * @param NamespaceInfo $nsInfo
107	 * @param LoggerInterface $logger
108	 * @param BadFileLookup $badFileLookup
109	 * @param LanguageConverterFactory $languageConverterFactory
110	 * @param HookContainer $hookContainer
111	 * @param TidyDriverBase $tidy
112	 * @param WANObjectCache $wanCache
113	 * @param UserOptionsLookup $userOptionsLookup
114	 * @param UserFactory $userFactory
115	 * @param TitleFormatter $titleFormatter
116	 * @param HttpRequestFactory $httpRequestFactory
117	 * @since 1.32
118	 * @internal
119	 */
120	public function __construct(
121		ServiceOptions $svcOptions,
122		MagicWordFactory $magicWordFactory,
123		Language $contLang,
124		string $urlProtocols,
125		SpecialPageFactory $spFactory,
126		LinkRendererFactory $linkRendererFactory,
127		NamespaceInfo $nsInfo,
128		LoggerInterface $logger,
129		BadFileLookup $badFileLookup,
130		LanguageConverterFactory $languageConverterFactory,
131		HookContainer $hookContainer,
132		TidyDriverBase $tidy,
133		WANObjectCache $wanCache,
134		UserOptionsLookup $userOptionsLookup,
135		UserFactory $userFactory,
136		TitleFormatter $titleFormatter,
137		HttpRequestFactory $httpRequestFactory
138	) {
139		$svcOptions->assertRequiredOptions( Parser::CONSTRUCTOR_OPTIONS );
140
141		wfDebug( __CLASS__ . ": using default preprocessor" );
142
143		$this->svcOptions = $svcOptions;
144		$this->magicWordFactory = $magicWordFactory;
145		$this->contLang = $contLang;
146		$this->urlProtocols = $urlProtocols;
147		$this->specialPageFactory = $spFactory;
148		$this->linkRendererFactory = $linkRendererFactory;
149		$this->nsInfo = $nsInfo;
150		$this->logger = $logger;
151		$this->badFileLookup = $badFileLookup;
152		$this->languageConverterFactory = $languageConverterFactory;
153		$this->hookContainer = $hookContainer;
154		$this->tidy = $tidy;
155		$this->wanCache = $wanCache;
156		$this->userOptionsLookup = $userOptionsLookup;
157		$this->userFactory = $userFactory;
158		$this->titleFormatter = $titleFormatter;
159		$this->httpRequestFactory = $httpRequestFactory;
160	}
161
162	/**
163	 * Creates a new parser
164	 *
165	 * @return Parser
166	 * @since 1.32
167	 */
168	public function create(): Parser {
169		self::$inParserFactory++;
170		try {
171			return new Parser(
172				$this->svcOptions,
173				$this->magicWordFactory,
174				$this->contLang,
175				$this,
176				$this->urlProtocols,
177				$this->specialPageFactory,
178				$this->linkRendererFactory,
179				$this->nsInfo,
180				$this->logger,
181				$this->badFileLookup,
182				$this->languageConverterFactory,
183				$this->hookContainer,
184				$this->tidy,
185				$this->wanCache,
186				$this->userOptionsLookup,
187				$this->userFactory,
188				$this->titleFormatter,
189				$this->httpRequestFactory
190			);
191		} finally {
192			self::$inParserFactory--;
193		}
194	}
195}
196