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