Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
90.91% covered (success)
90.91%
10 / 11
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
NaiveForeignTitleFactory
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
2 / 2
5
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 createForeignTitle
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
4
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 Language;
24
25/**
26 * A parser that translates page titles on a foreign wiki into ForeignTitle
27 * objects, with no knowledge of the namespace setup on the foreign site.
28 */
29class NaiveForeignTitleFactory implements ForeignTitleFactory {
30
31    /** @var Language */
32    private $contentLanguage;
33
34    /**
35     * @param Language $contentLanguage
36     */
37    public function __construct( Language $contentLanguage ) {
38        $this->contentLanguage = $contentLanguage;
39    }
40
41    /**
42     * Create a ForeignTitle object.
43     *
44     * Based on the page title and optionally the namespace ID, of a page on a foreign wiki.
45     * These values could be, for example, the `<title>` and `<ns>` attributes found in an
46     * XML dump.
47     *
48     * Although exported XML dumps have contained a map of namespace IDs to names
49     * since MW 1.5, the importer used to completely ignore the `<siteinfo>` tag
50     * before MW 1.25.  It is therefore possible that custom XML dumps (i.e. not
51     * generated by Special:Export) have been created without this metadata.
52     * As a result, this code falls back to using namespace data for the local
53     * wiki (similar to buggy pre-1.25 behaviour) if $ns is not supplied.
54     *
55     * @param string $title The page title
56     * @param int|null $ns The namespace ID, or null if this data is not available
57     * @return ForeignTitle
58     */
59    public function createForeignTitle( $title, $ns = null ) {
60        $pieces = explode( ':', $title, 2 );
61
62        /**
63         * Can we assume that the part of the page title before the colon is a
64         * namespace name?
65         *
66         * XML export schema version 0.5 and earlier (MW 1.18 and earlier) does not
67         * contain a <ns> tag, so we need to be able to handle that case.
68         *
69         * If we know the namespace ID, we assume a non-zero namespace ID means
70         * the ':' sets off a valid namespace name. If we don't know the namespace
71         * ID, we fall back to using the local wiki's namespace names to resolve
72         * this -- better than nothing, and mimics the old crappy behavior
73         */
74        $isNamespacePartValid = $ns === null
75            ? $this->contentLanguage->getNsIndex( $pieces[0] ) !== false
76            : $ns != 0;
77
78        if ( count( $pieces ) === 2 && $isNamespacePartValid ) {
79            [ $namespaceName, $pageName ] = $pieces;
80        } else {
81            $namespaceName = '';
82            $pageName = $title;
83        }
84
85        return new ForeignTitle( $ns, $namespaceName, $pageName );
86    }
87}
88
89/** @deprecated class alias since 1.41 */
90class_alias( NaiveForeignTitleFactory::class, 'NaiveForeignTitleFactory' );