MediaWiki  master
NaiveImportTitleFactory.php
Go to the documentation of this file.
1 <?php
22 
36  private $contentLanguage;
37 
39  private $namespaceInfo;
40 
42  private $titleFactory;
43 
49  public function __construct(
50  Language $contentLanguage,
51  NamespaceInfo $namespaceInfo,
52  TitleFactory $titleFactory
53  ) {
54  $this->contentLanguage = $contentLanguage;
55  $this->namespaceInfo = $namespaceInfo;
56  $this->titleFactory = $titleFactory;
57  }
58 
67  public function createTitleFromForeignTitle( ForeignTitle $foreignTitle ) {
68  if ( $foreignTitle->isNamespaceIdKnown() ) {
69  $foreignNs = $foreignTitle->getNamespaceId();
70 
71  // For built-in namespaces (0 <= ID < 100), we try to find a local NS with
72  // the same namespace ID
73  if (
74  $foreignNs < 100 &&
75  $this->namespaceInfo->exists( $foreignNs )
76  ) {
77  return $this->titleFactory->makeTitleSafe( $foreignNs, $foreignTitle->getText() );
78  }
79  }
80 
81  // Do we have a local namespace by the same name as the foreign
82  // namespace?
83  $targetNs = $this->contentLanguage->getNsIndex( $foreignTitle->getNamespaceName() );
84  if ( $targetNs !== false ) {
85  return $this->titleFactory->makeTitleSafe( $targetNs, $foreignTitle->getText() );
86  }
87 
88  // Otherwise, just fall back to main namespace
89  return $this->titleFactory->makeTitleSafe( 0, $foreignTitle->getFullText() );
90  }
91 }
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?
Base class for language-specific code.
Definition: Language.php:56
Creates Title objects.
A class to convert page titles on a foreign wiki (ForeignTitle objects) into page titles on the local...
__construct(Language $contentLanguage, NamespaceInfo $namespaceInfo, TitleFactory $titleFactory)
createTitleFromForeignTitle(ForeignTitle $foreignTitle)
Determines which local title best corresponds to the given foreign title.
This is a utility class for dealing with namespaces that encodes all the "magic" behaviors of them ba...
Represents an object that can convert page titles on a foreign wiki (ForeignTitle objects) into page ...