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
|
<?
# Rebuild interwiki table using the file on meta and the language list
# Wikimedia specific!
$oldCwd = getcwd();
$optionsWithArgs = array( "o" );
include_once( "commandLine.inc" );
class Site {
var $suffix, $lateral, $url;
function Site( $s, $l, $u ) {
$this->suffix = $s;
$this->lateral = $l;
$this->url = $u;
}
function getURL( $lang ) {
return "http://$lang.{$this->url}/wiki/\$1";
}
}
# Initialise lists of wikis
$sites = array(
'wiki' => new Site( 'wiki', 'w', 'wikipedia.org' ),
'wiktionary' => new Site( 'wiktionary', 'wikt', 'wiktionary.org' )
);
$langlist = array_map( "trim", file( "/home/wikipedia/common/langlist" ) );
$specials = array(
'sourceswiki' => 'sources.wikipedia.org',
'quotewiki' => 'wikiquote.org',
'textbookwiki' => 'wikibooks.org',
'sep11wiki' => 'sep11.wikipedia.org',
'metawiki' => 'meta.wikipedia.org',
);
$extraLinks = array(
array( 'm', 'http://meta.wikipedia.org/wiki/$1', 1 ),
array( 'meta', 'http://meta.wikipedia.org/wiki/$1', 1 ),
array( 'sep11', 'http://sep11.wikipedia.org/wiki/$1', 1 ),
);
$languageAliases = array(
'zh-cn' => 'zh',
'zh-tw' => 'zh',
);
# Extract the intermap from meta
$row = wfGetArray( "metawiki.cur", array( "cur_text" ), array( "cur_namespace" => 0, "cur_title" => "Interwiki_map" ) );
if ( !$row ) {
die( "m:Interwiki_map not found" );
}
$lines = explode( "\n", $row->cur_text );
$iwArray = array();
foreach ( $lines as $line ) {
if ( preg_match( '/^\|\s*(.*?)\s*\|\|\s*(.*?)\s*$/', $line, $matches ) ) {
$prefix = $matches[1];
$url = $matches[2];
if ( preg_match( '/(wikipedia|wiktionary|wikisource|wikiquote|wikibooks)\.org/', $url ) ) {
$local = 1;
} else {
$local = 0;
}
$iwArray[] = array( "iw_prefix" => $prefix, "iw_url" => $url, "iw_local" => $local );
}
}
# Insert links into special wikis
# These have intermap links and interlanguage links pointing to wikipedia
$sql = "-- Generated by rebuildInterwiki.php";
foreach ( $specials as $db => $host ) {
$sql .= "\nUSE $db;\n" .
"TRUNCATE TABLE interwiki;\n" .
"INSERT INTO interwiki (iw_prefix, iw_url, iw_local) VALUES \n";
$first = true;
# Intermap links
foreach ( $iwArray as $iwEntry ) {
# Suppress links to self
if ( strpos( $iwEntry['iw_url'], $host ) === false ) {
$sql .= makeLink( $iwEntry, $first );
}
}
# w link
$sql .= makeLink( array("w", "http://en.wikipedia.org/wiki/$1", 1 ), $first );
# Interlanguage links to wikipedia
$sql .= makeLanguageLinks( $sites['wiki'], $first );
# Extra links
foreach ( $extraLinks as $link ) {
$sql .= makeLink( $link, $first );
}
$sql .= ";\n";
}
$sql .= "\n";
# Insert links into multilanguage sites
foreach ( $sites as $site ) {
$sql .= <<<EOS
---
--- {$site->suffix}
---
EOS;
foreach ( $langlist as $lang ) {
$db = $lang . $site->suffix;
$db = str_replace( "-", "_", $db );
$sql .= "USE $db;\n" .
"TRUNCATE TABLE interwiki;\n" .
"INSERT INTO interwiki (iw_prefix,iw_url,iw_local) VALUES\n";
$first = true;
# Intermap links
foreach ( $iwArray as $iwEntry ) {
# Suppress links to self
if ( strpos( $iwEntry['iw_url'], $site->url ) === false ||
strpos( $iwEntry['iw_url'], 'meta.wikipedia.org' ) !== false ) {
$sql .= makeLink( $iwEntry, $first );
}
}
# Lateral links
foreach ( $sites as $targetSite ) {
# Suppress link to self
if ( $targetSite->suffix != $site->suffix ) {
$sql .= makeLink( array( $targetSite->lateral, $targetSite->getURL( $lang ), 1 ), $first );
}
}
# Interlanguage links
$sql .= makeLanguageLinks( $site, $first );
# w link within wikipedias
# Other sites already have it as a lateral link
if ( $site->suffix == "wiki" ) {
$sql .= makeLink( array("w", "http://en.wikipedia.org/wiki/$1", 1), $first );
}
# Extra links
foreach ( $extraLinks as $link ){
$sql .= makeLink( $link, $first );
}
$sql .= ";\n\n";
}
}
# Output
if ( isset( $options['o'] ) ) {
# To file specified with -o
chdir( $oldCwd );
$file = fopen( $options['o'], "w" );
fwrite( $file, $sql );
fclose( $file );
} else {
# To stdout
print $sql;
}
# ------------------------------------------------------------------------------------------
# Returns part of an INSERT statement, corresponding to all interlanguage links to a particular site
function makeLanguageLinks( &$site, &$first ) {
global $langlist, $languageAliases;
$sql = "";
# Actual languages with their own databases
foreach ( $langlist as $targetLang ) {
$sql .= makeLink( array( $targetLang, $site->getURL( $targetLang ), 1 ), $first );
}
# Language aliases
foreach ( $languageAliases as $alias => $lang ) {
$sql .= makeLink( array( $alias, $site->getURL( $lang ), 1 ), $first );
}
return $sql;
}
# Make SQL for a single link from an array
function makeLink( $entry, &$first ) {
$sql = "";
# Add comma
if ( $first ) {
$first = false;
} else {
$sql .= ",\n";
}
$sql .= "(" . Database::makeList( $entry ) . ")";
return $sql;
}
?>
|