Main Page | Namespace List | Class Hierarchy | Class List | File List | Class Members | File Members | Related Pages

rebuildInterwiki.php

Go to the documentation of this file.
00001 <? 00002 00003 # Rebuild interwiki table using the file on meta and the language list 00004 # Wikimedia specific! 00005 $oldCwd = getcwd(); 00006 00007 $optionsWithArgs = array( "o" ); 00008 include( "commandLine.inc" ); 00009 00010 class Site { 00011 var $suffix, $lateral, $url; 00012 00013 function Site( $s, $l, $u ) { 00014 $this->suffix = $s; 00015 $this->lateral = $l; 00016 $this->url = $u; 00017 } 00018 00019 function getURL( $lang ) { 00020 return "http://$lang.{$this->url}/wiki/\$1"; 00021 } 00022 } 00023 00024 # Initialise lists of wikis 00025 $sites = array( 00026 'wiki' => new Site( 'wiki', 'w', 'wikipedia.org' ), 00027 'wiktionary' => new Site( 'wiktionary', 'wikt', 'wiktionary.org' ) 00028 ); 00029 $langlist = array_map( "trim", file( "/home/wikipedia/common/langlist" ) ); 00030 00031 $specials = array( 00032 'sourceswiki' => 'sources.wikipedia.org', 00033 'quotewiki' => 'wikiquote.org', 00034 'textbookwiki' => 'wikibooks.org', 00035 'sep11wiki' => 'sep11.wikipedia.org', 00036 'metawiki' => 'meta.wikipedia.org', 00037 ); 00038 00039 $extraLinks = array( 00040 array( 'm', 'http://meta.wikipedia.org/wiki/$1', 1 ), 00041 array( 'meta', 'http://meta.wikipedia.org/wiki/$1', 1 ), 00042 array( 'sep11', 'http://sep11.wikipedia.org/wiki/$1', 1 ), 00043 ); 00044 00045 $languageAliases = array( 00046 'zh-cn' => 'zh', 00047 'zh-tw' => 'zh', 00048 ); 00049 00050 # Extract the intermap from meta 00051 00052 $row = wfGetArray( "metawiki.cur", array( "cur_text" ), array( "cur_namespace" => 0, "cur_title" => "Interwiki_map" ) ); 00053 00054 if ( !$row ) { 00055 die( "m:Interwiki_map not found" ); 00056 } 00057 00058 $lines = explode( "\n", $row->cur_text ); 00059 $iwArray = array(); 00060 00061 foreach ( $lines as $line ) { 00062 if ( preg_match( '/^\|\s*(.*?)\s*\|\|\s*(.*?)\s*$/', $line, $matches ) ) { 00063 $prefix = $matches[1]; 00064 $url = $matches[2]; 00065 if ( preg_match( '/(wikipedia|wiktionary|wikisource|wikiquote|wikibooks)\.org/', $url ) ) { 00066 $local = 1; 00067 } else { 00068 $local = 0; 00069 } 00070 00071 $iwArray[] = array( "iw_prefix" => $prefix, "iw_url" => $url, "iw_local" => $local ); 00072 } 00073 } 00074 00075 00076 # Insert links into special wikis 00077 # These have intermap links and interlanguage links pointing to wikipedia 00078 00079 $sql = "-- Generated by rebuildInterwiki.php"; 00080 00081 foreach ( $specials as $db => $host ) { 00082 $sql .= "\nUSE $db;\n" . 00083 "TRUNCATE TABLE interwiki;\n" . 00084 "INSERT INTO interwiki (iw_prefix, iw_url, iw_local) VALUES \n"; 00085 $first = true; 00086 00087 # Intermap links 00088 foreach ( $iwArray as $iwEntry ) { 00089 # Suppress links to self 00090 if ( strpos( $iwEntry['iw_url'], $host ) === false ) { 00091 $sql .= makeLink( $iwEntry, $first ); 00092 } 00093 } 00094 # w link 00095 $sql .= makeLink( array("w", "http://en.wikipedia.org/wiki/$1", 1 ), $first ); 00096 00097 # Interlanguage links to wikipedia 00098 $sql .= makeLanguageLinks( $sites['wiki'], $first ); 00099 00100 # Extra links 00101 foreach ( $extraLinks as $link ) { 00102 $sql .= makeLink( $link, $first ); 00103 } 00104 00105 $sql .= ";\n"; 00106 } 00107 $sql .= "\n"; 00108 00109 # Insert links into multilanguage sites 00110 00111 foreach ( $sites as $site ) { 00112 $sql .= <<<EOS 00113 00114 --- 00115 --- {$site->suffix} 00116 --- 00117 00118 EOS; 00119 foreach ( $langlist as $lang ) { 00120 $db = $lang . $site->suffix; 00121 $db = str_replace( "-", "_", $db ); 00122 00123 $sql .= "USE $db;\n" . 00124 "TRUNCATE TABLE interwiki;\n" . 00125 "INSERT INTO interwiki (iw_prefix,iw_url,iw_local) VALUES\n"; 00126 $first = true; 00127 00128 # Intermap links 00129 foreach ( $iwArray as $iwEntry ) { 00130 # Suppress links to self 00131 if ( strpos( $iwEntry['iw_url'], $site->url ) === false || 00132 strpos( $iwEntry['iw_url'], 'meta.wikipedia.org' ) !== false ) { 00133 $sql .= makeLink( $iwEntry, $first ); 00134 } 00135 } 00136 00137 # Lateral links 00138 foreach ( $sites as $targetSite ) { 00139 # Suppress link to self 00140 if ( $targetSite->suffix != $site->suffix ) { 00141 $sql .= makeLink( array( $targetSite->lateral, $targetSite->getURL( $lang ), 1 ), $first ); 00142 } 00143 } 00144 00145 # Interlanguage links 00146 $sql .= makeLanguageLinks( $site, $first ); 00147 00148 # w link within wikipedias 00149 # Other sites already have it as a lateral link 00150 if ( $site->suffix == "wiki" ) { 00151 $sql .= makeLink( array("w", "http://en.wikipedia.org/wiki/$1", 1), $first ); 00152 } 00153 00154 # Extra links 00155 foreach ( $extraLinks as $link ){ 00156 $sql .= makeLink( $link, $first ); 00157 } 00158 $sql .= ";\n\n"; 00159 } 00160 } 00161 00162 # Output 00163 if ( isset( $options['o'] ) ) { 00164 # To file specified with -o 00165 chdir( $oldCwd ); 00166 $file = fopen( $options['o'], "w" ); 00167 fwrite( $file, $sql ); 00168 fclose( $file ); 00169 } else { 00170 # To stdout 00171 print $sql; 00172 } 00173 00174 # ------------------------------------------------------------------------------------------ 00175 00176 # Returns part of an INSERT statement, corresponding to all interlanguage links to a particular site 00177 function makeLanguageLinks( &$site, &$first ) { 00178 global $langlist, $languageAliases; 00179 00180 $sql = ""; 00181 00182 # Actual languages with their own databases 00183 foreach ( $langlist as $targetLang ) { 00184 $sql .= makeLink( array( $targetLang, $site->getURL( $targetLang ), 1 ), $first ); 00185 } 00186 00187 # Language aliases 00188 foreach ( $languageAliases as $alias => $lang ) { 00189 $sql .= makeLink( array( $alias, $site->getURL( $lang ), 1 ), $first ); 00190 } 00191 return $sql; 00192 } 00193 00194 # Make SQL for a single link from an array 00195 function makeLink( $entry, &$first ) { 00196 $sql = ""; 00197 # Add comma 00198 if ( $first ) { 00199 $first = false; 00200 } else { 00201 $sql .= ",\n"; 00202 } 00203 $sql .= "(" . Database::makeList( $entry ) . ")"; 00204 return $sql; 00205 } 00206 00207 ?>

Generated on Tue Jun 29 23:40:06 2004 for Mediawiki by doxygen 1.3.7