3 # Rebuild interwiki table using the file on meta and the language list
7 $optionsWithArgs = array( "o" );
8 include_once( "commandLine.inc" );
11 var $suffix, $lateral, $url;
13 function Site( $s, $l, $u ) {
19 function getURL( $lang ) {
20 return "http://$lang.{$this->url}/wiki/\$1";
24 function getRebuildInterwikiSQL() {
25 global $langlist, $languageAliases;
27 # Initialise lists of wikis
29 'wiki' => new Site( 'wiki', 'w', 'wikipedia.org' ),
30 'wiktionary' => new Site( 'wiktionary', 'wikt', 'wiktionary.org' ),
31 'wikiquote' => new Site( 'wikiquote', 'q', 'wikiquote.org' ),
32 'wikibooks' => new Site( 'wikibooks', 'b', 'wikibooks.org' )
34 $langlist = array_map( "trim", file( "/home/wikipedia/common/langlist" ) );
35 $dblist = array_map( "trim", file( "/home/wikipedia/common/all.dblist" ) );
38 'sourceswiki' => 'sources.wikipedia.org',
39 'quotewiki' => 'wikiquote.org',
40 'textbookwiki' => 'wikibooks.org',
41 'sep11wiki' => 'sep11.wikipedia.org',
42 'metawiki' => 'meta.wikimedia.org',
46 array( 'm', 'http://meta.wikimedia.org/wiki/$1', 1 ),
47 array( 'meta', 'http://meta.wikimedia.org/wiki/$1', 1 ),
48 array( 'sep11', 'http://sep11.wikipedia.org/wiki/$1', 1 ),
51 $languageAliases = array(
57 # Construct a list of reserved prefixes
59 foreach ( $langlist as $lang ) {
62 foreach ( $languageAliases as $alias => $lang ) {
63 $reserved[$alias] = 1;
65 foreach( $sites as $site ) {
66 $reserved[$site->lateral] = 1;
69 # Extract the intermap from meta
70 $dbr =& wfGetDB( DB_WRITE );
71 $row = $dbr->getArray( "metawiki.cur", array( "cur_text" ),
72 array( "cur_namespace" => 0, "cur_title" => "Interwiki_map" ) );
75 die( "m:Interwiki_map not found" );
78 $lines = explode( "\n", $row->cur_text );
81 foreach ( $lines as $line ) {
82 if ( preg_match( '/^\|\s*(.*?)\s*\|\|\s*(.*?)\s*$/', $line, $matches ) ) {
83 $prefix = strtolower( $matches[1] );
85 if ( preg_match( '/(wikipedia|wiktionary|wikisource|wikiquote|wikibooks)\.org/', $url ) ) {
91 if ( empty( $reserved[$prefix] ) ) {
92 $iwArray[] = array( "iw_prefix" => $prefix, "iw_url" => $url, "iw_local" => $local );
97 $sql = "-- Generated by rebuildInterwiki.php";
100 foreach ( $dblist as $db ) {
101 if ( isset( $specials[$db] ) ) {
103 # Has interwiki links and interlanguage links to wikipedia
105 $host = $specials[$db];
106 $sql .= "\n--$host\n\n";
107 $sql .= "USE $db;\n" .
108 "TRUNCATE TABLE interwiki;\n" .
109 "INSERT INTO interwiki (iw_prefix, iw_url, iw_local) VALUES \n";
113 foreach ( $iwArray as $iwEntry ) {
114 $sql .= makeLink( $iwEntry, $first );
117 # Links to multilanguage sites
118 foreach ( $sites as $targetSite ) {
119 $sql .= makeLink( array( $targetSite->lateral, $targetSite->getURL( 'en' ), 1 ), $first );
122 # Interlanguage links to wikipedia
123 $sql .= makeLanguageLinks( $sites['wiki'], $first );
126 foreach ( $extraLinks as $link ) {
127 $sql .= makeLink( $link, $first );
132 # Find out which site this DB belongs to
134 foreach( $sites as $candidateSite ) {
135 $suffix = $candidateSite->suffix;
136 if ( preg_match( "/(.*)$suffix$/", $db, $matches ) ) {
137 $site = $candidateSite;
142 print "Invalid database $db\n";
146 $host = "$lang." . $site->url;
147 $sql .= "\n--$host\n\n";
149 $sql .= "USE $db;\n" .
150 "TRUNCATE TABLE interwiki;\n" .
151 "INSERT INTO interwiki (iw_prefix,iw_url,iw_local) VALUES\n";
155 foreach ( $iwArray as $iwEntry ) {
156 # Suppress links with the same name as the site
157 if ( ( $suffix == 'wiki' && $iwEntry['iw_prefix'] != 'wikipedia' ) ||
158 ( $suffix != 'wiki' && $suffix != $iwEntry['iw_prefix'] ) )
160 $sql .= makeLink( $iwEntry, $first );
165 foreach ( $sites as $targetSite ) {
166 # Suppress link to self
167 if ( $targetSite->suffix != $site->suffix ) {
168 $sql .= makeLink( array( $targetSite->lateral, $targetSite->getURL( $lang ), 1 ), $first );
172 # Interlanguage links
173 $sql .= makeLanguageLinks( $site, $first );
175 # w link within wikipedias
176 # Other sites already have it as a lateral link
177 if ( $site->suffix == "wiki" ) {
178 $sql .= makeLink( array("w", "http://en.wikipedia.org/wiki/$1", 1), $first );
182 foreach ( $extraLinks as $link ){
183 $sql .= makeLink( $link, $first );
191 # ------------------------------------------------------------------------------------------
193 # Returns part of an INSERT statement, corresponding to all interlanguage links to a particular site
194 function makeLanguageLinks( &$site, &$first ) {
195 global $langlist, $languageAliases;
199 # Actual languages with their own databases
200 foreach ( $langlist as $targetLang ) {
201 $sql .= makeLink( array( $targetLang, $site->getURL( $targetLang ), 1 ), $first );
205 foreach ( $languageAliases as $alias => $lang ) {
206 $sql .= makeLink( array( $alias, $site->getURL( $lang ), 1 ), $first );
211 # Make SQL for a single link from an array
212 function makeLink( $entry, &$first ) {
220 $sql .= "(" . Database::makeList( $entry ) . ")";