root / src / process_links.pl
History | View | Annotate | Download (2.6 kB)
1 |
# Copyright (c) 2010, Panos Louridas, GRNET S.A. |
---|---|
2 |
# |
3 |
# All rights reserved. |
4 |
# |
5 |
# Redistribution and use in source and binary forms, with or without |
6 |
# modification, are permitted provided that the following conditions are met: |
7 |
# |
8 |
# * Redistributions of source code must retain the above copyright notice, this |
9 |
# list of conditions and the following disclaimer. |
10 |
# |
11 |
# * Redistributions in binary form must reproduce the above copyright notice, |
12 |
# this list of conditions and the following disclaimer in the documentation |
13 |
# and/or other materials provided with the distribution. |
14 |
# |
15 |
# * Neither the name of GRNET S.A, nor the names of its contributors may be |
16 |
# used to endorse or promote products derived from this software without |
17 |
# specific prior written permission. |
18 |
# |
19 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
20 |
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
21 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
22 |
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
23 |
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
24 |
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
25 |
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
26 |
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
27 |
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 |
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 |
|
30 |
use strict; |
31 |
|
32 |
use Prefixes qw(%other_namespaces %namespaces %languages); |
33 |
|
34 |
my %redirect; |
35 |
|
36 |
open(REDIRECT_FILE, "< $ARGV[0]"); |
37 |
|
38 |
while (my $line = <REDIRECT_FILE>) { |
39 |
chomp $line; |
40 |
my ($from , $to, $anchor) = $line =~ /(.+) #REDIRECT ([^#]+)(#.*)?/; |
41 |
$redirect{$from} = $to if $from ne $to; # makes sure we do not redirect to self |
42 |
} |
43 |
|
44 |
close(REDIRECT_FILE); |
45 |
|
46 |
open(LINKS_FILE, "< $ARGV[1]"); |
47 |
|
48 |
while (my $line = <LINKS_FILE>) { |
49 |
chomp $line; |
50 |
my ($from, $to, $anchor) = $line =~ /(.+) => ([^#]+)(#.*)?/; |
51 |
next unless $from && $to; |
52 |
next if $to =~ /^:/; |
53 |
my $prefix = substr($to, 0, 1 + index($to, ":")); |
54 |
if ($prefix ne "" && ($other_namespaces{$prefix} || $namespaces{$prefix} |
55 |
|| $languages{$prefix})) { |
56 |
next; |
57 |
} |
58 |
my $new_target = $to; |
59 |
my %seen = (); |
60 |
while ($new_target && !exists($seen{$new_target})) { |
61 |
$seen{$new_target} = 1; |
62 |
$to = $new_target; |
63 |
$new_target = $redirect{$to}; |
64 |
} |
65 |
$from = $from; |
66 |
if (index("$from => $to\n", "#") >= 0) { |
67 |
die "line: $line from: $from to: $to\n"; |
68 |
} |
69 |
print "$from => $to\n"; |
70 |
} |
71 |
|
72 |
close(LINKS_FILE); |