MediaWiki master
NaiveImportTitleFactory.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Title;
22
24
37 private Language $contentLanguage;
38 private NamespaceInfo $namespaceInfo;
39 private TitleFactory $titleFactory;
40
41 public function __construct(
42 Language $contentLanguage,
43 NamespaceInfo $namespaceInfo,
44 TitleFactory $titleFactory
45 ) {
46 $this->contentLanguage = $contentLanguage;
47 $this->namespaceInfo = $namespaceInfo;
48 $this->titleFactory = $titleFactory;
49 }
50
59 public function createTitleFromForeignTitle( ForeignTitle $foreignTitle ) {
60 if ( $foreignTitle->isNamespaceIdKnown() ) {
61 $foreignNs = $foreignTitle->getNamespaceId();
62
63 // For built-in namespaces (0 <= ID < 100), we try to find a local NS with
64 // the same namespace ID
65 if (
66 $foreignNs < 100 &&
67 $this->namespaceInfo->exists( $foreignNs )
68 ) {
69 return $this->titleFactory->makeTitleSafe( $foreignNs, $foreignTitle->getText() );
70 }
71 }
72
73 // Do we have a local namespace by the same name as the foreign
74 // namespace?
75 $targetNs = $this->contentLanguage->getNsIndex( $foreignTitle->getNamespaceName() );
76 if ( $targetNs !== false ) {
77 return $this->titleFactory->makeTitleSafe( $targetNs, $foreignTitle->getText() );
78 }
79
80 // Otherwise, just fall back to main namespace
81 return $this->titleFactory->makeTitleSafe( 0, $foreignTitle->getFullText() );
82 }
83}
84
86class_alias( NaiveImportTitleFactory::class, 'NaiveImportTitleFactory' );
Base class for language-specific code.
Definition Language.php:81
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 ...