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