/
LogUpdate.pm
219 lines (176 loc) · 4.9 KB
/
LogUpdate.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package SVNLog::Controller::LogUpdate;
use strict;
use base 'Catalyst::Controller';
use XML::Simple;
use XML::Atom::SimpleFeed;
use Data::UUID;
sub doupdate : Local
{
my ($self, $c) = @_;
my $latest = SVNLog::Model::CDBI::Logentry->maximum_value_of('revision') || 0;
my $min = $latest;
my $max = 'HEAD';
my $svn_cmd_data = '--username '.$c->config->{svn}{username}.' --password '.$c->config->{svn}{password}.' '.$c->config->{svn}{url};
my $log_xml = `svn log --xml --verbose -r $min:$max $svn_cmd_data`;
die "SVN request failed" if not (defined $log_xml and $log_xml =~ m~</log>~);
my $log = XMLin($log_xml, ContentKey => 'path');
my $max_seen = -1;
$c->transaction(sub {
for my $logentry (ref $log->{logentry} eq 'ARRAY' ? @{$log->{logentry}} : ($log->{logentry}))
{
$max_seen = $logentry->{revision} if $logentry->{revision} > $max_seen;
next if $logentry->{revision} <= $latest;
my $paths = $logentry->{paths}->{path};
delete $logentry->{paths};
# Convert <msg></msg> into just a string (because XML::Simple doesn't know that's what it should be)
$logentry->{msg} = '' if ref $logentry->{msg};
my $entry = SVNLog::Model::CDBI::Logentry->insert($logentry);
for my $path (ref $paths eq 'ARRAY' ? @$paths : ($paths))
{
SVNLog::Model::CDBI::Paths->insert({logentry => $entry->id, filter_copyfrom(%$path)});
}
}
}) or $c->log->error("Transaction failed: " . $c->error->[-1]);
add_default_public_msgs($c);
open my $text_fh, '>', $c->config->{output}{filename} or die $!;
print $text_fh $self->generate_text($c);
open my $feed_fh, '>', $c->config->{output}{filename_feed} or die $!;
print $feed_fh $self->generate_feed($c);
$c->res->body("Updated log to $max_seen.");
}
sub defaultise : Local
{
my ($self, $c) = @_;
add_default_public_msgs($c);
$c->res->body("Done");
}
# (To reset the database:
# delete from public_message; delete from sqlite_sequence where name = "public_message";
# )
sub add_default_public_msgs
{
my ($c) = @_;
$c->transaction(sub {
my @unmessaged = SVNLog::Model::CDBI::Logentry->retrieve_from_sql(qq{
NOT (SELECT COUNT(*) FROM public_message WHERE public_message.logentry = logentry.id)
ORDER BY logentry.id
});
for my $logentry (@unmessaged)
{
my @lines;
for (split /\n/, $logentry->msg)
{
push @lines, $_ if s/^#\s*//;
}
my $msg = join "\n", @lines;
SVNLog::Model::CDBI::PublicMessage->insert({
logentry => $logentry->id,
msg => $msg,
});
}
});
}
sub createtext : Local
{
my ($self, $c) = @_;
my $out = $self->generate_text($c);
$c->res->body($out);
}
sub createfeed : Local
{
my ($self, $c) = @_;
my $out = $self->generate_feed($c);
$c->res->body($out);
}
sub get_log_entries
{
my ($days, $max) = @_;
my @logentries = ();
for (SVNLog::Model::CDBI::Logentry->recent($days))
{
my $msg = $_->public_msg;
next unless defined $msg and $msg->msg; # skip ones with empty messages
push @logentries, $_;
last if $max and @logentries >= $max;
}
return @logentries;
}
sub generate_text
{
my ($self, $c) = @_;
my $feed_url = $c->config->{output}{feed_url};
my $out = <<EOF;
<!DOCTYPE html>
<title>0 A.D. Revision Log</title>
<style>
body {
color: #fff;
margin: 3px;
background-color: #000;
font-family: Geneva, Arial, Helvetica, sans-serif;
font-size: 10px;
}
img {
border: 0;
}
</style>
<a href="$feed_url" target="_top"><img alt="Atom feed" title="Subscribe to feed of revision log" src="/feed-icon-16x16.png" style="float: right"></a>
EOF
my @logentries = get_log_entries(28, 10);
for (@logentries)
{
my ($revision, $author, $date, $msg) = ($_->revision, $_->author, $_->date, $_->public_msg);
$date =~ s/T.*Z//;
$out .= <<EOF;
<b>revision:</b> $revision<br>
<b>author:</b> $author<br>
<b>date:</b> $date<br>
EOF
my $text = $msg->msg;
$text =~ s/&/&/g;
$text =~ s/</</g;
$text =~ s/>/>/g;
$text =~ s/\n/<br>/g;
$out .= $text . "\n<hr>\n";
}
if (not @logentries)
{
$out .= 'Sorry, no data is available right now.';
}
return $out;
}
sub generate_feed
{
my ($self, $c) = @_;
my $uid_gen = new Data::UUID;
my $feed_url = $c->config->{output}{feed_url};
my $feed = new XML::Atom::SimpleFeed(
title => "0 A.D. Revision Log",
link => "http://play0ad.com/",
link => { rel => 'self', href => $feed_url },
id => "urn:uuid:" . $uid_gen->create_from_name_str('WFG SVN feed', 'feed'),
);
my @logentries = get_log_entries(7);
for (@logentries)
{
my ($revision, $author, $date, $msg) = ($_->revision, $_->author, $_->date, $_->public_msg);
my $uid = $uid_gen->create_from_name_str('WFG SVN feed', $revision);
$feed->add_entry(
title => "Revision $revision - ".$msg->msg,
id => "urn:uuid:$uid",
author => $author,
content => $msg->msg,
published => $date,
updated => $date,
link => "http://play0ad.com/",
);
}
return $feed->as_string;
}
sub filter_copyfrom
{
my @r = @_;
s/^(copyfrom)-(rev|path)$/$1_$2/ for @r;
@r;
}
1;