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