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
27 require_once( dirname( __FILE__
) . '/Site.php' );
29 require_once( dirname( __FILE__
) . '/Maintenance.php' );
31 class DumpInterwiki
extends Maintenance
{
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 );
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" ) ) );
47 # List of all database names
48 $this->dblist
= array_map( "trim", file( $this->getOption( 'dblist', "/home/wikipedia/common/all.dblist" ) ) );
50 # Special-case databases
51 $this->specials
= array_flip( array_map( "trim", file( $this->getOption( 'specialdbs', "/home/wikipedia/common/special.dblist" ) ) ) );
53 if ( $this->hasOption( 'o' ) ) {
54 $this->dbFile
= CdbWriter
::open( $this->getOption( 'o' ) ) ;
56 $this->dbFile
= false;
59 if ( $this->hasOption( 'protocolrelative' ) ) {
60 $this->urlprotocol
= '';
62 $this->urlprotocol
= 'http:';
65 $this->getRebuildInterwikiDump();
68 function getRebuildInterwikiDump() {
71 # Multi-language sites
72 # db suffix => db suffix, iw prefix, hostname
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' ),
84 # Extra interwiki links that can't be in the intermap for some reason
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 ),
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
95 $this->languageAliases
= array(
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' ),
108 # Construct a list of reserved prefixes
110 foreach ( $this->langlist
as $lang ) {
111 $reserved[$lang] = 1;
113 foreach ( $this->languageAliases
as $alias => $lang ) {
114 $reserved[$alias] = 1;
116 foreach ( $sites as $site ) {
117 $reserved[$site->lateral
] = 1;
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 ) ) );
124 if ( !$lines ||
count( $lines ) < 2 ) {
125 $this->error( "m:Interwiki_map not found", true );
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 );
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 );
148 if ( empty( $reserved[$prefix] ) ) {
149 $imap = array( "iw_prefix" => $prefix, "iw_url" => $url, "iw_local" => $local );
150 $this->makeLink ( $imap, "__global" );
155 # Exclude Wikipedia for Wikipedia
156 $this->makeLink ( array ( 'iw_prefix' => 'wikipedia', 'is_url' => null ), "_wiki" );
158 # Multilanguage sites
159 foreach ( $sites as $site ) {
160 $this->makeLanguageLinks ( $site, "_" . $site->suffix
);
163 foreach ( $this->dblist
as $db ) {
164 if ( isset( $this->specials
[$db] ) ) {
166 # Has interwiki links and interlanguage links to wikipedia
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 );
176 # Find out which site this DB belongs to
178 foreach ( $sites as $candidateSite ) {
179 $suffix = $candidateSite->suffix
;
180 if ( preg_match( "/(.*)$suffix$/", $db, $matches ) ) {
181 $site = $candidateSite;
185 $this->makeLink( array( 'iw_prefix' => $db, 'iw_url' => $site->suffix
), "__sites" );
187 $this->error( "Invalid database $db\n" );
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 );
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 );
209 foreach ( $extraLinks as $link ) {
210 $this->makeLink( $link, "__global" );
213 # List prefixes for each source
214 foreach ( $this->prefixLists
as $source => $hash ) {
215 $list = array_keys( $hash );
217 if ( $this->dbFile
) {
218 $this->dbFile
->set( "__list:{$source}", implode( ' ', $list ) );
220 print "__list:{$source} " . implode( ' ', $list ) . "\n";
225 # ------------------------------------------------------------------------------------------
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 );
235 foreach ( $this->languageAliases
as $alias => $lang ) {
236 $this->makeLink( array( $alias, $site->getURL( $lang, $this->urlprotocol
), 1 ), $source );
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]];
244 if ( !array_key_exists( "iw_prefix", $entry ) ) {
245 $entry = array( "iw_prefix" => $entry[0], "iw_url" => $entry[1], "iw_local" => $entry[2] );
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']];
252 if ( $this->dbFile
) {
253 $this->dbFile
->set( "{$source}:{$entry['iw_prefix']}", trim( "{$entry['iw_local']} {$entry['iw_url']}" ) );
255 $this->output( "{$source}:{$entry['iw_prefix']} {$entry['iw_url']} {$entry['iw_local']}\n" );
258 # Add to list of prefixes
259 $this->prefixLists
[$source][$entry['iw_prefix']] = 1;
263 $maintClass = "DumpInterwiki";
264 require_once( RUN_MAINTENANCE_IF_MAIN
);