3 * Rebuild interwiki table using the file on meta and the language list
8 * @subpackage Maintenance
14 $optionsWithArgs = array( "o" );
15 include_once( "commandLine.inc" );
20 * @subpackage Maintenance
23 var $suffix, $lateral, $url;
25 function Site( $s, $l, $u ) {
31 function getURL( $lang ) {
32 return "http://$lang.{$this->url}/wiki/\$1";
36 function getRebuildInterwikiSQL() {
37 global $langlist, $languageAliases;
39 # Initialise lists of wikis
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' )
46 $langlist = array_map( "trim", file( "/home/wikipedia/common/langlist" ) );
47 $dblist = array_map( "trim", file( "/home/wikipedia/common/all.dblist" ) );
50 'sourceswiki' => 'sources.wikipedia.org',
51 'quotewiki' => 'wikiquote.org',
52 'textbookwiki' => 'wikibooks.org',
53 'sep11wiki' => 'sep11.wikipedia.org',
54 'metawiki' => 'meta.wikimedia.org',
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 ),
63 $languageAliases = array(
69 # Construct a list of reserved prefixes
71 foreach ( $langlist as $lang ) {
74 foreach ( $languageAliases as $alias => $lang ) {
75 $reserved[$alias] = 1;
77 foreach( $sites as $site ) {
78 $reserved[$site->lateral] = 1;
81 # Extract the intermap from meta
82 $dbr =& wfGetDB( DB_WRITE );
83 $row = $dbr->getArray( "metawiki.cur", array( "cur_text" ),
84 array( "cur_namespace" => 0, "cur_title" => "Interwiki_map" ) );
87 die( "m:Interwiki_map not found" );
90 $lines = explode( "\n", $row->cur_text );
93 foreach ( $lines as $line ) {
94 if ( preg_match( '/^\|\s*(.*?)\s*\|\|\s*(.*?)\s*$/', $line, $matches ) ) {
95 $prefix = strtolower( $matches[1] );
97 if ( preg_match( '/(wikipedia|wiktionary|wikisource|wikiquote|wikibooks)\.org/', $url ) ) {
103 if ( empty( $reserved[$prefix] ) ) {
104 $iwArray[] = array( "iw_prefix" => $prefix, "iw_url" => $url, "iw_local" => $local );
109 $sql = "-- Generated by rebuildInterwiki.php";
112 foreach ( $dblist as $db ) {
113 if ( isset( $specials[$db] ) ) {
115 # Has interwiki links and interlanguage links to wikipedia
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";
125 foreach ( $iwArray as $iwEntry ) {
126 $sql .= makeLink( $iwEntry, $first );
129 # Links to multilanguage sites
130 foreach ( $sites as $targetSite ) {
131 $sql .= makeLink( array( $targetSite->lateral, $targetSite->getURL( 'en' ), 1 ), $first );
134 # Interlanguage links to wikipedia
135 $sql .= makeLanguageLinks( $sites['wiki'], $first );
138 foreach ( $extraLinks as $link ) {
139 $sql .= makeLink( $link, $first );
144 # Find out which site this DB belongs to
146 foreach( $sites as $candidateSite ) {
147 $suffix = $candidateSite->suffix;
148 if ( preg_match( "/(.*)$suffix$/", $db, $matches ) ) {
149 $site = $candidateSite;
154 print "Invalid database $db\n";
158 $host = "$lang." . $site->url;
159 $sql .= "\n--$host\n\n";
161 $sql .= "USE $db;\n" .
162 "TRUNCATE TABLE interwiki;\n" .
163 "INSERT INTO interwiki (iw_prefix,iw_url,iw_local) VALUES\n";
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'] ) )
172 $sql .= makeLink( $iwEntry, $first );
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 );
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 );
194 foreach ( $extraLinks as $link ){
195 $sql .= makeLink( $link, $first );
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;
211 # Actual languages with their own databases
212 foreach ( $langlist as $targetLang ) {
213 $sql .= makeLink( array( $targetLang, $site->getURL( $targetLang ), 1 ), $first );
217 foreach ( $languageAliases as $alias => $lang ) {
218 $sql .= makeLink( array( $alias, $site->getURL( $lang ), 1 ), $first );
223 # Make SQL for a single link from an array
224 function makeLink( $entry, &$first ) {
232 $sql .= "(" . Database::makeList( $entry ) . ")";