]> sipb.mit.edu Git - ikiwiki.git/blob - ikiwiki
web commit by joey
[ikiwiki.git] / ikiwiki
1 #!/usr/bin/perl -T
2 $ENV{PATH}="/usr/local/bin:/usr/bin:/bin";
3
4 package IkiWiki;
5 use warnings;
6 use strict;
7 use File::Spec;
8 use HTML::Template;
9 use lib '.'; # For use without installation, removed by Makefile.
10
11 use vars qw{%config %links %oldlinks %oldpagemtime %pagectime
12             %renderedfiles %pagesources %inlinepages};
13
14 sub usage () { #{{{
15         die "usage: ikiwiki [options] source dest\n";
16 } #}}}
17
18 sub getconfig () { #{{{
19         if (! exists $ENV{WRAPPED_OPTIONS}) {
20                 %config=(
21                         wiki_file_prune_regexp => qr{((^|/).svn/|\.\.|^\.|\/\.|\.html?$)},
22                         wiki_link_regexp => qr/\[\[([^\s\]]+)\]\]/,
23                         wiki_processor_regexp => qr/\[\[(\w+)\s+([^\]]+)\]\]/,
24                         wiki_file_regexp => qr/(^[-A-Za-z0-9_.:\/+]+$)/,
25                         verbose => 0,
26                         wikiname => "wiki",
27                         default_pageext => ".mdwn",
28                         cgi => 0,
29                         svn => 1,
30                         url => '',
31                         cgiurl => '',
32                         historyurl => '',
33                         diffurl => '',
34                         anonok => 0,
35                         rss => 0,
36                         rebuild => 0,
37                         wrapper => undef,
38                         wrappermode => undef,
39                         srcdir => undef,
40                         destdir => undef,
41                         templatedir => "/usr/share/ikiwiki/templates",
42                         setup => undef,
43                         adminuser => undef,
44                 );
45
46                 eval q{use Getopt::Long};
47                 GetOptions(
48                         "setup|s=s" => \$config{setup},
49                         "wikiname=s" => \$config{wikiname},
50                         "verbose|v!" => \$config{verbose},
51                         "rebuild!" => \$config{rebuild},
52                         "wrappermode=i" => \$config{wrappermode},
53                         "svn!" => \$config{svn},
54                         "anonok!" => \$config{anonok},
55                         "rss!" => \$config{rss},
56                         "cgi!" => \$config{cgi},
57                         "url=s" => \$config{url},
58                         "cgiurl=s" => \$config{cgiurl},
59                         "historyurl=s" => \$config{historyurl},
60                         "diffurl=s" => \$config{diffurl},
61                         "exclude=s@" => sub {
62                                 $config{wiki_file_prune_regexp}=qr/$config{wiki_file_prune_regexp}|$_[1]/;
63                         },
64                         "adminuser=s@" => sub {
65                                 push @{$config{adminuser}}, $_[1]
66                         },
67                         "templatedir=s" => sub {
68                                 $config{templatedir}=possibly_foolish_untaint($_[1])
69                         },
70                         "wrapper:s" => sub {
71                                 $config{wrapper}=$_[1] ? $_[1] : "ikiwiki-wrap"
72                         },
73                 ) || usage();
74
75                 if (! $config{setup}) {
76                         usage() unless @ARGV == 2;
77                         $config{srcdir} = possibly_foolish_untaint(shift @ARGV);
78                         $config{destdir} = possibly_foolish_untaint(shift @ARGV);
79                         checkconfig();
80                 }
81         }
82         else {
83                 # wrapper passes a full config structure in the environment
84                 # variable
85                 eval possibly_foolish_untaint($ENV{WRAPPED_OPTIONS});
86                 checkconfig();
87         }
88 } #}}}
89
90 sub checkconfig () { #{{{
91         if ($config{cgi} && ! length $config{url}) {
92                 error("Must specify url to wiki with --url when using --cgi\n");
93         }
94         if ($config{rss} && ! length $config{url}) {
95                 error("Must specify url to wiki with --url when using --rss\n");
96         }
97         
98         $config{wikistatedir}="$config{srcdir}/.ikiwiki"
99                 unless exists $config{wikistatedir};
100         
101         if ($config{svn}) {
102                 require IkiWiki::Rcs::SVN;
103                 $config{rcs}=1;
104         }
105         else {
106                 require IkiWiki::Rcs::Stub;
107                 $config{rcs}=0;
108         }
109 } #}}}
110
111 sub error ($) { #{{{
112         if ($config{cgi}) {
113                 print "Content-type: text/html\n\n";
114                 print misctemplate("Error", "<p>Error: @_</p>");
115         }
116         die @_;
117 } #}}}
118
119 sub possibly_foolish_untaint ($) { #{{{
120         my $tainted=shift;
121         my ($untainted)=$tainted=~/(.*)/;
122         return $untainted;
123 } #}}}
124
125 sub debug ($) { #{{{
126         return unless $config{verbose};
127         if (! $config{cgi}) {
128                 print "@_\n";
129         }
130         else {
131                 print STDERR "@_\n";
132         }
133 } #}}}
134
135 sub basename ($) { #{{{
136         my $file=shift;
137
138         $file=~s!.*/!!;
139         return $file;
140 } #}}}
141
142 sub dirname ($) { #{{{
143         my $file=shift;
144
145         $file=~s!/?[^/]+$!!;
146         return $file;
147 } #}}}
148
149 sub pagetype ($) { #{{{
150         my $page=shift;
151         
152         if ($page =~ /\.mdwn$/) {
153                 return ".mdwn";
154         }
155         else {
156                 return "unknown";
157         }
158 } #}}}
159
160 sub pagename ($) { #{{{
161         my $file=shift;
162
163         my $type=pagetype($file);
164         my $page=$file;
165         $page=~s/\Q$type\E*$// unless $type eq 'unknown';
166         return $page;
167 } #}}}
168
169 sub htmlpage ($) { #{{{
170         my $page=shift;
171
172         return $page.".html";
173 } #}}}
174
175 sub readfile ($) { #{{{
176         my $file=shift;
177
178         if (-l $file) {
179                 error("cannot read a symlink ($file)");
180         }
181         
182         local $/=undef;
183         open (IN, "$file") || error("failed to read $file: $!");
184         my $ret=<IN>;
185         close IN;
186         return $ret;
187 } #}}}
188
189 sub writefile ($$) { #{{{
190         my $file=shift;
191         my $content=shift;
192         
193         if (-l $file) {
194                 error("cannot write to a symlink ($file)");
195         }
196
197         my $dir=dirname($file);
198         if (! -d $dir) {
199                 my $d="";
200                 foreach my $s (split(m!/+!, $dir)) {
201                         $d.="$s/";
202                         if (! -d $d) {
203                                 mkdir($d) || error("failed to create directory $d: $!");
204                         }
205                 }
206         }
207         
208         open (OUT, ">$file") || error("failed to write $file: $!");
209         print OUT $content;
210         close OUT;
211 } #}}}
212
213 sub bestlink ($$) { #{{{
214         # Given a page and the text of a link on the page, determine which
215         # existing page that link best points to. Prefers pages under a
216         # subdirectory with the same name as the source page, failing that
217         # goes down the directory tree to the base looking for matching
218         # pages.
219         my $page=shift;
220         my $link=lc(shift);
221         
222         my $cwd=$page;
223         do {
224                 my $l=$cwd;
225                 $l.="/" if length $l;
226                 $l.=$link;
227
228                 if (exists $links{$l}) {
229                         #debug("for $page, \"$link\", use $l");
230                         return $l;
231                 }
232         } while $cwd=~s!/?[^/]+$!!;
233
234         #print STDERR "warning: page $page, broken link: $link\n";
235         return "";
236 } #}}}
237
238 sub isinlinableimage ($) { #{{{
239         my $file=shift;
240         
241         $file=~/\.(png|gif|jpg|jpeg)$/;
242 } #}}}
243
244 sub pagetitle ($) { #{{{
245         my $page=shift;
246         $page=~s/__(\d+)__/&#$1;/g;
247         $page=~y/_/ /;
248         return $page;
249 } #}}}
250
251 sub htmllink ($$;$$) { #{{{
252         my $page=shift;
253         my $link=shift;
254         my $noimageinline=shift; # don't turn links into inline html images
255         my $forcesubpage=shift; # force a link to a subpage
256
257         my $bestlink;
258         if (! $forcesubpage) {
259                 $bestlink=bestlink($page, $link);
260         }
261         else {
262                 $bestlink="$page/".lc($link);
263         }
264
265         my $linktext=pagetitle(basename($link));
266         
267         return $linktext if length $bestlink && $page eq $bestlink;
268         
269         # TODO BUG: %renderedfiles may not have it, if the linked to page
270         # was also added and isn't yet rendered! Note that this bug is
271         # masked by the bug mentioned below that makes all new files
272         # be rendered twice.
273         if (! grep { $_ eq $bestlink } values %renderedfiles) {
274                 $bestlink=htmlpage($bestlink);
275         }
276         if (! grep { $_ eq $bestlink } values %renderedfiles) {
277                 return "<span><a href=\"$config{cgiurl}?do=create&page=$link&from=$page\">?</a>$linktext</span>"
278         }
279         
280         $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
281         
282         if (! $noimageinline && isinlinableimage($bestlink)) {
283                 return "<img src=\"$bestlink\">";
284         }
285         return "<a href=\"$bestlink\">$linktext</a>";
286 } #}}}
287
288 sub indexlink () { #{{{
289         return "<a href=\"$config{url}\">$config{wikiname}</a>";
290 } #}}}
291
292 sub lockwiki () { #{{{
293         # Take an exclusive lock on the wiki to prevent multiple concurrent
294         # run issues. The lock will be dropped on program exit.
295         if (! -d $config{wikistatedir}) {
296                 mkdir($config{wikistatedir});
297         }
298         open(WIKILOCK, ">$config{wikistatedir}/lockfile") ||
299                 error ("cannot write to $config{wikistatedir}/lockfile: $!");
300         if (! flock(WIKILOCK, 2 | 4)) {
301                 debug("wiki seems to be locked, waiting for lock");
302                 my $wait=600; # arbitrary, but don't hang forever to 
303                               # prevent process pileup
304                 for (1..600) {
305                         return if flock(WIKILOCK, 2 | 4);
306                         sleep 1;
307                 }
308                 error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
309         }
310 } #}}}
311
312 sub unlockwiki () { #{{{
313         close WIKILOCK;
314 } #}}}
315
316 sub loadindex () { #{{{
317         open (IN, "$config{wikistatedir}/index") || return;
318         while (<IN>) {
319                 $_=possibly_foolish_untaint($_);
320                 chomp;
321                 my %items;
322                 $items{link}=[];
323                 foreach my $i (split(/ /, $_)) {
324                         my ($item, $val)=split(/=/, $i, 2);
325                         push @{$items{$item}}, $val;
326                 }
327
328                 next unless exists $items{src}; # skip bad lines for now
329
330                 my $page=pagename($items{src}[0]);
331                 if (! $config{rebuild}) {
332                         $pagesources{$page}=$items{src}[0];
333                         $oldpagemtime{$page}=$items{mtime}[0];
334                         $oldlinks{$page}=[@{$items{link}}];
335                         $links{$page}=[@{$items{link}}];
336                         $inlinepages{$page}=join(" ", @{$items{inlinepage}})
337                                 if exists $items{inlinepage};
338                         $renderedfiles{$page}=$items{dest}[0];
339                 }
340                 $pagectime{$page}=$items{ctime}[0];
341         }
342         close IN;
343 } #}}}
344
345 sub saveindex () { #{{{
346         if (! -d $config{wikistatedir}) {
347                 mkdir($config{wikistatedir});
348         }
349         open (OUT, ">$config{wikistatedir}/index") || 
350                 error("cannot write to $config{wikistatedir}/index: $!");
351         foreach my $page (keys %oldpagemtime) {
352                 next unless $oldpagemtime{$page};
353                 my $line="mtime=$oldpagemtime{$page} ".
354                         "ctime=$pagectime{$page} ".
355                         "src=$pagesources{$page} ".
356                         "dest=$renderedfiles{$page}";
357                 $line.=" link=$_" foreach @{$links{$page}};
358                 if (exists $inlinepages{$page}) {
359                         $line.=" inlinepage=$_" foreach split " ", $inlinepages{$page};
360                 }
361                 print OUT $line."\n";
362         }
363         close OUT;
364 } #}}}
365
366 sub misctemplate ($$) { #{{{
367         my $title=shift;
368         my $pagebody=shift;
369         
370         my $template=HTML::Template->new(
371                 filename => "$config{templatedir}/misc.tmpl"
372         );
373         $template->param(
374                 title => $title,
375                 indexlink => indexlink(),
376                 wikiname => $config{wikiname},
377                 pagebody => $pagebody,
378         );
379         return $template->output;
380 }#}}}
381
382 sub userinfo_get ($$) { #{{{
383         my $user=shift;
384         my $field=shift;
385
386         eval q{use Storable};
387         my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
388         if (! defined $userdata || ! ref $userdata || 
389             ! exists $userdata->{$user} || ! ref $userdata->{$user} ||
390             ! exists $userdata->{$user}->{$field}) {
391                 return "";
392         }
393         return $userdata->{$user}->{$field};
394 } #}}}
395
396 sub userinfo_set ($$$) { #{{{
397         my $user=shift;
398         my $field=shift;
399         my $value=shift;
400         
401         eval q{use Storable};
402         my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
403         if (! defined $userdata || ! ref $userdata || 
404             ! exists $userdata->{$user} || ! ref $userdata->{$user}) {
405                 return "";
406         }
407         
408         $userdata->{$user}->{$field}=$value;
409         my $oldmask=umask(077);
410         my $ret=Storable::lock_store($userdata, "$config{wikistatedir}/userdb");
411         umask($oldmask);
412         return $ret;
413 } #}}}
414
415 sub userinfo_setall ($$) { #{{{
416         my $user=shift;
417         my $info=shift;
418         
419         eval q{use Storable};
420         my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
421         if (! defined $userdata || ! ref $userdata) {
422                 $userdata={};
423         }
424         $userdata->{$user}=$info;
425         my $oldmask=umask(077);
426         my $ret=Storable::lock_store($userdata, "$config{wikistatedir}/userdb");
427         umask($oldmask);
428         return $ret;
429 } #}}}
430
431 sub is_admin ($) { #{{{
432         my $user_name=shift;
433
434         return grep { $_ eq $user_name } @{$config{adminuser}};
435 } #}}}
436
437 sub glob_match ($$) { #{{{
438         my $page=shift;
439         my $glob=shift;
440
441         # turn glob into safe regexp
442         $glob=quotemeta($glob);
443         $glob=~s/\\\*/.*/g;
444         $glob=~s/\\\?/./g;
445         $glob=~s!\\/!/!g;
446         
447         $page=~/^$glob$/i;
448 } #}}}
449
450 sub globlist_match ($$) { #{{{
451         my $page=shift;
452         my @globlist=split(" ", shift);
453
454         # check any negated globs first
455         foreach my $glob (@globlist) {
456                 return 0 if $glob=~/^!(.*)/ && glob_match($page, $1);
457         }
458
459         foreach my $glob (@globlist) {
460                 return 1 if glob_match($page, $glob);
461         }
462         
463         return 0;
464 } #}}}
465
466 sub main () { #{{{
467         getconfig();
468         
469         if ($config{cgi}) {
470                 lockwiki();
471                 loadindex();
472                 require IkiWiki::CGI;
473                 cgi();
474         }
475         elsif ($config{setup}) {
476                 require IkiWiki::Setup;
477                 setup();
478         }
479         elsif ($config{wrapper}) {
480                 lockwiki();
481                 require IkiWiki::Wrapper;
482                 gen_wrapper();
483         }
484         else {
485                 lockwiki();
486                 loadindex();
487                 require IkiWiki::Render;
488                 rcs_update();
489                 refresh();
490                 saveindex();
491         }
492 } #}}}
493
494 main;