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