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', 'wikiquote', 'wikiquote.org' ),
32 'wikibooks' => new Site( 'wikibooks', 'wikibooks', '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(
56 # Construct a list of reserved prefixes
58 foreach ( $langlist as $lang ) {
61 foreach ( $languageAliases as $alias => $lang ) {
62 $reserved[$alias] = 1;
64 foreach( $sites as $site ) {
65 $reserved[$site->lateral] = 1;
68 # Extract the intermap from meta
69 $dbr =& wfGetDB( DB_WRITE );
70 $row = $dbr->getArray( "metawiki.cur", array( "cur_text" ),
71 array( "cur_namespace" => 0, "cur_title" => "Interwiki_map" ) );
74 die( "m:Interwiki_map not found" );
77 $lines = explode( "\n", $row->cur_text );
80 foreach ( $lines as $line ) {
81 if ( preg_match( '/^\|\s*(.*?)\s*\|\|\s*(.*?)\s*$/', $line, $matches ) ) {
82 $prefix = strtolower( $matches[1] );
84 if ( preg_match( '/(wikipedia|wiktionary|wikisource|wikiquote|wikibooks)\.org/', $url ) ) {
90 if ( empty( $reserved[$prefix] ) ) {
91 $iwArray[] = array( "iw_prefix" => $prefix, "iw_url" => $url, "iw_local" => $local );
96 $sql = "-- Generated by rebuildInterwiki.php";
99 foreach ( $dblist as $db ) {
100 if ( isset( $specials[$db] ) ) {
102 # Has interwiki links and interlanguage links to wikipedia
104 $host = $specials[$db];
105 $sql .= "\n--$host\n\n";
106 $sql .= "USE $db;\n" .
107 "TRUNCATE TABLE interwiki;\n" .
108 "INSERT INTO interwiki (iw_prefix, iw_url, iw_local) VALUES \n";
112 foreach ( $iwArray as $iwEntry ) {
113 $sql .= makeLink( $iwEntry, $first );
116 # Links to multilanguage sites
117 foreach ( $sites as $targetSite ) {
118 $sql .= makeLink( array( $targetSite->lateral, $targetSite->getURL( 'en' ), 1 ), $first );
121 # Interlanguage links to wikipedia
122 $sql .= makeLanguageLinks( $sites['wiki'], $first );
125 foreach ( $extraLinks as $link ) {
126 $sql .= makeLink( $link, $first );
131 # Find out which site this DB belongs to
133 foreach( $sites as $candidateSite ) {
134 $suffix = $candidateSite->suffix;
135 if ( preg_match( "/(.*)$suffix$/", $db, $matches ) ) {
136 $site = $candidateSite;
141 print "Invalid database $db\n";
145 $host = "$lang." . $site->url;
146 $sql .= "\n--$host\n\n";
148 $sql .= "USE $db;\n" .
149 "TRUNCATE TABLE interwiki;\n" .
150 "INSERT INTO interwiki (iw_prefix,iw_url,iw_local) VALUES\n";
154 foreach ( $iwArray as $iwEntry ) {
155 $sql .= makeLink( $iwEntry, $first );
159 foreach ( $sites as $targetSite ) {
160 # Suppress link to self
161 if ( $targetSite->suffix != $site->suffix ) {
162 $sql .= makeLink( array( $targetSite->lateral, $targetSite->getURL( $lang ), 1 ), $first );
166 # Interlanguage links
167 $sql .= makeLanguageLinks( $site, $first );
169 # w link within wikipedias
170 # Other sites already have it as a lateral link
171 if ( $site->suffix == "wiki" ) {
172 $sql .= makeLink( array("w", "http://en.wikipedia.org/wiki/$1", 1), $first );
176 foreach ( $extraLinks as $link ){
177 $sql .= makeLink( $link, $first );
185 # ------------------------------------------------------------------------------------------
187 # Returns part of an INSERT statement, corresponding to all interlanguage links to a particular site
188 function makeLanguageLinks( &$site, &$first ) {
189 global $langlist, $languageAliases;
193 # Actual languages with their own databases
194 foreach ( $langlist as $targetLang ) {
195 $sql .= makeLink( array( $targetLang, $site->getURL( $targetLang ), 1 ), $first );
199 foreach ( $languageAliases as $alias => $lang ) {
200 $sql .= makeLink( array( $alias, $site->getURL( $lang ), 1 ), $first );
205 # Make SQL for a single link from an array
206 function makeLink( $entry, &$first ) {
214 $sql .= "(" . Database::makeList( $entry ) . ")";