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