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