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