Plagger/Plugin/Filter/TruePermalink.pm untested

filter: Statementstmt 0.0% 0 / 94 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 159 CC 14 SCAR 35.1
CC
14
Cov
0%
rewrite_link
90.0
follow_redirect
12.0
init
6.0
CRAP 33.3
1 package Plagger::Plugin::Filter::TruePermalink;
2 use strict;
3 use base qw( Plagger::Plugin );
4  
5 use DirHandle;
6 use YAML;
7 use Plagger::UserAgent;
8 use URI;
9 use URI::QueryParam;
10  
11 sub init {
12 my $self = shift;
13 $self->SUPER::init(@_);
14 $self->conf->{follow_redirect} = 1 unless exists $self->conf->{follow_redirect};
15 $self->load_plugins;
16 }
17  
18 sub load_plugins {
19 my $self = shift;
20  
21 $self->load_assets(
22 '*.yaml',
23 sub {
24 my $file = shift;
25 Plagger->context->log(debug => "loading $file");
26 my $data = YAML::LoadFile($file);
27 if (ref($data) eq 'ARRAY') {
28 # redirectors.yaml ... make it backward compatible to ignore
29 } else {
30 push @{$self->{plugins}}, $data;
31 }
32 },
33 );
34 }
35  
36 sub register {
37 my($self, $context) = @_;
38 $context->register_hook(
39 $self,
40 'update.entry.fixup' => \&update,
41 );
42 }
43  
44 sub update {
45 my($self, $context, $args) = @_;
46  
47 $self->rewrite(sub { $args->{entry}->permalink }, sub { $args->{entry}->permalink(@_) }, $args);
48 for my $enclosure ($args->{entry}->enclosures) {
49 $self->rewrite(sub { $enclosure->url }, sub { $enclosure->url( URI->new(@_) ) }, $args);
50 }
51 }
52  
53 sub rewrite {
54 my($self, $getter, $callback, $args) = @_;
55  
56 my $loop;
57 while ($self->rewrite_link($getter, $callback, $args)) {
58 if ($loop++ >= 10) {
59 $self->log(error => "Possible infinite loop on " . $getter->());
60 return;
61 }
62 }
63 }
64  
65 sub rewrite_link {
66 my($self, $getter, $callback, $args) = @_;
67  
68 my $context = Plagger->context;
69  
70 my $link = $getter->();
71 my $orig = $link; # copy
72 my $count = 0;
73 my $rewritten;
74  
75 for my $plugin (@{ $self->{plugins}}) {
76 my $match = $plugin->{match} || '.'; # anything
77 next unless $link =~ m/$match/i;
78  
79 if ($plugin->{rewrite}) {
80 local $_ = $link;
81 my $done = eval $plugin->{rewrite};
82 if ($@) {
83 $context->error("$@ in $plugin->{rewrite}");
84 } elsif ($done) {
85 $count += $done;
86 $rewritten = $_;
87 last;
88 }
89 } elsif ($plugin->{query_param}) {
90 my $param = URI->new($link)->query_param($plugin->{query_param})
91 or $context->error("No query param $plugin->{query_param} in " . $link);
92 $count++;
93 $rewritten = $param;
94 last;
95 }
96 }
97  
98 # No match to known sites. Try redirect by issuing GET
99 if (!$count && $self->conf->{follow_redirect}) {
100 my $url = $self->follow_redirect($link);
101 if ($url && $url ne $link) {
102 $count++;
103 $rewritten = $url;
104 }
105 }
106  
107 if ($count) {
108 $callback->($rewritten);
109 $context->log(info => "Link $orig rewritten to $rewritten");
110 }
111  
112 return $count;
113 }
114  
115 sub follow_redirect {
116 my($self, $link) = @_;
117  
118 my $url = $self->cache->get_callback(
119 "redirector:$link",
120 sub {
121 Plagger->context->log(debug => "Issuing GET to $link to follow redirects");
122 my $ua = Plagger::UserAgent->new;
123  
124 # don't care about content body ... immediately die
125 my $res = $ua->simple_request( HTTP::Request->new(GET => $link), sub { die } );
126 if ($res->is_redirect) {
127 return $res->header('Location');
128 }
129 return '';
130 },
131 '1 day',
132 );
133  
134 Plagger->context->log(debug => "Resolved redirection of $link => $url") if $url;
135  
136 return $url;
137 }
138  
139 1;
140  
141 __END__