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