MediaWiki master
NaiveImportTitleFactory.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Title;
8
10
23 private Language $contentLanguage;
24 private NamespaceInfo $namespaceInfo;
25 private TitleFactory $titleFactory;
26
27 public function __construct(
28 Language $contentLanguage,
29 NamespaceInfo $namespaceInfo,
30 TitleFactory $titleFactory
31 ) {
32 $this->contentLanguage = $contentLanguage;
33 $this->namespaceInfo = $namespaceInfo;
34 $this->titleFactory = $titleFactory;
35 }
36
45 public function createTitleFromForeignTitle( ForeignTitle $foreignTitle ) {
46 if ( $foreignTitle->isNamespaceIdKnown() ) {
47 $foreignNs = $foreignTitle->getNamespaceId();
48
49 // For built-in namespaces (0 <= ID < 100), we try to find a local NS with
50 // the same namespace ID
51 if (
52 $foreignNs < 100 &&
53 $this->namespaceInfo->exists( $foreignNs )
54 ) {
55 return $this->titleFactory->makeTitleSafe( $foreignNs, $foreignTitle->getText() );
56 }
57 }
58
59 // Do we have a local namespace by the same name as the foreign
60 // namespace?
61 $targetNs = $this->contentLanguage->getNsIndex( $foreignTitle->getNamespaceName() );
62 if ( $targetNs !== false ) {
63 return $this->titleFactory->makeTitleSafe( $targetNs, $foreignTitle->getText() );
64 }
65
66 // Otherwise, just fall back to main namespace
67 return $this->titleFactory->makeTitleSafe( 0, $foreignTitle->getFullText() );
68 }
69}
70
72class_alias( NaiveImportTitleFactory::class, 'NaiveImportTitleFactory' );
Base class for language-specific code.
Definition Language.php:70
A simple, immutable structure to hold the title of a page on a foreign MediaWiki installation.
isNamespaceIdKnown()
Do we know the namespace ID of the page on the foreign wiki?
A class to convert page titles on a foreign wiki (ForeignTitle objects) into page titles on the local...
createTitleFromForeignTitle(ForeignTitle $foreignTitle)
Determines which local title best corresponds to the given foreign title.
__construct(Language $contentLanguage, NamespaceInfo $namespaceInfo, TitleFactory $titleFactory)
This is a utility class for dealing with namespaces that encodes all the "magic" behaviors of them ba...
Creates Title objects.
Represents an object that can convert page titles on a foreign wiki (ForeignTitle objects) into page ...