Reverting r16989 -- makes it extremely difficult to watch or unwatch a page that...
[mediawiki.git] / maintenance / dumpInterwiki.inc
blob2039f2df4bf3216830037211c1c854a82a58e1e0
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;
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                 'wikiversity' => new Site( 'wikiversity', 'v', 'wikiversity.org' ),
47         );
49         # List of language prefixes likely to be found in multi-language sites
50         $langlist = array_map( "trim", file( "/home/wikipedia/common/langlist" ) );
52         # List of all database names
53         $dblist = array_map( "trim", file( "/home/wikipedia/common/all.dblist" ) );
55         # Special-case hostnames
56         $specials = array(
57                 'sourceswiki' => 'sources.wikipedia.org',
58                 'quotewiki' => 'wikiquote.org',
59                 'textbookwiki' => 'wikibooks.org',
60                 'sep11wiki' => 'sep11.wikipedia.org',
61                 'metawiki' => 'meta.wikimedia.org',
62                 'commonswiki' => 'commons.wikimedia.org',
63         );
65         # Extra interwiki links that can't be in the intermap for some reason
66         $extraLinks = array(
67                 array( 'm', 'http://meta.wikimedia.org/wiki/$1', 1 ),
68                 array( 'meta', 'http://meta.wikimedia.org/wiki/$1', 1 ),
69                 array( 'sep11', 'http://sep11.wikipedia.org/wiki/$1', 1 ),
70         );
72         # Language aliases, usually configured as redirects to the real wiki in apache
73         # Interlanguage links are made directly to the real wiki
74         # Something horrible happens if you forget to list an alias here, I can't
75         #   remember what
76         $languageAliases = array(
77                 'zh-cn' => 'zh',
78                 'zh-tw' => 'zh',
79                 'dk' => 'da',
80                 'nb' => 'no',
81         );
83         # Special case prefix rewrites, for the benefit of Swedish which uses s:t
84         # as an abbreviation for saint
85         $prefixRewrites = array(
86                 'svwiki' => array ( 's' => 'src'),
87         ); 
89         # Construct a list of reserved prefixes
90         $reserved = array();
91         foreach ( $langlist as $lang ) {
92                 $reserved[$lang] = 1;
93         }
94         foreach ( $languageAliases as $alias => $lang ) {
95                 $reserved[$alias] = 1;
96         }
97         foreach( $sites as $site ) {
98                 $reserved[$site->lateral] = 1;
99         }
101         # Extract the intermap from meta
102         $intermap = wfGetHTTP( 'http://meta.wikimedia.org/w/index.php?title=Interwiki_map&action=raw', 30 );
103         $lines = array_map( 'trim', explode( "\n", trim( $intermap ) ) );
105         if ( !$lines || count( $lines ) < 2 ) {
106                 wfDie( "m:Interwiki_map not found" );
107         }
109         $iwArray = array();
110         # Global iterwiki map
111         foreach ( $lines as $line ) {
112                 if ( preg_match( '/^\|\s*(.*?)\s*\|\|\s*(.*?)\s*$/', $line, $matches ) ) {
113                         $prefix = strtolower( $matches[1] );
114                         $url = $matches[2];
115                         if ( preg_match( '/(wikipedia|wiktionary|wikisource|wikiquote|wikibooks|wikimedia)\.org/', $url ) ) {
116                                 $local = 1;
117                         } else {
118                                 $local = 0;
119                         }
121                         if ( empty( $reserved[$prefix] ) ) {
122                             $imap  = array( "iw_prefix" => $prefix, "iw_url" => $url, "iw_local" => $local );
123                             makeLink ($imap, "__global");
124                         }
125                 }
126         }
128         # Exclude Wikipedia for Wikipedia
129         makeLink ( array ('iw_prefix' => 'wikipedia', 'is_url' => null ), "_wiki" );
130         
131         #Multilanguage sites
132         foreach ($sites as $site) 
133             $sql .= makeLanguageLinks ( $site, "_".$site->suffix );
136         foreach ( $dblist as $db ) {
137                 if ( isset( $specials[$db] ) ) {
138                         # Special wiki
139                         # Has interwiki links and interlanguage links to wikipedia
141                         makeLink( array( 'iw_prefix' => $db, 'iw_url' => "wiki"), "__sites" );
142                         # Links to multilanguage sites
143                         foreach ( $sites as $targetSite ) {
144                                 makeLink( array( 'iw_prefix' => $targetSite->lateral, 
145                                                     'iw_url' =>$targetSite->getURL( 'en' ),
146                                                     'iw_local' => 1 ), $db );
147                         }
149                 } else {
150                         # Find out which site this DB belongs to
151                         $site = false;
152                         foreach( $sites as $candidateSite ) {
153                                 $suffix = $candidateSite->suffix;
154                                 if ( preg_match( "/(.*)$suffix$/", $db, $matches ) ) {
155                                 $site = $candidateSite;
156                                         break;
157                                 }
158                         }
159                         makeLink( array( 'iw_prefix' => $db, 'iw_url' => $site->suffix), "__sites" );
160                         if ( !$site ) {
161                                 print "Invalid database $db\n";
162                                 continue;
163                         }
164                         $lang = $matches[1];
165                         $host = "$lang." . $site->url;
167                         # Lateral links
168                         foreach ( $sites as $targetSite ) {
169                                 if ( $targetSite->suffix != $site->suffix ) {
170                                         makeLink( array( 'iw_prefix' => $targetSite->lateral, 
171                                                             'iw_url' => $targetSite->getURL( $lang ), 
172                                                             'iw_local' => 1 ), $db );
173                                 }
174                         }
176                         if ( $site->suffix == "wiki" ) {
177                                 makeLink( array('iw_prefix' => 'w', 
178                                                 'iw_url' => "http://en.wikipedia.org/wiki/$1", 
179                                                 'iw_local' => 1), $db );
180                         }
182                 }
183         }
184         foreach ( $extraLinks as $link )
185                 makeLink( $link, "__global" );
188 # ------------------------------------------------------------------------------------------
190 # Returns part of an INSERT statement, corresponding to all interlanguage links to a particular site
191 function makeLanguageLinks( &$site, $source ) {
192         global $langlist, $languageAliases;
193         # Actual languages with their own databases
194         foreach ( $langlist as $targetLang ) {
195                 makeLink( array( $targetLang, $site->getURL( $targetLang ), 1 ), $source );
196         }
198         # Language aliases
199         foreach ( $languageAliases as $alias => $lang ) {
200                 makeLink( array( $alias, $site->getURL( $lang ), 1 ), $source );
201         }
204 function makeLink( $entry, $source ) {
205         global $prefixRewrites, $dbFile;
206         if ( isset( $prefixRewrites[$source] ) && isset( $prefixRewrites[$source][$entry[0]] ) )
207                 $entry[0] = $prefixRewrites[$source][$entry[0]];
208         if (!array_key_exists("iw_prefix",$entry))
209             $entry = array("iw_prefix" => $entry[0], "iw_url" => $entry[1], "iw_local" => $entry[2]);
210         if ( array_key_exists($source,$prefixRewrites) && 
211                 array_key_exists($entry['iw_prefix'],$prefixRewrites[$source]))
212                     $entry['iw_prefix'] = $prefixRewrites[$source][$entry['iw_prefix']];
213         if ($dbFile)
214             dba_insert("{$source}:{$entry['iw_prefix']}", trim("{$entry['iw_local']} {$entry['iw_url']}"),$dbFile);
215         else
216             print "{$source}:{$entry['iw_prefix']} {$entry['iw_url']} {$entry['iw_local']}\n";
218     }