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