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