Method for reconstructing the Wikimedia interwiki tables in all their glory, back...
[lhc/web/wiklou.git] / maintenance / rebuildInterwiki.php
1 <?
2
3 # Rebuild interwiki table using the file on meta and the language list
4 # Wikimedia specific!
5
6 $optionsWithArgs = array( "o" );
7 include( "commandLine.inc" );
8
9 class Site {
10 var $suffix, $lateral, $url;
11
12 function Site( $s, $l, $u ) {
13 $this->suffix = $s;
14 $this->lateral = $l;
15 $this->url = $u;
16 }
17
18 function getURL( $lang ) {
19 return "$lang.{$this->url}/wiki/\$1";
20 }
21 }
22
23 $row = wfGetArray( "metawiki.cur", array( "cur_text" ), array( "cur_namespace" => 0, "cur_title" => "Interwiki_map" ) );
24
25 if ( !$row ) {
26 die( "m:Interwiki_map not found" );
27 }
28
29 $lines = explode( "\n", $row->cur_text );
30 $iwArray = array();
31
32 foreach ( $lines as $line ) {
33 if ( preg_match( '/^\|\s*(.*?)\s*\|\|\s*(.*?)\s*$/', $line, $matches ) ) {
34 $prefix = $matches[1];
35 $url = $matches[2];
36 if ( preg_match( '/^http:\/\/(a-z)*\.(wikipedia|wiktionary|wikisource|wikiquote|wikibooks)/', $url ) ) {
37 $local = 1;
38 } else {
39 $local = 0;
40 }
41
42 $iwArray[] = array( "iw_prefix" => $prefix, "iw_url" => $url, "iw_local" => $local );
43 }
44 }
45
46 $langlist = array_map( "trim", file( "/home/wikipedia/langlist" ) );
47
48 # Insert links into special wikis
49 # No interlanguage links, that's the definition of a special wiki
50 # Just intermap links
51
52 $specials = array(
53 'sourceswiki' => 'sources.wikipedia.org',
54 'quotewiki' => 'wikiquote.org',
55 'textbookwiki' => 'wikibooks.org',
56 'sep11wiki' => 'sep11.wikipedia.org',
57 'metawiki' => 'meta.wikipedia.org',
58 );
59
60 $sql = "-- Generated by rebuildInterwiki.php";
61
62 foreach ( $specials as $db => $host ) {
63 $sql .= "\nUSE $db;\n" .
64 "TRUNCATE TABLE interwiki;\n" .
65 "INSERT INTO interwiki (iw_prefix, iw_url, iw_local) VALUES \n";
66 $first = true;
67
68 foreach ( $iwArray as $iwEntry ) {
69 # Add comma
70 if ( $first ) {
71 $first = false;
72 } else {
73 $sql .= ",\n";
74 }
75 # Suppress links to self
76 if ( strpos( $host, $iwEntry['iw_url'] ) === false ) {
77 $sql .= "(" . Database::makeList( $iwEntry ) . ")";
78 }
79 }
80 $sql .= ";\n";
81 }
82 $sql .= "\n";
83
84 # Insert links into multilanguage sites
85
86 $sites = array(
87 new Site( 'wiki', 'w', 'wikipedia.org' ),
88 new Site( 'wiktionary.org', 'wiktionary.org' );
89 );
90
91 foreach ( $sites as $site ) {
92 $sql <<<EOS
93
94 ---
95 --- $site
96 ---
97 EOS;
98 foreach ( $langlist as $lang ) {
99 $db = $lang . $site->suffix;
100 $db = str_replace( "-", "_", $db );
101
102 $sql .= "USE $db;\n" .
103 "TRUNCATE TABLE interwiki\n" .
104 "INSERT INTO interwiki (iw_prefix,iw_url,iw_local\n";
105 $first = true;
106
107 # Intermap links
108 foreach ( $iwArray as $iwEntry ) {
109 # Add comma
110 if ( $first ) {
111 $first = false;
112 } else {
113 $sql .= ",\n";
114 }
115 # Suppress links to self
116 if ( strpos( $host, $iwEntry['iw_url'] ) === false ) {
117 $sql .= "(" . Database::makeList( $iwEntry ) . ")";
118 }
119 }
120
121 # Lateral links
122 foreach ( $sites as $targetSite ) {
123 if ( $first ) {
124 $first = false;
125 } else {
126 $sql .= ",\n";
127 }
128 $link = array( $targetSite->lateral, $targetSite->getURL( $lang ), 1 );
129 $sql .= "(" . Database::makeList( $link ) . ")";
130 }
131
132 # Interlanguage links
133 foreach ( $langlist as $targetLang ) {
134 if ( $first ) {
135 $first = false;
136 } else {
137 $sql .= ",\n";
138 }
139 $link = array( $targetLang, $site->getURL( $targetLang ), 1 );
140 $sql .= "(" . Database::makeList( $link ) . ")";
141 }
142 }
143 $sql .= ";\n\n";
144 }
145
146 # Output
147 if ( isset( $options['o'] ) ) {
148 # To file specified with -o
149 $file = fopen( $options['o'], "w" );
150 fwrite( $file, $sql );
151 fclose( $file );
152 } else {
153 # To stdout
154 print $sql;
155 }
156 ?>