summaryrefslogtreecommitdiff
path: root/ikiwiki-transition
blob: 6524297ee6e0fa00749210a5593e70290b3913a4 (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 aggregateinternal {
  90. $config{srcdir} = shift;
  91. $config{wikistatedir} = $config{srcdir}."/.ikiwiki";
  92. $config{htmlext} = (shift or "html");
  93. require IkiWiki::Plugin::aggregate;
  94. IkiWiki::Plugin::aggregate::migrate_to_internal();
  95. print "... now add aggregateinternal => 1 to your .setup file\n";
  96. }
  97. sub usage {
  98. print STDERR "Usage: ikiwiki-transition type ...\n";
  99. print STDERR "Currently supported transition subcommands:\n";
  100. print STDERR " prefix_directives file\n";
  101. print STDERR " indexdb srcdir\n";
  102. print STDERR " hashpassword srcdir\n";
  103. print STDERR " aggregateinternal srcdir [htmlext]\n";
  104. exit 1;
  105. }
  106. usage() unless @ARGV;
  107. my $mode=shift;
  108. if ($mode eq 'prefix_directives') {
  109. prefix_directives(@ARGV);
  110. }
  111. elsif ($mode eq 'hashpassword') {
  112. hashpassword(@ARGV);
  113. }
  114. elsif ($mode eq 'indexdb') {
  115. indexdb(@ARGV);
  116. }
  117. elsif ($mode eq 'aggregateinternal') {
  118. aggregateinternal(@ARGV);
  119. }
  120. else {
  121. usage();
  122. }
  123. package IkiWiki;
  124. # A slightly modified version of the old loadindex function.
  125. sub oldloadindex {
  126. %oldrenderedfiles=%pagectime=();
  127. if (! $config{rebuild}) {
  128. %pagesources=%pagemtime=%oldlinks=%links=%depends=
  129. %destsources=%renderedfiles=%pagecase=%pagestate=();
  130. }
  131. open (my $in, "<", "$config{wikistatedir}/index") || return;
  132. while (<$in>) {
  133. chomp;
  134. my %items;
  135. $items{link}=[];
  136. $items{dest}=[];
  137. foreach my $i (split(/ /, $_)) {
  138. my ($item, $val)=split(/=/, $i, 2);
  139. push @{$items{$item}}, decode_entities($val);
  140. }
  141. next unless exists $items{src}; # skip bad lines for now
  142. my $page=pagename($items{src}[0]);
  143. if (! $config{rebuild}) {
  144. $pagesources{$page}=$items{src}[0];
  145. $pagemtime{$page}=$items{mtime}[0];
  146. $oldlinks{$page}=[@{$items{link}}];
  147. $links{$page}=[@{$items{link}}];
  148. $depends{$page}=$items{depends}[0] if exists $items{depends};
  149. $destsources{$_}=$page foreach @{$items{dest}};
  150. $renderedfiles{$page}=[@{$items{dest}}];
  151. $pagecase{lc $page}=$page;
  152. foreach my $k (grep /_/, keys %items) {
  153. my ($id, $key)=split(/_/, $k, 2);
  154. $pagestate{$page}{decode_entities($id)}{decode_entities($key)}=$items{$k}[0];
  155. }
  156. }
  157. $oldrenderedfiles{$page}=[@{$items{dest}}];
  158. $pagectime{$page}=$items{ctime}[0];
  159. }
  160. # saveindex relies on %hooks being populated, else it won't save
  161. # the page state owned by a given hook. But no plugins are loaded
  162. # by this program, so populate %hooks with all hook ids that
  163. # currently have page state.
  164. foreach my $page (keys %pagemtime) {
  165. foreach my $id (keys %{$pagestate{$page}}) {
  166. $hooks{_dummy}{$id}=1;
  167. }
  168. }
  169. return close($in);
  170. }