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