328e765ea01a83b9119e7ecb35226b1def9212ec
[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_once( "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 # Initialise lists of wikis
25 $sites = array(
26 'wiki' => new Site( 'wiki', 'w', 'wikipedia.org' ),
27 'wiktionary' => new Site( 'wiktionary', 'wikt', 'wiktionary.org' )
28 );
29 $langlist = array_map( "trim", file( "/home/wikipedia/common/langlist" ) );
30
31 $specials = array(
32 'sourceswiki' => 'sources.wikipedia.org',
33 'quotewiki' => 'wikiquote.org',
34 'textbookwiki' => 'wikibooks.org',
35 'sep11wiki' => 'sep11.wikipedia.org',
36 'metawiki' => 'meta.wikipedia.org',
37 );
38
39 $extraLinks = array(
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 ),
43 );
44
45 $languageAliases = array(
46 'zh-cn' => 'zh',
47 'zh-tw' => 'zh',
48 );
49
50 # Extract the intermap from meta
51
52 $row = wfGetArray( "metawiki.cur", array( "cur_text" ), array( "cur_namespace" => 0, "cur_title" => "Interwiki_map" ) );
53
54 if ( !$row ) {
55 die( "m:Interwiki_map not found" );
56 }
57
58 $lines = explode( "\n", $row->cur_text );
59 $iwArray = array();
60
61 foreach ( $lines as $line ) {
62 if ( preg_match( '/^\|\s*(.*?)\s*\|\|\s*(.*?)\s*$/', $line, $matches ) ) {
63 $prefix = $matches[1];
64 $url = $matches[2];
65 if ( preg_match( '/(wikipedia|wiktionary|wikisource|wikiquote|wikibooks)\.org/', $url ) ) {
66 $local = 1;
67 } else {
68 $local = 0;
69 }
70
71 $iwArray[] = array( "iw_prefix" => $prefix, "iw_url" => $url, "iw_local" => $local );
72 }
73 }
74
75
76 # Insert links into special wikis
77 # These have intermap links and interlanguage links pointing to wikipedia
78
79 $sql = "-- Generated by rebuildInterwiki.php";
80
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";
85 $first = true;
86
87 # Intermap links
88 foreach ( $iwArray as $iwEntry ) {
89 # Suppress links to self
90 if ( strpos( $iwEntry['iw_url'], $host ) === false ) {
91 $sql .= makeLink( $iwEntry, $first );
92 }
93 }
94 # w link
95 $sql .= makeLink( array("w", "http://en.wikipedia.org/wiki/$1", 1 ), $first );
96
97 # Interlanguage links to wikipedia
98 $sql .= makeLanguageLinks( $sites['wiki'], $first );
99
100 # Extra links
101 foreach ( $extraLinks as $link ) {
102 $sql .= makeLink( $link, $first );
103 }
104
105 $sql .= ";\n";
106 }
107 $sql .= "\n";
108
109 # Insert links into multilanguage sites
110
111 foreach ( $sites as $site ) {
112 $sql .= <<<EOS
113
114 ---
115 --- {$site->suffix}
116 ---
117
118 EOS;
119 foreach ( $langlist as $lang ) {
120 $db = $lang . $site->suffix;
121 $db = str_replace( "-", "_", $db );
122
123 $sql .= "USE $db;\n" .
124 "TRUNCATE TABLE interwiki;\n" .
125 "INSERT INTO interwiki (iw_prefix,iw_url,iw_local) VALUES\n";
126 $first = true;
127
128 # Intermap links
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 );
134 }
135 }
136
137 # Lateral links
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 );
142 }
143 }
144
145 # Interlanguage links
146 $sql .= makeLanguageLinks( $site, $first );
147
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 );
152 }
153
154 # Extra links
155 foreach ( $extraLinks as $link ){
156 $sql .= makeLink( $link, $first );
157 }
158 $sql .= ";\n\n";
159 }
160 }
161
162 # Output
163 if ( isset( $options['o'] ) ) {
164 # To file specified with -o
165 chdir( $oldCwd );
166 $file = fopen( $options['o'], "w" );
167 fwrite( $file, $sql );
168 fclose( $file );
169 } else {
170 # To stdout
171 print $sql;
172 }
173
174 # ------------------------------------------------------------------------------------------
175
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;
179
180 $sql = "";
181
182 # Actual languages with their own databases
183 foreach ( $langlist as $targetLang ) {
184 $sql .= makeLink( array( $targetLang, $site->getURL( $targetLang ), 1 ), $first );
185 }
186
187 # Language aliases
188 foreach ( $languageAliases as $alias => $lang ) {
189 $sql .= makeLink( array( $alias, $site->getURL( $lang ), 1 ), $first );
190 }
191 return $sql;
192 }
193
194 # Make SQL for a single link from an array
195 function makeLink( $entry, &$first ) {
196 $sql = "";
197 # Add comma
198 if ( $first ) {
199 $first = false;
200 } else {
201 $sql .= ",\n";
202 }
203 $sql .= "(" . Database::makeList( $entry ) . ")";
204 return $sql;
205 }
206
207 ?>