Change from global to parameter.
[lhc/web/wiklou.git] / maintenance / dumpInterwiki.inc
1 <?php
2 /**
3 * Rebuild interwiki table using the file on meta and the language list
4 * Wikimedia specific!
5 *
6 * @file
7 * @todo document
8 * @ingroup Maintenance
9 * @ingroup Wikimedia
10 */
11
12 /**
13 * @todo document
14 * @ingroup Maintenance
15 */
16 class Site {
17 var $suffix, $lateral, $url;
18
19 function __construct( $s, $l, $u ) {
20 $this->suffix = $s;
21 $this->lateral = $l;
22 $this->url = $u;
23 }
24
25 function getURL( $lang ) {
26 $xlang = str_replace( '_', '-', $lang );
27 return "http://$xlang.{$this->url}/wiki/\$1";
28 }
29 }
30
31 function getRebuildInterwikiDump() {
32 global $langlist, $languageAliases, $prefixRewrites, $wgContLang;
33
34 # Multi-language sites
35 # db suffix => db suffix, iw prefix, hostname
36 $sites = array(
37 'wiki' => new Site( 'wiki', 'w', 'wikipedia.org' ),
38 'wiktionary' => new Site( 'wiktionary', 'wikt', 'wiktionary.org' ),
39 'wikiquote' => new Site( 'wikiquote', 'q', 'wikiquote.org' ),
40 'wikibooks' => new Site( 'wikibooks', 'b', 'wikibooks.org' ),
41 'wikinews' => new Site( 'wikinews', 'n', 'wikinews.org' ),
42 'wikisource' => new Site( 'wikisource', 's', 'wikisource.org' ),
43 'wikimedia' => new Site( 'wikimedia', 'chapter', 'wikimedia.org' ),
44 'wikiversity' => new Site( 'wikiversity', 'v', 'wikiversity.org' ),
45 );
46
47 # List of language prefixes likely to be found in multi-language sites
48 $langlist = array_map( "trim", file( "/home/wikipedia/common/langlist" ) );
49
50 # List of all database names
51 $dblist = array_map( "trim", file( "/home/wikipedia/common/all.dblist" ) );
52
53 # Special-case databases
54 $specials = array_flip(
55 array_map( "trim",
56 file( "/home/wikipedia/common/special.dblist" ) ) );
57
58 # Extra interwiki links that can't be in the intermap for some reason
59 $extraLinks = array(
60 array( 'm', 'http://meta.wikimedia.org/wiki/$1', 1 ),
61 array( 'meta', 'http://meta.wikimedia.org/wiki/$1', 1 ),
62 array( 'sep11', 'http://sep11.wikipedia.org/wiki/$1', 1 ),
63 );
64
65 # Language aliases, usually configured as redirects to the real wiki in apache
66 # Interlanguage links are made directly to the real wiki
67 # Something horrible happens if you forget to list an alias here, I can't
68 # remember what
69 $languageAliases = array(
70 'zh-cn' => 'zh',
71 'zh-tw' => 'zh',
72 'dk' => 'da',
73 'nb' => 'no',
74 );
75
76 # Special case prefix rewrites, for the benefit of Swedish which uses s:t
77 # as an abbreviation for saint
78 $prefixRewrites = array(
79 'svwiki' => array ( 's' => 'src' ),
80 );
81
82 # Construct a list of reserved prefixes
83 $reserved = array();
84 foreach ( $langlist as $lang ) {
85 $reserved[$lang] = 1;
86 }
87 foreach ( $languageAliases as $alias => $lang ) {
88 $reserved[$alias] = 1;
89 }
90 foreach ( $sites as $site ) {
91 $reserved[$site->lateral] = 1;
92 }
93
94 # Extract the intermap from meta
95 $intermap = Http::get( 'http://meta.wikimedia.org/w/index.php?title=Interwiki_map&action=raw', 30 );
96 $lines = array_map( 'trim', explode( "\n", trim( $intermap ) ) );
97
98 if ( !$lines || count( $lines ) < 2 ) {
99 wfDie( "m:Interwiki_map not found" );
100 }
101
102 # Global iterwiki map
103 foreach ( $lines as $line ) {
104 if ( preg_match( '/^\|\s*(.*?)\s*\|\|\s*(.*?)\s*$/', $line, $matches ) ) {
105 $prefix = $wgContLang->lc( $matches[1] );
106 $prefix = str_replace( ' ', '_', $prefix );
107 $prefix = strtolower( $matches[1] );
108 $url = $matches[2];
109 if ( preg_match( '/(wikipedia|wiktionary|wikisource|wikiquote|wikibooks|wikimedia)\.org/', $url ) ) {
110 $local = 1;
111 } else {
112 $local = 0;
113 }
114
115 if ( empty( $reserved[$prefix] ) ) {
116 $imap = array( "iw_prefix" => $prefix, "iw_url" => $url, "iw_local" => $local );
117 makeLink ( $imap, "__global" );
118 }
119 }
120 }
121
122 # Exclude Wikipedia for Wikipedia
123 makeLink ( array ( 'iw_prefix' => 'wikipedia', 'is_url' => null ), "_wiki" );
124
125 # Multilanguage sites
126 foreach ( $sites as $site )
127 makeLanguageLinks ( $site, "_" . $site->suffix );
128
129
130 foreach ( $dblist as $db ) {
131 if ( isset( $specials[$db] ) ) {
132 # Special wiki
133 # Has interwiki links and interlanguage links to wikipedia
134
135 makeLink( array( 'iw_prefix' => $db, 'iw_url' => "wiki" ), "__sites" );
136 # Links to multilanguage sites
137 foreach ( $sites as $targetSite ) {
138 makeLink( array( 'iw_prefix' => $targetSite->lateral,
139 'iw_url' => $targetSite->getURL( 'en' ),
140 'iw_local' => 1 ), $db );
141 }
142
143 } else {
144 # Find out which site this DB belongs to
145 $site = false;
146 foreach ( $sites as $candidateSite ) {
147 $suffix = $candidateSite->suffix;
148 if ( preg_match( "/(.*)$suffix$/", $db, $matches ) ) {
149 $site = $candidateSite;
150 break;
151 }
152 }
153 makeLink( array( 'iw_prefix' => $db, 'iw_url' => $site->suffix ), "__sites" );
154 if ( !$site ) {
155 print "Invalid database $db\n";
156 continue;
157 }
158 $lang = $matches[1];
159
160 # Lateral links
161 foreach ( $sites as $targetSite ) {
162 if ( $targetSite->suffix != $site->suffix ) {
163 makeLink( array( 'iw_prefix' => $targetSite->lateral,
164 'iw_url' => $targetSite->getURL( $lang ),
165 'iw_local' => 1 ), $db );
166 }
167 }
168
169 if ( $site->suffix == "wiki" ) {
170 makeLink( array( 'iw_prefix' => 'w',
171 'iw_url' => "http://en.wikipedia.org/wiki/$1",
172 'iw_local' => 1 ), $db );
173 }
174
175 }
176 }
177 foreach ( $extraLinks as $link )
178 makeLink( $link, "__global" );
179 }
180
181 # ------------------------------------------------------------------------------------------
182
183 # Executes part of an INSERT statement, corresponding to all interlanguage links to a particular site
184 function makeLanguageLinks( &$site, $source ) {
185 global $langlist, $languageAliases;
186 # Actual languages with their own databases
187 foreach ( $langlist as $targetLang ) {
188 makeLink( array( $targetLang, $site->getURL( $targetLang ), 1 ), $source );
189 }
190
191 # Language aliases
192 foreach ( $languageAliases as $alias => $lang ) {
193 makeLink( array( $alias, $site->getURL( $lang ), 1 ), $source );
194 }
195 }
196
197 function makeLink( $entry, $source ) {
198 global $prefixRewrites, $dbFile;
199 if ( isset( $prefixRewrites[$source] ) && isset( $prefixRewrites[$source][$entry[0]] ) )
200 $entry[0] = $prefixRewrites[$source][$entry[0]];
201 if ( !array_key_exists( "iw_prefix", $entry ) )
202 $entry = array( "iw_prefix" => $entry[0], "iw_url" => $entry[1], "iw_local" => $entry[2] );
203 if ( array_key_exists( $source, $prefixRewrites ) &&
204 array_key_exists( $entry['iw_prefix'], $prefixRewrites[$source] ) )
205 $entry['iw_prefix'] = $prefixRewrites[$source][$entry['iw_prefix']];
206 if ( $dbFile )
207 $dbFile->set( "{$source}:{$entry['iw_prefix']}", trim( "{$entry['iw_local']} {$entry['iw_url']}" ) );
208 else
209 print "{$source}:{$entry['iw_prefix']} {$entry['iw_url']} {$entry['iw_local']}\n";
210
211 }