]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/tla.pm
confirmed
[ikiwiki.git] / IkiWiki / Plugin / tla.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::tla;
3
4 use warnings;
5 use strict;
6 use IkiWiki;
7
8 sub import {
9         hook(type => "checkconfig", id => "tla", call => \&checkconfig);
10         hook(type => "getsetup", id => "tla", call => \&getsetup);
11         hook(type => "rcs", id => "rcs_update", call => \&rcs_update);
12         hook(type => "rcs", id => "rcs_prepedit", call => \&rcs_prepedit);
13         hook(type => "rcs", id => "rcs_commit", call => \&rcs_commit);
14         hook(type => "rcs", id => "rcs_commit_staged", call => \&rcs_commit_staged);
15         hook(type => "rcs", id => "rcs_add", call => \&rcs_add);
16         hook(type => "rcs", id => "rcs_remove", call => \&rcs_remove);
17         hook(type => "rcs", id => "rcs_rename", call => \&rcs_rename);
18         hook(type => "rcs", id => "rcs_recentchanges", call => \&rcs_recentchanges);
19         hook(type => "rcs", id => "rcs_diff", call => \&rcs_diff);
20         hook(type => "rcs", id => "rcs_getctime", call => \&rcs_getctime);
21         hook(type => "rcs", id => "rcs_getmtime", call => \&rcs_getmtime);
22 }
23
24 sub checkconfig () {
25         if (defined $config{tla_wrapper} && length $config{tla_wrapper}) {
26                 push @{$config{wrappers}}, {
27                         wrapper => $config{tla_wrapper},
28                         wrappermode => (defined $config{tla_wrappermode} ? $config{tla_wrappermode} : "06755"),
29                 };
30         }
31 }
32
33 sub getsetup () {
34         return
35                 plugin => {
36                         safe => 0, # rcs plugin
37                         rebuild => undef,
38                         section => "rcs",
39                 },
40                 tla_wrapper => {
41                         type => "string",
42                         #example => "", # TODO example
43                         description => "tla post-commit hook to generate",
44                         safe => 0, # file
45                         rebuild => 0,
46                 },
47                 tla_wrappermode => {
48                         type => "string",
49                         example => '06755',
50                         description => "mode for tla_wrapper (can safely be made suid)",
51                         safe => 0,
52                         rebuild => 0,
53                 },
54                 historyurl => {
55                         type => "string",
56                         #example => "", # TODO example
57                         description => "url to show file history ([[file]] substituted)",
58                         safe => 1,
59                         rebuild => 1,
60                 },
61                 diffurl => {
62                         type => "string",
63                         #example => "", # TODO example
64                         description => "url to show a diff ([[file]] and [[rev]] substituted)",
65                         safe => 1,
66                         rebuild => 1,
67                 },
68 }
69
70 sub quiet_system (@) {
71         # See Debian bug #385939.
72         open (SAVEOUT, ">&STDOUT");
73         close STDOUT;
74         open (STDOUT, ">/dev/null");
75         my $ret=system(@_);
76         close STDOUT;
77         open (STDOUT, ">&SAVEOUT");
78         close SAVEOUT;
79         return $ret;
80 }
81
82 sub rcs_update () {
83         if (-d "$config{srcdir}/{arch}") {
84                 if (quiet_system("tla", "replay", "-d", $config{srcdir}) != 0) {
85                         warn("tla replay failed\n");
86                 }
87         }
88 }
89
90 sub rcs_prepedit ($) {
91         my $file=shift;
92
93         if (-d "$config{srcdir}/{arch}") {
94                 # For Arch, return the tree-id of archive when
95                 # editing begins.
96                 my $rev=`tla tree-id $config{srcdir}`;
97                 return defined $rev ? $rev : "";
98         }
99 }
100
101 sub rcs_commit (@) {
102         my %params=@_;
103
104         my ($file, $message, $rcstoken)=
105                 ($params{file}, $params{message}, $params{token});
106
107         if (defined $params{session}) {
108                 if (defined $params{session}->param("name")) {
109                         $message="web commit by ".
110                                 $params{session}->param("name").
111                                 (length $message ? ": $message" : "");
112                 }
113                 elsif (defined $params{session}->remote_addr()) {
114                         $message="web commit from ".
115                                 $params{session}->remote_addr().
116                                 (length $message ? ": $message" : "");
117                 }
118         }
119
120         if (-d "$config{srcdir}/{arch}") {
121                 # Check to see if the page has been changed by someone
122                 # else since rcs_prepedit was called.
123                 my ($oldrev)=$rcstoken=~/^([A-Za-z0-9@\/._-]+)$/; # untaint
124                 my $rev=`tla tree-id $config{srcdir}`;
125                 if (defined $rev && defined $oldrev && $rev ne $oldrev) {
126                         # Merge their changes into the file that we've
127                         # changed.
128                         if (quiet_system("tla", "update", "-d",
129                                    "$config{srcdir}") != 0) {
130                                 warn("tla update failed\n");
131                         }
132                 }
133
134                 if (quiet_system("tla", "commit",
135                            "-L".IkiWiki::possibly_foolish_untaint($message),
136                            '-d', $config{srcdir}) != 0) {
137                         my $conflict=readfile("$config{srcdir}/$file");
138                         if (system("tla", "undo", "-n", "--quiet", "-d", "$config{srcdir}") != 0) {
139                                 warn("tla undo failed\n");
140                         }
141                         return $conflict;
142                 }
143         }
144         return undef # success
145 }
146
147 sub rcs_commit_staged (@) {
148         # Commits all staged changes. Changes can be staged using rcs_add,
149         # rcs_remove, and rcs_rename.
150         my %params=@_;
151         
152         error("rcs_commit_staged not implemented for tla"); # TODO
153 }
154
155 sub rcs_add ($) {
156         my $file=shift;
157
158         if (-d "$config{srcdir}/{arch}") {
159                 if (quiet_system("tla", "add", "$config{srcdir}/$file") != 0) {
160                         warn("tla add failed\n");
161                 }
162         }
163 }
164
165 sub rcs_remove ($) {
166         my $file = shift;
167
168         error("rcs_remove not implemented for tla"); # TODO
169 }
170
171 sub rcs_rename ($$) {
172         my ($src, $dest) = @_;
173
174         error("rcs_rename not implemented for tla"); # TODO
175 }
176
177 sub rcs_recentchanges ($) {
178         my $num=shift;
179         my @ret;
180
181         return unless -d "$config{srcdir}/{arch}";
182
183         eval q{use Date::Parse};
184         error($@) if $@;
185         eval q{use Mail::Header};
186         error($@) if $@;
187
188         my $logs = `tla logs -d $config{srcdir}`;
189         my @changesets = reverse split(/\n/, $logs);
190
191         for (my $i=0; $i<$num && $i<$#changesets; $i++) {
192                 my ($change)=$changesets[$i]=~/^([A-Za-z0-9@\/._-]+)$/; # untaint
193
194                 open(LOG, "tla cat-log -d $config{srcdir} $change|");
195                 my $head = Mail::Header->new(\*LOG);
196                 close(LOG);
197
198                 my $rev = $head->get("Revision");
199                 my $summ = $head->get("Summary");
200                 my $newfiles = $head->get("New-files");
201                 my $modfiles = $head->get("Modified-files");
202                 my $remfiles = $head->get("Removed-files");
203                 my $user = $head->get("Creator");
204
205                 my @paths = grep { !/^(.*\/)?\.arch-ids\/.*\.id$/ }
206                         split(/ /, "$newfiles $modfiles .arch-ids/fake.id");
207
208                 my $sdate = $head->get("Standard-date");
209                 my $when = str2time($sdate, 'UTC');
210
211                 my $committype = "web";
212                 if (defined $summ && $summ =~ /$config{web_commit_regexp}/) {
213                         $user = defined $2 ? "$2" : "$3";
214                         $summ = $4;
215                 }
216                 else {
217                         $committype="tla";
218                 }
219
220                 my @message;
221                 push @message, { line => $summ };
222
223                 my @pages;
224
225                 foreach my $file (@paths) {
226                         my $diffurl=defined $config{diffurl} ? $config{diffurl} : "";
227                         $diffurl=~s/\[\[file\]\]/$file/g;
228                         $diffurl=~s/\[\[rev\]\]/$change/g;
229                         push @pages, {
230                                 page => pagename($file),
231                                 diffurl => $diffurl,
232                         } if length $file;
233                 }
234                 push @ret, {
235                         rev => $change,
236                         user => $user,
237                         committype => $committype,
238                         when => $when,
239                         message => [@message],
240                         pages => [@pages],
241                 } if @pages;
242
243                 last if $i == $num;
244         }
245
246         return @ret;
247 }
248
249 sub rcs_diff ($) {
250         my $rev=shift;
251         my $logs = `tla logs -d $config{srcdir}`;
252         my @changesets = reverse split(/\n/, $logs);
253         my $i;
254
255         for($i=0;$i<$#changesets;$i++) {
256                 last if $changesets[$i] eq $rev;
257         }
258
259         my $revminusone = $changesets[$i+1];
260         return `tla diff -d $config{srcdir} $revminusone`;
261 }
262
263 sub rcs_getctime ($) {
264         my $file=shift;
265         eval q{use Date::Parse};
266         error($@) if $@;
267         eval q{use Mail::Header};
268         error($@) if $@;
269
270         my $logs = `tla logs -d $config{srcdir}`;
271         my @changesets = reverse split(/\n/, $logs);
272         my $sdate;
273
274         for (my $i=0; $i<$#changesets; $i++) {
275                 my $change = $changesets[$i];
276
277                 open(LOG, "tla cat-log -d $config{srcdir} $change|");
278                 my $head = Mail::Header->new(\*LOG);
279                 close(LOG);
280
281                 $sdate = $head->get("Standard-date");
282                 my $newfiles = $head->get("New-files");
283
284                 my ($lastcreation) = grep {/^$file$/} split(/ /, "$newfiles");
285                 last if defined($lastcreation);
286         }
287
288         my $date=str2time($sdate, 'UTC');
289         debug("found ctime ".localtime($date)." for $file");
290         return $date;
291 }
292
293 sub rcs_getmtime ($) {
294         error "rcs_getmtime is not implemented for tla\n"; # TODO
295 }
296
297 1