summaryrefslogtreecommitdiff
path: root/ikiwiki-transition
blob: a257347a1ad1da259ecb23ebc9d56f3269f62c5b (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. my $dir=shift;
  49. if (! defined $dir) {
  50. usage();
  51. }
  52. $config{wikistatedir}=$dir."/.ikiwiki";
  53. # Note: No lockwiki here because ikiwiki already locks it
  54. # before calling this.
  55. if (! IkiWiki::oldloadindex()) {
  56. die "failed to load index\n";
  57. }
  58. if (! IkiWiki::saveindex()) {
  59. die "failed to save indexdb\n"
  60. }
  61. if (! IkiWiki::loadindex()) {
  62. die "transition failed, cannot load new indexdb\n";
  63. }
  64. if (! unlink("$config{wikistatedir}/index")) {
  65. die "unlink failed: $!\n";
  66. }
  67. }
  68. sub hashpassword {
  69. my $dir=shift;
  70. if (! defined $dir) {
  71. usage();
  72. }
  73. $config{wikistatedir}=$dir."/.ikiwiki";
  74. eval q{use IkiWiki::UserInfo};
  75. eval q{use Authen::Passphrase::BlowfishCrypt};
  76. if ($@) {
  77. error("ikiwiki-transition hashpassword: failed to load Authen::Passphrase, passwords not hashed");
  78. }
  79. IkiWiki::lockwiki();
  80. IkiWiki::loadplugin("passwordauth");
  81. my $userinfo = IkiWiki::userinfo_retrieve();
  82. foreach my $user (keys %{$userinfo}) {
  83. if (ref $userinfo->{$user} &&
  84. exists $userinfo->{$user}->{password} &&
  85. length $userinfo->{$user}->{password} &&
  86. ! exists $userinfo->{$user}->{cryptpassword}) {
  87. IkiWiki::Plugin::passwordauth::setpassword($user, $userinfo->{$user}->{password});
  88. }
  89. }
  90. }
  91. sub aggregateinternal {
  92. my $setup=shift;
  93. if (! defined $setup) {
  94. usage();
  95. }
  96. require IkiWiki::Setup;
  97. require IkiWiki::Plugin::aggregate;
  98. %config = IkiWiki::defaultconfig();
  99. IkiWiki::Setup::load();
  100. IkiWiki::checkconfig();
  101. IkiWiki::Plugin::aggregate::migrate_to_internal();
  102. print "... now add aggregateinternal => 1 to your .setup file\n";
  103. }
  104. sub setupformat {
  105. my $setup=shift;
  106. if (! defined $setup) {
  107. usage();
  108. }
  109. require IkiWiki::Setup;
  110. %config = IkiWiki::defaultconfig();
  111. IkiWiki::Setup::load($setup);
  112. IkiWiki::checkconfig();
  113. # unpack old-format wrappers setting into new fields
  114. foreach my $wrapper (@{$config{wrappers}}) {
  115. if ($wrapper->{cgi}) {
  116. print "setting cgi_wrapper to ".$wrapper->{wrapper}."\n";
  117. $config{cgi_wrapper}=$wrapper->{wrapper};
  118. $config{cgi_wrappermode}=$wrapper->{wrappermode}
  119. if exists $wrapper->{wrappermode};
  120. }
  121. elsif ($config{rcs}) {
  122. print "setting $config{rcs}_wrapper to ".$wrapper->{wrapper}."\n";
  123. $config{$config{rcs}."_wrapper"}=$wrapper->{wrapper};
  124. $config{$config{rcs}."_wrappermode"}=$wrapper->{wrappermode}
  125. if exists $wrapper->{wrappermode};
  126. }
  127. else {
  128. die "don't know what to do with wrapper ".$wrapper->{wrapper}."\n";
  129. }
  130. }
  131. IkiWiki::Setup::dump($setup);
  132. }
  133. sub usage {
  134. print STDERR "Usage: ikiwiki-transition type ...\n";
  135. print STDERR "Currently supported transition subcommands:\n";
  136. print STDERR "\tprefix_directives file\n";
  137. print STDERR "\tindexdb srcdir\n";
  138. print STDERR "\thashpassword srcdir\n";
  139. print STDERR "\taggregateinternal setupfile\n";
  140. print STDERR "\tsetupformat setupfile\n";
  141. exit 1;
  142. }
  143. usage() unless @ARGV;
  144. my $mode=shift;
  145. if ($mode eq 'prefix_directives') {
  146. prefix_directives(@ARGV);
  147. }
  148. elsif ($mode eq 'hashpassword') {
  149. hashpassword(@ARGV);
  150. }
  151. elsif ($mode eq 'indexdb') {
  152. indexdb(@ARGV);
  153. }
  154. elsif ($mode eq 'aggregateinternal') {
  155. aggregateinternal(@ARGV);
  156. }
  157. elsif ($mode eq 'setupformat') {
  158. setupformat(@ARGV);
  159. }
  160. else {
  161. usage();
  162. }
  163. package IkiWiki;
  164. # A slightly modified version of the old loadindex function.
  165. sub oldloadindex {
  166. %oldrenderedfiles=%pagectime=();
  167. if (! $config{rebuild}) {
  168. %pagesources=%pagemtime=%oldlinks=%links=%depends=
  169. %destsources=%renderedfiles=%pagecase=%pagestate=();
  170. }
  171. open (my $in, "<", "$config{wikistatedir}/index") || return;
  172. while (<$in>) {
  173. chomp;
  174. my %items;
  175. $items{link}=[];
  176. $items{dest}=[];
  177. foreach my $i (split(/ /, $_)) {
  178. my ($item, $val)=split(/=/, $i, 2);
  179. push @{$items{$item}}, decode_entities($val);
  180. }
  181. next unless exists $items{src}; # skip bad lines for now
  182. my $page=pagename($items{src}[0]);
  183. if (! $config{rebuild}) {
  184. $pagesources{$page}=$items{src}[0];
  185. $pagemtime{$page}=$items{mtime}[0];
  186. $oldlinks{$page}=[@{$items{link}}];
  187. $links{$page}=[@{$items{link}}];
  188. $depends{$page}=$items{depends}[0] if exists $items{depends};
  189. $destsources{$_}=$page foreach @{$items{dest}};
  190. $renderedfiles{$page}=[@{$items{dest}}];
  191. $pagecase{lc $page}=$page;
  192. foreach my $k (grep /_/, keys %items) {
  193. my ($id, $key)=split(/_/, $k, 2);
  194. $pagestate{$page}{decode_entities($id)}{decode_entities($key)}=$items{$k}[0];
  195. }
  196. }
  197. $oldrenderedfiles{$page}=[@{$items{dest}}];
  198. $pagectime{$page}=$items{ctime}[0];
  199. }
  200. # saveindex relies on %hooks being populated, else it won't save
  201. # the page state owned by a given hook. But no plugins are loaded
  202. # by this program, so populate %hooks with all hook ids that
  203. # currently have page state.
  204. foreach my $page (keys %pagemtime) {
  205. foreach my $id (keys %{$pagestate{$page}}) {
  206. $hooks{_dummy}{$id}=1;
  207. }
  208. }
  209. return close($in);
  210. }