Apply patch from Karsten Düsterloh in Bug #28103.
[lhc/web/wiklou.git] / maintenance / rebuildInterwiki.php
1 <?php
2 /**
3 * Rebuild interwiki table using the file on meta and the language list
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 RebuildInterwiki extends Maintenance {
32 public function __construct() {
33 parent::__construct();
34 $this->mDescription = "Rebuild the interwiki table using the file on meta and the language list.";
35 $this->addOption( 'langlist', 'File with one language code per line', false, true );
36 $this->addOption( 'dblist', 'File with one db per line', false, true );
37 $this->addOption( 'd', 'Output folder', false, true );
38 }
39
40 function execute() {
41 # List of language prefixes likely to be found in multi-language sites
42 $this->langlist = array_map( "trim", file( $this->getOption( 'langlist', "/home/wikipedia/common/langlist" ) ) );
43
44 # List of all database names
45 $this->dblist = array_map( "trim", file( $this->getOption( 'dblist', "/home/wikipedia/common/all.dblist" ) ) );
46
47 # Special-case databases
48 //$this->specials = array_flip( array_map( "trim", file( $this->getOption( 'specialdbs', "/home/wikipedia/common/special.dblist" ) ) ) );
49
50 $this->makeInterwikiSQL( $this->getOption( 'd', '/home/wikipedia/conf/interwiki/sql' ) );
51 }
52
53 function makeInterwikiSQL( $destDir ) {
54 $this->output( "Making new interwiki SQL files in $destDir\n" );
55
56 # Multi-language sites
57 # db suffix => db suffix, iw prefix, hostname
58 $sites = array(
59 'wiki' => new Site( 'wiki', 'w', 'wikipedia.org' ),
60 'wiktionary' => new Site( 'wiktionary', 'wikt', 'wiktionary.org' ),
61 'wikiquote' => new Site( 'wikiquote', 'q', 'wikiquote.org' ),
62 'wikibooks' => new Site( 'wikibooks', 'b', 'wikibooks.org' ),
63 'wikinews' => new Site( 'wikinews', 'n', 'wikinews.org' ),
64 'wikisource' => new Site( 'wikisource', 's', 'wikisource.org' ),
65 'wikimedia' => new Site( 'wikimedia', 'chapter', 'wikimedia.org' ),
66 'wikiversity' => new Site( 'wikiversity', 'v', 'wikiversity.org' ),
67 );
68
69 # Special-case hostnames
70 $this->specials = array(
71 'sourceswiki' => 'sources.wikipedia.org',
72 'quotewiki' => 'wikiquote.org',
73 'textbookwiki' => 'wikibooks.org',
74 'sep11wiki' => 'sep11.wikipedia.org',
75 'metawiki' => 'meta.wikimedia.org',
76 'commonswiki' => 'commons.wikimedia.org',
77 'specieswiki' => 'species.wikimedia.org',
78 );
79
80 # Extra interwiki links that can't be in the intermap for some reason
81 $extraLinks = array(
82 array( 'm', 'http://meta.wikimedia.org/wiki/$1', 1 ),
83 array( 'meta', 'http://meta.wikimedia.org/wiki/$1', 1 ),
84 array( 'sep11', 'http://sep11.wikipedia.org/wiki/$1', 1 ),
85 );
86
87 # Language aliases, usually configured as redirects to the real wiki in apache
88 # Interlanguage links are made directly to the real wiki
89 # Something horrible happens if you forget to list an alias here, I can't
90 # remember what
91 $this->languageAliases = array(
92 'zh-cn' => 'zh',
93 'zh-tw' => 'zh',
94 'dk' => 'da',
95 'nb' => 'no',
96 );
97
98 # Special case prefix rewrites, for the benefit of Swedish which uses s:t
99 # as an abbreviation for saint
100 $this->prefixRewrites = array(
101 'svwiki' => array( 's' => 'src' ),
102 );
103
104 # Construct a list of reserved prefixes
105 $reserved = array();
106 foreach ( $this->langlist as $lang ) {
107 $reserved[$lang] = 1;
108 }
109 foreach ( $this->languageAliases as $alias => $lang ) {
110 $reserved[$alias] = 1;
111 }
112 foreach ( $sites as $site ) {
113 $reserved[$site->lateral] = 1;
114 }
115
116 # Extract the intermap from meta
117 $intermap = Http::get( 'http://meta.wikimedia.org/w/index.php?title=Interwiki_map&action=raw', 30 );
118 $lines = array_map( 'trim', explode( "\n", trim( $intermap ) ) );
119
120 if ( !$lines || count( $lines ) < 2 ) {
121 $this->error( "m:Interwiki_map not found", true );
122 }
123
124 $iwArray = array();
125
126 foreach ( $lines as $line ) {
127 $matches = array();
128 if ( preg_match( '/^\|\s*(.*?)\s*\|\|\s*(https?:\/\/.*?)\s*$/', $line, $matches ) ) {
129 $prefix = strtolower( $matches[1] );
130 $url = $matches[2];
131 if ( preg_match( '/(wikipedia|wiktionary|wikisource|wikiquote|wikibooks|wikimedia)\.org/', $url ) ) {
132 $local = 1;
133 } else {
134 $local = 0;
135 }
136
137 if ( empty( $reserved[$prefix] ) ) {
138 $iwArray[$prefix] = array( "iw_prefix" => $prefix, "iw_url" => $url, "iw_local" => $local );
139 }
140 }
141 }
142
143 foreach ( $this->dblist as $db ) {
144 $sql = "-- Generated by rebuildInterwiki.php";
145 if ( isset( $this->specials[$db] ) ) {
146 # Special wiki
147 # Has interwiki links and interlanguage links to wikipedia
148
149 $host = $this->specials[$db];
150 $sql .= "\n--$host\n\n";
151 $sql .= "USE $db;\n" .
152 "TRUNCATE TABLE interwiki;\n" .
153 "INSERT INTO interwiki (iw_prefix, iw_url, iw_local) VALUES \n";
154 $first = true;
155
156 # Intermap links
157 foreach ( $iwArray as $iwEntry ) {
158 $sql .= $this->makeLink( $iwEntry, $first, $db );
159 }
160
161 # Links to multilanguage sites
162 foreach ( $sites as $targetSite ) {
163 $sql .= $this->makeLink( array( $targetSite->lateral, $targetSite->getURL( 'en' ), 1 ), $first, $db );
164 }
165
166 # Interlanguage links to wikipedia
167 $sql .= $this->makeLanguageLinks( $sites['wiki'], $first, $db );
168
169 # Extra links
170 foreach ( $extraLinks as $link ) {
171 $sql .= $this->makeLink( $link, $first, $db );
172 }
173
174 $sql .= ";\n";
175 } else {
176 # Find out which site this DB belongs to
177 $site = false;
178 foreach ( $sites as $candidateSite ) {
179 $suffix = $candidateSite->suffix;
180 if ( preg_match( "/(.*)$suffix$/", $db, $matches ) ) {
181 $site = $candidateSite;
182 break;
183 }
184 }
185 if ( !$site ) {
186 print "Invalid database $db\n";
187 continue;
188 }
189 $lang = $matches[1];
190 $host = "$lang." . $site->url;
191 $sql .= "\n--$host\n\n";
192
193 $sql .= "USE $db;\n" .
194 "TRUNCATE TABLE interwiki;\n" .
195 "INSERT INTO interwiki (iw_prefix,iw_url,iw_local) VALUES\n";
196 $first = true;
197
198 # Intermap links
199 foreach ( $iwArray as $iwEntry ) {
200 # Suppress links with the same name as the site
201 if ( ( $suffix == 'wiki' && $iwEntry['iw_prefix'] != 'wikipedia' ) ||
202 ( $suffix != 'wiki' && $suffix != $iwEntry['iw_prefix'] ) )
203 {
204 $sql .= $this->makeLink( $iwEntry, $first, $db );
205 }
206 }
207
208 # Lateral links
209 foreach ( $sites as $targetSite ) {
210 # Suppress link to self
211 if ( $targetSite->suffix != $site->suffix ) {
212 $sql .= $this->makeLink( array( $targetSite->lateral, $targetSite->getURL( $lang ), 1 ), $first, $db );
213 }
214 }
215
216 # Interlanguage links
217 $sql .= $this->makeLanguageLinks( $site, $first, $db );
218
219 # w link within wikipedias
220 # Other sites already have it as a lateral link
221 if ( $site->suffix == "wiki" ) {
222 $sql .= $this->makeLink( array( "w", "http://en.wikipedia.org/wiki/$1", 1 ), $first, $db );
223 }
224
225 # Extra links
226 foreach ( $extraLinks as $link ) {
227 $sql .= $this->makeLink( $link, $first, $db );
228 }
229 $sql .= ";\n";
230 }
231 file_put_contents( "$destDir/$db.sql", $sql );
232 }
233 }
234
235 # ------------------------------------------------------------------------------------------
236
237 # Returns part of an INSERT statement, corresponding to all interlanguage links to a particular site
238 function makeLanguageLinks( &$site, &$first, $source ) {
239 $sql = "";
240
241 # Actual languages with their own databases
242 foreach ( $this->langlist as $targetLang ) {
243 $sql .= $this->makeLink( array( $targetLang, $site->getURL( $targetLang ), 1 ), $first, $source );
244 }
245
246 # Language aliases
247 foreach ( $this->languageAliases as $alias => $lang ) {
248 $sql .= $this->makeLink( array( $alias, $site->getURL( $lang ), 1 ), $first, $source );
249 }
250 return $sql;
251 }
252
253 # Make SQL for a single link from an array
254 function makeLink( $entry, &$first, $source ) {
255
256 if ( isset( $this->prefixRewrites[$source] ) && isset($entry[0]) && isset( $this->prefixRewrites[$source][$entry[0]] ) ) {
257 $entry[0] = $this->prefixRewrites[$source][$entry[0]];
258 }
259
260 $sql = "";
261 # Add comma
262 if ( $first ) {
263 $first = false;
264 } else {
265 $sql .= ",\n";
266 }
267 $dbr = wfGetDB( DB_SLAVE );
268 $sql .= "(" . $dbr->makeList( $entry ) . ")";
269 return $sql;
270 }
271 }
272
273 $maintClass = "RebuildInterwiki";
274 require_once( RUN_MAINTENANCE_IF_MAIN );
275