Merge "Type hint against LinkTarget in WatchedItemStore"
[lhc/web/wiklou.git] / includes / parser / ParserFactory.php
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 use MediaWiki\Config\ServiceOptions;
23 use MediaWiki\Linker\LinkRendererFactory;
24 use MediaWiki\MediaWikiServices;
25 use MediaWiki\Special\SpecialPageFactory;
26 use Psr\Log\LoggerInterface;
27 use Psr\Log\NullLogger;
28
29 /**
30 * @since 1.32
31 */
32 class ParserFactory {
33 /** @var ServiceOptions */
34 private $svcOptions;
35
36 /** @var MagicWordFactory */
37 private $magicWordFactory;
38
39 /** @var Language */
40 private $contLang;
41
42 /** @var string */
43 private $urlProtocols;
44
45 /** @var SpecialPageFactory */
46 private $specialPageFactory;
47
48 /** @var LinkRendererFactory */
49 private $linkRendererFactory;
50
51 /** @var NamespaceInfo */
52 private $nsInfo;
53
54 /** @var LoggerInterface */
55 private $logger;
56
57 /**
58 * Old parameter list, which we support for backwards compatibility, were:
59 * array $parserConf See $wgParserConf documentation
60 * MagicWordFactory $magicWordFactory
61 * Language $contLang Content language
62 * string $urlProtocols As returned from wfUrlProtocols()
63 * SpecialPageFactory $spFactory
64 * Config $siteConfig
65 * LinkRendererFactory $linkRendererFactory
66 * NamespaceInfo|null $nsInfo
67 *
68 * Some type declarations were intentionally omitted so that the backwards compatibility code
69 * would work. When backwards compatibility is no longer required, we should remove it, and
70 * and add the omitted type declarations.
71 *
72 * @param ServiceOptions|array $svcOptions
73 * @param MagicWordFactory $magicWordFactory
74 * @param Language $contLang Content language
75 * @param string $urlProtocols As returned from wfUrlProtocols()
76 * @param SpecialPageFactory $spFactory
77 * @param LinkRendererFactory $linkRendererFactory
78 * @param NamespaceInfo|LinkRendererFactory|null $nsInfo
79 * @param LoggerInterface|null $logger
80 * @since 1.32
81 */
82 public function __construct(
83 $svcOptions,
84 MagicWordFactory $magicWordFactory,
85 Language $contLang,
86 $urlProtocols,
87 SpecialPageFactory $spFactory,
88 $linkRendererFactory,
89 $nsInfo = null,
90 $logger = null
91 ) {
92 // @todo Do we need to retain compat for constructing this class directly?
93 if ( !$nsInfo ) {
94 wfDeprecated( __METHOD__ . ' with no NamespaceInfo argument', '1.34' );
95 $nsInfo = MediaWikiServices::getInstance()->getNamespaceInfo();
96 }
97 if ( $linkRendererFactory instanceof Config ) {
98 // Old calling convention had an array in the format of $wgParserConf as the first
99 // parameter, and a Config as the sixth, with LinkRendererFactory as the seventh.
100 wfDeprecated( __METHOD__ . ' with Config parameter', '1.34' );
101 $svcOptions = new ServiceOptions( Parser::$constructorOptions,
102 $svcOptions,
103 [ 'class' => Parser::class,
104 'preprocessorClass' => Parser::getDefaultPreprocessorClass() ],
105 func_get_arg( 5 )
106 );
107 $linkRendererFactory = func_get_arg( 6 );
108 $nsInfo = func_num_args() > 7 ? func_get_arg( 7 ) : null;
109 }
110 $svcOptions->assertRequiredOptions( Parser::$constructorOptions );
111
112 wfDebug( __CLASS__ . ": using preprocessor: {$svcOptions->get( 'preprocessorClass' )}\n" );
113
114 $this->svcOptions = $svcOptions;
115 $this->magicWordFactory = $magicWordFactory;
116 $this->contLang = $contLang;
117 $this->urlProtocols = $urlProtocols;
118 $this->specialPageFactory = $spFactory;
119 $this->linkRendererFactory = $linkRendererFactory;
120 $this->nsInfo = $nsInfo;
121 $this->logger = $logger ?: new NullLogger();
122 }
123
124 /**
125 * @return Parser
126 * @since 1.32
127 */
128 public function create() : Parser {
129 return new Parser(
130 $this->svcOptions,
131 $this->magicWordFactory,
132 $this->contLang,
133 $this,
134 $this->urlProtocols,
135 $this->specialPageFactory,
136 $this->linkRendererFactory,
137 $this->nsInfo,
138 $this->logger
139 );
140 }
141 }