Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
WikiLinkParserFactory
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
2 / 2
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getWikiLinkParser
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3namespace FileImporter\Services\Wikitext;
4
5use FileImporter\Remote\MediaWiki\MediaWikiSourceUrlParser;
6use MediaWiki\Languages\LanguageFactory;
7use MediaWiki\Title\NamespaceInfo;
8use MediaWiki\Title\TitleParser;
9
10/**
11 * @license GPL-2.0-or-later
12 * @author Thiemo Kreuz
13 */
14class WikiLinkParserFactory {
15    use MediaWikiSourceUrlParser;
16
17    private TitleParser $titleParser;
18    private NamespaceInfo $namespaceInfo;
19    private LanguageFactory $languageFactory;
20
21    public function __construct(
22        TitleParser $titleParser,
23        NamespaceInfo $namespaceInfo,
24        LanguageFactory $languageFactory
25    ) {
26        // FIXME: This needs to be a parser in the context of the *source* wiki
27        $this->titleParser = $titleParser;
28        $this->namespaceInfo = $namespaceInfo;
29        $this->languageFactory = $languageFactory;
30    }
31
32    public function getWikiLinkParser( ?string $languageCode, string $interWikiPrefix ): WikiLinkParser {
33        $parser = new WikiLinkParser();
34
35        // Minor performance optimization: skip this step if there is nothing to unlocalize
36        if ( $languageCode && $languageCode !== 'en' ) {
37            $language = $this->languageFactory->getLanguage( $languageCode );
38            $parser->registerWikiLinkCleaner( new NamespaceUnlocalizer(
39                new LocalizedMediaWikiNamespaceLookup( $language ),
40                $this->namespaceInfo
41            ) );
42        }
43
44        $parser->registerWikiLinkCleaner( new WikiLinkPrefixer(
45            $interWikiPrefix,
46            $this->titleParser
47        ) );
48
49        return $parser;
50    }
51
52}