summaryrefslogtreecommitdiff
path: root/ikiwiki
blob: 9e9c293544170cb61ed6a60310e7967496825098 (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 htmllink ($$;$$$) { #{{{
  219. my $page=shift;
  220. my $link=shift;
  221. my $noimageinline=shift; # don't turn links into inline html images
  222. my $forcesubpage=shift; # force a link to a subpage
  223. my $linktext=shift; # set to force the link text to something
  224. my $bestlink;
  225. if (! $forcesubpage) {
  226. $bestlink=bestlink($page, $link);
  227. }
  228. else {
  229. $bestlink="$page/".lc($link);
  230. }
  231. $linktext=pagetitle(basename($link)) unless defined $linktext;
  232. return $linktext if length $bestlink && $page eq $bestlink;
  233. # TODO BUG: %renderedfiles may not have it, if the linked to page
  234. # was also added and isn't yet rendered! Note that this bug is
  235. # masked by the bug mentioned below that makes all new files
  236. # be rendered twice.
  237. if (! grep { $_ eq $bestlink } values %renderedfiles) {
  238. $bestlink=htmlpage($bestlink);
  239. }
  240. if (! grep { $_ eq $bestlink } values %renderedfiles) {
  241. return "<span><a href=\"$config{cgiurl}?do=create&page=$link&from=$page\">?</a>$linktext</span>"
  242. }
  243. $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
  244. if (! $noimageinline && isinlinableimage($bestlink)) {
  245. return "<img src=\"$bestlink\">";
  246. }
  247. return "<a href=\"$bestlink\">$linktext</a>";
  248. } #}}}
  249. sub indexlink () { #{{{
  250. return "<a href=\"$config{url}\">$config{wikiname}</a>";
  251. } #}}}
  252. sub lockwiki () { #{{{
  253. # Take an exclusive lock on the wiki to prevent multiple concurrent
  254. # run issues. The lock will be dropped on program exit.
  255. if (! -d $config{wikistatedir}) {
  256. mkdir($config{wikistatedir});
  257. }
  258. open(WIKILOCK, ">$config{wikistatedir}/lockfile") ||
  259. error ("cannot write to $config{wikistatedir}/lockfile: $!");
  260. if (! flock(WIKILOCK, 2 | 4)) {
  261. debug("wiki seems to be locked, waiting for lock");
  262. my $wait=600; # arbitrary, but don't hang forever to
  263. # prevent process pileup
  264. for (1..600) {
  265. return if flock(WIKILOCK, 2 | 4);
  266. sleep 1;
  267. }
  268. error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
  269. }
  270. } #}}}
  271. sub unlockwiki () { #{{{
  272. close WIKILOCK;
  273. } #}}}
  274. sub loadindex () { #{{{
  275. open (IN, "$config{wikistatedir}/index") || return;
  276. while (<IN>) {
  277. $_=possibly_foolish_untaint($_);
  278. chomp;
  279. my %items;
  280. $items{link}=[];
  281. foreach my $i (split(/ /, $_)) {
  282. my ($item, $val)=split(/=/, $i, 2);
  283. push @{$items{$item}}, $val;
  284. }
  285. next unless exists $items{src}; # skip bad lines for now
  286. my $page=pagename($items{src}[0]);
  287. if (! $config{rebuild}) {
  288. $pagesources{$page}=$items{src}[0];
  289. $oldpagemtime{$page}=$items{mtime}[0];
  290. $oldlinks{$page}=[@{$items{link}}];
  291. $links{$page}=[@{$items{link}}];
  292. $inlinepages{$page}=join(" ", @{$items{inlinepage}})
  293. if exists $items{inlinepage};
  294. $renderedfiles{$page}=$items{dest}[0];
  295. }
  296. $pagectime{$page}=$items{ctime}[0];
  297. }
  298. close IN;
  299. } #}}}
  300. sub saveindex () { #{{{
  301. if (! -d $config{wikistatedir}) {
  302. mkdir($config{wikistatedir});
  303. }
  304. open (OUT, ">$config{wikistatedir}/index") ||
  305. error("cannot write to $config{wikistatedir}/index: $!");
  306. foreach my $page (keys %oldpagemtime) {
  307. next unless $oldpagemtime{$page};
  308. my $line="mtime=$oldpagemtime{$page} ".
  309. "ctime=$pagectime{$page} ".
  310. "src=$pagesources{$page} ".
  311. "dest=$renderedfiles{$page}";
  312. $line.=" link=$_" foreach @{$links{$page}};
  313. if (exists $inlinepages{$page}) {
  314. $line.=" inlinepage=$_" foreach split " ", $inlinepages{$page};
  315. }
  316. print OUT $line."\n";
  317. }
  318. close OUT;
  319. } #}}}
  320. sub misctemplate ($$) { #{{{
  321. my $title=shift;
  322. my $pagebody=shift;
  323. my $template=HTML::Template->new(
  324. filename => "$config{templatedir}/misc.tmpl"
  325. );
  326. $template->param(
  327. title => $title,
  328. indexlink => indexlink(),
  329. wikiname => $config{wikiname},
  330. pagebody => $pagebody,
  331. );
  332. return $template->output;
  333. }#}}}
  334. sub userinfo_get ($$) { #{{{
  335. my $user=shift;
  336. my $field=shift;
  337. eval q{use Storable};
  338. my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
  339. if (! defined $userdata || ! ref $userdata ||
  340. ! exists $userdata->{$user} || ! ref $userdata->{$user} ||
  341. ! exists $userdata->{$user}->{$field}) {
  342. return "";
  343. }
  344. return $userdata->{$user}->{$field};
  345. } #}}}
  346. sub userinfo_set ($$$) { #{{{
  347. my $user=shift;
  348. my $field=shift;
  349. my $value=shift;
  350. eval q{use Storable};
  351. my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
  352. if (! defined $userdata || ! ref $userdata ||
  353. ! exists $userdata->{$user} || ! ref $userdata->{$user}) {
  354. return "";
  355. }
  356. $userdata->{$user}->{$field}=$value;
  357. my $oldmask=umask(077);
  358. my $ret=Storable::lock_store($userdata, "$config{wikistatedir}/userdb");
  359. umask($oldmask);
  360. return $ret;
  361. } #}}}
  362. sub userinfo_setall ($$) { #{{{
  363. my $user=shift;
  364. my $info=shift;
  365. eval q{use Storable};
  366. my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
  367. if (! defined $userdata || ! ref $userdata) {
  368. $userdata={};
  369. }
  370. $userdata->{$user}=$info;
  371. my $oldmask=umask(077);
  372. my $ret=Storable::lock_store($userdata, "$config{wikistatedir}/userdb");
  373. umask($oldmask);
  374. return $ret;
  375. } #}}}
  376. sub is_admin ($) { #{{{
  377. my $user_name=shift;
  378. return grep { $_ eq $user_name } @{$config{adminuser}};
  379. } #}}}
  380. sub glob_match ($$) { #{{{
  381. my $page=shift;
  382. my $glob=shift;
  383. # turn glob into safe regexp
  384. $glob=quotemeta($glob);
  385. $glob=~s/\\\*/.*/g;
  386. $glob=~s/\\\?/./g;
  387. $glob=~s!\\/!/!g;
  388. $page=~/^$glob$/i;
  389. } #}}}
  390. sub globlist_match ($$) { #{{{
  391. my $page=shift;
  392. my @globlist=split(" ", shift);
  393. # check any negated globs first
  394. foreach my $glob (@globlist) {
  395. return 0 if $glob=~/^!(.*)/ && glob_match($page, $1);
  396. }
  397. foreach my $glob (@globlist) {
  398. return 1 if glob_match($page, $glob);
  399. }
  400. return 0;
  401. } #}}}
  402. sub main () { #{{{
  403. getconfig();
  404. if ($config{cgi}) {
  405. lockwiki();
  406. loadindex();
  407. require IkiWiki::CGI;
  408. cgi();
  409. }
  410. elsif ($config{setup}) {
  411. require IkiWiki::Setup;
  412. setup();
  413. }
  414. elsif ($config{wrapper}) {
  415. lockwiki();
  416. require IkiWiki::Wrapper;
  417. gen_wrapper();
  418. }
  419. else {
  420. lockwiki();
  421. loadindex();
  422. require IkiWiki::Render;
  423. rcs_update();
  424. rcs_getctime() if $config{getctime};
  425. refresh();
  426. saveindex();
  427. }
  428. } #}}}
  429. main;