summaryrefslogtreecommitdiff
path: root/ikiwiki
blob: 8eebcdf77e90f48a3c6c74087408e3d836c81369 (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/(^[-A-Za-z0-9_.:\/+]+$)/,
  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. if (-l $file) {
  174. error("cannot read a symlink ($file)");
  175. }
  176. local $/=undef;
  177. open (IN, "$file") || error("failed to read $file: $!");
  178. my $ret=<IN>;
  179. close IN;
  180. return $ret;
  181. } #}}}
  182. sub writefile ($$$) { #{{{
  183. my $file=shift; # can include subdirs
  184. my $destdir=shift; # directory to put file in
  185. my $content=shift;
  186. my $test=$file;
  187. while (length $test) {
  188. if (-l "$destdir/$test") {
  189. error("cannot write to a symlink ($test)");
  190. }
  191. $test=dirname($test);
  192. }
  193. my $dir=dirname("$destdir/$file");
  194. if (! -d $dir) {
  195. my $d="";
  196. foreach my $s (split(m!/+!, $dir)) {
  197. $d.="$s/";
  198. if (! -d $d) {
  199. mkdir($d) || error("failed to create directory $d: $!");
  200. }
  201. }
  202. }
  203. open (OUT, ">$destdir/$file") || error("failed to write $destdir/$file: $!");
  204. print OUT $content;
  205. close OUT;
  206. } #}}}
  207. sub bestlink ($$) { #{{{
  208. # Given a page and the text of a link on the page, determine which
  209. # existing page that link best points to. Prefers pages under a
  210. # subdirectory with the same name as the source page, failing that
  211. # goes down the directory tree to the base looking for matching
  212. # pages.
  213. my $page=shift;
  214. my $link=lc(shift);
  215. my $cwd=$page;
  216. do {
  217. my $l=$cwd;
  218. $l.="/" if length $l;
  219. $l.=$link;
  220. if (exists $links{$l}) {
  221. #debug("for $page, \"$link\", use $l");
  222. return $l;
  223. }
  224. } while $cwd=~s!/?[^/]+$!!;
  225. #print STDERR "warning: page $page, broken link: $link\n";
  226. return "";
  227. } #}}}
  228. sub isinlinableimage ($) { #{{{
  229. my $file=shift;
  230. $file=~/\.(png|gif|jpg|jpeg)$/i;
  231. } #}}}
  232. sub pagetitle ($) { #{{{
  233. my $page=shift;
  234. $page=~s/__(\d+)__/&#$1;/g;
  235. $page=~y/_/ /;
  236. return $page;
  237. } #}}}
  238. sub titlepage ($) { #{{{
  239. my $title=shift;
  240. $title=~y/ /_/;
  241. $title=~s/([^-A-Za-z0-9_:+\/.])/"__".ord($1)."__"/eg;
  242. return $title;
  243. } #}}}
  244. sub cgiurl (@) { #{{{
  245. my %params=@_;
  246. return $config{cgiurl}."?".join("&amp;", map "$_=$params{$_}", keys %params);
  247. } #}}}
  248. sub styleurl (;$) { #{{{
  249. my $page=shift;
  250. return "$config{url}/style.css" if ! defined $page;
  251. $page=~s/[^\/]+$//;
  252. $page=~s/[^\/]+\//..\//g;
  253. return $page."style.css";
  254. } #}}}
  255. sub htmllink ($$;$$$) { #{{{
  256. my $page=shift;
  257. my $link=shift;
  258. my $noimageinline=shift; # don't turn links into inline html images
  259. my $forcesubpage=shift; # force a link to a subpage
  260. my $linktext=shift; # set to force the link text to something
  261. my $bestlink;
  262. if (! $forcesubpage) {
  263. $bestlink=bestlink($page, $link);
  264. }
  265. else {
  266. $bestlink="$page/".lc($link);
  267. }
  268. $linktext=pagetitle(basename($link)) unless defined $linktext;
  269. return $linktext if length $bestlink && $page eq $bestlink;
  270. # TODO BUG: %renderedfiles may not have it, if the linked to page
  271. # was also added and isn't yet rendered! Note that this bug is
  272. # masked by the bug mentioned below that makes all new files
  273. # be rendered twice.
  274. if (! grep { $_ eq $bestlink } values %renderedfiles) {
  275. $bestlink=htmlpage($bestlink);
  276. }
  277. if (! grep { $_ eq $bestlink } values %renderedfiles) {
  278. return "<span><a href=\"".
  279. cgiurl(do => "create", page => $link, from =>$page).
  280. "\">?</a>$linktext</span>"
  281. }
  282. $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
  283. if (! $noimageinline && isinlinableimage($bestlink)) {
  284. return "<img src=\"$bestlink\" alt=\"$linktext\" />";
  285. }
  286. return "<a href=\"$bestlink\">$linktext</a>";
  287. } #}}}
  288. sub indexlink () { #{{{
  289. return "<a href=\"$config{url}\">$config{wikiname}</a>";
  290. } #}}}
  291. sub lockwiki () { #{{{
  292. # Take an exclusive lock on the wiki to prevent multiple concurrent
  293. # run issues. The lock will be dropped on program exit.
  294. if (! -d $config{wikistatedir}) {
  295. mkdir($config{wikistatedir});
  296. }
  297. open(WIKILOCK, ">$config{wikistatedir}/lockfile") ||
  298. error ("cannot write to $config{wikistatedir}/lockfile: $!");
  299. if (! flock(WIKILOCK, 2 | 4)) {
  300. debug("wiki seems to be locked, waiting for lock");
  301. my $wait=600; # arbitrary, but don't hang forever to
  302. # prevent process pileup
  303. for (1..600) {
  304. return if flock(WIKILOCK, 2 | 4);
  305. sleep 1;
  306. }
  307. error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
  308. }
  309. } #}}}
  310. sub unlockwiki () { #{{{
  311. close WIKILOCK;
  312. } #}}}
  313. sub loadindex () { #{{{
  314. open (IN, "$config{wikistatedir}/index") || return;
  315. while (<IN>) {
  316. $_=possibly_foolish_untaint($_);
  317. chomp;
  318. my %items;
  319. $items{link}=[];
  320. foreach my $i (split(/ /, $_)) {
  321. my ($item, $val)=split(/=/, $i, 2);
  322. push @{$items{$item}}, $val;
  323. }
  324. next unless exists $items{src}; # skip bad lines for now
  325. my $page=pagename($items{src}[0]);
  326. if (! $config{rebuild}) {
  327. $pagesources{$page}=$items{src}[0];
  328. $oldpagemtime{$page}=$items{mtime}[0];
  329. $oldlinks{$page}=[@{$items{link}}];
  330. $links{$page}=[@{$items{link}}];
  331. $inlinepages{$page}=join(" ", @{$items{inlinepage}})
  332. if exists $items{inlinepage};
  333. $renderedfiles{$page}=$items{dest}[0];
  334. }
  335. $pagectime{$page}=$items{ctime}[0];
  336. }
  337. close IN;
  338. } #}}}
  339. sub saveindex () { #{{{
  340. if (! -d $config{wikistatedir}) {
  341. mkdir($config{wikistatedir});
  342. }
  343. open (OUT, ">$config{wikistatedir}/index") ||
  344. error("cannot write to $config{wikistatedir}/index: $!");
  345. foreach my $page (keys %oldpagemtime) {
  346. next unless $oldpagemtime{$page};
  347. my $line="mtime=$oldpagemtime{$page} ".
  348. "ctime=$pagectime{$page} ".
  349. "src=$pagesources{$page} ".
  350. "dest=$renderedfiles{$page}";
  351. $line.=" link=$_" foreach @{$links{$page}};
  352. if (exists $inlinepages{$page}) {
  353. $line.=" inlinepage=$_" foreach split " ", $inlinepages{$page};
  354. }
  355. print OUT $line."\n";
  356. }
  357. close OUT;
  358. } #}}}
  359. sub misctemplate ($$) { #{{{
  360. my $title=shift;
  361. my $pagebody=shift;
  362. my $template=HTML::Template->new(
  363. filename => "$config{templatedir}/misc.tmpl"
  364. );
  365. $template->param(
  366. title => $title,
  367. indexlink => indexlink(),
  368. wikiname => $config{wikiname},
  369. pagebody => $pagebody,
  370. styleurl => styleurl(),
  371. );
  372. return $template->output;
  373. }#}}}
  374. sub userinfo_get ($$) { #{{{
  375. my $user=shift;
  376. my $field=shift;
  377. eval q{use Storable};
  378. my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
  379. if (! defined $userdata || ! ref $userdata ||
  380. ! exists $userdata->{$user} || ! ref $userdata->{$user} ||
  381. ! exists $userdata->{$user}->{$field}) {
  382. return "";
  383. }
  384. return $userdata->{$user}->{$field};
  385. } #}}}
  386. sub userinfo_set ($$$) { #{{{
  387. my $user=shift;
  388. my $field=shift;
  389. my $value=shift;
  390. eval q{use Storable};
  391. my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
  392. if (! defined $userdata || ! ref $userdata ||
  393. ! exists $userdata->{$user} || ! ref $userdata->{$user}) {
  394. return "";
  395. }
  396. $userdata->{$user}->{$field}=$value;
  397. my $oldmask=umask(077);
  398. my $ret=Storable::lock_store($userdata, "$config{wikistatedir}/userdb");
  399. umask($oldmask);
  400. return $ret;
  401. } #}}}
  402. sub userinfo_setall ($$) { #{{{
  403. my $user=shift;
  404. my $info=shift;
  405. eval q{use Storable};
  406. my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
  407. if (! defined $userdata || ! ref $userdata) {
  408. $userdata={};
  409. }
  410. $userdata->{$user}=$info;
  411. my $oldmask=umask(077);
  412. my $ret=Storable::lock_store($userdata, "$config{wikistatedir}/userdb");
  413. umask($oldmask);
  414. return $ret;
  415. } #}}}
  416. sub is_admin ($) { #{{{
  417. my $user_name=shift;
  418. return grep { $_ eq $user_name } @{$config{adminuser}};
  419. } #}}}
  420. sub glob_match ($$) { #{{{
  421. my $page=shift;
  422. my $glob=shift;
  423. # turn glob into safe regexp
  424. $glob=quotemeta($glob);
  425. $glob=~s/\\\*/.*/g;
  426. $glob=~s/\\\?/./g;
  427. $glob=~s!\\/!/!g;
  428. $page=~/^$glob$/i;
  429. } #}}}
  430. sub globlist_match ($$) { #{{{
  431. my $page=shift;
  432. my @globlist=split(" ", shift);
  433. # check any negated globs first
  434. foreach my $glob (@globlist) {
  435. return 0 if $glob=~/^!(.*)/ && glob_match($page, $1);
  436. }
  437. foreach my $glob (@globlist) {
  438. return 1 if glob_match($page, $glob);
  439. }
  440. return 0;
  441. } #}}}
  442. sub main () { #{{{
  443. getconfig();
  444. if ($config{cgi}) {
  445. lockwiki();
  446. loadindex();
  447. require IkiWiki::CGI;
  448. cgi();
  449. }
  450. elsif ($config{setup}) {
  451. require IkiWiki::Setup;
  452. setup();
  453. }
  454. elsif ($config{wrapper}) {
  455. lockwiki();
  456. require IkiWiki::Wrapper;
  457. gen_wrapper();
  458. }
  459. else {
  460. lockwiki();
  461. loadindex();
  462. require IkiWiki::Render;
  463. rcs_update();
  464. rcs_getctime() if $config{getctime};
  465. refresh();
  466. saveindex();
  467. }
  468. } #}}}
  469. main;