3 * Rebuild interwiki table using the file on meta and the language list
8 * @subpackage Maintenance
16 * @subpackage Maintenance
19 var $suffix, $lateral, $url;
21 function Site( $s, $l, $u ) {
27 function getURL( $lang ) {
28 return "http://$lang.{$this->url}/wiki/\$1";
32 function getRebuildInterwikiSQL() {
33 global $langlist, $languageAliases, $wgDBname;
35 # Initialise lists of wikis
37 'wiki' => new Site( 'wiki', 'w', 'wikipedia.org' ),
38 'wiktionary' => new Site( 'wiktionary', 'wikt', 'wiktionary.org' ),
39 'wikiquote' => new Site( 'wikiquote', 'q', 'wikiquote.org' ),
40 'wikibooks' => new Site( 'wikibooks', 'b', 'wikibooks.org' ),
41 'wikinews' => new Site( 'wikinews', 'n', 'wikinews.org' ),
43 $langlist = array_map( "trim", file( "/home/wikipedia/common/langlist" ) );
44 $dblist = array_map( "trim", file( "/home/wikipedia/common/all.dblist" ) );
47 'sourceswiki' => 'sources.wikipedia.org',
48 'quotewiki' => 'wikiquote.org',
49 'textbookwiki' => 'wikibooks.org',
50 'sep11wiki' => 'sep11.wikipedia.org',
51 'metawiki' => 'meta.wikimedia.org',
52 'commonswiki' => 'commons.wikimedia.org',
56 array( 'm', 'http://meta.wikimedia.org/wiki/$1', 1 ),
57 array( 'meta', 'http://meta.wikimedia.org/wiki/$1', 1 ),
58 array( 'sep11', 'http://sep11.wikipedia.org/wiki/$1', 1 ),
61 $languageAliases = array(
68 # Construct a list of reserved prefixes
70 foreach ( $langlist as $lang ) {
73 foreach ( $languageAliases as $alias => $lang ) {
74 $reserved[$alias] = 1;
76 foreach( $sites as $site ) {
77 $reserved[$site->lateral] = 1;
80 # Extract the intermap from meta
81 $intermap = wfGetHTTP( 'http://meta.wikimedia.org/w/index.php?title=Interwiki_map&action=raw' );
82 $lines = array_map( 'trim', explode( "\n", trim( $intermap ) ) );
85 die( "m:Interwiki_map not found" );
90 foreach ( $lines as $line ) {
91 if ( preg_match( '/^\|\s*(.*?)\s*\|\|\s*(.*?)\s*$/', $line, $matches ) ) {
92 $prefix = strtolower( $matches[1] );
94 if ( preg_match( '/(wikipedia|wiktionary|wikisource|wikiquote|wikibooks)\.org/', $url ) ) {
100 if ( empty( $reserved[$prefix] ) ) {
101 $iwArray[$prefix] = array( "iw_prefix" => $prefix, "iw_url" => $url, "iw_local" => $local );
106 $sql = "-- Generated by rebuildInterwiki.php";
109 foreach ( $dblist as $db ) {
110 if ( isset( $specials[$db] ) ) {
112 # Has interwiki links and interlanguage links to wikipedia
114 $host = $specials[$db];
115 $sql .= "\n--$host\n\n";
116 $sql .= "USE $db;\n" .
117 "TRUNCATE TABLE interwiki;\n" .
118 "INSERT INTO interwiki (iw_prefix, iw_url, iw_local) VALUES \n";
122 foreach ( $iwArray as $iwEntry ) {
123 $sql .= makeLink( $iwEntry, $first );
126 # Links to multilanguage sites
127 foreach ( $sites as $targetSite ) {
128 $sql .= makeLink( array( $targetSite->lateral, $targetSite->getURL( 'en' ), 1 ), $first );
131 # Interlanguage links to wikipedia
132 $sql .= makeLanguageLinks( $sites['wiki'], $first );
135 foreach ( $extraLinks as $link ) {
136 $sql .= makeLink( $link, $first );
141 # Find out which site this DB belongs to
143 foreach( $sites as $candidateSite ) {
144 $suffix = $candidateSite->suffix;
145 if ( preg_match( "/(.*)$suffix$/", $db, $matches ) ) {
146 $site = $candidateSite;
151 print "Invalid database $db\n";
155 $host = "$lang." . $site->url;
156 $sql .= "\n--$host\n\n";
158 $sql .= "USE $db;\n" .
159 "TRUNCATE TABLE interwiki;\n" .
160 "INSERT INTO interwiki (iw_prefix,iw_url,iw_local) VALUES\n";
164 foreach ( $iwArray as $iwEntry ) {
165 # Suppress links with the same name as the site
166 if ( ( $suffix == 'wiki' && $iwEntry['iw_prefix'] != 'wikipedia' ) ||
167 ( $suffix != 'wiki' && $suffix != $iwEntry['iw_prefix'] ) )
169 $sql .= makeLink( $iwEntry, $first );
174 foreach ( $sites as $targetSite ) {
175 # Suppress link to self
176 if ( $targetSite->suffix != $site->suffix ) {
177 $sql .= makeLink( array( $targetSite->lateral, $targetSite->getURL( $lang ), 1 ), $first );
181 # Interlanguage links
182 $sql .= makeLanguageLinks( $site, $first );
184 # w link within wikipedias
185 # Other sites already have it as a lateral link
186 if ( $site->suffix == "wiki" ) {
187 $sql .= makeLink( array("w", "http://en.wikipedia.org/wiki/$1", 1), $first );
191 foreach ( $extraLinks as $link ){
192 $sql .= makeLink( $link, $first );
200 # ------------------------------------------------------------------------------------------
202 # Returns part of an INSERT statement, corresponding to all interlanguage links to a particular site
203 function makeLanguageLinks( &$site, &$first ) {
204 global $langlist, $languageAliases;
208 # Actual languages with their own databases
209 foreach ( $langlist as $targetLang ) {
210 $sql .= makeLink( array( $targetLang, $site->getURL( $targetLang ), 1 ), $first );
214 foreach ( $languageAliases as $alias => $lang ) {
215 $sql .= makeLink( array( $alias, $site->getURL( $lang ), 1 ), $first );
220 # Make SQL for a single link from an array
221 function makeLink( $entry, &$first ) {
229 $dbr =& wfGetDB( DB_SLAVE );
230 $sql .= "(" . $dbr->makeList( $entry ) . ")";