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 # Initialise lists of wikis
26 'wiki' => new Site( 'wiki', 'w', 'wikipedia.org' ),
27 'wiktionary' => new Site( 'wiktionary', 'wikt', 'wiktionary.org' )
29 $langlist = array_map( "trim", file( "/home/wikipedia/common/langlist" ) );
32 'sourceswiki' => 'sources.wikipedia.org',
33 'quotewiki' => 'wikiquote.org',
34 'textbookwiki' => 'wikibooks.org',
35 'sep11wiki' => 'sep11.wikipedia.org',
36 'metawiki' => 'meta.wikipedia.org',
40 array( 'm', 'http://meta.wikipedia.org/wiki/$1', 1 ),
41 array( 'meta', 'http://meta.wikipedia.org/wiki/$1', 1 ),
42 array( 'sep11', 'http://sep11.wikipedia.org/wiki/$1', 1 ),
45 $languageAliases = array(
50 # Extract the intermap from meta
52 $row = wfGetArray( "metawiki.cur", array( "cur_text" ), array( "cur_namespace" => 0, "cur_title" => "Interwiki_map" ) );
55 die( "m:Interwiki_map not found" );
58 $lines = explode( "\n", $row->cur_text
);
61 foreach ( $lines as $line ) {
62 if ( preg_match( '/^\|\s*(.*?)\s*\|\|\s*(.*?)\s*$/', $line, $matches ) ) {
63 $prefix = $matches[1];
65 if ( preg_match( '/(wikipedia|wiktionary|wikisource|wikiquote|wikibooks)\.org/', $url ) ) {
71 $iwArray[] = array( "iw_prefix" => $prefix, "iw_url" => $url, "iw_local" => $local );
76 # Insert links into special wikis
77 # These have intermap links and interlanguage links pointing to wikipedia
79 $sql = "-- Generated by rebuildInterwiki.php";
81 foreach ( $specials as $db => $host ) {
82 $sql .= "\nUSE $db;\n" .
83 "TRUNCATE TABLE interwiki;\n" .
84 "INSERT INTO interwiki (iw_prefix, iw_url, iw_local) VALUES \n";
88 foreach ( $iwArray as $iwEntry ) {
89 # Suppress links to self
90 if ( strpos( $iwEntry['iw_url'], $host ) === false ) {
91 $sql .= makeLink( $iwEntry, $first );
95 $sql .= makeLink( array("w", "http://en.wikipedia.org/wiki/$1", 1 ), $first );
97 # Interlanguage links to wikipedia
98 $sql .= makeLanguageLinks( $sites['wiki'], $first );
101 foreach ( $extraLinks as $link ) {
102 $sql .= makeLink( $link, $first );
109 # Insert links into multilanguage sites
111 foreach ( $sites as $site ) {
119 foreach ( $langlist as $lang ) {
120 $db = $lang . $site->suffix
;
121 $db = str_replace( "-", "_", $db );
123 $sql .= "USE $db;\n" .
124 "TRUNCATE TABLE interwiki;\n" .
125 "INSERT INTO interwiki (iw_prefix,iw_url,iw_local) VALUES\n";
129 foreach ( $iwArray as $iwEntry ) {
130 # Suppress links to self
131 if ( strpos( $iwEntry['iw_url'], $site->url
) === false ||
132 strpos( $iwEntry['iw_url'], 'meta.wikipedia.org' ) !== false ) {
133 $sql .= makeLink( $iwEntry, $first );
138 foreach ( $sites as $targetSite ) {
139 # Suppress link to self
140 if ( $targetSite->suffix
!= $site->suffix
) {
141 $sql .= makeLink( array( $targetSite->lateral
, $targetSite->getURL( $lang ), 1 ), $first );
145 # Interlanguage links
146 $sql .= makeLanguageLinks( $site, $first );
148 # w link within wikipedias
149 # Other sites already have it as a lateral link
150 if ( $site->suffix
== "wiki" ) {
151 $sql .= makeLink( array("w", "http://en.wikipedia.org/wiki/$1", 1), $first );
155 foreach ( $extraLinks as $link ){
156 $sql .= makeLink( $link, $first );
163 if ( isset( $options['o'] ) ) {
164 # To file specified with -o
166 $file = fopen( $options['o'], "w" );
167 fwrite( $file, $sql );
174 # ------------------------------------------------------------------------------------------
176 # Returns part of an INSERT statement, corresponding to all interlanguage links to a particular site
177 function makeLanguageLinks( &$site, &$first ) {
178 global $langlist, $languageAliases;
182 # Actual languages with their own databases
183 foreach ( $langlist as $targetLang ) {
184 $sql .= makeLink( array( $targetLang, $site->getURL( $targetLang ), 1 ), $first );
188 foreach ( $languageAliases as $alias => $lang ) {
189 $sql .= makeLink( array( $alias, $site->getURL( $lang ), 1 ), $first );
194 # Make SQL for a single link from an array
195 function makeLink( $entry, &$first ) {
203 $sql .= "(" . Database
::makeList( $entry ) . ")";