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