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