Remove cruft; that happened in-branch, within 48 hours of the first change...don...
[mediawiki.git] / maintenance / dumpInterwiki.inc
blob3cca1e02d90eaf31985cd328948704741f422b2f
1 <?php
2 /**
3  * Rebuild interwiki table using the file on meta and the language list
4  * Wikimedia specific!
5  *
6  * @todo document
7  * @package MediaWiki
8  * @subpackage Maintenance
9  */
11 /** */
13 /**
14  * @todo document
15  * @package MediaWiki
16  * @subpackage Maintenance
17  */
18 class Site {
19         var $suffix, $lateral, $url;
21         function Site( $s, $l, $u ) {
22                 $this->suffix = $s;
23                 $this->lateral = $l;
24                 $this->url = $u;
25         }
27         function getURL( $lang ) {
28                 $xlang = str_replace( '_', '-', $lang );
29                 return "http://$xlang.{$this->url}/wiki/\$1";
30         }
33 function getRebuildInterwikiDump() {
34         global $langlist, $languageAliases, $prefixRewrites, $wgDBname;
36         # Multi-language sites
37         # db suffix => db suffix, iw prefix, hostname
38         $sites = array(
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' ),
46         );
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
55         $specials = array(
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',
62         );
64         # Extra interwiki links that can't be in the intermap for some reason
65         $extraLinks = array(
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 ),
69         );
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
74         #   remember what
75         $languageAliases = array(
76                 'zh-cn' => 'zh',
77                 'zh-tw' => 'zh',
78                 'dk' => 'da',
79                 'nb' => 'no',
80         );
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'),
86         ); 
88         # Construct a list of reserved prefixes
89         $reserved = array();
90         foreach ( $langlist as $lang ) {
91                 $reserved[$lang] = 1;
92         }
93         foreach ( $languageAliases as $alias => $lang ) {
94                 $reserved[$alias] = 1;
95         }
96         foreach( $sites as $site ) {
97                 $reserved[$site->lateral] = 1;
98         }
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" );
106         }
108         $iwArray = array();
109         # Global iterwiki map
110         foreach ( $lines as $line ) {
111                 if ( preg_match( '/^\|\s*(.*?)\s*\|\|\s*(.*?)\s*$/', $line, $matches ) ) {
112                         $prefix = strtolower( $matches[1] );
113                         $url = $matches[2];
114                         if ( preg_match( '/(wikipedia|wiktionary|wikisource|wikiquote|wikibooks|wikimedia)\.org/', $url ) ) {
115                                 $local = 1;
116                         } else {
117                                 $local = 0;
118                         }
120                         if ( empty( $reserved[$prefix] ) ) {
121                             $imap  = array( "iw_prefix" => $prefix, "iw_url" => $url, "iw_local" => $local );
122                             makeLink ($imap, "__global");
123                         }
124                 }
125         }
127         # Exclude Wikipedia for Wikipedia
128         makeLink ( array ('iw_prefix' => 'wikipedia', 'is_url' => null ), "_wiki" );
129         
130         #Multilanguage sites
131         foreach ($sites as $site) 
132             $sql .= makeLanguageLinks ( $site, "_".$site->suffix );
135         foreach ( $dblist as $db ) {
136                 if ( isset( $specials[$db] ) ) {
137                         # Special wiki
138                         # Has interwiki links and interlanguage links to wikipedia
140                         makeLink( array( 'iw_prefix' => $db, 'iw_url' => "wiki"), "__sites" );
141                         # Links to multilanguage sites
142                         foreach ( $sites as $targetSite ) {
143                                 makeLink( array( 'iw_prefix' => $targetSite->lateral, 
144                                                     'iw_url' =>$targetSite->getURL( 'en' ),
145                                                     'iw_local' => 1 ), $db );
146                         }
148                 } else {
149                         # Find out which site this DB belongs to
150                         $site = false;
151                         foreach( $sites as $candidateSite ) {
152                                 $suffix = $candidateSite->suffix;
153                                 if ( preg_match( "/(.*)$suffix$/", $db, $matches ) ) {
154                                 $site = $candidateSite;
155                                         break;
156                                 }
157                         }
158                         makeLink( array( 'iw_prefix' => $db, 'iw_url' => $site->suffix), "__sites" );
159                         if ( !$site ) {
160                                 print "Invalid database $db\n";
161                                 continue;
162                         }
163                         $lang = $matches[1];
164                         $host = "$lang." . $site->url;
166                         # Lateral links
167                         foreach ( $sites as $targetSite ) {
168                                 if ( $targetSite->suffix != $site->suffix ) {
169                                         makeLink( array( 'iw_prefix' => $targetSite->lateral, 
170                                                             'iw_url' => $targetSite->getURL( $lang ), 
171                                                             'iw_local' => 1 ), $db );
172                                 }
173                         }
175                         if ( $site->suffix == "wiki" ) {
176                                 makeLink( array('iw_prefix' => 'w', 
177                                                 'iw_url' => "http://en.wikipedia.org/wiki/$1", 
178                                                 'iw_local' => 1), $db );
179                         }
181                 }
182         }
183         foreach ( $extraLinks as $link )
184                 makeLink( $link, "__global" );
187 # ------------------------------------------------------------------------------------------
189 # Returns part of an INSERT statement, corresponding to all interlanguage links to a particular site
190 function makeLanguageLinks( &$site, $source ) {
191         global $langlist, $languageAliases;
192         # Actual languages with their own databases
193         foreach ( $langlist as $targetLang ) {
194                 makeLink( array( $targetLang, $site->getURL( $targetLang ), 1 ), $source );
195         }
197         # Language aliases
198         foreach ( $languageAliases as $alias => $lang ) {
199                 makeLink( array( $alias, $site->getURL( $lang ), 1 ), $source );
200         }
203 function makeLink( $entry, $source ) {
204         global $prefixRewrites, $dbFile;
205         if ( isset( $prefixRewrites[$source] ) && isset( $prefixRewrites[$source][$entry[0]] ) )
206                 $entry[0] = $prefixRewrites[$source][$entry[0]];
207         if (!array_key_exists("iw_prefix",$entry))
208             $entry = array("iw_prefix" => $entry[0], "iw_url" => $entry[1], "iw_local" => $entry[2]);
209         if ( array_key_exists($source,$prefixRewrites) && 
210                 array_key_exists($entry['iw_prefix'],$prefixRewrites[$source]))
211                     $entry['iw_prefix'] = $prefixRewrites[$source][$entry['iw_prefix']];
212         if ($dbFile)
213             dba_insert("{$source}:{$entry['iw_prefix']}", trim("{$entry['iw_local']} {$entry['iw_url']}"),$dbFile);
214         else
215             print "{$source}:{$entry['iw_prefix']} {$entry['iw_url']} {$entry['iw_local']}\n";
217     }