Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
61.11% covered (warning)
61.11%
11 / 18
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
TranslatableBundleImportTitleFactory
61.11% covered (warning)
61.11%
11 / 18
50.00% covered (danger)
50.00%
1 / 2
4.94
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 createTitleFromForeignTitle
53.33% covered (warning)
53.33%
8 / 15
0.00% covered (danger)
0.00%
0 / 1
3.91
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\MessageGroupProcessing;
5
6use InvalidArgumentException;
7use MediaWiki\Extension\Translate\PageTranslation\PageTitleRenamer;
8use MediaWiki\Title\ForeignTitle;
9use MediaWiki\Title\ImportTitleFactory;
10use MediaWiki\Title\NamespaceInfo;
11use MediaWiki\Title\Title;
12use MediaWiki\Title\TitleFactory;
13
14/**
15 * A parser that translates page titles from a foreign wiki into titles on the local wiki,
16 * keeping a specified target page in mind
17 * @since 2024.02
18 * @license GPL-2.0-or-later
19 * @author Abijeet Patro
20 */
21class TranslatableBundleImportTitleFactory implements ImportTitleFactory {
22    private NamespaceInfo $namespaceInfo;
23    private PageTitleRenamer $pageTitleRenamer;
24    private Title $targetPage;
25    private TitleFactory $titleFactory;
26    private ForeignTitle $sourcePage;
27
28    public function __construct( NamespaceInfo $namespaceInfo, TitleFactory $titleFactory, Title $targetPage ) {
29        $this->namespaceInfo = $namespaceInfo;
30        $this->targetPage = $targetPage;
31        $this->titleFactory = $titleFactory;
32    }
33
34    public function createTitleFromForeignTitle( ForeignTitle $foreignTitle ): Title {
35        if ( !$foreignTitle->isNamespaceIdKnown() ) {
36            throw new InvalidArgumentException(
37                "Unable to determine namespace for foreign title {$foreignTitle}"
38            );
39        }
40
41        $foreignTitleNamespaceId = $foreignTitle->getNamespaceId();
42        $foreignTitleText = $foreignTitle->getText();
43
44        if ( !$this->namespaceInfo->exists( $foreignTitleNamespaceId ) ) {
45            throw new InvalidArgumentException(
46                "The foreign title $foreignTitle has a namespace that does not exist in the current wiki.\n" .
47                __CLASS__ . " does not support this functionality yet."
48            );
49        }
50
51        $titleFromForeignTitle = $this->titleFactory->makeTitle( $foreignTitleNamespaceId, $foreignTitleText );
52        // Assume that the first title is the source title
53        $this->sourcePage ??= $foreignTitle;
54        $this->pageTitleRenamer ??= new PageTitleRenamer( $titleFromForeignTitle, $this->targetPage );
55
56        return $this->pageTitleRenamer->getNewTitle( $titleFromForeignTitle );
57    }
58}