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