]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Rcs/svn.pm
* Add an openid plugin to support logging in using OpenID.
[ikiwiki.git] / IkiWiki / Rcs / svn.pm
1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5 use IkiWiki;
6 use POSIX qw(setlocale LC_CTYPE);
7
8 package IkiWiki;
9                 
10 # svn needs LC_CTYPE set to a UTF-8 locale, so try to find one. Any will do.
11 sub find_lc_ctype() {
12         my $current = setlocale(LC_CTYPE());
13         return $current if $current =~ m/UTF-?8$/i;
14
15         # Make some obvious attempts to avoid calling `locale -a`
16         foreach my $locale ("$current.UTF-8", "en_US.UTF-8", "en_GB.UTF-8") {
17                 return $locale if setlocale(LC_CTYPE(), $locale);
18         }
19
20         # Try to get all available locales and pick the first UTF-8 one found.
21         if (my @locale = grep(/UTF-?8$/i, `locale -a`)) {
22                 chomp @locale;
23                 return $locale[0] if setlocale(LC_CTYPE(), $locale[0]);
24         }
25
26         # fallback to the current locale
27         return $current;
28 } # }}}
29 $ENV{LC_CTYPE} = $ENV{LC_CTYPE} || find_lc_ctype();
30
31 sub svn_info ($$) { #{{{
32         my $field=shift;
33         my $file=shift;
34
35         my $info=`LANG=C svn info $file`;
36         my ($ret)=$info=~/^$field: (.*)$/m;
37         return $ret;
38 } #}}}
39
40 sub rcs_update () { #{{{
41         if (-d "$config{srcdir}/.svn") {
42                 if (system("svn", "update", "--quiet", $config{srcdir}) != 0) {
43                         warn("svn update failed\n");
44                 }
45         }
46 } #}}}
47
48 sub rcs_prepedit ($) { #{{{
49         # Prepares to edit a file under revision control. Returns a token
50         # that must be passed into rcs_commit when the file is ready
51         # for committing.
52         # The file is relative to the srcdir.
53         my $file=shift;
54         
55         if (-d "$config{srcdir}/.svn") {
56                 # For subversion, return the revision of the file when
57                 # editing begins.
58                 my $rev=svn_info("Revision", "$config{srcdir}/$file");
59                 return defined $rev ? $rev : "";
60         }
61 } #}}}
62
63 sub rcs_commit ($$$) { #{{{
64         # Tries to commit the page; returns undef on _success_ and
65         # a version of the page with the rcs's conflict markers on failure.
66         # The file is relative to the srcdir.
67         my $file=shift;
68         my $message=shift;
69         my $rcstoken=shift;
70
71         if (-d "$config{srcdir}/.svn") {
72                 # Check to see if the page has been changed by someone
73                 # else since rcs_prepedit was called.
74                 my ($oldrev)=$rcstoken=~/^([0-9]+)$/; # untaint
75                 my $rev=svn_info("Revision", "$config{srcdir}/$file");
76                 if (defined $rev && defined $oldrev && $rev != $oldrev) {
77                         # Merge their changes into the file that we've
78                         # changed.
79                         chdir($config{srcdir}); # svn merge wants to be here
80                         if (system("svn", "merge", "--quiet", "-r$oldrev:$rev",
81                                    "$config{srcdir}/$file") != 0) {
82                                 warn("svn merge -r$oldrev:$rev failed\n");
83                         }
84                 }
85
86                 if (system("svn", "commit", "--quiet", 
87                            "--encoding", "UTF-8", "-m",
88                            possibly_foolish_untaint($message),
89                            "$config{srcdir}") != 0) {
90                         my $conflict=readfile("$config{srcdir}/$file");
91                         if (system("svn", "revert", "--quiet", "$config{srcdir}/$file") != 0) {
92                                 warn("svn revert failed\n");
93                         }
94                         return $conflict;
95                 }
96         }
97         return undef # success
98 } #}}}
99
100 sub rcs_add ($) { #{{{
101         # filename is relative to the root of the srcdir
102         my $file=shift;
103
104         if (-d "$config{srcdir}/.svn") {
105                 my $parent=dirname($file);
106                 while (! -d "$config{srcdir}/$parent/.svn") {
107                         $file=$parent;
108                         $parent=dirname($file);
109                 }
110                 
111                 if (system("svn", "add", "--quiet", "$config{srcdir}/$file") != 0) {
112                         warn("svn add failed\n");
113                 }
114         }
115 } #}}}
116
117 sub rcs_recentchanges ($) { #{{{
118         my $num=shift;
119         my @ret;
120         
121         return unless -d "$config{srcdir}/.svn";
122
123         eval q{
124                 use Date::Parse;
125                 use Time::Duration;
126                 use XML::SAX;
127                 use XML::Simple;
128         };
129         error($@) if $@;
130
131         # avoid using XML::SAX::PurePerl, it's buggy with UTF-8 data
132         my @parsers = map { ${$_}{Name} } @{XML::SAX->parsers()};
133         do {
134                 $XML::Simple::PREFERRED_PARSER = pop @parsers;
135         } until $XML::Simple::PREFERRED_PARSER ne 'XML::SAX::PurePerl';
136
137         # --limit is only supported on Subversion 1.2.0+
138         my $svn_version=`svn --version -q`;
139         my $svn_limit='';
140         $svn_limit="--limit $num"
141                 if $svn_version =~ /\d\.(\d)\.\d/ && $1 >= 2;
142
143         my $svn_url=svn_info("URL", $config{srcdir});
144         my $xml = XMLin(scalar `svn $svn_limit --xml -v log '$svn_url'`,
145                 ForceArray => [ 'logentry', 'path' ],
146                 GroupTags => { paths => 'path' },
147                 KeyAttr => { path => 'content' },
148         );
149         foreach my $logentry (@{$xml->{logentry}}) {
150                 my (@pages, @message);
151
152                 my $rev = $logentry->{revision};
153                 my $user = $logentry->{author};
154
155                 my $when=time - str2time($logentry->{date}, 'UTC');
156
157                 foreach my $msgline (split(/\n/, $logentry->{msg})) {
158                         push @message, { line => $msgline };
159                 }
160
161                 my $committype="web";
162                 if (defined $message[0] &&
163                     $message[0]->{line}=~/$config{web_commit_regexp}/) {
164                         $user=defined $2 ? "$2" : "$3";
165                         $message[0]->{line}=$4;
166                 }
167                 else {
168                         $committype="svn";
169                 }
170
171                 foreach (keys %{$logentry->{paths}}) {
172                         next unless /^\/\Q$config{svnpath}\E\/([^ ]+)(?:$|\s)/;
173                         my $file=$1;
174                         my $diffurl=$config{diffurl};
175                         $diffurl=~s/\[\[file\]\]/$file/g;
176                         $diffurl=~s/\[\[r1\]\]/$rev - 1/eg;
177                         $diffurl=~s/\[\[r2\]\]/$rev/g;
178                         push @pages, {
179                                 page => pagename($file),
180                                 diffurl => $diffurl,
181                         } if length $file;
182                 }
183                 push @ret, { rev => $rev,
184                         user => $user,
185                         committype => $committype,
186                         when => $when,
187                         message => [@message],
188                         pages => [@pages],
189                 } if @pages;
190                 return @ret if @ret >= $num;
191         }
192
193         return @ret;
194 } #}}}
195
196 sub rcs_notify () { #{{{
197         if (! exists $ENV{REV}) {
198                 error("REV is not set, not running from svn post-commit hook, cannot send notifications");
199         }
200         my $rev=int(possibly_foolish_untaint($ENV{REV}));
201         
202         my $user=`svnlook author $config{svnrepo} -r $rev`;
203         chomp $user;
204         my $message=`svnlook log $config{svnrepo} -r $rev`;
205         if ($message=~/$config{web_commit_regexp}/) {
206                 $user=defined $2 ? "$2" : "$3";
207                 $message=$4;
208         }
209
210         my @changed_pages;
211         foreach my $change (`svnlook changed $config{svnrepo} -r $rev`) {
212                 chomp $change;
213                 if ($change =~ /^[A-Z]+\s+\Q$config{svnpath}\E\/(.*)/) {
214                         push @changed_pages, $1;
215                 }
216         }
217                 
218         require IkiWiki::UserInfo;
219         my @email_recipients=commit_notify_list($user, @changed_pages);
220         if (@email_recipients) {
221                 # TODO: if a commit spans multiple pages, this will send
222                 # subscribers a diff that might contain pages they did not
223                 # sign up for. Should separate the diff per page and
224                 # reassemble into one mail with just the pages subscribed to.
225                 my $diff=`svnlook diff $config{svnrepo} -r $rev --no-diff-deleted`;
226
227                 my $subject="$config{wikiname} update of ";
228                 if (@changed_pages > 2) {
229                         $subject.="$changed_pages[0] $changed_pages[1] etc";
230                 }
231                 else {
232                         $subject.=join(" ", @changed_pages);
233                 }
234                 $subject.=" by $user";
235
236                 my $template=template("notifymail.tmpl");
237                 $template->param(
238                         wikiname => $config{wikiname},
239                         diff => $diff,
240                         user => $user,
241                         message => $message,
242                 );
243                 
244                 eval q{use Mail::Sendmail};
245                 error($@) if $@;
246                 foreach my $email (@email_recipients) {
247                         sendmail(
248                                 To => $email,
249                                 From => "$config{wikiname} <$config{adminemail}>",
250                                 Subject => $subject,
251                                 Message => $template->output,
252                         ) or error("Failed to send update notification mail");
253                 }
254         }
255 } #}}}
256
257 sub rcs_getctime ($) { #{{{
258         my $file=shift;
259
260         my $svn_log_infoline=qr/^r\d+\s+\|\s+[^\s]+\s+\|\s+(\d+-\d+-\d+\s+\d+:\d+:\d+\s+[-+]?\d+).*/;
261                 
262         my $child = open(SVNLOG, "-|");
263         if (! $child) {
264                 exec("svn", "log", $file) || error("svn log $file failed to run");
265         }
266
267         my $date;
268         while (<SVNLOG>) {
269                 if (/$svn_log_infoline/) {
270                         $date=$1;
271                 }
272         }
273         close SVNLOG || warn "svn log $file exited $?";
274
275         if (! defined $date) {
276                 warn "failed to parse svn log for $file\n";
277                 return 0;
278         }
279                 
280         eval q{use Date::Parse};
281         error($@) if $@;
282         $date=str2time($date);
283         debug("found ctime ".localtime($date)." for $file");
284         return $date;
285 } #}}}
286
287 1