MediaWiki master
NaiveImportTitleFactory.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Title;
22
23use Language;
24
38 private $contentLanguage;
39
41 private $namespaceInfo;
42
44 private $titleFactory;
45
51 public function __construct(
52 Language $contentLanguage,
53 NamespaceInfo $namespaceInfo,
54 TitleFactory $titleFactory
55 ) {
56 $this->contentLanguage = $contentLanguage;
57 $this->namespaceInfo = $namespaceInfo;
58 $this->titleFactory = $titleFactory;
59 }
60
69 public function createTitleFromForeignTitle( ForeignTitle $foreignTitle ) {
70 if ( $foreignTitle->isNamespaceIdKnown() ) {
71 $foreignNs = $foreignTitle->getNamespaceId();
72
73 // For built-in namespaces (0 <= ID < 100), we try to find a local NS with
74 // the same namespace ID
75 if (
76 $foreignNs < 100 &&
77 $this->namespaceInfo->exists( $foreignNs )
78 ) {
79 return $this->titleFactory->makeTitleSafe( $foreignNs, $foreignTitle->getText() );
80 }
81 }
82
83 // Do we have a local namespace by the same name as the foreign
84 // namespace?
85 $targetNs = $this->contentLanguage->getNsIndex( $foreignTitle->getNamespaceName() );
86 if ( $targetNs !== false ) {
87 return $this->titleFactory->makeTitleSafe( $targetNs, $foreignTitle->getText() );
88 }
89
90 // Otherwise, just fall back to main namespace
91 return $this->titleFactory->makeTitleSafe( 0, $foreignTitle->getFullText() );
92 }
93}
94
96class_alias( NaiveImportTitleFactory::class, 'NaiveImportTitleFactory' );
Base class for language-specific code.
Definition Language.php:65
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 ...