summaryrefslogtreecommitdiff
path: root/maintenance/dumpInterwiki.inc
blob: c366b08cce7e495a8c3c70aaea189a4191efbe22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
/**
 * Rebuild interwiki table using the file on meta and the language list
 * Wikimedia specific!
 *
 * @file
 * @todo document
 * @ingroup Maintenance
 * @ingroup Wikimedia
 */

/**
 * @todo document
 * @ingroup Maintenance
 */
class Site {
	var $suffix, $lateral, $url;

	function __construct( $s, $l, $u ) {
		$this->suffix = $s;
		$this->lateral = $l;
		$this->url = $u;
	}

	function getURL( $lang ) {
		$xlang = str_replace( '_', '-', $lang );
		return "http://$xlang.{$this->url}/wiki/\$1";
	}
}

function getRebuildInterwikiDump() {
	global $langlist, $languageAliases, $prefixRewrites;

	# Multi-language sites
	# db suffix => db suffix, iw prefix, hostname
	$sites = array(
		'wiki' => new Site( 'wiki', 'w', 'wikipedia.org' ),
		'wiktionary' => new Site( 'wiktionary', 'wikt', 'wiktionary.org' ),
		'wikiquote' => new Site( 'wikiquote', 'q', 'wikiquote.org' ),
		'wikibooks' => new Site( 'wikibooks', 'b', 'wikibooks.org' ),
		'wikinews' => new Site( 'wikinews', 'n', 'wikinews.org' ),
		'wikisource' => new Site( 'wikisource', 's', 'wikisource.org' ),
		'wikimedia' => new Site( 'wikimedia', 'chapter', 'wikimedia.org' ),
		'wikiversity' => new Site( 'wikiversity', 'v', 'wikiversity.org' ),
	);

	# List of language prefixes likely to be found in multi-language sites
	$langlist = array_map( "trim", file( "/home/wikipedia/common/langlist" ) );

	# List of all database names
	$dblist = array_map( "trim", file( "/home/wikipedia/common/all.dblist" ) );

	# Special-case databases
	$specials = array_flip(
		array_map( "trim",
			file( "/home/wikipedia/common/special.dblist" ) ) );

	# Extra interwiki links that can't be in the intermap for some reason
	$extraLinks = array(
		array( 'm', 'http://meta.wikimedia.org/wiki/$1', 1 ),
		array( 'meta', 'http://meta.wikimedia.org/wiki/$1', 1 ),
		array( 'sep11', 'http://sep11.wikipedia.org/wiki/$1', 1 ),
	);

	# Language aliases, usually configured as redirects to the real wiki in apache
	# Interlanguage links are made directly to the real wiki
	# Something horrible happens if you forget to list an alias here, I can't
	#   remember what
	$languageAliases = array(
		'zh-cn' => 'zh',
		'zh-tw' => 'zh',
		'dk' => 'da',
		'nb' => 'no',
	);

	# Special case prefix rewrites, for the benefit of Swedish which uses s:t
	# as an abbreviation for saint
	$prefixRewrites = array(
                'svwiki' => array ( 's' => 'src'),
	); 

	# Construct a list of reserved prefixes
	$reserved = array();
	foreach ( $langlist as $lang ) {
		$reserved[$lang] = 1;
	}
	foreach ( $languageAliases as $alias => $lang ) {
		$reserved[$alias] = 1;
	}
	foreach( $sites as $site ) {
		$reserved[$site->lateral] = 1;
	}

	# Extract the intermap from meta
	$intermap = Http::get( 'http://meta.wikimedia.org/w/index.php?title=Interwiki_map&action=raw', 30 );
	$lines = array_map( 'trim', explode( "\n", trim( $intermap ) ) );

	if ( !$lines || count( $lines ) < 2 ) {
		wfDie( "m:Interwiki_map not found" );
	}

	# Global iterwiki map
	foreach ( $lines as $line ) {
		if ( preg_match( '/^\|\s*(.*?)\s*\|\|\s*(.*?)\s*$/', $line, $matches ) ) {
			$prefix = strtolower( $matches[1] );
			$url = $matches[2];
			if ( preg_match( '/(wikipedia|wiktionary|wikisource|wikiquote|wikibooks|wikimedia)\.org/', $url ) ) {
				$local = 1;
			} else {
				$local = 0;
			}

			if ( empty( $reserved[$prefix] ) ) {
			    $imap  = array( "iw_prefix" => $prefix, "iw_url" => $url, "iw_local" => $local );
                            makeLink ($imap, "__global");
			}
		}
	}

        # Exclude Wikipedia for Wikipedia
        makeLink ( array ('iw_prefix' => 'wikipedia', 'is_url' => null ), "_wiki" );
        
        #Multilanguage sites
        foreach ($sites as $site) 
            makeLanguageLinks ( $site, "_".$site->suffix );


        foreach ( $dblist as $db ) {
		if ( isset( $specials[$db] ) ) {
			# Special wiki
			# Has interwiki links and interlanguage links to wikipedia

                        makeLink( array( 'iw_prefix' => $db, 'iw_url' => "wiki"), "__sites" );
			# Links to multilanguage sites
			foreach ( $sites as $targetSite ) {
				makeLink( array( 'iw_prefix' => $targetSite->lateral, 
                                                    'iw_url' =>$targetSite->getURL( 'en' ),
                                                    'iw_local' => 1 ), $db );
			}

		} else {
			# Find out which site this DB belongs to
			$site = false;
			foreach( $sites as $candidateSite ) {
				$suffix = $candidateSite->suffix;
				if ( preg_match( "/(.*)$suffix$/", $db, $matches ) ) {
				$site = $candidateSite;
					break;
				}
			}
                        makeLink( array( 'iw_prefix' => $db, 'iw_url' => $site->suffix), "__sites" );
			if ( !$site ) {
				print "Invalid database $db\n";
				continue;
			}
			$lang = $matches[1];

			# Lateral links
			foreach ( $sites as $targetSite ) {
				if ( $targetSite->suffix != $site->suffix ) {
					makeLink( array( 'iw_prefix' => $targetSite->lateral, 
                                                            'iw_url' => $targetSite->getURL( $lang ), 
                                                            'iw_local' => 1 ), $db );
				}
			}

			if ( $site->suffix == "wiki" ) {
				makeLink( array('iw_prefix' => 'w', 
                                                'iw_url' => "http://en.wikipedia.org/wiki/$1", 
                                                'iw_local' => 1), $db );
			}

		}
	}
	foreach ( $extraLinks as $link )
		makeLink( $link, "__global" );
}

