* rmed redundant code
[lhc/web/wiklou.git] / maintenance / fetchInterwiki.pl
1 #!/usr/bin/env perl
2 # Copyright (C) 2005 Ævar Arnfjörð Bjarmason
3 use strict;
4 use warnings;
5 use Socket;
6
7 # Conf
8 my $map = &get(&url('http://usemod.com/intermap.txt'));
9
10 # --- #
11 my $cont;
12 my @map = split /\n/, $map;
13
14 $cont .= '<?php
15 # Note: this file is generated by maintenance/fetchInterwiki.pl
16 # Edit and rerun that script rather than modifying this directly.
17
18 /* private */ $wgValidInterwikis = array(
19 ';
20
21 $cont .= "\t# The usemod interwiki map\n";
22 for (my $i=0;$i<=$#map;++$i) {
23 my ($name, $url) = $map[$i] =~ m#^([^ ]+) (.+)#i;
24 $cont .= "\t'$name' => '$url\$1',\n";
25 }
26
27 my @iso = qw(
28 aa ab af als am ar as ay az ba be bg bh bi bn bo bs ca chr co cs csb cy da de dk:da dz el en eo
29 es et eu fa fi fj fo fr fy ga gd gl gn gu gv ha he hi hr hu hy ia id ik io is it iu ja jv ka kk
30 kl km kn ko ks ku ky la lo lt lv mg mi mk ml mn mo mr ms my na nah nb nds ne nl no oc om or pa
31 pl ps pt qu rm rn ro ru rw sa sd sg sh si sk sl sm sn so sq sr ss st su sv sw ta te tg th ti tk
32 tl tn to tp tpi tr ts tt tw ug uk ur uz vi vo wa wo xh yi yo za zh zh-cn zh-tw zu);
33
34 $cont .= '
35 # Some custom additions:
36 "ReVo" => "http://purl.org/NET/voko/revo/art/$1.html",
37 # eg [[ReVo:cerami]], [[ReVo:astero]] - note X-sensitive!
38 "EcheI" => "http://www.ikso.net/cgi-bin/wiki.pl?$1",
39 "E\\xc4\\x89eI" => "http://www.ikso.net/cgi-bin/wiki.pl?$1",
40 "UnuMondo" => "http://unumondo.com/cgi-bin/wiki.pl?$1", # X-sensitive!
41 "JEFO" => "http://esperanto.jeunes.free.fr/vikio/index.php?$1",
42 "PMEG" => "http://www.bertilow.com/pmeg/$1.php",
43 # ekz [[PMEG:gramatiko/kunligaj vortetoj/au]]
44 "EnciclopediaLibre" => "http://enciclopedia.us.es/wiki.phtml?title=$1",
45
46 # Wikipedia-specific stuff:
47 # Special cases
48 "w" => "http://www.wikipedia.org/wiki/$1",
49 "m" => "http://meta.wikipedia.org/wiki/$1",
50 "meta" => "http://meta.wikipedia.org/wiki/$1",
51 "sep11" => "http://sep11.wikipedia.org/wiki/$1",
52 "simple"=> "http://simple.wikipedia.com/wiki.cgi?$1",
53 "wiktionary" => "http://wiktionary.wikipedia.org/wiki/$1",
54 "PageHistory" => "http://www.wikipedia.org/w/wiki.phtml?title=$1&action=history",
55 "UserContributions" => "http://www.wikipedia.org/w/wiki.phtml?title=Special:Contributions&target=$1",
56 "BackLinks" => "http://www.wikipedia.org/w/wiki.phtml?title=Special:Whatlinkshere&target=$1",
57
58 # ISO 639 2-letter language codes
59 ';
60
61 for(my $i=0; $i<=$#iso;++$i) {
62 my @arr = split /:/, $iso[$i];
63 $cont .= "\t";
64 $cont .= "'$arr[0]' => 'http://";
65
66 if ($arr[1]) {
67 $cont .= $arr[1];
68 } else {
69 $cont .= $arr[0];
70 }
71 $cont .= ".wikipedia.org/wiki/\$1',\n";
72 }
73
74 $cont .= '
75 );
76 ?>
77 ';
78
79 open IW, ">Interwiki.php";
80 print IW $cont;
81 close IW;
82
83 sub get {
84 my ($host, $url) = @_;
85 my $cont;
86 my $eat;
87
88 my $proto = getprotobyname('tcp');
89 socket(Socket, AF_INET, SOCK_STREAM, $proto);
90 my $iaddr = inet_aton("$host");
91 my $port = getservbyname('http', 'tcp');
92 my $sin = sockaddr_in($port, $iaddr);
93 connect(Socket, $sin);
94 send Socket, "GET $url HTTP/1.0\r\nHost: $host\r\n\r\n",0;
95 while (<Socket>) {
96 $cont .= $_ if $eat; # mmm, food
97 ++$eat if ($_ =~ /^(\n|\r\n|)$/);
98 }
99 return $cont;
100 }
101
102 sub url {my ($server, $path) = $_[0] =~ m#.*(?=//)//([^/]*)(.*)#g;}