Method for reconstructing the Wikimedia interwiki tables in all their glory, back...
authorTim Starling <tstarling@users.mediawiki.org>
Fri, 18 Jun 2004 15:08:25 +0000 (15:08 +0000)
committerTim Starling <tstarling@users.mediawiki.org>
Fri, 18 Jun 2004 15:08:25 +0000 (15:08 +0000)
maintenance/rebuildInterwiki.php [new file with mode: 0644]

diff --git a/maintenance/rebuildInterwiki.php b/maintenance/rebuildInterwiki.php
new file mode 100644 (file)
index 0000000..f07c94d
--- /dev/null
@@ -0,0 +1,156 @@
+<?
+
+# Rebuild interwiki table using the file on meta and the language list
+# Wikimedia specific!
+
+$optionsWithArgs = array( "o" );
+include( "commandLine.inc" );
+
+class Site {
+       var $suffix, $lateral, $url;
+
+       function Site( $s, $l, $u ) {
+               $this->suffix = $s;
+               $this->lateral = $l;
+               $this->url = $u;
+       }
+
+       function getURL( $lang ) {
+               return "$lang.{$this->url}/wiki/\$1";
+       }
+}
+
+$row = wfGetArray( "metawiki.cur", array( "cur_text" ), array( "cur_namespace" => 0, "cur_title" => "Interwiki_map" ) );
+
+if ( !$row ) {
+       die( "m:Interwiki_map not found" );
+}
+
+$lines = explode( "\n", $row->cur_text );
+$iwArray = array();
+
+foreach ( $lines as $line ) {
+       if ( preg_match( '/^\|\s*(.*?)\s*\|\|\s*(.*?)\s*$/', $line, $matches ) ) {
+               $prefix = $matches[1];
+               $url = $matches[2];
+               if ( preg_match( '/^http:\/\/(a-z)*\.(wikipedia|wiktionary|wikisource|wikiquote|wikibooks)/', $url ) ) {
+                       $local = 1;
+               } else {
+                       $local = 0;
+               }
+
+               $iwArray[] = array( "iw_prefix" => $prefix, "iw_url" => $url, "iw_local" => $local );
+       }
+}
+
+$langlist = array_map( "trim", file( "/home/wikipedia/langlist" ) );
+
+# Insert links into special wikis
+# No interlanguage links, that's the definition of a special wiki
+# Just intermap links
+
+$specials = array( 
+       'sourceswiki' => 'sources.wikipedia.org',
+       'quotewiki' => 'wikiquote.org',
+       'textbookwiki' => 'wikibooks.org',
+       'sep11wiki' => 'sep11.wikipedia.org',
+       'metawiki' => 'meta.wikipedia.org',
+);
+
+$sql = "-- Generated by rebuildInterwiki.php";
+
+foreach ( $specials as $db => $host ) {
+       $sql .= "\nUSE $db;\n" .
+                       "TRUNCATE TABLE interwiki;\n" . 
+                       "INSERT INTO interwiki (iw_prefix, iw_url, iw_local) VALUES \n";
+       $first = true;
+       
+       foreach ( $iwArray as $iwEntry ) {
+               # Add comma
+               if ( $first ) {
+                       $first = false;
+               } else {
+                       $sql .= ",\n";
+               }
+               # Suppress links to self
+               if ( strpos( $host, $iwEntry['iw_url'] ) === false ) {
+                       $sql .= "(" . Database::makeList( $iwEntry ) . ")";
+               }
+       }
+       $sql .= ";\n";
+}
+$sql .= "\n";
+
+# Insert links into multilanguage sites
+
+$sites = array( 
+       new Site( 'wiki', 'w', 'wikipedia.org' ),
+       new Site( 'wiktionary.org', 'wiktionary.org' );
+);
+
+foreach ( $sites as $site ) {
+       $sql <<<EOS
+
+---
+--- $site
+---
+EOS;
+       foreach ( $langlist as $lang ) {
+               $db = $lang . $site->suffix;
+               $db = str_replace( "-", "_", $db );
+
+               $sql .= "USE $db;\n" .
+                               "TRUNCATE TABLE interwiki\n" .
+                               "INSERT INTO interwiki (iw_prefix,iw_url,iw_local\n";
+               $first = true;
+
+               # Intermap links
+               foreach ( $iwArray as $iwEntry ) {
+                       # Add comma
+                       if ( $first ) {
+                               $first = false;
+                       } else {
+                               $sql .= ",\n";
+                       }
+                       # Suppress links to self
+                       if ( strpos( $host, $iwEntry['iw_url'] ) === false ) {
+                               $sql .= "(" . Database::makeList( $iwEntry ) . ")";
+                       }
+               }
+
+               # Lateral links
+               foreach ( $sites as $targetSite ) {
+                       if ( $first ) {
+                               $first = false;
+                       } else {
+                               $sql .= ",\n";
+                       }
+                       $link = array( $targetSite->lateral, $targetSite->getURL( $lang ), 1 );
+                       $sql .= "(" . Database::makeList( $link ) . ")";
+               }
+
+               # Interlanguage links
+               foreach ( $langlist as $targetLang ) {
+                       if ( $first ) {
+                               $first = false;
+                       } else {
+                               $sql .= ",\n";
+                       }
+                       $link = array( $targetLang, $site->getURL( $targetLang ), 1 );
+                       $sql .= "(" . Database::makeList( $link ) . ")";
+               }
+       }
+       $sql .= ";\n\n";
+}
+
+# Output
+if ( isset( $options['o'] ) ) {
+       # To file specified with -o
+       $file = fopen( $options['o'], "w" );
+       fwrite( $file, $sql );
+       fclose( $file );
+} else {
+       # To stdout
+       print $sql;
+}
+?>