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;
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
21class TranslatableBundleImportTitleFactory implements ImportTitleFactory {
22
23 private PageTitleRenamer $pageTitleRenamer;
24
25 public function __construct(
26 private readonly NamespaceInfo $namespaceInfo,
27 private readonly TitleFactory $titleFactory,
28 private readonly Title $targetPage,
29 ) {
30 }
31
32 public function createTitleFromForeignTitle( ForeignTitle $foreignTitle ): Title {
33 if ( !$foreignTitle->isNamespaceIdKnown() ) {
34 throw new InvalidArgumentException(
35 "Unable to determine namespace for foreign title {$foreignTitle}"
36 );
37 }
38
39 $foreignTitleNamespaceId = $foreignTitle->getNamespaceId();
40 $foreignTitleText = $foreignTitle->getText();
41
42 if ( !$this->namespaceInfo->exists( $foreignTitleNamespaceId ) ) {
43 throw new InvalidArgumentException(
44 "The foreign title $foreignTitle has a namespace that does not exist in the current wiki.\n" .
45 __CLASS__ . " does not support this functionality yet."
46 );
47 }
48
49 $titleFromForeignTitle = $this->titleFactory->makeTitle( $foreignTitleNamespaceId, $foreignTitleText );
50 // Assume that the first title is the source title
51 $this->pageTitleRenamer ??= new PageTitleRenamer( $titleFromForeignTitle, $this->targetPage );
52
53 return $this->pageTitleRenamer->getNewTitle( $titleFromForeignTitle );
54 }
55}
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.