Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
66.67% covered (warning)
66.67%
4 / 6
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
NamespaceImportTitleFactory
80.00% covered (warning)
80.00%
4 / 5
50.00% covered (danger)
50.00%
1 / 2
3.07
0.00% covered (danger)
0.00%
0 / 1
 __construct
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
 createTitleFromForeignTitle
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
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
21namespace MediaWiki\Title;
22
23use InvalidArgumentException;
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), placing all pages in a fixed
28 * local namespace.
29 */
30class NamespaceImportTitleFactory implements ImportTitleFactory {
31    /** @var TitleFactory */
32    private $titleFactory;
33
34    /** @var int */
35    private $ns;
36
37    /**
38     * @param NamespaceInfo $namespaceInfo
39     * @param TitleFactory $titleFactory
40     * @param int $ns The namespace to use for all pages
41     */
42    public function __construct(
43        NamespaceInfo $namespaceInfo,
44        TitleFactory $titleFactory,
45        int $ns
46    ) {
47        if ( !$namespaceInfo->exists( $ns ) ) {
48            throw new InvalidArgumentException( "Namespace $ns doesn't exist on this wiki" );
49        }
50        $this->titleFactory = $titleFactory;
51        $this->ns = $ns;
52    }
53
54    /**
55     * Determines which local title best corresponds to the given foreign title.
56     * If such a title can't be found or would be locally invalid, null is
57     * returned.
58     *
59     * @param ForeignTitle $foreignTitle The ForeignTitle to convert
60     * @return Title|null
61     */
62    public function createTitleFromForeignTitle( ForeignTitle $foreignTitle ) {
63        return $this->titleFactory->makeTitleSafe( $this->ns, $foreignTitle->getText() );
64    }
65}
66
67/** @deprecated class alias since 1.41 */
68class_alias( NamespaceImportTitleFactory::class, 'NamespaceImportTitleFactory' );