summaryrefslogtreecommitdiff
path: root/ikiwiki-transition
blob: e02c3aaedceb0a206a920c166600332d4eeac3c0 (plain)
  1. #!/usr/bin/perl -i
  2. use warnings;
  3. use strict;
  4. use IkiWiki;
  5. use HTML::Entities;
  6. my $regex = qr{
  7. (\\?) # 1: escape?
  8. \[\[(!?) # directive open; 2: optional prefix
  9. ([-\w]+) # 3: command
  10. ( # 4: the parameters (including initial whitespace)
  11. \s+
  12. (?:
  13. (?:[-\w]+=)? # named parameter key?
  14. (?:
  15. """.*?""" # triple-quoted value
  16. |
  17. "[^"]+" # single-quoted value
  18. |
  19. [^\s\]]+ # unquoted value
  20. )
  21. \s* # whitespace or end
  22. # of directive
  23. )
  24. *) # 0 or more parameters
  25. \]\] # directive closed
  26. }sx;
  27. sub handle_directive {
  28. my $escape = shift;
  29. my $prefix = shift;
  30. my $directive = shift;
  31. my $args = shift;
  32. if (length $escape) {
  33. return "${escape}[[${prefix}${directive}${args}]]"
  34. }
  35. if ($directive =~ m/^(if|more|table|template|toggleable)$/) {
  36. $args =~ s{$regex}{handle_directive($1, $2, $3, $4)}eg;
  37. }
  38. return "[[!${directive}${args}]]"
  39. }
  40. sub prefix_directives {
  41. $/=undef; # process whole files at once
  42. while (<>) {
  43. s{$regex}{handle_directive($1, $2, $3, $4)}eg;
  44. print;
  45. }
  46. }
  47. sub indexdb {
  48. $config{wikistatedir}=shift()."/.ikiwiki";
  49. if (! defined $config{wikistatedir}) {
  50. usage();
  51. }
  52. # Note: No lockwiki here because ikiwiki already locks it
  53. # before calling this.
  54. if (! IkiWiki::oldloadindex()) {
  55. die "failed to load index\n";
  56. }
  57. if (! IkiWiki::saveindex()) {
  58. die "failed to save indexdb\n"
  59. }
  60. if (! IkiWiki::loadindex()) {
  61. die "transition failed, cannot load new indexdb\n";
  62. }
  63. if (! unlink("$config{wikistatedir}/index")) {
  64. die "unlink failed: $!\n";
  65. }
  66. }
  67. sub hashpassword {
  68. $config{wikistatedir}=shift()."/.ikiwiki";
  69. if (! defined $config{wikistatedir}) {
  70. usage();
  71. }
  72. eval q{use IkiWiki::UserInfo};
  73. eval q{use Authen::Passphrase::BlowfishCrypt};
  74. if ($@) {
  75. error("ikiwiki-transition hashpassword: failed to load Authen::Passphrase, passwords not hashed");
  76. }
  77. IkiWiki::lockwiki();
  78. IkiWiki::loadplugin("passwordauth");
  79. my $userinfo = IkiWiki::userinfo_retrieve();
  80. foreach my $user (keys %{$userinfo}) {
  81. if (ref $userinfo->{$user} &&
  82. exists $userinfo->{$user}->{password} &&
  83. length $userinfo->{$user}->{password} &&
  84. ! exists $userinfo->{$user}->{cryptpassword}) {
  85. IkiWiki::Plugin::passwordauth::setpassword($user, $userinfo->{$user}->{password});
  86. }
  87. }
  88. }
  89. sub usage {
  90. print STDERR "Usage: ikiwiki-transition type ...\n";
  91. print STDERR "Currently supported transition subcommands:\n";
  92. print STDERR " prefix_directives file\n";
  93. print STDERR " indexdb srcdir\n";
  94. print STDERR " hashpassword srcdir\n";
  95. exit 1;
  96. }
  97. usage() unless @ARGV;
  98. my $mode=shift;
  99. if ($mode eq 'prefix_directives') {
  100. prefix_directives(@ARGV);
  101. }
  102. if ($mode eq 'hashpassword') {
  103. hashpassword(@ARGV);
  104. }
  105. elsif ($mode eq 'indexdb') {
  106. indexdb(@ARGV);
  107. }
  108. else {
  109. usage();
  110. }
  111. package IkiWiki;
  112. # A slightly modified version of the old loadindex function.
  113. sub oldloadindex {
  114. %oldrenderedfiles=%pagectime=();
  115. if (! $config{rebuild}) {
  116. %pagesources=%pagemtime=%oldlinks=%links=%depends=
  117. %destsources=%renderedfiles=%pagecase=%pagestate=();
  118. }
  119. open (my $in, "<", "$config{wikistatedir}/index") || return;
  120. while (<$in>) {
  121. chomp;
  122. my %items;
  123. $items{link}=[];
  124. $items{dest}=[];
  125. foreach my $i (split(/ /, $_)) {
  126. my ($item, $val)=split(/=/, $i, 2);
  127. push @{$items{$item}}, decode_entities($val);
  128. }
  129. next unless exists $items{src}; # skip bad lines for now
  130. my $page=pagename($items{src}[0]);
  131. if (! $config{rebuild}) {
  132. $pagesources{$page}=$items{src}[0];
  133. $pagemtime{$page}=$items{mtime}[0];
  134. $oldlinks{$page}=[@{$items{link}}];
  135. $links{$page}=[@{$items{link}}];
  136. $depends{$page}=$items{depends}[0] if exists $items{depends};
  137. $destsources{$_}=$page foreach @{$items{dest}};
  138. $renderedfiles{$page}=[@{$items{dest}}];
  139. $pagecase{lc $page}=$page;
  140. foreach my $k (grep /_/, keys %items) {
  141. my ($id, $key)=split(/_/, $k, 2);
  142. $pagestate{$page}{decode_entities($id)}{decode_entities($key)}=$items{$k}[0];
  143. }
  144. }
  145. $oldrenderedfiles{$page}=[@{$items{dest}}];
  146. $pagectime{$page}=$items{ctime}[0];
  147. }
  148. # saveindex relies on %hooks being populated, else it won't save
  149. # the page state owned by a given hook. But no plugins are loaded
  150. # by this program, so populate %hooks with all hook ids that
  151. # currently have page state.
  152. foreach my $page (keys %pagemtime) {
  153. foreach my $id (keys %{$pagestate{$page}}) {
  154. $hooks{_dummy}{$id}=1;
  155. }
  156. }
  157. return close($in);
  158. }