Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
61.11% |
11 / 18 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
TranslatableBundleImportTitleFactory | |
61.11% |
11 / 18 |
|
50.00% |
1 / 2 |
4.94 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
createTitleFromForeignTitle | |
53.33% |
8 / 15 |
|
0.00% |
0 / 1 |
3.91 |
1 | <?php |
2 | declare( strict_types = 1 ); |
3 | |
4 | namespace MediaWiki\Extension\Translate\MessageGroupProcessing; |
5 | |
6 | use InvalidArgumentException; |
7 | use MediaWiki\Extension\Translate\PageTranslation\PageTitleRenamer; |
8 | use MediaWiki\Title\ForeignTitle; |
9 | use MediaWiki\Title\ImportTitleFactory; |
10 | use MediaWiki\Title\NamespaceInfo; |
11 | use MediaWiki\Title\Title; |
12 | use 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 | */ |
21 | class 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 | } |