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