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 makeInterwikiSQL( $destDir ) {
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 hostnames
54 'sourceswiki' => 'sources.wikipedia.org',
55 'quotewiki' => 'wikiquote.org',
56 'textbookwiki' => 'wikibooks.org',
57 'sep11wiki' => 'sep11.wikipedia.org',
58 'metawiki' => 'meta.wikimedia.org',
59 'commonswiki' => 'commons.wikimedia.org',
60 'specieswiki' => 'species.wikimedia.org',
63 # Extra interwiki links that can't be in the intermap for some reason
65 array( 'm', 'http://meta.wikimedia.org/wiki/$1', 1 ),
66 array( 'meta', 'http://meta.wikimedia.org/wiki/$1', 1 ),
67 array( 'sep11', 'http://sep11.wikipedia.org/wiki/$1', 1 ),
70 # Language aliases, usually configured as redirects to the real wiki in apache
71 # Interlanguage links are made directly to the real wiki
72 # Something horrible happens if you forget to list an alias here, I can't
74 $languageAliases = array(
81 # Special case prefix rewrites, for the benefit of Swedish which uses s:t
82 # as an abbreviation for saint
83 $prefixRewrites = array(
84 'svwiki' => array( 's' => 'src' ),
87 # Construct a list of reserved prefixes
89 foreach ( $langlist as $lang ) {
92 foreach ( $languageAliases as $alias => $lang ) {
93 $reserved[$alias] = 1;
95 foreach( $sites as $site ) {
96 $reserved[$site->lateral] = 1;
99 # Extract the intermap from meta
100 $intermap = Http::get( 'http://meta.wikimedia.org/w/index.php?title=Interwiki_map&action=raw', 30 );
101 $lines = array_map( 'trim', explode( "\n", trim( $intermap ) ) );
103 if ( !$lines || count( $lines ) < 2 ) {
104 wfDie( "m:Interwiki_map not found" );
109 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 );
128 foreach ( $dblist as $db ) {
129 $sql = "-- Generated by rebuildInterwiki.php";
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 );
216 file_put_contents( "$destDir/$db.sql", $sql );
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 ) . ")";