From 005b99f6696ecbc852af4a5c227ae7895da71cc7 Mon Sep 17 00:00:00 2001 From: Tim Starling Date: Fri, 18 Jun 2004 15:08:25 +0000 Subject: [PATCH] Method for reconstructing the Wikimedia interwiki tables in all their glory, back to a form similar to before the DB crash. Experimental, will test and debug on live server. --- maintenance/rebuildInterwiki.php | 156 +++++++++++++++++++++++++++++++ 1 file changed, 156 insertions(+) create mode 100644 maintenance/rebuildInterwiki.php diff --git a/maintenance/rebuildInterwiki.php b/maintenance/rebuildInterwiki.php new file mode 100644 index 0000000000..f07c94d4ab --- /dev/null +++ b/maintenance/rebuildInterwiki.php @@ -0,0 +1,156 @@ +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 <<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; +} +?> -- 2.20.1