rm UTF-8 BOM
[lhc/web/wiklou.git] / maintenance / rebuildInterwiki.inc
1 <?
2
3 # Rebuild interwiki table using the file on meta and the language list
4 # Wikimedia specific!
5 $oldCwd = getcwd();
6
7 $optionsWithArgs = array( "o" );
8 include_once( "commandLine.inc" );
9
10 class Site {
11 var $suffix, $lateral, $url;
12
13 function Site( $s, $l, $u ) {
14 $this->suffix = $s;
15 $this->lateral = $l;
16 $this->url = $u;
17 }
18
19 function getURL( $lang ) {
20 return "http://$lang.{$this->url}/wiki/\$1";
21 }
22 }
23
24 function getRebuildInterwikiSQL() {
25 global $langlist, $languageAliases;
26
27 # Initialise lists of wikis
28 $sites = array(
29 'wiki' => new Site( 'wiki', 'w', 'wikipedia.org' ),
30 'wiktionary' => new Site( 'wiktionary', 'wikt', 'wiktionary.org' ),
31 'wikiquote' => new Site( 'wikiquote', 'q', 'wikiquote.org' ),
32 'wikibooks' => new Site( 'wikibooks', 'b', 'wikibooks.org' )
33 );
34 $langlist = array_map( "trim", file( "/home/wikipedia/common/langlist" ) );
35 $dblist = array_map( "trim", file( "/home/wikipedia/common/all.dblist" ) );
36
37 $specials = array(
38 'sourceswiki' => 'sources.wikipedia.org',
39 'quotewiki' => 'wikiquote.org',
40 'textbookwiki' => 'wikibooks.org',
41 'sep11wiki' => 'sep11.wikipedia.org',
42 'metawiki' => 'meta.wikimedia.org',
43 );
44
45 $extraLinks = array(
46 array( 'm', 'http://meta.wikimedia.org/wiki/$1', 1 ),
47 array( 'meta', 'http://meta.wikimedia.org/wiki/$1', 1 ),
48 array( 'sep11', 'http://sep11.wikipedia.org/wiki/$1', 1 ),
49 );
50
51 $languageAliases = array(
52 'zh-cn' => 'zh',
53 'zh-tw' => 'zh',
54 'dk' => 'da',
55 );
56
57 # Construct a list of reserved prefixes
58 $reserved = array();
59 foreach ( $langlist as $lang ) {
60 $reserved[$lang] = 1;
61 }
62 foreach ( $languageAliases as $alias => $lang ) {
63 $reserved[$alias] = 1;
64 }
65 foreach( $sites as $site ) {
66 $reserved[$site->lateral] = 1;
67 }
68
69 # Extract the intermap from meta
70 $dbr =& wfGetDB( DB_WRITE );
71 $row = $dbr->getArray( "metawiki.cur", array( "cur_text" ),
72 array( "cur_namespace" => 0, "cur_title" => "Interwiki_map" ) );
73
74 if ( !$row ) {
75 die( "m:Interwiki_map not found" );
76 }
77
78 $lines = explode( "\n", $row->cur_text );
79 $iwArray = array();
80
81 foreach ( $lines as $line ) {
82 if ( preg_match( '/^\|\s*(.*?)\s*\|\|\s*(.*?)\s*$/', $line, $matches ) ) {
83 $prefix = strtolower( $matches[1] );
84 $url = $matches[2];
85 if ( preg_match( '/(wikipedia|wiktionary|wikisource|wikiquote|wikibooks)\.org/', $url ) ) {
86 $local = 1;
87 } else {
88 $local = 0;
89 }
90
91 if ( empty( $reserved[$prefix] ) ) {
92 $iwArray[] = array( "iw_prefix" => $prefix, "iw_url" => $url, "iw_local" => $local );
93 }
94 }
95 }
96
97 $sql = "-- Generated by rebuildInterwiki.php";
98
99
100 foreach ( $dblist as $db ) {
101 if ( isset( $specials[$db] ) ) {
102 # Special wiki
103 # Has interwiki links and interlanguage links to wikipedia
104
105 $host = $specials[$db];
106 $sql .= "\n--$host\n\n";
107 $sql .= "USE $db;\n" .
108 "TRUNCATE TABLE interwiki;\n" .
109 "INSERT INTO interwiki (iw_prefix, iw_url, iw_local) VALUES \n";
110 $first = true;
111
112 # Intermap links
113 foreach ( $iwArray as $iwEntry ) {
114 $sql .= makeLink( $iwEntry, $first );
115 }
116
117 # Links to multilanguage sites
118 foreach ( $sites as $targetSite ) {
119 $sql .= makeLink( array( $targetSite->lateral, $targetSite->getURL( 'en' ), 1 ), $first );
120 }
121
122 # Interlanguage links to wikipedia
123 $sql .= makeLanguageLinks( $sites['wiki'], $first );
124
125 # Extra links
126 foreach ( $extraLinks as $link ) {
127 $sql .= makeLink( $link, $first );
128 }
129
130 $sql .= ";\n";
131 } else {
132 # Find out which site this DB belongs to
133 $site = false;
134 foreach( $sites as $candidateSite ) {
135 $suffix = $candidateSite->suffix;
136 if ( preg_match( "/(.*)$suffix$/", $db, $matches ) ) {
137 $site = $candidateSite;
138 break;
139 }
140 }
141 if ( !$site ) {
142 print "Invalid database $db\n";
143 continue;
144 }
145 $lang = $matches[1];
146 $host = "$lang." . $site->url;
147 $sql .= "\n--$host\n\n";
148
149 $sql .= "USE $db;\n" .
150 "TRUNCATE TABLE interwiki;\n" .
151 "INSERT INTO interwiki (iw_prefix,iw_url,iw_local) VALUES\n";
152 $first = true;
153
154 # Intermap links
155 foreach ( $iwArray as $iwEntry ) {
156 # Suppress links with the same name as the site
157 if ( ( $suffix == 'wiki' && $iwEntry['iw_prefix'] != 'wikipedia' ) ||
158 ( $suffix != 'wiki' && $suffix != $iwEntry['iw_prefix'] ) )
159 {
160 $sql .= makeLink( $iwEntry, $first );
161 }
162 }
163
164 # Lateral links
165 foreach ( $sites as $targetSite ) {
166 # Suppress link to self
167 if ( $targetSite->suffix != $site->suffix ) {
168 $sql .= makeLink( array( $targetSite->lateral, $targetSite->getURL( $lang ), 1 ), $first );
169 }
170 }
171
172 # Interlanguage links
173 $sql .= makeLanguageLinks( $site, $first );
174
175 # w link within wikipedias
176 # Other sites already have it as a lateral link
177 if ( $site->suffix == "wiki" ) {
178 $sql .= makeLink( array("w", "http://en.wikipedia.org/wiki/$1", 1), $first );
179 }
180
181 # Extra links
182 foreach ( $extraLinks as $link ){
183 $sql .= makeLink( $link, $first );
184 }
185 $sql .= ";\n\n";
186 }
187 }
188 return $sql;
189 }
190
191 # ------------------------------------------------------------------------------------------
192
193 # Returns part of an INSERT statement, corresponding to all interlanguage links to a particular site
194 function makeLanguageLinks( &$site, &$first ) {
195 global $langlist, $languageAliases;
196
197 $sql = "";
198
199 # Actual languages with their own databases
200 foreach ( $langlist as $targetLang ) {
201 $sql .= makeLink( array( $targetLang, $site->getURL( $targetLang ), 1 ), $first );
202 }
203
204 # Language aliases
205 foreach ( $languageAliases as $alias => $lang ) {
206 $sql .= makeLink( array( $alias, $site->getURL( $lang ), 1 ), $first );
207 }
208 return $sql;
209 }
210
211 # Make SQL for a single link from an array
212 function makeLink( $entry, &$first ) {
213 $sql = "";
214 # Add comma
215 if ( $first ) {
216 $first = false;
217 } else {
218 $sql .= ",\n";
219 }
220 $sql .= "(" . Database::makeList( $entry ) . ")";
221 return $sql;
222 }
223
224 ?>