Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
TranslatableBundleImportTitleFactory.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\MessageGroupProcessing;
5
6use InvalidArgumentException;
8use MediaWiki\Title\ForeignTitle;
9use MediaWiki\Title\ImportTitleFactory;
10use MediaWiki\Title\NamespaceInfo;
11use MediaWiki\Title\Title;
12use MediaWiki\Title\TitleFactory;
13
21class TranslatableBundleImportTitleFactory implements ImportTitleFactory {
22
23 private PageTitleRenamer $pageTitleRenamer;
24 private ForeignTitle $sourcePage;
25
26 public function __construct(
27 private readonly NamespaceInfo $namespaceInfo,
28 private readonly TitleFactory $titleFactory,
29 private readonly Title $targetPage,
30 ) {
31 }
32
33 public function createTitleFromForeignTitle( ForeignTitle $foreignTitle ): Title {
34 if ( !$foreignTitle->isNamespaceIdKnown() ) {
35 throw new InvalidArgumentException(
36 "Unable to determine namespace for foreign title {$foreignTitle}"
37 );
38 }
39
40 $foreignTitleNamespaceId = $foreignTitle->getNamespaceId();
41 $foreignTitleText = $foreignTitle->getText();
42
43 if ( !$this->namespaceInfo->exists( $foreignTitleNamespaceId ) ) {
44 throw new InvalidArgumentException(
45 "The foreign title $foreignTitle has a namespace that does not exist in the current wiki.\n" .
46 __CLASS__ . " does not support this functionality yet."
47 );
48 }
49
50 $titleFromForeignTitle = $this->titleFactory->makeTitle( $foreignTitleNamespaceId, $foreignTitleText );
51 // Assume that the first title is the source title
52 $this->sourcePage ??= $foreignTitle;
53 $this->pageTitleRenamer ??= new PageTitleRenamer( $titleFromForeignTitle, $this->targetPage );
54
55 return $this->pageTitleRenamer->getNewTitle( $titleFromForeignTitle );
56 }
57}
A parser that translates page titles from a foreign wiki into titles on the local wiki,...
Contains logic to determine the new title of translatable pages and dependent pages being moved.