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