]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/tla.pm
Removed the openidsignup option.
[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 $file=shift;
103         my $message=shift;
104         my $rcstoken=shift;
105         my $user=shift;
106         my $ipaddr=shift;
107
108         if (defined $user) {
109                 $message="web commit by $user".(length $message ? ": $message" : "");
110         }
111         elsif (defined $ipaddr) {
112                 $message="web commit from $ipaddr".(length $message ? ": $message" : "");
113         }
114
115         if (-d "$config{srcdir}/{arch}") {
116                 # Check to see if the page has been changed by someone
117                 # else since rcs_prepedit was called.
118                 my ($oldrev)=$rcstoken=~/^([A-Za-z0-9@\/._-]+)$/; # untaint
119                 my $rev=`tla tree-id $config{srcdir}`;
120                 if (defined $rev && defined $oldrev && $rev ne $oldrev) {
121                         # Merge their changes into the file that we've
122                         # changed.
123                         if (quiet_system("tla", "update", "-d",
124                                    "$config{srcdir}") != 0) {
125                                 warn("tla update failed\n");
126                         }
127                 }
128
129                 if (quiet_system("tla", "commit",
130                            "-L".IkiWiki::possibly_foolish_untaint($message),
131                            '-d', $config{srcdir}) != 0) {
132                         my $conflict=readfile("$config{srcdir}/$file");
133                         if (system("tla", "undo", "-n", "--quiet", "-d", "$config{srcdir}") != 0) {
134                                 warn("tla undo failed\n");
135                         }
136                         return $conflict;
137                 }
138         }
139         return undef # success
140 }
141
142 sub rcs_commit_staged ($$$) {
143         # Commits all staged changes. Changes can be staged using rcs_add,
144         # rcs_remove, and rcs_rename.
145         my ($message, $user, $ipaddr)=@_;
146         
147         error("rcs_commit_staged not implemented for tla"); # TODO
148 }
149
150 sub rcs_add ($) {
151         my $file=shift;
152
153         if (-d "$config{srcdir}/{arch}") {
154                 if (quiet_system("tla", "add", "$config{srcdir}/$file") != 0) {
155                         warn("tla add failed\n");
156                 }
157         }
158 }
159
160 sub rcs_remove ($) {
161         my $file = shift;
162
163         error("rcs_remove not implemented for tla"); # TODO
164 }
165
166 sub rcs_rename ($$) {
167         my ($src, $dest) = @_;
168
169         error("rcs_rename not implemented for tla"); # TODO
170 }
171
172 sub rcs_recentchanges ($) {
173         my $num=shift;
174         my @ret;
175
176         return unless -d "$config{srcdir}/{arch}";
177
178         eval q{use Date::Parse};
179         error($@) if $@;
180         eval q{use Mail::Header};
181         error($@) if $@;
182
183         my $logs = `tla logs -d $config{srcdir}`;
184         my @changesets = reverse split(/\n/, $logs);
185
186         for (my $i=0; $i<$num && $i<$#changesets; $i++) {
187                 my ($change)=$changesets[$i]=~/^([A-Za-z0-9@\/._-]+)$/; # untaint
188
189                 open(LOG, "tla cat-log -d $config{srcdir} $change|");
190                 my $head = Mail::Header->new(\*LOG);
191                 close(LOG);
192
193                 my $rev = $head->get("Revision");
194                 my $summ = $head->get("Summary");
195                 my $newfiles = $head->get("New-files");
196                 my $modfiles = $head->get("Modified-files");
197                 my $remfiles = $head->get("Removed-files");
198                 my $user = $head->get("Creator");
199
200                 my @paths = grep { !/^(.*\/)?\.arch-ids\/.*\.id$/ }
201                         split(/ /, "$newfiles $modfiles .arch-ids/fake.id");
202
203                 my $sdate = $head->get("Standard-date");
204                 my $when = str2time($sdate, 'UTC');
205
206                 my $committype = "web";
207                 if (defined $summ && $summ =~ /$config{web_commit_regexp}/) {
208                         $user = defined $2 ? "$2" : "$3";
209                         $summ = $4;
210                 }
211                 else {
212                         $committype="tla";
213                 }
214
215                 my @message;
216                 push @message, { line => $summ };
217
218                 my @pages;
219
220                 foreach my $file (@paths) {
221                         my $diffurl=defined $config{diffurl} ? $config{diffurl} : "";
222                         $diffurl=~s/\[\[file\]\]/$file/g;
223                         $diffurl=~s/\[\[rev\]\]/$change/g;
224                         push @pages, {
225                                 page => pagename($file),
226                                 diffurl => $diffurl,
227                         } if length $file;
228                 }
229                 push @ret, {
230                         rev => $change,
231                         user => $user,
232                         committype => $committype,
233                         when => $when,
234                         message => [@message],
235                         pages => [@pages],
236                 } if @pages;
237
238                 last if $i == $num;
239         }
240
241         return @ret;
242 }
243
244 sub rcs_diff ($) {
245         my $rev=shift;
246         my $logs = `tla logs -d $config{srcdir}`;
247         my @changesets = reverse split(/\n/, $logs);
248         my $i;
249
250         for($i=0;$i<$#changesets;$i++) {
251                 last if $changesets[$i] eq $rev;
252         }
253
254         my $revminusone = $changesets[$i+1];
255         return `tla diff -d $config{srcdir} $revminusone`;
256 }
257
258 sub rcs_getctime ($) {
259         my $file=shift;
260         eval q{use Date::Parse};
261         error($@) if $@;
262         eval q{use Mail::Header};
263         error($@) if $@;
264
265         my $logs = `tla logs -d $config{srcdir}`;
266         my @changesets = reverse split(/\n/, $logs);
267         my $sdate;
268
269         for (my $i=0; $i<$#changesets; $i++) {
270                 my $change = $changesets[$i];
271
272                 open(LOG, "tla cat-log -d $config{srcdir} $change|");
273                 my $head = Mail::Header->new(\*LOG);
274                 close(LOG);
275
276                 $sdate = $head->get("Standard-date");
277                 my $newfiles = $head->get("New-files");
278
279                 my ($lastcreation) = grep {/^$file$/} split(/ /, "$newfiles");
280                 last if defined($lastcreation);
281         }
282
283         my $date=str2time($sdate, 'UTC');
284         debug("found ctime ".localtime($date)." for $file");
285         return $date;
286 }
287
288 sub rcs_getmtime ($) {
289         error "rcs_getmtime is not implemented for tla\n"; # TODO
290 }
291
292 1