]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/svn.pm
Merge branch 'master' of ssh://git.ikiwiki.info
[ikiwiki.git] / IkiWiki / Plugin / svn.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::svn;
3
4 use warnings;
5 use strict;
6 use IkiWiki;
7 use POSIX qw(setlocale LC_CTYPE);
8
9 sub import {
10         hook(type => "checkconfig", id => "svn", call => \&checkconfig);
11         hook(type => "getsetup", id => "svn", call => \&getsetup);
12         hook(type => "rcs", id => "rcs_update", call => \&rcs_update);
13         hook(type => "rcs", id => "rcs_prepedit", call => \&rcs_prepedit);
14         hook(type => "rcs", id => "rcs_commit", call => \&rcs_commit);
15         hook(type => "rcs", id => "rcs_commit_staged", call => \&rcs_commit_staged);
16         hook(type => "rcs", id => "rcs_add", call => \&rcs_add);
17         hook(type => "rcs", id => "rcs_remove", call => \&rcs_remove);
18         hook(type => "rcs", id => "rcs_rename", call => \&rcs_rename);
19         hook(type => "rcs", id => "rcs_recentchanges", call => \&rcs_recentchanges);
20         hook(type => "rcs", id => "rcs_diff", call => \&rcs_diff);
21         hook(type => "rcs", id => "rcs_getctime", call => \&rcs_getctime);
22         hook(type => "rcs", id => "rcs_getmtime", call => \&rcs_getmtime);
23 }
24
25 sub checkconfig () {
26         if (! defined $config{svnpath}) {
27                 $config{svnpath}="trunk";
28         }
29         if (exists $config{svnpath}) {
30                 # code depends on the path not having extraneous slashes
31                 $config{svnpath}=~tr#/#/#s;
32                 $config{svnpath}=~s/\/$//;
33                 $config{svnpath}=~s/^\///;
34         }
35         if (defined $config{svn_wrapper} && length $config{svn_wrapper}) {
36                 push @{$config{wrappers}}, {
37                         wrapper => $config{svn_wrapper},
38                         wrappermode => (defined $config{svn_wrappermode} ? $config{svn_wrappermode} : "04755"),
39                 };
40         }
41 }
42
43 sub getsetup () {
44         return
45                 plugin => {
46                         safe => 0, # rcs plugin
47                         rebuild => undef,
48                         section => "rcs",
49                 },
50                 svnrepo => {
51                         type => "string",
52                         example => "/svn/wiki",
53                         description => "subversion repository location",
54                         safe => 0, # path
55                         rebuild => 0,
56                 },
57                 svnpath => {
58                         type => "string",
59                         example => "trunk",
60                         description => "path inside repository where the wiki is located",
61                         safe => 0, # paranoia
62                         rebuild => 0,
63                 },
64                 svn_wrapper => {
65                         type => "string",
66                         example => "/svn/wikirepo/hooks/post-commit",
67                         description => "svn post-commit hook to generate",
68                         safe => 0, # file
69                         rebuild => 0,
70                 },
71                 svn_wrappermode => {
72                         type => "string",
73                         example => '04755',
74                         description => "mode for svn_wrapper (can safely be made suid)",
75                         safe => 0,
76                         rebuild => 0,
77                 },
78                 historyurl => {
79                         type => "string",
80                         example => "http://svn.example.org/trunk/[[file]]",
81                         description => "viewvc url to show file history ([[file]] substituted)",
82                         safe => 1,
83                         rebuild => 1,
84                 },
85                 diffurl => {
86                         type => "string",
87                         example => "http://svn.example.org/trunk/[[file]]?root=wiki&r1=[[r1]]&r2=[[r2]]",
88                         description => "viewvc url to show a diff ([[file]], [[r1]], and [[r2]] substituted)",
89                         safe => 1,
90                         rebuild => 1,
91                 },
92 }
93
94 # svn needs LC_CTYPE set to a UTF-8 locale, so try to find one. Any will do.
95 sub find_lc_ctype() {
96         my $current = setlocale(LC_CTYPE());
97         return $current if $current =~ m/UTF-?8$/i;
98
99         # Make some obvious attempts to avoid calling `locale -a`
100         foreach my $locale ("$current.UTF-8", "en_US.UTF-8", "en_GB.UTF-8") {
101                 return $locale if setlocale(LC_CTYPE(), $locale);
102         }
103
104         # Try to get all available locales and pick the first UTF-8 one found.
105         if (my @locale = grep(/UTF-?8$/i, `locale -a`)) {
106                 chomp @locale;
107                 return $locale[0] if setlocale(LC_CTYPE(), $locale[0]);
108         }
109
110         # fallback to the current locale
111         return $current;
112 }
113 $ENV{LC_CTYPE} = $ENV{LC_CTYPE} || find_lc_ctype();
114
115 sub svn_info ($$) {
116         my $field=shift;
117         my $file=shift;
118
119         my $info=`LANG=C svn info $file`;
120         my ($ret)=$info=~/^$field: (.*)$/m;
121         return $ret;
122 }
123
124 sub rcs_update () {
125         if (system("svn", "update", "--quiet", $config{srcdir}) != 0) {
126                 warn("svn update failed\n");
127         }
128 }
129
130 sub rcs_prepedit ($) {
131         # Prepares to edit a file under revision control. Returns a token
132         # that must be passed into rcs_commit when the file is ready
133         # for committing.
134         # The file is relative to the srcdir.
135         my $file=shift;
136         
137         # For subversion, return the revision of the file when
138         # editing begins.
139         my $rev=svn_info("Revision", "$config{srcdir}/$file");
140         return defined $rev ? $rev : "";
141 }
142
143 sub commitmessage (@) {
144         my %params=@_;
145
146         if (defined $params{session}) {
147                 if (defined $params{session}->param("name")) {
148                         return "web commit by ".
149                                 $params{session}->param("name").
150                                 (length $params{message} ? ": $params{message}" : "");
151                 }
152                 elsif (defined $params{session}->remote_addr()) {
153                         return "web commit from ".
154                                 $params{session}->remote_addr().
155                                 (length $params{message} ? ": $params{message}" : "");
156                 }
157         }
158         return $params{message};
159 }
160
161 sub rcs_commit (@) {
162         # Tries to commit the page; returns undef on _success_ and
163         # a version of the page with the rcs's conflict markers on failure.
164         # The file is relative to the srcdir.
165         my %params=@_;
166
167         # Check to see if the page has been changed by someone
168         # else since rcs_prepedit was called.
169         my ($oldrev)=$params{token}=~/^([0-9]+)$/; # untaint
170         my $rev=svn_info("Revision", "$config{srcdir}/$params{file}");
171         if (defined $rev && defined $oldrev && $rev != $oldrev) {
172                 # Merge their changes into the file that we've
173                 # changed.
174                 if (system("svn", "merge", "--quiet", "-r$oldrev:$rev",
175                            "$config{srcdir}/$params{file}", "$config{srcdir}/$params{file}") != 0) {
176                         warn("svn merge -r$oldrev:$rev failed\n");
177                 }
178         }
179
180         if (system("svn", "commit", "--quiet", 
181                    "--encoding", "UTF-8", "-m",
182                    IkiWiki::possibly_foolish_untaint(commitmessage(%params)),
183                    $config{srcdir}) != 0) {
184                 my $conflict=readfile("$config{srcdir}/$params{file}");
185                 if (system("svn", "revert", "--quiet", "$config{srcdir}/$params{file}") != 0) {
186                         warn("svn revert failed\n");
187                 }
188                 return $conflict;
189         }
190
191         return undef # success
192 }
193
194 sub rcs_commit_staged (@) {
195         # Commits all staged changes. Changes can be staged using rcs_add,
196         # rcs_remove, and rcs_rename.
197         my %params=@_;
198         
199         if (system("svn", "commit", "--quiet",
200                    "--encoding", "UTF-8", "-m",
201                    IkiWiki::possibly_foolish_untaint(commitmessage(%params)),
202                    $config{srcdir}) != 0) {
203                 warn("svn commit failed\n");
204                 return 1; # failure     
205         }
206         return undef # success
207 }
208
209 sub rcs_add ($) {
210         # filename is relative to the root of the srcdir
211         my $file=shift;
212
213         if (system("svn", "add", "--parents", "--quiet", "$config{srcdir}/$file") != 0) {
214                 warn("svn add failed\n");
215         }
216 }
217
218 sub rcs_remove ($) {
219         # filename is relative to the root of the srcdir
220         my $file=shift;
221
222         if (system("svn", "rm", "--force", "--quiet", "$config{srcdir}/$file") != 0) {
223                 warn("svn rm failed\n");
224         }
225 }
226
227 sub rcs_rename ($$) {
228         # filenames relative to the root of the srcdir
229         my ($src, $dest)=@_;
230         
231         if (system("svn", "mv", "--parents", "--force", "--quiet", 
232             "$config{srcdir}/$src", "$config{srcdir}/$dest") != 0) {
233                 warn("svn rename failed\n");
234         }
235 }
236
237 sub rcs_recentchanges ($) {
238         my $num=shift;
239         my @ret;
240         
241         eval q{
242                 use Date::Parse;
243                 use XML::SAX;
244                 use XML::Simple;
245         };
246         error($@) if $@;
247
248         # avoid using XML::SAX::PurePerl, it's buggy with UTF-8 data
249         my @parsers = map { ${$_}{Name} } @{XML::SAX->parsers()};
250         do {
251                 $XML::Simple::PREFERRED_PARSER = pop @parsers;
252         } until $XML::Simple::PREFERRED_PARSER ne 'XML::SAX::PurePerl';
253
254         # --limit is only supported on Subversion 1.2.0+
255         my $svn_version=`svn --version -q`;
256         my $svn_limit='';
257         $svn_limit="--limit $num"
258                 if $svn_version =~ /\d\.(\d)\.\d/ && $1 >= 2;
259
260         my $svn_url=svn_info("URL", $config{srcdir});
261         my $xml = XMLin(scalar `svn $svn_limit --xml -v log '$svn_url'`,
262                 ForceArray => [ 'logentry', 'path' ],
263                 GroupTags => { paths => 'path' },
264                 KeyAttr => { path => 'content' },
265         );
266         foreach my $logentry (@{$xml->{logentry}}) {
267                 my (@pages, @message);
268
269                 my $rev = $logentry->{revision};
270                 my $user = $logentry->{author};
271
272                 my $when=str2time($logentry->{date}, 'UTC');
273
274                 foreach my $msgline (split(/\n/, $logentry->{msg})) {
275                         push @message, { line => $msgline };
276                 }
277
278                 my $committype="web";
279                 if (defined $message[0] &&
280                     $message[0]->{line}=~/$config{web_commit_regexp}/) {
281                         $user=defined $2 ? "$2" : "$3";
282                         $message[0]->{line}=$4;
283                 }
284                 else {
285                         $committype="svn";
286                 }
287
288                 foreach my $file (keys %{$logentry->{paths}}) {
289                         if (length $config{svnpath}) {
290                                 next unless $file=~/^\/\Q$config{svnpath}\E\/([^ ]+)(?:$|\s)/;
291                                 $file=$1;
292                         }
293
294                         my $diffurl=defined $config{diffurl} ? $config{diffurl} : "";
295                         $diffurl=~s/\[\[file\]\]/$file/g;
296                         $diffurl=~s/\[\[r1\]\]/$rev - 1/eg;
297                         $diffurl=~s/\[\[r2\]\]/$rev/g;
298
299                         push @pages, {
300                                 page => pagename($file),
301                                 diffurl => $diffurl,
302                         } if length $file;
303                 }
304                 push @ret, {
305                         rev => $rev,
306                         user => $user,
307                         committype => $committype,
308                         when => $when,
309                         message => [@message],
310                         pages => [@pages],
311                 } if @pages;
312                 return @ret if @ret >= $num;
313         }
314
315         return @ret;
316 }
317
318 sub rcs_diff ($;$) {
319         my $rev=IkiWiki::possibly_foolish_untaint(int(shift));
320         my $maxlines=shift;
321         return `svnlook diff $config{svnrepo} -r$rev --no-diff-deleted`;
322 }
323
324 {
325
326 my ($lastfile, $lastmtime, $lastctime);
327
328 sub findtimes ($) {
329         my $file=shift;
330
331         if (defined $lastfile && $lastfile eq $file) {
332                 return $lastmtime, $lastctime;
333         }
334         $lastfile=$file;
335
336         my $svn_log_infoline=qr/^r\d+\s+\|\s+[^\s]+\s+\|\s+(\d+-\d+-\d+\s+\d+:\d+:\d+\s+[-+]?\d+).*/;
337                 
338         my $child = open(SVNLOG, "-|");
339         if (! $child) {
340                 exec("svn", "log", "$config{srcdir}/$file") || error("svn log failed to run");
341         }
342
343         my ($cdate, $mdate);
344         while (<SVNLOG>) {
345                 if (/$svn_log_infoline/) {
346                         $cdate=$1;
347                         $mdate=$1 unless defined $mdate;
348                 }
349         }
350         close SVNLOG || error "svn log exited $?";
351
352         if (! defined $cdate) {
353                 error "failed to parse svn log for $file";
354         }
355                 
356         eval q{use Date::Parse};
357         error($@) if $@;
358         
359         $lastctime=str2time($cdate);
360         $lastmtime=str2time($mdate);
361         return $lastmtime, $lastctime;
362 }
363
364 }
365
366 sub rcs_getctime ($) {
367         my $file=shift;
368
369         return (findtimes($file))[1];
370 }
371
372 sub rcs_getmtime ($) {
373         my $file=shift;
374
375         return (findtimes($file))[0];
376 }
377
378 1