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