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