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