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