3 * Rebuild interwiki table using the file on meta and the language list
14 * @ingroup Maintenance
17 var $suffix, $lateral, $url;
19 function __construct( $s, $l, $u ) {
25 function getURL( $lang ) {
26 $xlang = str_replace( '_', '-', $lang );
27 return "http://$xlang.{$this->url}/wiki/\$1";
31 function makeInterwikiSQL( $destDir ) {
32 global $langlist, $languageAliases, $prefixRewrites;
34 # Multi-language sites
35 # db suffix => db suffix, iw prefix, hostname
37 'wiki' => new Site( 'wiki', 'w', 'wikipedia.org' ),
38 'wiktionary' => new Site( 'wiktionary', 'wikt', 'wiktionary.org' ),
39 'wikiquote' => new Site( 'wikiquote', 'q', 'wikiquote.org' ),
40 'wikibooks' => new Site( 'wikibooks', 'b', 'wikibooks.org' ),
41 'wikinews' => new Site( 'wikinews', 'n', 'wikinews.org' ),
42 'wikisource' => new Site( 'wikisource', 's', 'wikisource.org' ),
43 'wikimedia' => new Site( 'wikimedia', 'chapter', 'wikimedia.org' ),
44 'wikiversity' => new Site( 'wikiversity', 'v', 'wikiversity.org' ),
47 # List of language prefixes likely to be found in multi-language sites
48 $langlist = array_map( "trim", file( "/home/wikipedia/common/langlist" ) );
50 # List of all database names
51 $dblist = array_map( "trim", file( "/home/wikipedia/common/all.dblist" ) );
53 # Special-case hostnames
55 'sourceswiki' => 'sources.wikipedia.org',
56 'quotewiki' => 'wikiquote.org',
57 'textbookwiki' => 'wikibooks.org',
58 'sep11wiki' => 'sep11.wikipedia.org',
59 'metawiki' => 'meta.wikimedia.org',
60 'commonswiki' => 'commons.wikimedia.org',
61 'specieswiki' => 'species.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 = Http::get( '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 ) {
112 if ( preg_match( '/^\|\s*(.*?)\s*\|\|\s*(https?:\/\/.*?)\s*$/', $line, $matches ) ) {
113 $prefix = strtolower( $matches[1] );
115 if ( preg_match( '/(wikipedia|wiktionary|wikisource|wikiquote|wikibooks|wikimedia)\.org/', $url ) ) {
121 if ( empty( $reserved[$prefix] ) ) {
122 $iwArray[$prefix] = array( "iw_prefix" => $prefix, "iw_url" => $url, "iw_local" => $local );
129 foreach ( $dblist as $db ) {
130 $sql = "-- Generated by rebuildInterwiki.php";
131 if ( isset( $specials[$db] ) ) {
133 # Has interwiki links and interlanguage links to wikipedia
135 $host = $specials[$db];
136 $sql .= "\n--$host\n\n";
137 $sql .= "USE $db;\n" .
138 "TRUNCATE TABLE interwiki;\n" .
139 "INSERT INTO interwiki (iw_prefix, iw_url, iw_local) VALUES \n";
143 foreach ( $iwArray as $iwEntry ) {
144 $sql .= makeLink( $iwEntry, $first, $db );
147 # Links to multilanguage sites
148 foreach ( $sites as $targetSite ) {
149 $sql .= makeLink( array( $targetSite->lateral, $targetSite->getURL( 'en' ), 1 ), $first, $db );
152 # Interlanguage links to wikipedia
153 $sql .= makeLanguageLinks( $sites['wiki'], $first, $db );
156 foreach ( $extraLinks as $link ) {
157 $sql .= makeLink( $link, $first, $db );
162 # Find out which site this DB belongs to
164 foreach ( $sites as $candidateSite ) {
165 $suffix = $candidateSite->suffix;
166 if ( preg_match( "/(.*)$suffix$/", $db, $matches ) ) {
167 $site = $candidateSite;
172 print "Invalid database $db\n";
176 $host = "$lang." . $site->url;
177 $sql .= "\n--$host\n\n";
179 $sql .= "USE $db;\n" .
180 "TRUNCATE TABLE interwiki;\n" .
181 "INSERT INTO interwiki (iw_prefix,iw_url,iw_local) VALUES\n";
185 foreach ( $iwArray as $iwEntry ) {
186 # Suppress links with the same name as the site
187 if ( ( $suffix == 'wiki' && $iwEntry['iw_prefix'] != 'wikipedia' ) ||
188 ( $suffix != 'wiki' && $suffix != $iwEntry['iw_prefix'] ) )
190 $sql .= makeLink( $iwEntry, $first, $db );
195 foreach ( $sites as $targetSite ) {
196 # Suppress link to self
197 if ( $targetSite->suffix != $site->suffix ) {
198 $sql .= makeLink( array( $targetSite->lateral, $targetSite->getURL( $lang ), 1 ), $first, $db );
202 # Interlanguage links
203 $sql .= makeLanguageLinks( $site, $first, $db );
205 # w link within wikipedias
206 # Other sites already have it as a lateral link
207 if ( $site->suffix == "wiki" ) {
208 $sql .= makeLink( array( "w", "http://en.wikipedia.org/wiki/$1", 1 ), $first, $db );
212 foreach ( $extraLinks as $link ) {
213 $sql .= makeLink( $link, $first, $db );
217 file_put_contents( "$destDir/$db.sql", $sql );
221 # ------------------------------------------------------------------------------------------
223 # Returns part of an INSERT statement, corresponding to all interlanguage links to a particular site
224 function makeLanguageLinks( &$site, &$first, $source ) {
225 global $langlist, $languageAliases;
229 # Actual languages with their own databases
230 foreach ( $langlist as $targetLang ) {
231 $sql .= makeLink( array( $targetLang, $site->getURL( $targetLang ), 1 ), $first, $source );
235 foreach ( $languageAliases as $alias => $lang ) {
236 $sql .= makeLink( array( $alias, $site->getURL( $lang ), 1 ), $first, $source );
241 # Make SQL for a single link from an array
242 function makeLink( $entry, &$first, $source ) {
243 global $prefixRewrites;
245 if ( isset( $prefixRewrites[$source] ) && isset( $prefixRewrites[$source][$entry[0]] ) ) {
246 $entry[0] = $prefixRewrites[$source][$entry[0]];
256 $dbr = wfGetDB( DB_SLAVE );
257 $sql .= "(" . $dbr->makeList( $entry ) . ")";