* (bug 2792) Update rebuildrecentchanges.inc for new schema
[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, $wgDBname;
34
35 # Initialise lists of wikis
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 );
43 $langlist = array_map( "trim", file( "/home/wikipedia/common/langlist" ) );
44 $dblist = array_map( "trim", file( "/home/wikipedia/common/all.dblist" ) );
45
46 $specials = array(
47 'sourceswiki' => 'sources.wikipedia.org',
48 'quotewiki' => 'wikiquote.org',
49 'textbookwiki' => 'wikibooks.org',
50 'sep11wiki' => 'sep11.wikipedia.org',
51 'metawiki' => 'meta.wikimedia.org',
52 'commonswiki' => 'commons.wikimedia.org',
53 );
54
55 $extraLinks = array(
56 array( 'm', 'http://meta.wikimedia.org/wiki/$1', 1 ),
57 array( 'meta', 'http://meta.wikimedia.org/wiki/$1', 1 ),
58 array( 'sep11', 'http://sep11.wikipedia.org/wiki/$1', 1 ),
59 );
60
61 $languageAliases = array(
62 'zh-cn' => 'zh',
63 'zh-tw' => 'zh',
64 'dk' => 'da',
65 'nb' => 'no',
66 );
67
68 # Construct a list of reserved prefixes
69 $reserved = array();
70 foreach ( $langlist as $lang ) {
71 $reserved[$lang] = 1;
72 }
73 foreach ( $languageAliases as $alias => $lang ) {
74 $reserved[$alias] = 1;
75 }
76 foreach( $sites as $site ) {
77 $reserved[$site->lateral] = 1;
78 }
79
80 # Extract the intermap from meta
81 $intermap = wfGetHTTP( 'http://meta.wikimedia.org/w/index.php?title=Interwiki_map&action=raw' );
82 $lines = array_map( 'trim', explode( "\n", trim( $intermap ) ) );
83
84 if ( !$lines ) {
85 die( "m:Interwiki_map not found" );
86 }
87
88 $iwArray = array();
89
90 foreach ( $lines as $line ) {
91 if ( preg_match( '/^\|\s*(.*?)\s*\|\|\s*(.*?)\s*$/', $line, $matches ) ) {
92 $prefix = strtolower( $matches[1] );
93 $url = $matches[2];
94 if ( preg_match( '/(wikipedia|wiktionary|wikisource|wikiquote|wikibooks)\.org/', $url ) ) {
95 $local = 1;
96 } else {
97 $local = 0;
98 }
99
100 if ( empty( $reserved[$prefix] ) ) {
101 $iwArray[$prefix] = array( "iw_prefix" => $prefix, "iw_url" => $url, "iw_local" => $local );
102 }
103 }
104 }
105
106 $sql = "-- Generated by rebuildInterwiki.php";
107
108
109 foreach ( $dblist as $db ) {
110 if ( isset( $specials[$db] ) ) {
111 # Special wiki
112 # Has interwiki links and interlanguage links to wikipedia
113
114 $host = $specials[$db];
115 $sql .= "\n--$host\n\n";
116 $sql .= "USE $db;\n" .
117 "TRUNCATE TABLE interwiki;\n" .
118 "INSERT INTO interwiki (iw_prefix, iw_url, iw_local) VALUES \n";
119 $first = true;
120
121 # Intermap links
122 foreach ( $iwArray as $iwEntry ) {
123 $sql .= makeLink( $iwEntry, $first );
124 }
125
126 # Links to multilanguage sites
127 foreach ( $sites as $targetSite ) {
128 $sql .= makeLink( array( $targetSite->lateral, $targetSite->getURL( 'en' ), 1 ), $first );
129 }
130
131 # Interlanguage links to wikipedia
132 $sql .= makeLanguageLinks( $sites['wiki'], $first );
133
134 # Extra links
135 foreach ( $extraLinks as $link ) {
136 $sql .= makeLink( $link, $first );
137 }
138
139 $sql .= ";\n";
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 if ( !$site ) {
151 print "Invalid database $db\n";
152 continue;
153 }
154 $lang = $matches[1];
155 $host = "$lang." . $site->url;
156 $sql .= "\n--$host\n\n";
157
158 $sql .= "USE $db;\n" .
159 "TRUNCATE TABLE interwiki;\n" .
160 "INSERT INTO interwiki (iw_prefix,iw_url,iw_local) VALUES\n";
161 $first = true;
162
163 # Intermap links
164 foreach ( $iwArray as $iwEntry ) {
165 # Suppress links with the same name as the site
166 if ( ( $suffix == 'wiki' && $iwEntry['iw_prefix'] != 'wikipedia' ) ||
167 ( $suffix != 'wiki' && $suffix != $iwEntry['iw_prefix'] ) )
168 {
169 $sql .= makeLink( $iwEntry, $first );
170 }
171 }
172
173 # Lateral links
174 foreach ( $sites as $targetSite ) {
175 # Suppress link to self
176 if ( $targetSite->suffix != $site->suffix ) {
177 $sql .= makeLink( array( $targetSite->lateral, $targetSite->getURL( $lang ), 1 ), $first );
178 }
179 }
180
181 # Interlanguage links
182 $sql .= makeLanguageLinks( $site, $first );
183
184 # w link within wikipedias
185 # Other sites already have it as a lateral link
186 if ( $site->suffix == "wiki" ) {
187 $sql .= makeLink( array("w", "http://en.wikipedia.org/wiki/$1", 1), $first );
188 }
189
190 # Extra links
191 foreach ( $extraLinks as $link ){
192 $sql .= makeLink( $link, $first );
193 }
194 $sql .= ";\n\n";
195 }
196 }
197 return $sql;
198 }
199
200 # ------------------------------------------------------------------------------------------
201
202 # Returns part of an INSERT statement, corresponding to all interlanguage links to a particular site
203 function makeLanguageLinks( &$site, &$first ) {
204 global $langlist, $languageAliases;
205
206 $sql = "";
207
208 # Actual languages with their own databases
209 foreach ( $langlist as $targetLang ) {
210 $sql .= makeLink( array( $targetLang, $site->getURL( $targetLang ), 1 ), $first );
211 }
212
213 # Language aliases
214 foreach ( $languageAliases as $alias => $lang ) {
215 $sql .= makeLink( array( $alias, $site->getURL( $lang ), 1 ), $first );
216 }
217 return $sql;
218 }
219
220 # Make SQL for a single link from an array
221 function makeLink( $entry, &$first ) {
222 $sql = "";
223 # Add comma
224 if ( $first ) {
225 $first = false;
226 } else {
227 $sql .= ",\n";
228 }
229 $dbr =& wfGetDB( DB_SLAVE );
230 $sql .= "(" . $dbr->makeList( $entry ) . ")";
231 return $sql;
232 }
233
234 ?>