3 * Rebuild interwiki table using the file on meta and the language list
8 * @subpackage Maintenance
16 * @subpackage Maintenance
19 var $suffix, $lateral, $url;
21 function Site( $s, $l, $u ) {
27 function getURL( $lang ) {
28 $xlang = str_replace( '_', '-', $lang );
29 return "http://$xlang.{$this->url}/wiki/\$1";
33 function getRebuildInterwikiSQL() {
34 global $langlist, $languageAliases, $prefixRewrites, $wgDBname;
36 # Multi-language sites
37 # db suffix => db suffix, iw prefix, hostname
39 'wiki' => new Site( 'wiki', 'w', 'wikipedia.org' ),
40 'wiktionary' => new Site( 'wiktionary', 'wikt', 'wiktionary.org' ),
41 'wikiquote' => new Site( 'wikiquote', 'q', 'wikiquote.org' ),
42 'wikibooks' => new Site( 'wikibooks', 'b', 'wikibooks.org' ),
43 'wikinews' => new Site( 'wikinews', 'n', 'wikinews.org' ),
44 'wikisource' => new Site( 'wikisource', 's', 'wikisource.org' ),
45 'wikimedia' => new Site( 'wikimedia', 'chapter', 'wikimedia.org' ),
48 # List of language prefixes likely to be found in multi-language sites
49 $langlist = array_map( "trim", file( "/home/wikipedia/common/langlist" ) );
51 # List of all database names
52 $dblist = array_map( "trim", file( "/home/wikipedia/common/all.dblist" ) );
54 # Special-case hostnames
56 'sourceswiki' => 'sources.wikipedia.org',
57 'quotewiki' => 'wikiquote.org',
58 'textbookwiki' => 'wikibooks.org',
59 'sep11wiki' => 'sep11.wikipedia.org',
60 'metawiki' => 'meta.wikimedia.org',
61 'commonswiki' => 'commons.wikimedia.org',
64 # Extra interwiki links that can't be in the intermap for some reason
66 array( 'm', 'http://meta.wikimedia.org/wiki/$1', 1 ),
67 array( 'meta', 'http://meta.wikimedia.org/wiki/$1', 1 ),
68 array( 'sep11', 'http://sep11.wikipedia.org/wiki/$1', 1 ),
71 # Language aliases, usually configured as redirects to the real wiki in apache
72 # Interlanguage links are made directly to the real wiki
73 # Something horrible happens if you forget to list an alias here, I can't
75 $languageAliases = array(
82 # Special case prefix rewrites, for the benefit of Swedish which uses s:t
83 # as an abbreviation for saint
84 $prefixRewrites = array(
85 'svwiki' => array( 's' => 'src' ),
88 # Construct a list of reserved prefixes
90 foreach ( $langlist as $lang ) {
93 foreach ( $languageAliases as $alias => $lang ) {
94 $reserved[$alias] = 1;
96 foreach( $sites as $site ) {
97 $reserved[$site->lateral] = 1;
100 # Extract the intermap from meta
101 $intermap = wfGetHTTP( 'http://meta.wikimedia.org/w/index.php?title=Interwiki_map&action=raw', 30 );
102 $lines = array_map( 'trim', explode( "\n", trim( $intermap ) ) );
104 if ( !$lines || count( $lines ) < 2 ) {
105 wfDie( "m:Interwiki_map not found" );
110 foreach ( $lines as $line ) {
111 if ( preg_match( '/^\|\s*(.*?)\s*\|\|\s*(https?:\/\/.*?)\s*$/', $line, $matches ) ) {
112 $prefix = strtolower( $matches[1] );
114 if ( preg_match( '/(wikipedia|wiktionary|wikisource|wikiquote|wikibooks|wikimedia)\.org/', $url ) ) {
120 if ( empty( $reserved[$prefix] ) ) {
121 $iwArray[$prefix] = array( "iw_prefix" => $prefix, "iw_url" => $url, "iw_local" => $local );
126 $sql = "-- Generated by rebuildInterwiki.php";
129 foreach ( $dblist as $db ) {
130 if ( isset( $specials[$db] ) ) {
132 # Has interwiki links and interlanguage links to wikipedia
134 $host = $specials[$db];
135 $sql .= "\n--$host\n\n";
136 $sql .= "USE $db;\n" .
137 "TRUNCATE TABLE interwiki;\n" .
138 "INSERT INTO interwiki (iw_prefix, iw_url, iw_local) VALUES \n";
142 foreach ( $iwArray as $iwEntry ) {
143 $sql .= makeLink( $iwEntry, $first, $db );
146 # Links to multilanguage sites
147 foreach ( $sites as $targetSite ) {
148 $sql .= makeLink( array( $targetSite->lateral, $targetSite->getURL( 'en' ), 1 ), $first, $db );
151 # Interlanguage links to wikipedia
152 $sql .= makeLanguageLinks( $sites['wiki'], $first, $db );
155 foreach ( $extraLinks as $link ) {
156 $sql .= makeLink( $link, $first, $db );
161 # Find out which site this DB belongs to
163 foreach( $sites as $candidateSite ) {
164 $suffix = $candidateSite->suffix;
165 if ( preg_match( "/(.*)$suffix$/", $db, $matches ) ) {
166 $site = $candidateSite;
171 print "Invalid database $db\n";
175 $host = "$lang." . $site->url;
176 $sql .= "\n--$host\n\n";
178 $sql .= "USE $db;\n" .
179 "TRUNCATE TABLE interwiki;\n" .
180 "INSERT INTO interwiki (iw_prefix,iw_url,iw_local) VALUES\n";
184 foreach ( $iwArray as $iwEntry ) {
185 # Suppress links with the same name as the site
186 if ( ( $suffix == 'wiki' && $iwEntry['iw_prefix'] != 'wikipedia' ) ||
187 ( $suffix != 'wiki' && $suffix != $iwEntry['iw_prefix'] ) )
189 $sql .= makeLink( $iwEntry, $first, $db );
194 foreach ( $sites as $targetSite ) {
195 # Suppress link to self
196 if ( $targetSite->suffix != $site->suffix ) {
197 $sql .= makeLink( array( $targetSite->lateral, $targetSite->getURL( $lang ), 1 ), $first, $db );
201 # Interlanguage links
202 $sql .= makeLanguageLinks( $site, $first, $db );
204 # w link within wikipedias
205 # Other sites already have it as a lateral link
206 if ( $site->suffix == "wiki" ) {
207 $sql .= makeLink( array("w", "http://en.wikipedia.org/wiki/$1", 1), $first, $db );
211 foreach ( $extraLinks as $link ){
212 $sql .= makeLink( $link, $first, $db );
220 # ------------------------------------------------------------------------------------------
222 # Returns part of an INSERT statement, corresponding to all interlanguage links to a particular site
223 function makeLanguageLinks( &$site, &$first, $source ) {
224 global $langlist, $languageAliases;
228 # Actual languages with their own databases
229 foreach ( $langlist as $targetLang ) {
230 $sql .= makeLink( array( $targetLang, $site->getURL( $targetLang ), 1 ), $first, $source );
234 foreach ( $languageAliases as $alias => $lang ) {
235 $sql .= makeLink( array( $alias, $site->getURL( $lang ), 1 ), $first, $source );
240 # Make SQL for a single link from an array
241 function makeLink( $entry, &$first, $source ) {
242 global $prefixRewrites;
244 if ( isset( $prefixRewrites[$source] ) && isset( $prefixRewrites[$source][$entry[0]] ) ) {
245 $entry[0] = $prefixRewrites[$source][$entry[0]];
255 $dbr =& wfGetDB( DB_SLAVE );
256 $sql .= "(" . $dbr->makeList( $entry ) . ")";