line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Commons::Link; |
2
|
|
|
|
|
|
|
|
3
|
8
|
|
|
8
|
|
99483
|
use strict; |
|
8
|
|
|
|
|
51
|
|
|
8
|
|
|
|
|
237
|
|
4
|
8
|
|
|
8
|
|
40
|
use warnings; |
|
8
|
|
|
|
|
15
|
|
|
8
|
|
|
|
|
264
|
|
5
|
|
|
|
|
|
|
|
6
|
8
|
|
|
8
|
|
4386
|
use Class::Utils qw(set_params); |
|
8
|
|
|
|
|
264402
|
|
|
8
|
|
|
|
|
173
|
|
7
|
8
|
|
|
8
|
|
765
|
use Digest::MD5 qw(md5_hex); |
|
8
|
|
|
|
|
16
|
|
|
8
|
|
|
|
|
544
|
|
8
|
8
|
|
|
8
|
|
57
|
use File::Basename; |
|
8
|
|
|
|
|
20
|
|
|
8
|
|
|
|
|
756
|
|
9
|
8
|
|
|
8
|
|
54
|
use Readonly; |
|
8
|
|
|
|
|
15
|
|
|
8
|
|
|
|
|
362
|
|
10
|
8
|
|
|
8
|
|
4414
|
use Unicode::UTF8 qw(decode_utf8 encode_utf8); |
|
8
|
|
|
|
|
4471
|
|
|
8
|
|
|
|
|
483
|
|
11
|
8
|
|
|
8
|
|
5393
|
use URI; |
|
8
|
|
|
|
|
61707
|
|
|
8
|
|
|
|
|
7858
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
Readonly::Scalar our $UPLOAD_URI => q{http://upload.wikimedia.org}; |
14
|
|
|
|
|
|
|
Readonly::Scalar our $COMMONS_URI => q{https://commons.wikimedia.org}; |
15
|
|
|
|
|
|
|
Readonly::Array our @UPLOAD_SEGS => qw(wikipedia commons); |
16
|
|
|
|
|
|
|
Readonly::Array our @COMMONS_SEGS => qw(wiki); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
our $VERSION = 0.06; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub new { |
21
|
20
|
|
|
20
|
1
|
10594
|
my ($class, @params) = @_; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
# Create object. |
24
|
20
|
|
|
|
|
59
|
my $self = bless {}, $class; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
# UTF-8 mode. |
27
|
20
|
|
|
|
|
74
|
$self->{'utf-8'} = 1; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
# Process parameters. |
30
|
20
|
|
|
|
|
115
|
set_params($self, @params); |
31
|
|
|
|
|
|
|
|
32
|
20
|
|
|
|
|
340
|
return $self; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub link { |
36
|
5
|
|
|
5
|
1
|
32
|
my ($self, $file) = @_; |
37
|
|
|
|
|
|
|
|
38
|
5
|
|
|
|
|
16
|
$self->_cleanup(\$file); |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
# Digest characters. |
41
|
5
|
|
|
|
|
16
|
my ($a, $b) = $self->_compute_ab($file); |
42
|
|
|
|
|
|
|
|
43
|
5
|
|
|
|
|
29
|
my $u = URI->new($UPLOAD_URI); |
44
|
5
|
|
|
|
|
11538
|
$u->path_segments(@UPLOAD_SEGS, $a, $b, $file); |
45
|
|
|
|
|
|
|
|
46
|
5
|
|
|
|
|
703
|
return $u->as_string; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub mw_file_link { |
50
|
3
|
|
|
3
|
1
|
21
|
my ($self, $file) = @_; |
51
|
|
|
|
|
|
|
|
52
|
3
|
100
|
|
|
|
21
|
if ($file !~ m/^(File|Image):/ms) { |
53
|
1
|
|
|
|
|
20
|
$file = 'File:'.$file; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
3
|
|
|
|
|
10
|
return $self->mw_link($file); |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub mw_link { |
60
|
8
|
|
|
8
|
1
|
31
|
my ($self, $file) = @_; |
61
|
|
|
|
|
|
|
|
62
|
8
|
|
|
|
|
51
|
my $u = URI->new($COMMONS_URI); |
63
|
8
|
|
|
|
|
27635
|
$u->path_segments(@COMMONS_SEGS, $file); |
64
|
|
|
|
|
|
|
|
65
|
8
|
|
|
|
|
1269
|
return $u->as_string; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub mw_user_link { |
69
|
2
|
|
|
2
|
1
|
17
|
my ($self, $user) = @_; |
70
|
|
|
|
|
|
|
|
71
|
2
|
100
|
|
|
|
14
|
if ($user !~ m/^User:/ms) { |
72
|
1
|
|
|
|
|
20
|
$user = 'User:'.$user; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
2
|
|
|
|
|
9
|
return $self->mw_link($user); |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub thumb_link { |
79
|
5
|
|
|
5
|
1
|
40
|
my ($self, $file, $width) = @_; |
80
|
|
|
|
|
|
|
|
81
|
5
|
|
|
|
|
15
|
$self->_cleanup(\$file); |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
# Digest characters. |
84
|
5
|
|
|
|
|
12
|
my ($a, $b) = $self->_compute_ab($file); |
85
|
|
|
|
|
|
|
|
86
|
5
|
|
|
|
|
10
|
my $thumb_file = $file; |
87
|
5
|
|
|
|
|
196
|
my ($name, undef, $suffix) = fileparse($file, qr/\.[^.]*/ms); |
88
|
5
|
100
|
|
|
|
31
|
if ($suffix eq '.svg') { |
89
|
1
|
|
|
|
|
3
|
$suffix = '.png'; |
90
|
1
|
|
|
|
|
3
|
$thumb_file = $name.$suffix; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
5
|
|
|
|
|
22
|
my $u = URI->new($UPLOAD_URI); |
94
|
5
|
|
|
|
|
8143
|
$u->path_segments(@UPLOAD_SEGS, 'thumb', $a, $b, $file, |
95
|
|
|
|
|
|
|
$width.'px-'.$thumb_file); |
96
|
|
|
|
|
|
|
|
97
|
5
|
|
|
|
|
587
|
return $u->as_string; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
sub _cleanup { |
101
|
10
|
|
|
10
|
|
46
|
my ($self, $file_sr) = @_; |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
# Rewrite all spaces to '_'. |
104
|
10
|
|
|
|
|
17
|
${$file_sr} =~ s/ /_/g; |
|
10
|
|
|
|
|
56
|
|
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
# Remove 'File:' or 'Image:' prefixes. |
107
|
10
|
|
|
|
|
19
|
${$file_sr} =~ s/^(File|Image)://ig; |
|
10
|
|
|
|
|
34
|
|
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
# Upper case |
110
|
10
|
|
|
1
|
|
18
|
${$file_sr} =~ s/^(\w)/uc($1)/e; |
|
10
|
|
|
|
|
56
|
|
|
10
|
|
|
|
|
69
|
|
|
1
|
|
|
|
|
1433
|
|
|
1
|
|
|
|
|
17
|
|
|
1
|
|
|
|
|
15
|
|
111
|
|
|
|
|
|
|
|
112
|
10
|
|
|
|
|
35043
|
return; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
sub _compute_ab { |
116
|
10
|
|
|
10
|
|
24
|
my ($self, $file) = @_; |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
# MD5 only on bytes not utf8 chars. |
119
|
10
|
|
|
|
|
16
|
my $digest; |
120
|
10
|
100
|
|
|
|
29
|
if ($self->{'utf-8'}) { |
121
|
1
|
|
|
|
|
8
|
my $tmp = encode_utf8($file); |
122
|
1
|
|
|
|
|
9
|
$digest = lc(md5_hex($tmp)); |
123
|
|
|
|
|
|
|
} else { |
124
|
9
|
|
|
|
|
50
|
$digest = lc(md5_hex($file)); |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
10
|
|
|
|
|
31
|
my $a = substr $digest, 0, 1; |
128
|
10
|
|
|
|
|
17
|
my $b = substr $digest, 0, 2; |
129
|
|
|
|
|
|
|
|
130
|
10
|
|
|
|
|
62
|
return ($a, $b); |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
1; |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
__END__ |