Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
TranslatableBundleImportTitleFactory.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\MessageGroupProcessing;
5
6use ForeignTitle;
7use InvalidArgumentException;
9use MediaWiki\Title\ImportTitleFactory;
10use MediaWiki\Title\NamespaceInfo;
11use MediaWiki\Title\Title;
12use MediaWiki\Title\TitleFactory;
13
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}
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.