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