Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
92.31% |
12 / 13 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
NaiveImportTitleFactory | |
100.00% |
12 / 12 |
|
100.00% |
2 / 2 |
6 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
createTitleFromForeignTitle | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
5 |
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. |
7 | * |
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. |
12 | * |
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 |
17 | * |
18 | * @file |
19 | */ |
20 | |
21 | namespace MediaWiki\Title; |
22 | |
23 | use Language; |
24 | |
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. |
29 | * |
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. |
35 | */ |
36 | class NaiveImportTitleFactory implements ImportTitleFactory { |
37 | /** @var Language */ |
38 | private $contentLanguage; |
39 | |
40 | /** @var NamespaceInfo */ |
41 | private $namespaceInfo; |
42 | |
43 | /** @var TitleFactory */ |
44 | private $titleFactory; |
45 | |
46 | /** |
47 | * @param Language $contentLanguage |
48 | * @param NamespaceInfo $namespaceInfo |
49 | * @param TitleFactory $titleFactory |
50 | */ |
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 | |
61 | /** |
62 | * Determines which local title best corresponds to the given foreign title. |
63 | * If such a title can't be found or would be locally invalid, null is |
64 | * returned. |
65 | * |
66 | * @param ForeignTitle $foreignTitle The ForeignTitle to convert |
67 | * @return Title|null |
68 | */ |
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 | |
95 | /** @deprecated class alias since 1.41 */ |
96 | class_alias( NaiveImportTitleFactory::class, 'NaiveImportTitleFactory' ); |