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