00001 <?php 00016 class Site { 00017 var $suffix, $lateral, $url; 00018 00019 function __construct( $s, $l, $u ) { 00020 $this->suffix = $s; 00021 $this->lateral = $l; 00022 $this->url = $u; 00023 } 00024 00025 function getURL( $lang ) { 00026 $xlang = str_replace( '_', '-', $lang ); 00027 return "http://$xlang.{$this->url}/wiki/\$1"; 00028 } 00029 } 00030 00031 function getRebuildInterwikiDump() { 00032 global $langlist, $languageAliases, $prefixRewrites; 00033 00034 # Multi-language sites 00035 # db suffix => db suffix, iw prefix, hostname 00036 $sites = array( 00037 'wiki' => new Site( 'wiki', 'w', 'wikipedia.org' ), 00038 'wiktionary' => new Site( 'wiktionary', 'wikt', 'wiktionary.org' ), 00039 'wikiquote' => new Site( 'wikiquote', 'q', 'wikiquote.org' ), 00040 'wikibooks' => new Site( 'wikibooks', 'b', 'wikibooks.org' ), 00041 'wikinews' => new Site( 'wikinews', 'n', 'wikinews.org' ), 00042 'wikisource' => new Site( 'wikisource', 's', 'wikisource.org' ), 00043 'wikimedia' => new Site( 'wikimedia', 'chapter', 'wikimedia.org' ), 00044 'wikiversity' => new Site( 'wikiversity', 'v', 'wikiversity.org' ), 00045 ); 00046 00047 # List of language prefixes likely to be found in multi-language sites 00048 $langlist = array_map( "trim", file( "/home/wikipedia/common/langlist" ) ); 00049 00050 # List of all database names 00051 $dblist = array_map( "trim", file( "/home/wikipedia/common/all.dblist" ) ); 00052 00053 # Special-case databases 00054 $specials = array_flip( 00055 array_map( "trim", 00056 file( "/home/wikipedia/common/special.dblist" ) ) ); 00057 00058 # Extra interwiki links that can't be in the intermap for some reason 00059 $extraLinks = array( 00060 array( 'm', 'http://meta.wikimedia.org/wiki/$1', 1 ), 00061 array( 'meta', 'http://meta.wikimedia.org/wiki/$1', 1 ), 00062 array( 'sep11', 'http://sep11.wikipedia.org/wiki/$1', 1 ), 00063 ); 00064 00065 # Language aliases, usually configured as redirects to the real wiki in apache 00066 # Interlanguage links are made directly to the real wiki 00067 # Something horrible happens if you forget to list an alias here, I can't 00068 # remember what 00069 $languageAliases = array( 00070 'zh-cn' => 'zh', 00071 'zh-tw' => 'zh', 00072 'dk' => 'da', 00073 'nb' => 'no', 00074 ); 00075 00076 # Special case prefix rewrites, for the benefit of Swedish which uses s:t 00077 # as an abbreviation for saint 00078 $prefixRewrites = array( 00079 'svwiki' => array ( 's' => 'src'), 00080 ); 00081 00082 # Construct a list of reserved prefixes 00083 $reserved = array(); 00084 foreach ( $langlist as $lang ) { 00085 $reserved[$lang] = 1; 00086 } 00087 foreach ( $languageAliases as $alias => $lang ) { 00088 $reserved[$alias] = 1; 00089 } 00090 foreach( $sites as $site ) { 00091 $reserved[$site->lateral] = 1; 00092 } 00093 00094 # Extract the intermap from meta 00095 $intermap = Http::get( 'http://meta.wikimedia.org/w/index.php?title=Interwiki_map&action=raw', 30 ); 00096 $lines = array_map( 'trim', explode( "\n", trim( $intermap ) ) ); 00097 00098 if ( !$lines || count( $lines ) < 2 ) { 00099 wfDie( "m:Interwiki_map not found" ); 00100 } 00101 00102 # Global iterwiki map 00103 foreach ( $lines as $line ) { 00104 if ( preg_match( '/^\|\s*(.*?)\s*\|\|\s*(.*?)\s*$/', $line, $matches ) ) { 00105 $prefix = strtolower( $matches[1] ); 00106 $url = $matches[2]; 00107 if ( preg_match( '/(wikipedia|wiktionary|wikisource|wikiquote|wikibooks|wikimedia)\.org/', $url ) ) { 00108 $local = 1; 00109 } else { 00110 $local = 0; 00111 } 00112 00113 if ( empty( $reserved[$prefix] ) ) { 00114 $imap = array( "iw_prefix" => $prefix, "iw_url" => $url, "iw_local" => $local ); 00115 makeLink ($imap, "__global"); 00116 } 00117 } 00118 } 00119 00120 # Exclude Wikipedia for Wikipedia 00121 makeLink ( array ('iw_prefix' => 'wikipedia', 'is_url' => null ), "_wiki" ); 00122 00123 #Multilanguage sites 00124 foreach ($sites as $site) 00125 makeLanguageLinks ( $site, "_".$site->suffix ); 00126 00127 00128 foreach ( $dblist as $db ) { 00129 if ( isset( $specials[$db] ) ) { 00130 # Special wiki 00131 # Has interwiki links and interlanguage links to wikipedia 00132 00133 makeLink( array( 'iw_prefix' => $db, 'iw_url' => "wiki"), "__sites" ); 00134 # Links to multilanguage sites 00135 foreach ( $sites as $targetSite ) { 00136 makeLink( array( 'iw_prefix' => $targetSite->lateral, 00137 'iw_url' =>$targetSite->getURL( 'en' ), 00138 'iw_local' => 1 ), $db ); 00139 } 00140 00141 } else { 00142 # Find out which site this DB belongs to 00143 $site = false; 00144 foreach( $sites as $candidateSite ) { 00145 $suffix = $candidateSite->suffix; 00146 if ( preg_match( "/(.*)$suffix$/", $db, $matches ) ) { 00147 $site = $candidateSite; 00148 break; 00149 } 00150 } 00151 makeLink( array( 'iw_prefix' => $db, 'iw_url' => $site->suffix), "__sites" ); 00152 if ( !$site ) { 00153 print "Invalid database $db\n"; 00154 continue; 00155 } 00156 $lang = $matches[1]; 00157 00158 # Lateral links 00159 foreach ( $sites as $targetSite ) { 00160 if ( $targetSite->suffix != $site->suffix ) { 00161 makeLink( array( 'iw_prefix' => $targetSite->lateral, 00162 'iw_url' => $targetSite->getURL( $lang ), 00163 'iw_local' => 1 ), $db ); 00164 } 00165 } 00166 00167 if ( $site->suffix == "wiki" ) { 00168 makeLink( array('iw_prefix' => 'w', 00169 'iw_url' => "http://en.wikipedia.org/wiki/$1", 00170 'iw_local' => 1), $db ); 00171 } 00172 00173 } 00174 } 00175 foreach ( $extraLinks as $link ) 00176 makeLink( $link, "__global" ); 00177 } 00178 00179 # ------------------------------------------------------------------------------------------ 00180 00181 # Executes part of an INSERT statement, corresponding to all interlanguage links to a particular site 00182 function makeLanguageLinks( &$site, $source ) { 00183 global $langlist, $languageAliases; 00184 # Actual languages with their own databases 00185 foreach ( $langlist as $targetLang ) { 00186 makeLink( array( $targetLang, $site->getURL( $targetLang ), 1 ), $source ); 00187 } 00188 00189 # Language aliases 00190 foreach ( $languageAliases as $alias => $lang ) { 00191 makeLink( array( $alias, $site->getURL( $lang ), 1 ), $source ); 00192 } 00193 } 00194 00195 function makeLink( $entry, $source ) { 00196 global $prefixRewrites, $dbFile; 00197 if ( isset( $prefixRewrites[$source] ) && isset( $prefixRewrites[$source][$entry[0]] ) ) 00198 $entry[0] = $prefixRewrites[$source][$entry[0]]; 00199 if (!array_key_exists("iw_prefix",$entry)) 00200 $entry = array("iw_prefix" => $entry[0], "iw_url" => $entry[1], "iw_local" => $entry[2]); 00201 if ( array_key_exists($source,$prefixRewrites) && 00202 array_key_exists($entry['iw_prefix'],$prefixRewrites[$source])) 00203 $entry['iw_prefix'] = $prefixRewrites[$source][$entry['iw_prefix']]; 00204 if ($dbFile) 00205 $dbFile->set( "{$source}:{$entry['iw_prefix']}", trim("{$entry['iw_local']} {$entry['iw_url']}") ); 00206 else 00207 print "{$source}:{$entry['iw_prefix']} {$entry['iw_url']} {$entry['iw_local']}\n"; 00208 00209 }