replacing some calls from deprecated global functions to new modularized functions
[lhc/web/wiklou.git] / maintenance / dumpInterwiki.inc
1 <?php
2 /**
3 * Rebuild interwiki table using the file on meta and the language list
4 * Wikimedia specific!
5 *
6 * @todo document
7 * @addtogroup Maintenance
8 */
9
10 /** */
11
12 /**
13 * @todo document
14 * @addtogroup Maintenance
15 */
16 class Site {
17 var $suffix, $lateral, $url;
18
19 function __construct( $s, $l, $u ) {
20 $this->suffix = $s;
21 $this->lateral = $l;
22 $this->url = $u;
23 }
24
25 function getURL( $lang ) {
26 $xlang = str_replace( '_', '-', $lang );
27 return "http://$xlang.{$this->url}/wiki/\$1";
28 }
29 }
30
31 function getRebuildInterwikiDump() {
32 global $langlist, $languageAliases, $prefixRewrites;
33
34 # Multi-language sites
35 # db suffix => db suffix, iw prefix, hostname
36 $sites = array(
37 'wiki' => new Site( 'wiki', 'w', 'wikipedia.org' ),
38 'wiktionary' => new Site( 'wiktionary', 'wikt', 'wiktionary.org' ),
39 'wikiquote' => new Site( 'wikiquote', 'q', 'wikiquote.org' ),
40 'wikibooks' => new Site( 'wikibooks', 'b', 'wikibooks.org' ),
41 'wikinews' => new Site( 'wikinews', 'n', 'wikinews.org' ),
42 'wikisource' => new Site( 'wikisource', 's', 'wikisource.org' ),
43 'wikimedia' => new Site( 'wikimedia', 'chapter', 'wikimedia.org' ),
44 'wikiversity' => new Site( 'wikiversity', 'v', 'wikiversity.org' ),
45 );
46
47 # List of language prefixes likely to be found in multi-language sites
48 $langlist = array_map( "trim", file( "/home/wikipedia/common/langlist" ) );
49
50 # List of all database names
51 $dblist = array_map( "trim", file( "/home/wikipedia/common/all.dblist" ) );
52
53 # Special-case databases
54 $specials = array_flip(
55 array_map( "trim",
56 file( "/home/wikipedia/common/special.dblist" ) ) );
57
58 # Extra interwiki links that can't be in the intermap for some reason
59 $extraLinks = array(
60 array( 'm', 'http://meta.wikimedia.org/wiki/$1', 1 ),
61 array( 'meta', 'http://meta.wikimedia.org/wiki/$1', 1 ),
62 array( 'sep11', 'http://sep11.wikipedia.org/wiki/$1', 1 ),
63 );
64
65 # Language aliases, usually configured as redirects to the real wiki in apache
66 # Interlanguage links are made directly to the real wiki
67 # Something horrible happens if you forget to list an alias here, I can't
68 # remember what
69 $languageAliases = array(
70 'zh-cn' => 'zh',
71 'zh-tw' => 'zh',
72 'dk' => 'da',
73 'nb' => 'no',
74 );
75
76 # Special case prefix rewrites, for the benefit of Swedish which uses s:t
77 # as an abbreviation for saint
78 $prefixRewrites = array(
79 'svwiki' => array ( 's' => 'src'),
80 );
81
82 # Construct a list of reserved prefixes
83 $reserved = array();
84 foreach ( $langlist as $lang ) {
85 $reserved[$lang] = 1;
86 }
87 foreach ( $languageAliases as $alias => $lang ) {
88 $reserved[$alias] = 1;
89 }
90 foreach( $sites as $site ) {
91 $reserved[$site->lateral] = 1;
92 }
93
94 # Extract the intermap from meta
95 $intermap = Http::get( 'http://meta.wikimedia.org/w/index.php?title=Interwiki_map&action=raw', 30 );
96 $lines = array_map( 'trim', explode( "\n", trim( $intermap ) ) );
97
98 if ( !$lines || count( $lines ) < 2 ) {
99 wfDie( "m:Interwiki_map not found" );
100 }
101
102 # Global iterwiki map
103 foreach ( $lines as $line ) {
104 if ( preg_match( '/^\|\s*(.*?)\s*\|\|\s*(.*?)\s*$/', $line, $matches ) ) {
105 $prefix = strtolower( $matches[1] );
106 $url = $matches[2];
107 if ( preg_match( '/(wikipedia|wiktionary|wikisource|wikiquote|wikibooks|wikimedia)\.org/', $url ) ) {
108 $local = 1;
109 } else {
110 $local = 0;
111 }
112
113 if ( empty( $reserved[$prefix] ) ) {
114 $imap = array( "iw_prefix" => $prefix, "iw_url" => $url, "iw_local" => $local );
115 makeLink ($imap, "__global");
116 }
117 }
118 }
119
120 # Exclude Wikipedia for Wikipedia
121 makeLink ( array ('iw_prefix' => 'wikipedia', 'is_url' => null ), "_wiki" );
122
123 #Multilanguage sites
124 foreach ($sites as $site)
125 makeLanguageLinks ( $site, "_".$site->suffix );
126
127
128 foreach ( $dblist as $db ) {
129 if ( isset( $specials[$db] ) ) {
130 # Special wiki
131 # Has interwiki links and interlanguage links to wikipedia
132
133 makeLink( array( 'iw_prefix' => $db, 'iw_url' => "wiki"), "__sites" );
134 # Links to multilanguage sites
135 foreach ( $sites as $targetSite ) {
136 makeLink( array( 'iw_prefix' => $targetSite->lateral,
137 'iw_url' =>$targetSite->getURL( 'en' ),
138 'iw_local' => 1 ), $db );
139 }
140
141 } else {
142 # Find out which site this DB belongs to
143 $site = false;
144 foreach( $sites as $candidateSite ) {
145 $suffix = $candidateSite->suffix;
146 if ( preg_match( "/(.*)$suffix$/", $db, $matches ) ) {
147 $site = $candidateSite;
148 break;
149 }
150 }
151 makeLink( array( 'iw_prefix' => $db, 'iw_url' => $site->suffix), "__sites" );
152 if ( !$site ) {
153 print "Invalid database $db\n";
154 continue;
155 }
156 $lang = $matches[1];
157
158 # Lateral links
159 foreach ( $sites as $targetSite ) {
160 if ( $targetSite->suffix != $site->suffix ) {
161 makeLink( array( 'iw_prefix' => $targetSite->lateral,
162 'iw_url' => $targetSite->getURL( $lang ),
163 'iw_local' => 1 ), $db );
164 }
165 }
166
167 if ( $site->suffix == "wiki" ) {
168 makeLink( array('iw_prefix' => 'w',
169 'iw_url' => "http://en.wikipedia.org/wiki/$1",
170 'iw_local' => 1), $db );
171 }
172
173 }
174 }
175 foreach ( $extraLinks as $link )
176 makeLink( $link, "__global" );
177 }
178
179 # ------------------------------------------------------------------------------------------
180
181 # Executes part of an INSERT statement, corresponding to all interlanguage links to a particular site
182 function makeLanguageLinks( &$site, $source ) {
183 global $langlist, $languageAliases;
184 # Actual languages with their own databases
185 foreach ( $langlist as $targetLang ) {
186 makeLink( array( $targetLang, $site->getURL( $targetLang ), 1 ), $source );
187 }
188
189 # Language aliases
190 foreach ( $languageAliases as $alias => $lang ) {
191 makeLink( array( $alias, $site->getURL( $lang ), 1 ), $source );
192 }
193 }
194
195 function makeLink( $entry, $source ) {
196 global $prefixRewrites, $dbFile;
197 if ( isset( $prefixRewrites[$source] ) && isset( $prefixRewrites[$source][$entry[0]] ) )
198 $entry[0] = $prefixRewrites[$source][$entry[0]];
199 if (!array_key_exists("iw_prefix",$entry))
200 $entry = array("iw_prefix" => $entry[0], "iw_url" => $entry[1], "iw_local" => $entry[2]);
201 if ( array_key_exists($source,$prefixRewrites) &&
202 array_key_exists($entry['iw_prefix'],$prefixRewrites[$source]))
203 $entry['iw_prefix'] = $prefixRewrites[$source][$entry['iw_prefix']];
204 if ($dbFile)
205 dba_insert("{$source}:{$entry['iw_prefix']}", trim("{$entry['iw_local']} {$entry['iw_url']}"),$dbFile);
206 else
207 print "{$source}:{$entry['iw_prefix']} {$entry['iw_url']} {$entry['iw_local']}\n";
208
209 }
210
211 ?>