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