# ------------------------------------------------------------------------------------------

# Executes part of an INSERT statement, corresponding to all interlanguage links to a particular site
function makeLanguageLinks( &$site, $source ) {
	global $langlist, $languageAliases;
	# Actual languages with their own databases
	foreach ( $langlist as $targetLang ) {
		makeLink( array( $targetLang, $site->getURL( $targetLang ), 1 ), $source );
	}

	# Language aliases
	foreach ( $languageAliases as $alias => $lang ) {
		makeLink( array( $alias, $site->getURL( $lang ), 1 ), $source );
	}
}

function makeLink( $entry, $source ) {
	global $prefixRewrites, $dbFile;
	if ( isset( $prefixRewrites[$source] ) && isset( $prefixRewrites[$source][$entry[0]] ) )
		$entry[0] = $prefixRewrites[$source][$entry[0]];
        if (!array_key_exists("iw_prefix",$entry))
            $entry = array("iw_prefix" => $entry[0], "iw_url" => $entry[1], "iw_local" => $entry[2]);
        if ( array_key_exists($source,$prefixRewrites) && 
                array_key_exists($entry['iw_prefix'],$prefixRewrites[$source]))
                    $entry['iw_prefix'] = $prefixRewrites[$source][$entry['iw_prefix']];
        if ($dbFile)
            $dbFile->set( "{$source}:{$entry['iw_prefix']}", trim("{$entry['iw_local']} {$entry['iw_url']}") );
        else
            print "{$source}:{$entry['iw_prefix']} {$entry['iw_url']} {$entry['iw_local']}\n";

    }