Merge "Revert "Keep backend param to search API as long as there's a backend""
[lhc/web/wiklou.git] / includes / title / NaiveForeignTitleFactory.php
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 * @license GPL 2+
20 */
21
22 /**
23 * A parser that translates page titles on a foreign wiki into ForeignTitle
24 * objects, with no knowledge of the namespace setup on the foreign site.
25 */
26 class NaiveForeignTitleFactory implements ForeignTitleFactory {
27 /**
28 * Creates a ForeignTitle object based on the page title, and optionally the
29 * namespace ID, of a page on a foreign wiki. These values could be, for
30 * example, the <title> and <ns> attributes found in an XML dump.
31 *
32 * Although exported XML dumps have contained a map of namespace IDs to names
33 * since MW 1.5, the importer used to completely ignore the <siteinfo> tag
34 * before MW 1.25. It is therefore possible that custom XML dumps (i.e. not
35 * generated by Special:Export) have been created without this metadata.
36 * As a result, this code falls back to using namespace data for the local
37 * wiki (similar to buggy pre-1.25 behaviour) if $ns is not supplied.
38 *
39 * @param string $title The page title
40 * @param int|null $ns The namespace ID, or null if this data is not available
41 * @return ForeignTitle
42 */
43 public function createForeignTitle( $title, $ns = null ) {
44 $pieces = explode( ':', $title, 2 );
45
46 global $wgContLang;
47
48 // Can we assume that the part of the page title before the colon is a
49 // namespace name?
50 //
51 // XML export schema version 0.5 and earlier (MW 1.18 and earlier) does not
52 // contain a <ns> tag, so we need to be able to handle that case.
53 //
54 // If we know the namespace ID, we assume a non-zero namespace ID means
55 // the ':' sets off a valid namespace name. If we don't know the namespace
56 // ID, we fall back to using the local wiki's namespace names to resolve
57 // this -- better than nothing, and mimics the old crappy behavior
58 $isNamespacePartValid = is_null( $ns ) ?
59 ( $wgContLang->getNsIndex( $pieces[0] ) !== false ) :
60 $ns != 0;
61
62 if ( count( $pieces ) === 2 && $isNamespacePartValid ) {
63 list( $namespaceName, $pageName ) = $pieces;
64 } else {
65 $namespaceName = '';
66 $pageName = $title;
67 }
68
69 return new ForeignTitle( $ns, $namespaceName, $pageName );
70 }
71 }