Debian Codegraph @

includes/parser/ParserFactory.php

raw symbols SCIP
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
22 namespace MediaWiki\Parser;
23
24 use MediaWiki\Category\TrackingCategories;
25 use MediaWiki\Config\ServiceOptions;
26 use MediaWiki\HookContainer\HookContainer;
27 use MediaWiki\Http\HttpRequestFactory;
28 use MediaWiki\Language\Language;
29 use MediaWiki\Languages\LanguageConverterFactory;
30 use MediaWiki\Languages\LanguageNameUtils;
31 use MediaWiki\Linker\LinkRendererFactory;
32 use MediaWiki\Page\File\BadFileLookup;
33 use MediaWiki\Preferences\SignatureValidatorFactory;
34 use MediaWiki\SpecialPage\SpecialPageFactory;
35 use MediaWiki\Tidy\TidyDriverBase;
36 use MediaWiki\Title\NamespaceInfo;
37 use MediaWiki\Title\TitleFormatter;
38 use MediaWiki\User\Options\UserOptionsLookup;
39 use MediaWiki\User\UserFactory;
40 use MediaWiki\User\UserNameUtils;
41 use MediaWiki\Utils\UrlUtils;
42 use Psr\Log\LoggerInterface;
43 use Wikimedia\ObjectCache\WANObjectCache;
44
45 /**
46 * @since 1.32
47 */
48 class ParserFactory {
49 /** @var ServiceOptions */
50 private $svcOptions;
51
52 /** @var MagicWordFactory */
53 private $magicWordFactory;
54
55 /** @var Language */
56 private $contLang;
57
58 /** @var UrlUtils */
59 private $urlUtils;
60
61 /** @var SpecialPageFactory */
62 private $specialPageFactory;
63
64 /** @var LinkRendererFactory */
65 private $linkRendererFactory;
66
67 /** @var NamespaceInfo */
68 private $nsInfo;
69
70 /** @var LoggerInterface */
71 private $logger;
72
73 /** @var BadFileLookup */
74 private $badFileLookup;
75
76 /** @var LanguageConverterFactory */
77 private $languageConverterFactory;
78
79 /** @var LanguageNameUtils */
80 private $languageNameUtils;
81
82 /** @var UserOptionsLookup */
83 private $userOptionsLookup;
84
85 /** @var UserFactory */
86 private $userFactory;
87
88 /** @var TitleFormatter */
89 private $titleFormatter;
90
91 /** @var HttpRequestFactory */
92 private $httpRequestFactory;
93
94 /** @var TrackingCategories */
95 private $trackingCategories;
96
97 /** @var SignatureValidatorFactory */
98 private $signatureValidatorFactory;
99
100 /** @var UserNameUtils */
101 private $userNameUtils;
102
103 /**
104 * Track calls to Parser constructor to aid in deprecation of direct
105 * Parser invocation. This is temporary: it will be removed once the
106 * deprecation notice period is over and the underlying method calls
107 * are refactored.
108 * @internal
109 * @var int
110 */
111 public static $inParserFactory = 0;
112
113 /** @var HookContainer */
114 private $hookContainer;
115
116 /** @var TidyDriverBase */
117 private $tidy;
118
119 /** @var WANObjectCache */
120 private $wanCache;
121
122 /** @var Parser|null */
123 private $mainInstance;
124
125 /**
126 * @param ServiceOptions $svcOptions
127 * @param MagicWordFactory $magicWordFactory
128 * @param Language $contLang Content language
129 * @param UrlUtils $urlUtils
130 * @param SpecialPageFactory $spFactory
131 * @param LinkRendererFactory $linkRendererFactory
132 * @param NamespaceInfo $nsInfo
133 * @param LoggerInterface $logger
134 * @param BadFileLookup $badFileLookup
135 * @param LanguageConverterFactory $languageConverterFactory
136 * @param LanguageNameUtils $languageNameUtils
137 * @param HookContainer $hookContainer
138 * @param TidyDriverBase $tidy
139 * @param WANObjectCache $wanCache
140 * @param UserOptionsLookup $userOptionsLookup
141 * @param UserFactory $userFactory
142 * @param TitleFormatter $titleFormatter
143 * @param HttpRequestFactory $httpRequestFactory
144 * @param TrackingCategories $trackingCategories
145 * @param SignatureValidatorFactory $signatureValidatorFactory
146 * @param UserNameUtils $userNameUtils
147 * @since 1.32
148 * @internal
149 */
150 public function __construct(
151 ServiceOptions $svcOptions,
152 MagicWordFactory $magicWordFactory,
153 Language $contLang,
154 UrlUtils $urlUtils,
155 SpecialPageFactory $spFactory,
156 LinkRendererFactory $linkRendererFactory,
157 NamespaceInfo $nsInfo,
158 LoggerInterface $logger,
159 BadFileLookup $badFileLookup,
160 LanguageConverterFactory $languageConverterFactory,
161 LanguageNameUtils $languageNameUtils,
162 HookContainer $hookContainer,
163 TidyDriverBase $tidy,
164 WANObjectCache $wanCache,
165 UserOptionsLookup $userOptionsLookup,
166 UserFactory $userFactory,
167 TitleFormatter $titleFormatter,
168 HttpRequestFactory $httpRequestFactory,
169 TrackingCategories $trackingCategories,
170 SignatureValidatorFactory $signatureValidatorFactory,
171 UserNameUtils $userNameUtils
172 ) {
173 $svcOptions->assertRequiredOptions( Parser::CONSTRUCTOR_OPTIONS );
174
175 wfDebug( __CLASS__ . ": using default preprocessor" );
176
177 $this->svcOptions = $svcOptions;
178 $this->magicWordFactory = $magicWordFactory;
179 $this->contLang = $contLang;
180 $this->urlUtils = $urlUtils;
181 $this->specialPageFactory = $spFactory;
182 $this->linkRendererFactory = $linkRendererFactory;
183 $this->nsInfo = $nsInfo;
184 $this->logger = $logger;
185 $this->badFileLookup = $badFileLookup;
186 $this->languageConverterFactory = $languageConverterFactory;
187 $this->languageNameUtils = $languageNameUtils;
188 $this->hookContainer = $hookContainer;
189 $this->tidy = $tidy;
190 $this->wanCache = $wanCache;
191 $this->userOptionsLookup = $userOptionsLookup;
192 $this->userFactory = $userFactory;
193 $this->titleFormatter = $titleFormatter;
194 $this->httpRequestFactory = $httpRequestFactory;
195 $this->trackingCategories = $trackingCategories;
196 $this->signatureValidatorFactory = $signatureValidatorFactory;
197 $this->userNameUtils = $userNameUtils;
198 }
199
200 /**
201 * Creates a new parser
202 *
203 * @note Use this function to get a new Parser instance to store
204 * in a local class property. Where possible use lazy creation and
205 * create the Parser only when needed, not directly in service wiring.
206 *
207 * @return Parser
208 * @since 1.32
209 */
210 public function create(): Parser {
211 self::$inParserFactory++;
212 try {
213 return new Parser(
214 $this->svcOptions,
215 $this->magicWordFactory,
216 $this->contLang,
217 $this,
218 $this->urlUtils,
219 $this->specialPageFactory,
220 $this->linkRendererFactory,
221 $this->nsInfo,
222 $this->logger,
223 $this->badFileLookup,
224 $this->languageConverterFactory,
225 $this->languageNameUtils,
226 $this->hookContainer,
227 $this->tidy,
228 $this->wanCache,
229 $this->userOptionsLookup,
230 $this->userFactory,
231 $this->titleFormatter,
232 $this->httpRequestFactory,
233 $this->trackingCategories,
234 $this->signatureValidatorFactory,
235 $this->userNameUtils
236 );
237 } finally {
238 self::$inParserFactory--;
239 }
240 }
241
242 /**
243 * Get the main shared instance. This is unsafe when the caller is not in
244 * a top-level context, because re-entering the parser will throw an
245 * exception.
246 *
247 * @note This function is used to get metadata from the parser. Avoid
248 * using this function to parse wikitext. (Generally avoid using this
249 * function at all in new code.)
250 *
251 * @since 1.39
252 * @return Parser
253 */
254 public function getMainInstance() {
255 if ( $this->mainInstance === null ) {
256 $this->mainInstance = $this->create();
257 }
258 return $this->mainInstance;
259 }
260
261 /**
262 * Get the main shared instance, or if it is locked, get a new instance
263 *
264 * @note This function was used to parse wikitext. The instance it
265 * returned should be used only in local scope. Do not hold a
266 * reference to this parser in class properties. In general,
267 * avoid using this method and use ::create() instead.
268 *
269 * @since 1.39
270 * @return Parser
271 */
272 public function getInstance() {
273 $instance = $this->getMainInstance();
274 if ( $instance->isLocked() ) {
275 $instance = $this->create();
276 }
277 return $instance;
278 }
279
280 }
281
282 /** @deprecated class alias since 1.43 */
283 class_alias( ParserFactory::class, 'ParserFactory' );