]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - maintenance/dumpInterwiki.php
MediaWiki 1.17.0
[autoinstalls/mediawiki.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
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                 foreach ( $this->dblist as $db ) {
152                         if ( isset( $this->specials[$db] ) ) {
153                                 # Special wiki
154                                 # Has interwiki links and interlanguage links to wikipedia
155
156                                 $this->makeLink( array( 'iw_prefix' => $db, 'iw_url' => "wiki" ), "__sites" );
157                                 # Links to multilanguage sites
158                                 foreach ( $sites as $targetSite ) {
159                                         $this->makeLink( array( 'iw_prefix' => $targetSite->lateral,
160                                                 'iw_url' => $targetSite->getURL( 'en' ),
161                                                 'iw_local' => 1 ), $db );
162                                 }
163                         } else {
164                                 # Find out which site this DB belongs to
165                                 $site = false;
166                                 foreach ( $sites as $candidateSite ) {
167                                         $suffix = $candidateSite->suffix;
168                                         if ( preg_match( "/(.*)$suffix$/", $db, $matches ) ) {
169                                                 $site = $candidateSite;
170                                                 break;
171                                         }
172                                 }
173                                 $this->makeLink( array( 'iw_prefix' => $db, 'iw_url' => $site->suffix ), "__sites" );
174                                 if ( !$site ) {
175                                         $this->error( "Invalid database $db\n" );
176                                         continue;
177                                 }
178                                 $lang = $matches[1];
179
180                                 # Lateral links
181                                 foreach ( $sites as $targetSite ) {
182                                         if ( $targetSite->suffix != $site->suffix ) {
183                                                 $this->makeLink( array( 'iw_prefix' => $targetSite->lateral,
184                                                         'iw_url' => $targetSite->getURL( $lang ),
185                                                         'iw_local' => 1 ), $db );
186                                         }
187                                 }
188
189                                 if ( $site->suffix == "wiki" ) {
190                                         $this->makeLink( array( 'iw_prefix' => 'w',
191                                                 'iw_url' => "http://en.wikipedia.org/wiki/$1",
192                                                 'iw_local' => 1 ), $db );
193                                 }
194
195                         }
196                 }
197                 foreach ( $extraLinks as $link ) {
198                         $this->makeLink( $link, "__global" );
199                 }
200         }
201
202         # ------------------------------------------------------------------------------------------
203
204         # Executes part of an INSERT statement, corresponding to all interlanguage links to a particular site
205         function makeLanguageLinks( &$site, $source ) {
206                 # Actual languages with their own databases
207                 foreach ( $this->langlist as $targetLang ) {
208                         $this->makeLink( array( $targetLang, $site->getURL( $targetLang ), 1 ), $source );
209                 }
210
211                 # Language aliases
212                 foreach ( $this->languageAliases as $alias => $lang ) {
213                         $this->makeLink( array( $alias, $site->getURL( $lang ), 1 ), $source );
214                 }
215         }
216
217         function makeLink( $entry, $source ) {
218                 if ( isset( $this->prefixRewrites[$source] ) && isset( $this->prefixRewrites[$source][$entry[0]] ) )
219                         $entry[0] = $this->prefixRewrites[$source][$entry[0]];
220
221                 if ( !array_key_exists( "iw_prefix", $entry ) ) {
222                         $entry = array( "iw_prefix" => $entry[0], "iw_url" => $entry[1], "iw_local" => $entry[2] );
223                 }
224                 if ( array_key_exists( $source, $this->prefixRewrites ) &&
225                                 array_key_exists( $entry['iw_prefix'], $this->prefixRewrites[$source] ) ) {
226                         $entry['iw_prefix'] = $this->prefixRewrites[$source][$entry['iw_prefix']];
227                 }
228
229                 if ( $this->dbFile ) {
230                         $this->dbFile->set( "{$source}:{$entry['iw_prefix']}", trim( "{$entry['iw_local']} {$entry['iw_url']}" ) );
231                 } else {
232                         $this->output( "{$source}:{$entry['iw_prefix']} {$entry['iw_url']} {$entry['iw_local']}\n" );
233                 }
234         }
235 }
236
237 $maintClass = "DumpInterwiki";
238 require_once( RUN_MAINTENANCE_IF_MAIN );
239