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
21 namespace MediaWiki\Title
;
23 use MediaWiki\Language\Language
;
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.
29 class NaiveForeignTitleFactory
implements ForeignTitleFactory
{
31 private Language
$contentLanguage;
33 public function __construct( Language
$contentLanguage ) {
34 $this->contentLanguage
= $contentLanguage;
38 * Create a ForeignTitle object.
40 * Based on the page title and optionally the namespace ID, of a page on a foreign wiki.
41 * These values could be, for example, the `<title>` and `<ns>` attributes found in an
44 * Although exported XML dumps have contained a map of namespace IDs to names
45 * since MW 1.5, the importer used to completely ignore the `<siteinfo>` tag
46 * before MW 1.25. It is therefore possible that custom XML dumps (i.e. not
47 * generated by Special:Export) have been created without this metadata.
48 * As a result, this code falls back to using namespace data for the local
49 * wiki (similar to buggy pre-1.25 behaviour) if $ns is not supplied.
51 * @param string $title The page title
52 * @param int|null $ns The namespace ID, or null if this data is not available
53 * @return ForeignTitle
55 public function createForeignTitle( $title, $ns = null ) {
56 $pieces = explode( ':', $title, 2 );
59 * Can we assume that the part of the page title before the colon is a
62 * XML export schema version 0.5 and earlier (MW 1.18 and earlier) does not
63 * contain a <ns> tag, so we need to be able to handle that case.
65 * If we know the namespace ID, we assume a non-zero namespace ID means
66 * the ':' sets off a valid namespace name. If we don't know the namespace
67 * ID, we fall back to using the local wiki's namespace names to resolve
68 * this -- better than nothing, and mimics the old crappy behavior
70 $isNamespacePartValid = $ns === null
71 ?
$this->contentLanguage
->getNsIndex( $pieces[0] ) !== false
74 if ( count( $pieces ) === 2 && $isNamespacePartValid ) {
75 [ $namespaceName, $pageName ] = $pieces;
81 return new ForeignTitle( $ns, $namespaceName, $pageName );
85 /** @deprecated class alias since 1.41 */
86 class_alias( NaiveForeignTitleFactory
::class, 'NaiveForeignTitleFactory' );