WARNING: HUGE COMMIT
[lhc/web/wiklou.git] / maintenance / dumpInterwiki.inc
1 <?php
2 /**
3 * Rebuild interwiki table using the file on meta and the language list
4 * Wikimedia specific!
5 *
6 * @file
7 * @todo document
8 * @ingroup Maintenance
9 */
10
11 /**
12 * @todo document
13 * @ingroup Maintenance
14 */
15 class Site {
16 var $suffix, $lateral, $url;
17
18 function __construct( $s, $l, $u ) {
19 $this->suffix = $s;
20 $this->lateral = $l;
21 $this->url = $u;
22 }
23
24 function getURL( $lang ) {
25 $xlang = str_replace( '_', '-', $lang );
26 return "http://$xlang.{$this->url}/wiki/\$1";
27 }
28 }
29
30 function getRebuildInterwikiDump() {
31 global $langlist, $languageAliases, $prefixRewrites;
32
33 # Multi-language sites
34 # db suffix => db suffix, iw prefix, hostname
35 $sites = array(
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' ),
44 );
45
46 # List of language prefixes likely to be found in multi-language sites
47 $langlist = array_map( "trim", file( "/home/wikipedia/common/langlist" ) );
48
49 # List of all database names
50 $dblist = array_map( "trim", file( "/home/wikipedia/common/all.dblist" ) );
51
52 # Special-case databases
53 $specials = array_flip(
54 array_map( "trim",
55 file( "/home/wikipedia/common/special.dblist" ) ) );
56
57 # Extra interwiki links that can't be in the intermap for some reason
58 $extraLinks = array(
59 array( 'm', 'http://meta.wikimedia.org/wiki/$1', 1 ),
60 array( 'meta', 'http://meta.wikimedia.org/wiki/$1', 1 ),
61 array( 'sep11', 'http://sep11.wikipedia.org/wiki/$1', 1 ),
62 );
63
64 # Language aliases, usually configured as redirects to the real wiki in apache
65 # Interlanguage links are made directly to the real wiki
66 # Something horrible happens if you forget to list an alias here, I can't
67 # remember what
68 $languageAliases = array(
69 'zh-cn' => 'zh',
70 'zh-tw' => 'zh',
71 'dk' => 'da',
72 'nb' => 'no',
73 );
74
75 # Special case prefix rewrites, for the benefit of Swedish which uses s:t
76 # as an abbreviation for saint
77 $prefixRewrites = array(
78 'svwiki' => array ( 's' => 'src'),
79 );
80
81 # Construct a list of reserved prefixes
82 $reserved = array();
83 foreach ( $langlist as $lang ) {
84 $reserved[$lang] = 1;
85 }
86 foreach ( $languageAliases as $alias => $lang ) {
87 $reserved[$alias] = 1;
88 }
89 foreach( $sites as $site ) {
90 $reserved[$site->lateral] = 1;
91 }
92
93 # Extract the intermap from meta
94 $intermap = Http::get( 'http://meta.wikimedia.org/w/index.php?title=Interwiki_map&action=raw', 30 );
95 $lines = array_map( 'trim', explode( "\n", trim( $intermap ) ) );
96
97 if ( !$lines || count( $lines ) < 2 ) {
98 wfDie( "m:Interwiki_map not found" );
99 }
100
101 # Global iterwiki map
102 foreach ( $lines as $line ) {
103 if ( preg_match( '/^\|\s*(.*?)\s*\|\|\s*(.*?)\s*$/', $line, $matches ) ) {
104 $prefix = strtolower( $matches[1] );
105 $url = $matches[2];
106 if ( preg_match( '/(wikipedia|wiktionary|wikisource|wikiquote|wikibooks|wikimedia)\.org/', $url ) ) {
107 $local = 1;
108 } else {
109 $local = 0;
110 }
111
112 if ( empty( $reserved[$prefix] ) ) {
113 $imap = array( "iw_prefix" => $prefix, "iw_url" => $url, "iw_local" => $local );
114 makeLink ($imap, "__global");
115 }
116 }
117 }
118
119 # Exclude Wikipedia for Wikipedia
120 makeLink ( array ('iw_prefix' => 'wikipedia', 'is_url' => null ), "_wiki" );
121
122 #Multilanguage sites
123 foreach ($sites as $site)
124 makeLanguageLinks ( $site, "_".$site->suffix );
125
126
127 foreach ( $dblist as $db ) {
128 if ( isset( $specials[$db] ) ) {
129 # Special wiki
130 # Has interwiki links and interlanguage links to wikipedia
131
132 makeLink( array( 'iw_prefix' => $db, 'iw_url' => "wiki"), "__sites" );
133 # Links to multilanguage sites
134 foreach ( $sites as $targetSite ) {
135 makeLink( array( 'iw_prefix' => $targetSite->lateral,
136 'iw_url' =>$targetSite->getURL( 'en' ),
137 'iw_local' => 1 ), $db );
138 }
139
140 } else {
141 # Find out which site this DB belongs to
142 $site = false;
143 foreach( $sites as $candidateSite ) {
144 $suffix = $candidateSite->suffix;
145 if ( preg_match( "/(.*)$suffix$/", $db, $matches ) ) {
146 $site = $candidateSite;
147 break;
148 }
149 }
150 makeLink( array( 'iw_prefix' => $db, 'iw_url' => $site->suffix), "__sites" );
151 if ( !$site ) {
152 print "Invalid database $db\n";
153 continue;
154 }
155 $lang = $matches[1];
156
157 # Lateral links
158 foreach ( $sites as $targetSite ) {
159 if ( $targetSite->suffix != $site->suffix ) {
160 makeLink( array( 'iw_prefix' => $targetSite->lateral,
161 'iw_url' => $targetSite->getURL( $lang ),
162 'iw_local' => 1 ), $db );
163 }
164 }
165
166 if ( $site->suffix == "wiki" ) {
167 makeLink( array('iw_prefix' => 'w',
168 'iw_url' => "http://en.wikipedia.org/wiki/$1",
169 'iw_local' => 1), $db );
170 }
171
172 }
173 }
174 foreach ( $extraLinks as $link )
175 makeLink( $link, "__global" );
176 }
177
178 # ------------------------------------------------------------------------------------------
179
180 # Executes part of an INSERT statement, corresponding to all interlanguage links to a particular site
181 function makeLanguageLinks( &$site, $source ) {
182 global $langlist, $languageAliases;
183 # Actual languages with their own databases
184 foreach ( $langlist as $targetLang ) {
185 makeLink( array( $targetLang, $site->getURL( $targetLang ), 1 ), $source );
186 }
187
188 # Language aliases
189 foreach ( $languageAliases as $alias => $lang ) {
190 makeLink( array( $alias, $site->getURL( $lang ), 1 ), $source );
191 }
192 }
193
194 function makeLink( $entry, $source ) {
195 global $prefixRewrites, $dbFile;
196 if ( isset( $prefixRewrites[$source] ) && isset( $prefixRewrites[$source][$entry[0]] ) )
197 $entry[0] = $prefixRewrites[$source][$entry[0]];
198 if (!array_key_exists("iw_prefix",$entry))
199 $entry = array("iw_prefix" => $entry[0], "iw_url" => $entry[1], "iw_local" => $entry[2]);
200 if ( array_key_exists($source,$prefixRewrites) &&
201 array_key_exists($entry['iw_prefix'],$prefixRewrites[$source]))
202 $entry['iw_prefix'] = $prefixRewrites[$source][$entry['iw_prefix']];
203 if ($dbFile)
204 dba_insert("{$source}:{$entry['iw_prefix']}", trim("{$entry['iw_local']} {$entry['iw_url']}"),$dbFile);
205 else
206 print "{$source}:{$entry['iw_prefix']} {$entry['iw_url']} {$entry['iw_local']}\n";
207
208 }