3 * Build constant slightly compact database of interwiki prefixes
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.
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.
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
23 * @ingroup Maintenance
29 * @ingroup Maintenance
32 var $suffix, $lateral, $url;
34 function __construct( $s, $l, $u ) {
40 function getURL( $lang ) {
41 $xlang = str_replace( '_', '-', $lang );
42 return "http://$xlang.{$this->url}/wiki/\$1";
46 require_once( dirname( __FILE__
) . '/Maintenance.php' );
48 class DumpInterwiki
extends Maintenance
{
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 );
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" ) ) );
63 # List of all database names
64 $this->dblist
= array_map( "trim", file( $this->getOption( 'dblist', "/home/wikipedia/common/all.dblist" ) ) );
66 # Special-case databases
67 $this->specials
= array_flip( array_map( "trim", file( $this->getOption( 'specialdbs', "/home/wikipedia/common/special.dblist" ) ) ) );
69 if ( $this->hasOption( 'o' ) ) {
70 $this->dbFile
= CdbWriter
::open( $this->getOption( 'o' ) ) ;
72 $this->dbFile
= false;
75 $this->getRebuildInterwikiDump();
78 function getRebuildInterwikiDump() {
81 # Multi-language sites
82 # db suffix => db suffix, iw prefix, hostname
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' ),
94 # Extra interwiki links that can't be in the intermap for some reason
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 ),
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
105 $this->languageAliases
= array(
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' ),
118 # Construct a list of reserved prefixes
120 foreach ( $this->langlist
as $lang ) {
121 $reserved[$lang] = 1;
123 foreach ( $this->languageAliases
as $alias => $lang ) {
124 $reserved[$alias] = 1;
126 foreach ( $sites as $site ) {
127 $reserved[$site->lateral
] = 1;
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 ) ) );
134 if ( !$lines ||
count( $lines ) < 2 ) {
135 $this->error( "m:Interwiki_map not found", true );
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 );
145 if ( preg_match( '/(wikipedia|wiktionary|wikisource|wikiquote|wikibooks|wikimedia)\.org/', $url ) ) {
151 if ( empty( $reserved[$prefix] ) ) {
152 $imap = array( "iw_prefix" => $prefix, "iw_url" => $url, "iw_local" => $local );
153 $this->makeLink ( $imap, "__global" );
158 # Exclude Wikipedia for Wikipedia
159 $this->makeLink ( array ( 'iw_prefix' => 'wikipedia', 'is_url' => null ), "_wiki" );
161 # Multilanguage sites
162 foreach ( $sites as $site ) {
163 $this->makeLanguageLinks ( $site, "_" . $site->suffix
);
166 foreach ( $dblist as $db ) {
167 if ( isset( $this->specials
[$db] ) ) {
169 # Has interwiki links and interlanguage links to wikipedia
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 );
179 # Find out which site this DB belongs to
181 foreach ( $sites as $candidateSite ) {
182 $suffix = $candidateSite->suffix
;
183 if ( preg_match( "/(.*)$suffix$/", $db, $matches ) ) {
184 $site = $candidateSite;
188 $this->makeLink( array( 'iw_prefix' => $db, 'iw_url' => $site->suffix
), "__sites" );
190 $this->error( "Invalid database $db\n" );
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 );
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 );
212 foreach ( $extraLinks as $link ) {
213 $this->makeLink( $link, "__global" );
217 # ------------------------------------------------------------------------------------------
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 );
227 foreach ( $this->languageAliases
as $alias => $lang ) {
228 $this->makeLink( array( $alias, $site->getURL( $lang ), 1 ), $source );
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]];
236 if ( !array_key_exists( "iw_prefix", $entry ) ) {
237 $entry = array( "iw_prefix" => $entry[0], "iw_url" => $entry[1], "iw_local" => $entry[2] );
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']];
244 if ( $this->dbFile
) {
245 $this->dbFile
->set( "{$source}:{$entry['iw_prefix']}", trim( "{$entry['iw_local']} {$entry['iw_url']}" ) );
247 $this->output( "{$source}:{$entry['iw_prefix']} {$entry['iw_url']} {$entry['iw_local']}\n" );
252 $maintClass = "DumpInterwiki";
253 require_once( DO_MAINTENANCE
);