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