API * Implemented backlinks / imagelinks / embeddedin modules
[mediawiki.git] / maintenance / rebuildInterwiki.inc
blobd719fd4010029e736217ee3cb52ce58af22c68ff
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  */
11 /** */
13 /**
14  * @todo document
15  * @package MediaWiki
16  * @subpackage Maintenance
17  */
18 class Site {
19         var $suffix, $lateral, $url;
21         function Site( $s, $l, $u ) {
22                 $this->suffix = $s;
23                 $this->lateral = $l;
24                 $this->url = $u;
25         }
27         function getURL( $lang ) {
28                 $xlang = str_replace( '_', '-', $lang );
29                 return "http://$xlang.{$this->url}/wiki/\$1";
30         }
33 function getRebuildInterwikiSQL() {
34         global $langlist, $languageAliases, $prefixRewrites;
36         # Multi-language sites
37         # db suffix => db suffix, iw prefix, hostname
38         $sites = array(
39                 'wiki' => new Site( 'wiki', 'w', 'wikipedia.org' ),
40                 'wiktionary' => new Site( 'wiktionary', 'wikt', 'wiktionary.org' ),
41                 'wikiquote' => new Site( 'wikiquote', 'q', 'wikiquote.org' ),
42                 'wikibooks' => new Site( 'wikibooks', 'b', 'wikibooks.org' ),
43                 'wikinews' => new Site( 'wikinews', 'n', 'wikinews.org' ),
44                 'wikisource' => new Site( 'wikisource', 's', 'wikisource.org' ),
45                 'wikimedia' => new Site( 'wikimedia', 'chapter', 'wikimedia.org' ),
46         );
48         # List of language prefixes likely to be found in multi-language sites
49         $langlist = array_map( "trim", file( "/home/wikipedia/common/langlist" ) );
51         # List of all database names
52         $dblist = array_map( "trim", file( "/home/wikipedia/common/all.dblist" ) );
54         # Special-case hostnames
55         $specials = array(
56                 'sourceswiki' => 'sources.wikipedia.org',
57                 'quotewiki' => 'wikiquote.org',
58                 'textbookwiki' => 'wikibooks.org',
59                 'sep11wiki' => 'sep11.wikipedia.org',
60                 'metawiki' => 'meta.wikimedia.org',
61                 'commonswiki' => 'commons.wikimedia.org',
62         );
64         # Extra interwiki links that can't be in the intermap for some reason
65         $extraLinks = array(
66                 array( 'm', 'http://meta.wikimedia.org/wiki/$1', 1 ),
67                 array( 'meta', 'http://meta.wikimedia.org/wiki/$1', 1 ),
68                 array( 'sep11', 'http://sep11.wikipedia.org/wiki/$1', 1 ),
69         );
71         # Language aliases, usually configured as redirects to the real wiki in apache
72         # Interlanguage links are made directly to the real wiki
73         # Something horrible happens if you forget to list an alias here, I can't
74         #   remember what
75         $languageAliases = array(
76                 'zh-cn' => 'zh',
77                 'zh-tw' => 'zh',
78                 'dk' => 'da',
79                 'nb' => 'no',
80         );
82         # Special case prefix rewrites, for the benefit of Swedish which uses s:t
83         # as an abbreviation for saint
84         $prefixRewrites = array(
85                 'svwiki' => array( 's' => 'src' ),
86         );
88         # Construct a list of reserved prefixes
89         $reserved = array();
90         foreach ( $langlist as $lang ) {
91                 $reserved[$lang] = 1;
92         }
93         foreach ( $languageAliases as $alias => $lang ) {
94                 $reserved[$alias] = 1;
95         }
96         foreach( $sites as $site ) {
97                 $reserved[$site->lateral] = 1;
98         }
100         # Extract the intermap from meta
101         $intermap = wfGetHTTP( 'http://meta.wikimedia.org/w/index.php?title=Interwiki_map&action=raw', 30 );
102         $lines = array_map( 'trim', explode( "\n", trim( $intermap ) ) );
104         if ( !$lines || count( $lines ) < 2 ) {
105                 wfDie( "m:Interwiki_map not found" );
106         }
108         $iwArray = array();
110         foreach ( $lines as $line ) {
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                         }
120                         if ( empty( $reserved[$prefix] ) ) {
121                                 $iwArray[$prefix] = array( "iw_prefix" => $prefix, "iw_url" => $url, "iw_local" => $local );
122                         }
123                 }
124         }
126         $sql = "-- Generated by rebuildInterwiki.php";
129         foreach ( $dblist as $db ) {
130                 if ( isset( $specials[$db] ) ) {
131                         # Special wiki
132                         # Has interwiki links and interlanguage links to wikipedia
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;
141                         # Intermap links
142                         foreach ( $iwArray as $iwEntry ) {
143                                 $sql .= makeLink( $iwEntry, $first, $db );
144                         }
146                         # Links to multilanguage sites
147                         foreach ( $sites as $targetSite ) {
148                                 $sql .= makeLink( array( $targetSite->lateral, $targetSite->getURL( 'en' ), 1 ), $first, $db );
149                         }
151                         # Interlanguage links to wikipedia
152                         $sql .= makeLanguageLinks( $sites['wiki'], $first, $db );
154                         # Extra links
155                         foreach ( $extraLinks as $link ) {
156                                 $sql .= makeLink( $link, $first, $db );
157                         }
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";
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;
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                         }
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                         }
201                         # Interlanguage links
202                         $sql .= makeLanguageLinks( $site, $first, $db );
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                         }
210                         # Extra links
211                         foreach ( $extraLinks as $link ){
212                                         $sql .= makeLink( $link, $first, $db );
213                         }
214                         $sql .= ";\n\n";
215                 }
216         }
217         return $sql;
220 # ------------------------------------------------------------------------------------------
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;
226         $sql = "";
228         # Actual languages with their own databases
229         foreach ( $langlist as $targetLang ) {
230                 $sql .= makeLink( array( $targetLang, $site->getURL( $targetLang ), 1 ), $first, $source );
231         }
233         # Language aliases
234         foreach ( $languageAliases as $alias => $lang ) {
235                 $sql .= makeLink( array( $alias, $site->getURL( $lang ), 1 ), $first, $source );
236         }
237         return $sql;
240 # Make SQL for a single link from an array
241 function makeLink( $entry, &$first, $source ) {
242         global $prefixRewrites;
244         if ( isset( $prefixRewrites[$source] ) && isset( $prefixRewrites[$source][$entry[0]] ) ) {
245                 $entry[0] = $prefixRewrites[$source][$entry[0]];
246         }
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;