line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Commons::Link; |
2
|
|
|
|
|
|
|
|
3
|
8
|
|
|
8
|
|
52776
|
use strict; |
|
8
|
|
|
|
|
40
|
|
|
8
|
|
|
|
|
163
|
|
4
|
8
|
|
|
8
|
|
30
|
use warnings; |
|
8
|
|
|
|
|
12
|
|
|
8
|
|
|
|
|
167
|
|
5
|
|
|
|
|
|
|
|
6
|
8
|
|
|
8
|
|
2761
|
use Class::Utils qw(set_params); |
|
8
|
|
|
|
|
179446
|
|
|
8
|
|
|
|
|
121
|
|
7
|
8
|
|
|
8
|
|
546
|
use Digest::MD5 qw(md5_hex); |
|
8
|
|
|
|
|
16
|
|
|
8
|
|
|
|
|
357
|
|
8
|
8
|
|
|
8
|
|
77
|
use File::Basename; |
|
8
|
|
|
|
|
13
|
|
|
8
|
|
|
|
|
501
|
|
9
|
8
|
|
|
8
|
|
38
|
use List::MoreUtils qw(any); |
|
8
|
|
|
|
|
10
|
|
|
8
|
|
|
|
|
25
|
|
10
|
8
|
|
|
8
|
|
3731
|
use Readonly; |
|
8
|
|
|
|
|
13
|
|
|
8
|
|
|
|
|
281
|
|
11
|
8
|
|
|
8
|
|
2535
|
use Unicode::UTF8 qw(decode_utf8 encode_utf8); |
|
8
|
|
|
|
|
2749
|
|
|
8
|
|
|
|
|
316
|
|
12
|
8
|
|
|
8
|
|
3194
|
use URI; |
|
8
|
|
|
|
|
40466
|
|
|
8
|
|
|
|
|
5545
|
|
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.07; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub new { |
22
|
22
|
|
|
22
|
1
|
7704
|
my ($class, @params) = @_; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# Create object. |
25
|
22
|
|
|
|
|
47
|
my $self = bless {}, $class; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
# UTF-8 mode. |
28
|
22
|
|
|
|
|
56
|
$self->{'utf-8'} = 1; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
# Process parameters. |
31
|
22
|
|
|
|
|
74
|
set_params($self, @params); |
32
|
|
|
|
|
|
|
|
33
|
22
|
|
|
|
|
301
|
return $self; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub link { |
37
|
5
|
|
|
5
|
1
|
25
|
my ($self, $file) = @_; |
38
|
|
|
|
|
|
|
|
39
|
5
|
|
|
|
|
13
|
$self->_cleanup(\$file); |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
# Digest characters. |
42
|
5
|
|
|
|
|
13
|
my ($a, $b) = $self->_compute_ab($file); |
43
|
|
|
|
|
|
|
|
44
|
5
|
|
|
|
|
20
|
my $u = URI->new($UPLOAD_URI); |
45
|
5
|
|
|
|
|
5514
|
$u->path_segments(@UPLOAD_SEGS, $a, $b, $file); |
46
|
|
|
|
|
|
|
|
47
|
5
|
|
|
|
|
438
|
return $u->as_string; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub mw_file_link { |
51
|
3
|
|
|
3
|
1
|
14
|
my ($self, $file) = @_; |
52
|
|
|
|
|
|
|
|
53
|
3
|
100
|
|
|
|
14
|
if ($file !~ m/^(File|Image):/ms) { |
54
|
1
|
|
|
|
|
2
|
$file = 'File:'.$file; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
3
|
|
|
|
|
8
|
return $self->mw_link($file); |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub mw_link { |
61
|
8
|
|
|
8
|
1
|
20
|
my ($self, $file) = @_; |
62
|
|
|
|
|
|
|
|
63
|
8
|
|
|
|
|
27
|
my $u = URI->new($COMMONS_URI); |
64
|
8
|
|
|
|
|
17815
|
$u->path_segments(@COMMONS_SEGS, $file); |
65
|
|
|
|
|
|
|
|
66
|
8
|
|
|
|
|
744
|
return $u->as_string; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub mw_user_link { |
70
|
2
|
|
|
2
|
1
|
9
|
my ($self, $user) = @_; |
71
|
|
|
|
|
|
|
|
72
|
2
|
100
|
|
|
|
7
|
if ($user !~ m/^User:/ms) { |
73
|
1
|
|
|
|
|
3
|
$user = 'User:'.$user; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
2
|
|
|
|
|
6
|
return $self->mw_link($user); |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub thumb_link { |
80
|
7
|
|
|
7
|
1
|
23
|
my ($self, $file, $width) = @_; |
81
|
|
|
|
|
|
|
|
82
|
7
|
|
|
|
|
14
|
$self->_cleanup(\$file); |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
# Digest characters. |
85
|
7
|
|
|
|
|
12
|
my ($a, $b) = $self->_compute_ab($file); |
86
|
|
|
|
|
|
|
|
87
|
7
|
|
|
|
|
9
|
my $thumb_file = $file; |
88
|
7
|
|
|
|
|
164
|
my ($name, undef, $suffix) = fileparse($file, qr/\.[^.]*/ms); |
89
|
7
|
100
|
|
18
|
|
39
|
if (any { $_ eq $suffix } qw(.svg .tif .pdf)) { |
|
18
|
|
|
|
|
26
|
|
90
|
3
|
|
|
|
|
4
|
$suffix = '.png'; |
91
|
3
|
|
|
|
|
5
|
$thumb_file = $name.$suffix; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
7
|
|
|
|
|
27
|
my $u = URI->new($UPLOAD_URI); |
95
|
7
|
|
|
|
|
5321
|
$u->path_segments(@UPLOAD_SEGS, 'thumb', $a, $b, $file, |
96
|
|
|
|
|
|
|
$width.'px-'.$thumb_file); |
97
|
|
|
|
|
|
|
|
98
|
7
|
|
|
|
|
564
|
return $u->as_string; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
sub _cleanup { |
102
|
12
|
|
|
12
|
|
14
|
my ($self, $file_sr) = @_; |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
# Rewrite all spaces to '_'. |
105
|
12
|
|
|
|
|
13
|
${$file_sr} =~ s/ /_/g; |
|
12
|
|
|
|
|
45
|
|
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
# Remove 'File:' or 'Image:' prefixes. |
108
|
12
|
|
|
|
|
16
|
${$file_sr} =~ s/^(File|Image)://ig; |
|
12
|
|
|
|
|
28
|
|
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
# Upper case |
111
|
12
|
|
|
1
|
|
12
|
${$file_sr} =~ s/^(\w)/uc($1)/e; |
|
12
|
|
|
|
|
45
|
|
|
12
|
|
|
|
|
56
|
|
|
1
|
|
|
|
|
469
|
|
|
1
|
|
|
|
|
11
|
|
|
1
|
|
|
|
|
10
|
|
112
|
|
|
|
|
|
|
|
113
|
12
|
|
|
|
|
19379
|
return; |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
sub _compute_ab { |
117
|
12
|
|
|
12
|
|
21
|
my ($self, $file) = @_; |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
# MD5 only on bytes not utf8 chars. |
120
|
12
|
|
|
|
|
14
|
my $digest; |
121
|
12
|
100
|
|
|
|
27
|
if ($self->{'utf-8'}) { |
122
|
1
|
|
|
|
|
6
|
my $tmp = encode_utf8($file); |
123
|
1
|
|
|
|
|
5
|
$digest = lc(md5_hex($tmp)); |
124
|
|
|
|
|
|
|
} else { |
125
|
11
|
|
|
|
|
38
|
$digest = lc(md5_hex($file)); |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
12
|
|
|
|
|
23
|
my $a = substr $digest, 0, 1; |
129
|
12
|
|
|
|
|
17
|
my $b = substr $digest, 0, 2; |
130
|
|
|
|
|
|
|
|
131
|
12
|
|
|
|
|
24
|
return ($a, $b); |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
1; |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
__END__ |