e1170bd7eabb25e021a044d855769a57e388c2de
[lhc/web/wiklou.git] / maintenance / dumpInterwiki.php
1 <?php
2 /**
3 * Build constant slightly compact database of interwiki prefixes
4 * Wikimedia specific!
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @file
22 * @todo document
23 * @ingroup Maintenance
24 * @ingroup Wikimedia
25 */
26
27 require_once( dirname( __FILE__ ) . '/Site.php' );
28
29 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
30
31 class DumpInterwiki extends Maintenance {
32
33 public function __construct() {
34 parent::__construct();
35 $this->mDescription = "Build constant slightly compact database of interwiki prefixes.";
36 $this->addOption( 'langlist', 'File with one language code per line', false, true );
37 $this->addOption( 'dblist', 'File with one db per line', false, true );
38 $this->addOption( 'specialdbs', "File with one 'special' db per line", false, true );
39 $this->addOption( 'o', 'Cdb output file', false, true );
40 $this->addOption( 'protocolrelative', 'Output wikimedia interwiki urls as protocol relative', false, false );
41 }
42
43 function execute() {
44 # List of language prefixes likely to be found in multi-language sites
45 $this->langlist = array_map( "trim", file( $this->getOption( 'langlist', "/home/wikipedia/common/langlist" ) ) );
46
47 # List of all database names
48 $this->dblist = array_map( "trim", file( $this->getOption( 'dblist', "/home/wikipedia/common/all.dblist" ) ) );
49
50 # Special-case databases
51 $this->specials = array_flip( array_map( "trim", file( $this->getOption( 'specialdbs', "/home/wikipedia/common/special.dblist" ) ) ) );
52
53 if ( $this->hasOption( 'o' ) ) {
54 $this->dbFile = CdbWriter::open( $this->getOption( 'o' ) ) ;
55 } else {
56 $this->dbFile = false;
57 }
58
59 if ( $this->hasOption( 'protocolrelative' ) ) {
60 $this->urlprotocol = '';
61 } else {
62 $this->urlprotocol = 'http:';
63 }
64
65 $this->getRebuildInterwikiDump();
66 }
67
68 function getRebuildInterwikiDump() {
69 global $wgContLang;
70
71 # Multi-language sites
72 # db suffix => db suffix, iw prefix, hostname
73 $sites = array(
74 'wiki' => new Site( 'wiki', 'w', 'wikipedia.org' ),
75 'wiktionary' => new Site( 'wiktionary', 'wikt', 'wiktionary.org' ),
76 'wikiquote' => new Site( 'wikiquote', 'q', 'wikiquote.org' ),
77 'wikibooks' => new Site( 'wikibooks', 'b', 'wikibooks.org' ),
78 'wikinews' => new Site( 'wikinews', 'n', 'wikinews.org' ),
79 'wikisource' => new Site( 'wikisource', 's', 'wikisource.org' ),
80 'wikimedia' => new Site( 'wikimedia', 'chapter', 'wikimedia.org' ),
81 'wikiversity' => new Site( 'wikiversity', 'v', 'wikiversity.org' ),
82 );
83
84 # Extra interwiki links that can't be in the intermap for some reason
85 $extraLinks = array(
86 array( 'm', $this->urlprotocol . '//meta.wikimedia.org/wiki/$1', 1 ),
87 array( 'meta', $this->urlprotocol . '//meta.wikimedia.org/wiki/$1', 1 ),
88 array( 'sep11', $this->urlprotocol . '//sep11.wikipedia.org/wiki/$1', 1 ),
89 );
90
91 # Language aliases, usually configured as redirects to the real wiki in apache
92 # Interlanguage links are made directly to the real wiki
93 # Something horrible happens if you forget to list an alias here, I can't
94 # remember what
95 $this->languageAliases = array(
96 'zh-cn' => 'zh',
97 'zh-tw' => 'zh',
98 'dk' => 'da',
99 'nb' => 'no',
100 );
101
102 # Special case prefix rewrites, for the benefit of Swedish which uses s:t
103 # as an abbreviation for saint
104 $this->prefixRewrites = array(
105 'svwiki' => array( 's' => 'src' ),
106 );
107
108 # Construct a list of reserved prefixes
109 $reserved = array();
110 foreach ( $this->langlist as $lang ) {
111 $reserved[$lang] = 1;
112 }
113 foreach ( $this->languageAliases as $alias => $lang ) {
114 $reserved[$alias] = 1;
115 }
116 foreach ( $sites as $site ) {
117 $reserved[$site->lateral] = 1;
118 }
119
120 # Extract the intermap from meta
121 $intermap = Http::get( 'http://meta.wikimedia.org/w/index.php?title=Interwiki_map&action=raw', 30 );
122 $lines = array_map( 'trim', explode( "\n", trim( $intermap ) ) );
123
124 if ( !$lines || count( $lines ) < 2 ) {
125 $this->error( "m:Interwiki_map not found", true );
126 }
127
128 # Global interwiki map
129 foreach ( $lines as $line ) {
130 if ( preg_match( '/^\|\s*(.*?)\s*\|\|\s*(.*?)\s*$/', $line, $matches ) ) {
131 $prefix = $wgContLang->lc( $matches[1] );
132 $prefix = str_replace( ' ', '_', $prefix );
133
134 $url = $matches[2];
135 if ( preg_match( '/(wikipedia|wiktionary|wikisource|wikiquote|wikibooks|wikimedia)\.org/', $url ) ) {
136 if ( $this->hasOption( 'protocolrelative' ) ) {
137 if ( substr( $url, 0, 5 ) == 'http:' ) {
138 $url = substr( $url, 5 );
139 } else if ( substr( $url, 0, 6 ) == 'https:' ) {
140 $url = substr( $url, 6 );
141 }
142 }
143 $local = 1;
144 } else {
145 $local = 0;
146 }
147
148 if ( empty( $reserved[$prefix] ) ) {
149 $imap = array( "iw_prefix" => $prefix, "iw_url" => $url, "iw_local" => $local );
150 $this->makeLink ( $imap, "__global" );
151 }
152 }
153 }
154
155 # Exclude Wikipedia for Wikipedia
156 $this->makeLink ( array ( 'iw_prefix' => 'wikipedia', 'is_url' => null ), "_wiki" );
157
158 # Multilanguage sites
159 foreach ( $sites as $site ) {
160 $this->makeLanguageLinks ( $site, "_" . $site->suffix );
161 }
162
163 foreach ( $this->dblist as $db ) {
164 if ( isset( $this->specials[$db] ) ) {
165 # Special wiki
166 # Has interwiki links and interlanguage links to wikipedia
167
168 $this->makeLink( array( 'iw_prefix' => $db, 'iw_url' => "wiki" ), "__sites" );
169 # Links to multilanguage sites
170 foreach ( $sites as $targetSite ) {
171 $this->makeLink( array( 'iw_prefix' => $targetSite->lateral,
172 'iw_url' => $targetSite->getURL( 'en', $this->urlprotocol ),
173 'iw_local' => 1 ), $db );
174 }
175 } else {
176 # Find out which site this DB belongs to
177 $site = false;
178 foreach ( $sites as $candidateSite ) {
179 $suffix = $candidateSite->suffix;
180 if ( preg_match( "/(.*)$suffix$/", $db, $matches ) ) {
181 $site = $candidateSite;
182 break;
183 }
184 }
185 $this->makeLink( array( 'iw_prefix' => $db, 'iw_url' => $site->suffix ), "__sites" );
186 if ( !$site ) {
187 $this->error( "Invalid database $db\n" );
188 continue;
189 }
190 $lang = $matches[1];
191
192 # Lateral links
193 foreach ( $sites as $targetSite ) {
194 if ( $targetSite->suffix != $site->suffix ) {
195 $this->makeLink( array( 'iw_prefix' => $targetSite->lateral,
196 'iw_url' => $targetSite->getURL( $lang, $this->urlprotocol ),
197 'iw_local' => 1 ), $db );
198 }
199 }
200
201 if ( $site->suffix == "wiki" ) {
202 $this->makeLink( array( 'iw_prefix' => 'w',
203 'iw_url' => $this->urlprotocol . "//en.wikipedia.org/wiki/$1",
204 'iw_local' => 1 ), $db );
205 }
206
207 }
208 }
209 foreach ( $extraLinks as $link ) {
210 $this->makeLink( $link, "__global" );
211 }
212
213 # List prefixes for each source
214 foreach ( $this->prefixLists as $source => $hash ) {
215 $list = array_keys( $hash );
216 sort( $list );
217 if ( $this->dbFile ) {
218 $this->dbFile->set( "__list:{$source}", implode( ' ', $list ) );
219 } else {
220 print "__list:{$source} " . implode( ' ', $list ) . "\n";
221 }
222 }
223 }
224
225 # ------------------------------------------------------------------------------------------
226
227 # Executes part of an INSERT statement, corresponding to all interlanguage links to a particular site
228 function makeLanguageLinks( &$site, $source ) {
229 # Actual languages with their own databases
230 foreach ( $this->langlist as $targetLang ) {
231 $this->makeLink( array( $targetLang, $site->getURL( $targetLang, $this->urlprotocol ), 1 ), $source );
232 }
233
234 # Language aliases
235 foreach ( $this->languageAliases as $alias => $lang ) {
236 $this->makeLink( array( $alias, $site->getURL( $lang, $this->urlprotocol ), 1 ), $source );
237 }
238 }
239
240 function makeLink( $entry, $source ) {
241 if ( isset( $this->prefixRewrites[$source] ) && isset( $this->prefixRewrites[$source][$entry[0]] ) )
242 $entry[0] = $this->prefixRewrites[$source][$entry[0]];
243
244 if ( !array_key_exists( "iw_prefix", $entry ) ) {
245 $entry = array( "iw_prefix" => $entry[0], "iw_url" => $entry[1], "iw_local" => $entry[2] );
246 }
247 if ( array_key_exists( $source, $this->prefixRewrites ) &&
248 array_key_exists( $entry['iw_prefix'], $this->prefixRewrites[$source] ) ) {
249 $entry['iw_prefix'] = $this->prefixRewrites[$source][$entry['iw_prefix']];
250 }
251
252 if ( $this->dbFile ) {
253 $this->dbFile->set( "{$source}:{$entry['iw_prefix']}", trim( "{$entry['iw_local']} {$entry['iw_url']}" ) );
254 } else {
255 $this->output( "{$source}:{$entry['iw_prefix']} {$entry['iw_url']} {$entry['iw_local']}\n" );
256 }
257
258 # Add to list of prefixes
259 $this->prefixLists[$source][$entry['iw_prefix']] = 1;
260 }
261 }
262
263 $maintClass = "DumpInterwiki";
264 require_once( RUN_MAINTENANCE_IF_MAIN );
265