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