3 * Rebuild interwiki table using the file on meta and the language list
13 * @ingroup Maintenance
16 var $suffix, $lateral, $url;
18 function __construct( $s, $l, $u ) {
24 function getURL( $lang ) {
25 $xlang = str_replace( '_', '-', $lang );
26 return "http://$xlang.{$this->url}/wiki/\$1";
30 function getRebuildInterwikiDump() {
31 global $langlist, $languageAliases, $prefixRewrites;
33 # Multi-language sites
34 # db suffix => db suffix, iw prefix, hostname
36 'wiki' => new Site( 'wiki', 'w', 'wikipedia.org' ),
37 'wiktionary' => new Site( 'wiktionary', 'wikt', 'wiktionary.org' ),
38 'wikiquote' => new Site( 'wikiquote', 'q', 'wikiquote.org' ),
39 'wikibooks' => new Site( 'wikibooks', 'b', 'wikibooks.org' ),
40 'wikinews' => new Site( 'wikinews', 'n', 'wikinews.org' ),
41 'wikisource' => new Site( 'wikisource', 's', 'wikisource.org' ),
42 'wikimedia' => new Site( 'wikimedia', 'chapter', 'wikimedia.org' ),
43 'wikiversity' => new Site( 'wikiversity', 'v', 'wikiversity.org' ),
46 # List of language prefixes likely to be found in multi-language sites
47 $langlist = array_map( "trim", file( "/home/wikipedia/common/langlist" ) );
49 # List of all database names
50 $dblist = array_map( "trim", file( "/home/wikipedia/common/all.dblist" ) );
52 # Special-case databases
53 $specials = array_flip(
55 file( "/home/wikipedia/common/special.dblist" ) ) );
57 # Extra interwiki links that can't be in the intermap for some reason
59 array( 'm', 'http://meta.wikimedia.org/wiki/$1', 1 ),
60 array( 'meta', 'http://meta.wikimedia.org/wiki/$1', 1 ),
61 array( 'sep11', 'http://sep11.wikipedia.org/wiki/$1', 1 ),
64 # Language aliases, usually configured as redirects to the real wiki in apache
65 # Interlanguage links are made directly to the real wiki
66 # Something horrible happens if you forget to list an alias here, I can't
68 $languageAliases = array(
75 # Special case prefix rewrites, for the benefit of Swedish which uses s:t
76 # as an abbreviation for saint
77 $prefixRewrites = array(
78 'svwiki' => array ( 's' => 'src'),
81 # Construct a list of reserved prefixes
83 foreach ( $langlist as $lang ) {
86 foreach ( $languageAliases as $alias => $lang ) {
87 $reserved[$alias] = 1;
89 foreach( $sites as $site ) {
90 $reserved[$site->lateral] = 1;
93 # Extract the intermap from meta
94 $intermap = Http::get( 'http://meta.wikimedia.org/w/index.php?title=Interwiki_map&action=raw', 30 );
95 $lines = array_map( 'trim', explode( "\n", trim( $intermap ) ) );
97 if ( !$lines || count( $lines ) < 2 ) {
98 wfDie( "m:Interwiki_map not found" );
101 # Global iterwiki map
102 foreach ( $lines as $line ) {
103 if ( preg_match( '/^\|\s*(.*?)\s*\|\|\s*(.*?)\s*$/', $line, $matches ) ) {
104 $prefix = strtolower( $matches[1] );
106 if ( preg_match( '/(wikipedia|wiktionary|wikisource|wikiquote|wikibooks|wikimedia)\.org/', $url ) ) {
112 if ( empty( $reserved[$prefix] ) ) {
113 $imap = array( "iw_prefix" => $prefix, "iw_url" => $url, "iw_local" => $local );
114 makeLink ($imap, "__global");
119 # Exclude Wikipedia for Wikipedia
120 makeLink ( array ('iw_prefix' => 'wikipedia', 'is_url' => null ), "_wiki" );
123 foreach ($sites as $site)
124 makeLanguageLinks ( $site, "_".$site->suffix );
127 foreach ( $dblist as $db ) {
128 if ( isset( $specials[$db] ) ) {
130 # Has interwiki links and interlanguage links to wikipedia
132 makeLink( array( 'iw_prefix' => $db, 'iw_url' => "wiki"), "__sites" );
133 # Links to multilanguage sites
134 foreach ( $sites as $targetSite ) {
135 makeLink( array( 'iw_prefix' => $targetSite->lateral,
136 'iw_url' =>$targetSite->getURL( 'en' ),
137 'iw_local' => 1 ), $db );
141 # Find out which site this DB belongs to
143 foreach( $sites as $candidateSite ) {
144 $suffix = $candidateSite->suffix;
145 if ( preg_match( "/(.*)$suffix$/", $db, $matches ) ) {
146 $site = $candidateSite;
150 makeLink( array( 'iw_prefix' => $db, 'iw_url' => $site->suffix), "__sites" );
152 print "Invalid database $db\n";
158 foreach ( $sites as $targetSite ) {
159 if ( $targetSite->suffix != $site->suffix ) {
160 makeLink( array( 'iw_prefix' => $targetSite->lateral,
161 'iw_url' => $targetSite->getURL( $lang ),
162 'iw_local' => 1 ), $db );
166 if ( $site->suffix == "wiki" ) {
167 makeLink( array('iw_prefix' => 'w',
168 'iw_url' => "http://en.wikipedia.org/wiki/$1",
169 'iw_local' => 1), $db );
174 foreach ( $extraLinks as $link )
175 makeLink( $link, "__global" );
178 # ------------------------------------------------------------------------------------------
180 # Executes part of an INSERT statement, corresponding to all interlanguage links to a particular site
181 function makeLanguageLinks( &$site, $source ) {
182 global $langlist, $languageAliases;
183 # Actual languages with their own databases
184 foreach ( $langlist as $targetLang ) {
185 makeLink( array( $targetLang, $site->getURL( $targetLang ), 1 ), $source );
189 foreach ( $languageAliases as $alias => $lang ) {
190 makeLink( array( $alias, $site->getURL( $lang ), 1 ), $source );
194 function makeLink( $entry, $source ) {
195 global $prefixRewrites, $dbFile;
196 if ( isset( $prefixRewrites[$source] ) && isset( $prefixRewrites[$source][$entry[0]] ) )
197 $entry[0] = $prefixRewrites[$source][$entry[0]];
198 if (!array_key_exists("iw_prefix",$entry))
199 $entry = array("iw_prefix" => $entry[0], "iw_url" => $entry[1], "iw_local" => $entry[2]);
200 if ( array_key_exists($source,$prefixRewrites) &&
201 array_key_exists($entry['iw_prefix'],$prefixRewrites[$source]))
202 $entry['iw_prefix'] = $prefixRewrites[$source][$entry['iw_prefix']];
204 dba_insert("{$source}:{$entry['iw_prefix']}", trim("{$entry['iw_local']} {$entry['iw_url']}"),$dbFile);
206 print "{$source}:{$entry['iw_prefix']} {$entry['iw_url']} {$entry['iw_local']}\n";