Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / title / NaiveImportTitleFactory.php
blob8b3813aa661691a620399b9ca93efefe96460f39
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @file
21 namespace MediaWiki\Title;
23 use MediaWiki\Language\Language;
25 /**
26 * A class to convert page titles on a foreign wiki (ForeignTitle objects) into
27 * page titles on the local wiki (Title objects), using a default namespace
28 * mapping.
30 * For built-in namespaces (0 <= ID < 100), we try to find a local namespace
31 * with the same namespace ID as the foreign page. If no such namespace exists,
32 * or the namespace ID is unknown or > 100, we look for a local namespace with
33 * a matching namespace name. If that can't be found, we dump the page in the
34 * main namespace as a last resort.
36 class NaiveImportTitleFactory implements ImportTitleFactory {
37 private Language $contentLanguage;
38 private NamespaceInfo $namespaceInfo;
39 private TitleFactory $titleFactory;
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;
51 /**
52 * Determines which local title best corresponds to the given foreign title.
53 * If such a title can't be found or would be locally invalid, null is
54 * returned.
56 * @param ForeignTitle $foreignTitle The ForeignTitle to convert
57 * @return Title|null
59 public function createTitleFromForeignTitle( ForeignTitle $foreignTitle ) {
60 if ( $foreignTitle->isNamespaceIdKnown() ) {
61 $foreignNs = $foreignTitle->getNamespaceId();
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() );
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() );
80 // Otherwise, just fall back to main namespace
81 return $this->titleFactory->makeTitleSafe( 0, $foreignTitle->getFullText() );
85 /** @deprecated class alias since 1.41 */
86 class_alias( NaiveImportTitleFactory::class, 'NaiveImportTitleFactory' );