* Don't make the url to the sitemap file a full url
[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 * @todo document
7 * @package MediaWiki
8 * @subpackage Maintenance
9 */
10
11 /** */
12
13 /**
14 * @todo document
15 * @package MediaWiki
16 * @subpackage Maintenance
17 */
18 class Site {
19 var $suffix, $lateral, $url;
20
21 function Site( $s, $l, $u ) {
22 $this->suffix = $s;
23 $this->lateral = $l;
24 $this->url = $u;
25 }
26
27 function getURL( $lang ) {
28 return "http://$lang.{$this->url}/wiki/\$1";
29 }
30 }
31
32 function getRebuildInterwikiSQL() {
33 global $langlist, $languageAliases, $prefixRewrites, $wgDBname;
34
35 # Multi-language sites
36 # db suffix => db suffix, iw prefix, hostname
37 $sites = array(
38 'wiki' => new Site( 'wiki', 'w', 'wikipedia.org' ),
39 'wiktionary' => new Site( 'wiktionary', 'wikt', 'wiktionary.org' ),
40 'wikiquote' => new Site( 'wikiquote', 'q', 'wikiquote.org' ),
41 'wikibooks' => new Site( 'wikibooks', 'b', 'wikibooks.org' ),
42 'wikinews' => new Site( 'wikinews', 'n', 'wikinews.org' ),
43 'wikisource' => new Site( 'wikisource', 's', 'wikisource.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 hostnames
53 $specials = array(
54 'sourceswiki' => 'sources.wikipedia.org',
55 'quotewiki' => 'wikiquote.org',
56 'textbookwiki' => 'wikibooks.org',
57 'sep11wiki' => 'sep11.wikipedia.org',
58 'metawiki' => 'meta.wikimedia.org',
59 'commonswiki' => 'commons.wikimedia.org',
60 );
61
62 # Extra interwiki links that can't be in the intermap for some reason
63 $extraLinks = array(
64 array( 'm', 'http://meta.wikimedia.org/wiki/$1', 1 ),
65 array( 'meta', 'http://meta.wikimedia.org/wiki/$1', 1 ),
66 array( 'sep11', 'http://sep11.wikipedia.org/wiki/$1', 1 ),
67 );
68
69 # Language aliases, usually configured as redirects to the real wiki in apache
70 # Interlanguage links are made directly to the real wiki
71 # Something horrible happens if you forget to list an alias here, I can't
72 # remember what
73 $languageAliases = array(
74 'zh-cn' => 'zh',
75 'zh-tw' => 'zh',
76 'dk' => 'da',
77 'nb' => 'no',
78 );
79
80 # Special case prefix rewrites, for the benefit of Swedish which uses s:t
81 # as an abbreviation for saint
82 $prefixRewrites = array(
83 'svwiki' => array( 's' => 'src' ),
84 );
85
86 # Construct a list of reserved prefixes
87 $reserved = array();
88 foreach ( $langlist as $lang ) {
89 $reserved[$lang] = 1;
90 }
91 foreach ( $languageAliases as $alias => $lang ) {
92 $reserved[$alias] = 1;
93 }
94 foreach( $sites as $site ) {
95 $reserved[$site->lateral] = 1;
96 }
97
98 # Extract the intermap from meta
99 $intermap = wfGetHTTP( 'http://meta.wikimedia.org/w/index.php?title=Interwiki_map&action=raw', 30 );
100 $lines = array_map( 'trim', explode( "\n", trim( $intermap ) ) );
101
102 if ( !$lines || count( $lines ) < 2 ) {
103 die( "m:Interwiki_map not found" );
104 }
105
106 $iwArray = array();
107
108 foreach ( $lines as $line ) {
109 if ( preg_match( '/^\|\s*(.*?)\s*\|\|\s*(.*?)\s*$/', $line, $matches ) ) {
110 $prefix = strtolower( $matches[1] );
111 $url = $matches[2];
112 if ( preg_match( '/(wikipedia|wiktionary|wikisource|wikiquote|wikibooks)\.org/', $url ) ) {
113 $local = 1;
114 } else {
115 $local = 0;
116 }
117
118 if ( empty( $reserved[$prefix] ) ) {
119 $iwArray[$prefix] = array( "iw_prefix" => $prefix, "iw_url" => $url, "iw_local" => $local );
120 }
121 }
122 }
123
124 $sql = "-- Generated by rebuildInterwiki.php";
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 $host = $specials[$db];
133 $sql .= "\n--$host\n\n";
134 $sql .= "USE $db;\n" .
135 "TRUNCATE TABLE interwiki;\n" .
136 "INSERT INTO interwiki (iw_prefix, iw_url, iw_local) VALUES \n";
137 $first = true;
138
139 # Intermap links
140 foreach ( $iwArray as $iwEntry ) {
141 $sql .= makeLink( $iwEntry, $first, $db );
142 }
143
144 # Links to multilanguage sites
145 foreach ( $sites as $targetSite ) {
146 $sql .= makeLink( array( $targetSite->lateral, $targetSite->getURL( 'en' ), 1 ), $first, $db );
147 }
148
149 # Interlanguage links to wikipedia
150 $sql .= makeLanguageLinks( $sites['wiki'], $first, $db );
151
152 # Extra links
153 foreach ( $extraLinks as $link ) {
154 $sql .= makeLink( $link, $first, $db );
155 }
156
157 $sql .= ";\n";
158 } else {
159 # Find out which site this DB belongs to
160 $site = false;
161 foreach( $sites as $candidateSite ) {
162 $suffix = $candidateSite->suffix;
163 if ( preg_match( "/(.*)$suffix$/", $db, $matches ) ) {
164 $site = $candidateSite;
165 break;
166 }
167 }
168 if ( !$site ) {
169 print "Invalid database $db\n";
170 continue;
171 }
172 $lang = $matches[1];
173 $host = "$lang." . $site->url;
174 $sql .= "\n--$host\n\n";
175
176 $sql .= "USE $db;\n" .
177 "TRUNCATE TABLE interwiki;\n" .
178 "INSERT INTO interwiki (iw_prefix,iw_url,iw_local) VALUES\n";
179 $first = true;
180
181 # Intermap links
182 foreach ( $iwArray as $iwEntry ) {
183 # Suppress links with the same name as the site
184 if ( ( $suffix == 'wiki' && $iwEntry['iw_prefix'] != 'wikipedia' ) ||
185 ( $suffix != 'wiki' && $suffix != $iwEntry['iw_prefix'] ) )
186 {
187 $sql .= makeLink( $iwEntry, $first, $db );
188 }
189 }
190
191 # Lateral links
192 foreach ( $sites as $targetSite ) {
193 # Suppress link to self
194 if ( $targetSite->suffix != $site->suffix ) {
195 $sql .= makeLink( array( $targetSite->lateral, $targetSite->getURL( $lang ), 1 ), $first, $db );
196 }
197 }
198
199 # Interlanguage links
200 $sql .= makeLanguageLinks( $site, $first, $db );
201
202 # w link within wikipedias
203 # Other sites already have it as a lateral link
204 if ( $site->suffix == "wiki" ) {
205 $sql .= makeLink( array("w", "http://en.wikipedia.org/wiki/$1", 1), $first, $db );
206 }
207
208 # Extra links
209 foreach ( $extraLinks as $link ){
210 $sql .= makeLink( $link, $first, $db );
211 }
212 $sql .= ";\n\n";
213 }
214 }
215 return $sql;
216 }
217
218 # ------------------------------------------------------------------------------------------
219
220 # Returns part of an INSERT statement, corresponding to all interlanguage links to a particular site
221 function makeLanguageLinks( &$site, &$first, $source ) {
222 global $langlist, $languageAliases;
223
224 $sql = "";
225
226 # Actual languages with their own databases
227 foreach ( $langlist as $targetLang ) {
228 $sql .= makeLink( array( $targetLang, $site->getURL( $targetLang ), 1 ), $first, $source );
229 }
230
231 # Language aliases
232 foreach ( $languageAliases as $alias => $lang ) {
233 $sql .= makeLink( array( $alias, $site->getURL( $lang ), 1 ), $first, $source );
234 }
235 return $sql;
236 }
237
238 # Make SQL for a single link from an array
239 function makeLink( $entry, &$first, $source ) {
240 global $prefixRewrites;
241
242 if ( isset( $prefixRewrites[$source] ) && isset( $prefixRewrites[$source][$entry[0]] ) ) {
243 $entry[0] = $prefixRewrites[$source][$entry[0]];
244 }
245
246 $sql = "";
247 # Add comma
248 if ( $first ) {
249 $first = false;
250 } else {
251 $sql .= ",\n";
252 }
253 $dbr =& wfGetDB( DB_SLAVE );
254 $sql .= "(" . $dbr->makeList( $entry ) . ")";
255 return $sql;
256 }
257
258 ?>