add support for generating per-page rss feeds
[ikiwiki.git] / IkiWiki / Rcs / SVN.pm
1 #!/usr/bin/perl -T
2 # For subversion support.
3
4 use warnings;
5 use strict;
6
7 package IkiWiki;
8
9 sub svn_info ($$) { #{{{
10         my $field=shift;
11         my $file=shift;
12
13         my $info=`LANG=C svn info $file`;
14         my ($ret)=$info=~/^$field: (.*)$/m;
15         return $ret;
16 } #}}}
17
18 sub rcs_update () { #{{{
19         if (-d "$config{srcdir}/.svn") {
20                 if (system("svn", "update", "--quiet", $config{srcdir}) != 0) {
21                         warn("svn update failed\n");
22                 }
23         }
24 } #}}}
25
26 sub rcs_prepedit ($) { #{{{
27         # Prepares to edit a file under revision control. Returns a token
28         # that must be passed into rcs_commit when the file is ready
29         # for committing.
30         # The file is relative to the srcdir.
31         my $file=shift;
32         
33         if (-d "$config{srcdir}/.svn") {
34                 # For subversion, return the revision of the file when
35                 # editing begins.
36                 my $rev=svn_info("Revision", "$config{srcdir}/$file");
37                 return defined $rev ? $rev : "";
38         }
39 } #}}}
40
41 sub rcs_commit ($$$) { #{{{
42         # Tries to commit the page; returns undef on _success_ and
43         # a version of the page with the rcs's conflict markers on failure.
44         # The file is relative to the srcdir.
45         my $file=shift;
46         my $message=shift;
47         my $rcstoken=shift;
48
49         if (-d "$config{srcdir}/.svn") {
50                 # Check to see if the page has been changed by someone
51                 # else since rcs_prepedit was called.
52                 my ($oldrev)=$rcstoken=~/^([0-9]+)$/; # untaint
53                 my $rev=svn_info("Revision", "$config{srcdir}/$file");
54                 if (defined $rev && defined $oldrev && $rev != $oldrev) {
55                         # Merge their changes into the file that we've
56                         # changed.
57                         chdir($config{srcdir}); # svn merge wants to be here
58                         if (system("svn", "merge", "--quiet", "-r$oldrev:$rev",
59                                    "$config{srcdir}/$file") != 0) {
60                                 warn("svn merge -r$oldrev:$rev failed\n");
61                         }
62                 }
63
64                 if (system("svn", "commit", "--quiet", "-m",
65                            possibly_foolish_untaint($message),
66                            "$config{srcdir}") != 0) {
67                         my $conflict=readfile("$config{srcdir}/$file");
68                         if (system("svn", "revert", "--quiet", "$config{srcdir}/$file") != 0) {
69                                 warn("svn revert failed\n");
70                         }
71                         return $conflict;
72                 }
73         }
74         return undef # success
75 } #}}}
76
77 sub rcs_add ($) { #{{{
78         # filename is relative to the root of the srcdir
79         my $file=shift;
80
81         if (-d "$config{srcdir}/.svn") {
82                 my $parent=dirname($file);
83                 while (! -d "$config{srcdir}/$parent/.svn") {
84                         $file=$parent;
85                         $parent=dirname($file);
86                 }
87                 
88                 if (system("svn", "add", "--quiet", "$config{srcdir}/$file") != 0) {
89                         warn("svn add failed\n");
90                 }
91         }
92 } #}}}
93
94 sub rcs_recentchanges ($) { #{{{
95         my $num=shift;
96         my @ret;
97         
98         eval q{use CGI 'escapeHTML'};
99         eval q{use Date::Parse};
100         eval q{use Time::Duration};
101         
102         if (-d "$config{srcdir}/.svn") {
103                 my $svn_url=svn_info("URL", $config{srcdir});
104
105                 # FIXME: currently assumes that the wiki is somewhere
106                 # under trunk in svn, doesn't support other layouts.
107                 my ($svn_base)=$svn_url=~m!(/trunk(?:/.*)?)$!;
108                 
109                 my $div=qr/^--------------------+$/;
110                 my $infoline=qr/^r(\d+)\s+\|\s+([^\s]+)\s+\|\s+(\d+-\d+-\d+\s+\d+:\d+:\d+\s+[-+]?\d+).*/;
111                 my $state='start';
112                 my ($rev, $user, $when, @pages, @message);
113                 foreach (`LANG=C svn log --limit $num -v '$svn_url'`) {
114                         chomp;
115                         if ($state eq 'start' && /$div/) {
116                                 $state='header';
117                         }
118                         elsif ($state eq 'header' && /$infoline/) {
119                                 $rev=$1;
120                                 $user=$2;
121                                 $when=concise(ago(time - str2time($3)));
122                         }
123                         elsif ($state eq 'header' && /^\s+[A-Z]\s+\Q$svn_base\E\/([^ ]+)(?:$|\s)/) {
124                                 my $file=$1;
125                                 my $diffurl=$config{diffurl};
126                                 $diffurl=~s/\[\[file\]\]/$file/g;
127                                 $diffurl=~s/\[\[r1\]\]/$rev - 1/eg;
128                                 $diffurl=~s/\[\[r2\]\]/$rev/g;
129                                 push @pages, {
130                                         link => htmllink("", pagename($file), 1),
131                                         diffurl => $diffurl,
132                                 } if length $file;
133                         }
134                         elsif ($state eq 'header' && /^$/) {
135                                 $state='body';
136                         }
137                         elsif ($state eq 'body' && /$div/) {
138                                 my $committype="web";
139                                 if (defined $message[0] &&
140                                     $message[0]->{line}=~/^web commit by (\w+):?(.*)/) {
141                                         $user="$1";
142                                         $message[0]->{line}=$2;
143                                 }
144                                 else {
145                                         $committype="svn";
146                                 }
147                                 
148                                 push @ret, { rev => $rev,
149                                         user => htmllink("", $user, 1),
150                                         committype => $committype,
151                                         when => $when, message => [@message],
152                                         pages => [@pages],
153                                 } if @pages;
154                                 return @ret if @ret >= $num;
155                                 
156                                 $state='header';
157                                 $rev=$user=$when=undef;
158                                 @pages=@message=();
159                         }
160                         elsif ($state eq 'body') {
161                                 push @message, {line => escapeHTML($_)},
162                         }
163                 }
164         }
165
166         return @ret;
167 } #}}}
168
169 1