summaryrefslogtreecommitdiff
path: root/ikiwiki-transition
blob: e42a5137c72f4ef403aeda4e117944e575928d6d (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. my $cgi_seen=0;
  115. my $rcs_seen=0;
  116. foreach my $wrapper (@{$config{wrappers}}) {
  117. if ($wrapper->{cgi}) {
  118. if ($cgi_seen) {
  119. die "don't know what to do with second cgi wrapper ".$wrapper->{wrapper}."\n";
  120. }
  121. $cgi_seen++;
  122. print "setting cgi_wrapper to ".$wrapper->{wrapper}."\n";
  123. $config{cgi_wrapper}=$wrapper->{wrapper};
  124. $config{cgi_wrappermode}=$wrapper->{wrappermode}
  125. if exists $wrapper->{wrappermode};
  126. }
  127. elsif ($config{rcs}) {
  128. if ($rcs_seen) {
  129. die "don't know what to do with second rcs wrapper ".$wrapper->{wrapper}."\n";
  130. }
  131. $rcs_seen++;
  132. print "setting $config{rcs}_wrapper to ".$wrapper->{wrapper}."\n";
  133. $config{$config{rcs}."_wrapper"}=$wrapper->{wrapper};
  134. $config{$config{rcs}."_wrappermode"}=$wrapper->{wrappermode}
  135. if exists $wrapper->{wrappermode};
  136. }
  137. else {
  138. die "don't know what to do with wrapper ".$wrapper->{wrapper}."\n";
  139. }
  140. }
  141. IkiWiki::Setup::dump($setup);
  142. }
  143. sub usage {
  144. print STDERR "Usage: ikiwiki-transition type ...\n";
  145. print STDERR "Currently supported transition subcommands:\n";
  146. print STDERR "\tprefix_directives file\n";
  147. print STDERR "\tindexdb srcdir\n";
  148. print STDERR "\thashpassword srcdir\n";
  149. print STDERR "\taggregateinternal setupfile\n";
  150. print STDERR "\tsetupformat setupfile\n";
  151. exit 1;
  152. }
  153. usage() unless @ARGV;
  154. my $mode=shift;
  155. if ($mode eq 'prefix_directives') {
  156. prefix_directives(@ARGV);
  157. }
  158. elsif ($mode eq 'hashpassword') {
  159. hashpassword(@ARGV);
  160. }
  161. elsif ($mode eq 'indexdb') {
  162. indexdb(@ARGV);
  163. }
  164. elsif ($mode eq 'aggregateinternal') {
  165. aggregateinternal(@ARGV);
  166. }
  167. elsif ($mode eq 'setupformat') {
  168. setupformat(@ARGV);
  169. }
  170. else {
  171. usage();
  172. }
  173. package IkiWiki;
  174. # A slightly modified version of the old loadindex function.
  175. sub oldloadindex {
  176. %oldrenderedfiles=%pagectime=();
  177. if (! $config{rebuild}) {
  178. %pagesources=%pagemtime=%oldlinks=%links=%depends=
  179. %destsources=%renderedfiles=%pagecase=%pagestate=();
  180. }
  181. open (my $in, "<", "$config{wikistatedir}/index") || return;
  182. while (<$in>) {
  183. chomp;
  184. my %items;
  185. $items{link}=[];
  186. $items{dest}=[];
  187. foreach my $i (split(/ /, $_)) {
  188. my ($item, $val)=split(/=/, $i, 2);
  189. push @{$items{$item}}, decode_entities($val);
  190. }
  191. next unless exists $items{src}; # skip bad lines for now
  192. my $page=pagename($items{src}[0]);
  193. if (! $config{rebuild}) {
  194. $pagesources{$page}=$items{src}[0];
  195. $pagemtime{$page}=$items{mtime}[0];
  196. $oldlinks{$page}=[@{$items{link}}];
  197. $links{$page}=[@{$items{link}}];
  198. $depends{$page}=$items{depends}[0] if exists $items{depends};
  199. $destsources{$_}=$page foreach @{$items{dest}};
  200. $renderedfiles{$page}=[@{$items{dest}}];
  201. $pagecase{lc $page}=$page;
  202. foreach my $k (grep /_/, keys %items) {
  203. my ($id, $key)=split(/_/, $k, 2);
  204. $pagestate{$page}{decode_entities($id)}{decode_entities($key)}=$items{$k}[0];
  205. }
  206. }
  207. $oldrenderedfiles{$page}=[@{$items{dest}}];
  208. $pagectime{$page}=$items{ctime}[0];
  209. }
  210. # saveindex relies on %hooks being populated, else it won't save
  211. # the page state owned by a given hook. But no plugins are loaded
  212. # by this program, so populate %hooks with all hook ids that
  213. # currently have page state.
  214. foreach my $page (keys %pagemtime) {
  215. foreach my $id (keys %{$pagestate{$page}}) {
  216. $hooks{_dummy}{$id}=1;
  217. }
  218. }
  219. return close($in);
  220. }