ajaxwatch.js: coding style tweaks
[lhc/web/wiklou.git] / maintenance / rebuildInterwiki.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 * @ingroup Wikimedia
10 */
11
12 /**
13 * @todo document
14 * @ingroup Maintenance
15 */
16 class Site {
17 var $suffix, $lateral, $url;
18
19 function __construct( $s, $l, $u ) {
20 $this->suffix = $s;
21 $this->lateral = $l;
22 $this->url = $u;
23 }
24
25 function getURL( $lang ) {
26 $xlang = str_replace( '_', '-', $lang );
27 return "http://$xlang.{$this->url}/wiki/\$1";
28 }
29 }
30
31 function makeInterwikiSQL( $destDir ) {
32 global $langlist, $languageAliases, $prefixRewrites;
33
34 # Multi-language sites
35 # db suffix => db suffix, iw prefix, hostname
36 $sites = array(
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' ),
45 );
46
47 # List of language prefixes likely to be found in multi-language sites
48 $langlist = array_map( "trim", file( "/home/wikipedia/common/langlist" ) );
49
50 # List of all database names
51 $dblist = array_map( "trim", file( "/home/wikipedia/common/all.dblist" ) );
52
53 # Special-case hostnames
54 $specials = array(
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',
62 );
63
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 );
70
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 );
81
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 );
87
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 }
99
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 ) ) );
103
104 if ( !$lines || count( $lines ) < 2 ) {
105 wfDie( "m:Interwiki_map not found" );
106 }
107
108 $iwArray = array();
109
110 foreach ( $lines as $line ) {
111 $matches = array();
112 if ( preg_match( '/^\|\s*(.*?)\s*\|\|\s*(https?:\/\/.*?)\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 }
120
121 if ( empty( $reserved[$prefix] ) ) {
122 $iwArray[$prefix] = array( "iw_prefix" => $prefix, "iw_url" => $url, "iw_local" => $local );
123 }
124 }
125 }
126
127
128
129 foreach ( $dblist as $db ) {
130 $sql = "-- Generated by rebuildInterwiki.php";
131 if ( isset( $specials[$db] ) ) {
132 # Special wiki
133 # Has interwiki links and interlanguage links to wikipedia
134
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";
140 $first = true;
141
142 # Intermap links
143 foreach ( $iwArray as $iwEntry ) {
144 $sql .= makeLink( $iwEntry, $first, $db );
145 }
146
147 # Links to multilanguage sites
148 foreach ( $sites as $targetSite ) {
149 $sql .= makeLink( array( $targetSite->lateral, $targetSite->getURL( 'en' ), 1 ), $first, $db );
150 }
151
152 # Interlanguage links to wikipedia
153 $sql .= makeLanguageLinks( $sites['wiki'], $first, $db );
154
155 # Extra links
156 foreach ( $extraLinks as $link ) {
157 $sql .= makeLink( $link, $first, $db );
158 }
159
160 $sql .= ";\n";
161 } else {
162 # Find out which site this DB belongs to
163 $site = false;
164 foreach ( $sites as $candidateSite ) {
165 $suffix = $candidateSite->suffix;
166 if ( preg_match( "/(.*)$suffix$/", $db, $matches ) ) {
167 $site = $candidateSite;
168 break;
169 }
170 }
171 if ( !$site ) {
172 print "Invalid database $db\n";
173 continue;
174 }
175 $lang = $matches[1];
176 $host = "$lang." . $site->url;
177 $sql .= "\n--$host\n\n";
178
179 $sql .= "USE $db;\n" .
180 "TRUNCATE TABLE interwiki;\n" .
181 "INSERT INTO interwiki (iw_prefix,iw_url,iw_local) VALUES\n";
182 $first = true;
183
184 # Intermap links
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'] ) )
189 {
190 $sql .= makeLink( $iwEntry, $first, $db );
191 }
192 }
193
194 # Lateral links
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 );
199 }
200 }
201
202 # Interlanguage links
203 $sql .= makeLanguageLinks( $site, $first, $db );
204
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 );
209 }
210
211 # Extra links
212 foreach ( $extraLinks as $link ) {
213 $sql .= makeLink( $link, $first, $db );
214 }
215 $sql .= ";\n";
216 }
217 file_put_contents( "$destDir/$db.sql", $sql );
218 }
219 }
220
221 # ------------------------------------------------------------------------------------------
222
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;
226
227 $sql = "";
228
229 # Actual languages with their own databases
230 foreach ( $langlist as $targetLang ) {
231 $sql .= makeLink( array( $targetLang, $site->getURL( $targetLang ), 1 ), $first, $source );
232 }
233
234 # Language aliases
235 foreach ( $languageAliases as $alias => $lang ) {
236 $sql .= makeLink( array( $alias, $site->getURL( $lang ), 1 ), $first, $source );
237 }
238 return $sql;
239 }
240
241 # Make SQL for a single link from an array
242 function makeLink( $entry, &$first, $source ) {
243 global $prefixRewrites;
244
245 if ( isset( $prefixRewrites[$source] ) && isset( $prefixRewrites[$source][$entry[0]] ) ) {
246 $entry[0] = $prefixRewrites[$source][$entry[0]];
247 }
248
249 $sql = "";
250 # Add comma
251 if ( $first ) {
252 $first = false;
253 } else {
254 $sql .= ",\n";
255 }
256 $dbr = wfGetDB( DB_SLAVE );
257 $sql .= "(" . $dbr->makeList( $entry ) . ")";
258 return $sql;
259 }