summaryrefslogtreecommitdiff
path: root/ikiwiki-transition
blob: 9a5dd1362b6ec936758bf8e1f904d5d90787d5cf (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 moveprefs {
  143. my $setup=shift;
  144. if (! defined $setup) {
  145. usage();
  146. }
  147. require IkiWiki::Setup;
  148. %config = IkiWiki::defaultconfig();
  149. IkiWiki::Setup::load($setup);
  150. IkiWiki::checkconfig();
  151. eval q{use IkiWiki::UserInfo};
  152. error $@ if $@;
  153. foreach my $field (qw{allowed_attachments locked_pages}) {
  154. my $orig=$config{$field};
  155. foreach my $admin (@{$config{adminuser}}) {
  156. my $a=IkiWiki::userinfo_get($admin, $field);
  157. if (defined $a && length $a &&
  158. $a ne $orig && # might already have been moved
  159. defined $config{$field} &&
  160. length $config{$field}) {
  161. $config{$field}=IkiWiki::pagespec_merge($config{$field}, $a);
  162. }
  163. }
  164. }
  165. my %banned=map { $_ => 1 } @{$config{banned_users}}, IkiWiki::get_banned_users();
  166. $config{banned_users}=[sort keys %banned];
  167. IkiWiki::Setup::dump($setup);
  168. }
  169. sub usage {
  170. print STDERR "Usage: ikiwiki-transition type ...\n";
  171. print STDERR "Currently supported transition subcommands:\n";
  172. print STDERR "\tprefix_directives file ...\n";
  173. print STDERR "\tindexdb srcdir\n";
  174. print STDERR "\thashpassword srcdir\n";
  175. print STDERR "\taggregateinternal setupfile\n";
  176. print STDERR "\tsetupformat setupfile\n";
  177. print STDERR "\tmoveprefs setupfile\n";
  178. exit 1;
  179. }
  180. usage() unless @ARGV;
  181. my $mode=shift;
  182. if ($mode eq 'prefix_directives') {
  183. prefix_directives(@ARGV);
  184. }
  185. elsif ($mode eq 'hashpassword') {
  186. hashpassword(@ARGV);
  187. }
  188. elsif ($mode eq 'indexdb') {
  189. indexdb(@ARGV);
  190. }
  191. elsif ($mode eq 'aggregateinternal') {
  192. aggregateinternal(@ARGV);
  193. }
  194. elsif ($mode eq 'setupformat') {
  195. setupformat(@ARGV);
  196. }
  197. elsif ($mode eq 'moveprefs') {
  198. moveprefs(@ARGV);
  199. }
  200. else {
  201. usage();
  202. }
  203. package IkiWiki;
  204. # A slightly modified version of the old loadindex function.
  205. sub oldloadindex {
  206. %oldrenderedfiles=%pagectime=();
  207. if (! $config{rebuild}) {
  208. %pagesources=%pagemtime=%oldlinks=%links=%depends=
  209. %destsources=%renderedfiles=%pagecase=%pagestate=();
  210. }
  211. open (my $in, "<", "$config{wikistatedir}/index") || return;
  212. while (<$in>) {
  213. chomp;
  214. my %items;
  215. $items{link}=[];
  216. $items{dest}=[];
  217. foreach my $i (split(/ /, $_)) {
  218. my ($item, $val)=split(/=/, $i, 2);
  219. push @{$items{$item}}, decode_entities($val);
  220. }
  221. next unless exists $items{src}; # skip bad lines for now
  222. my $page=pagename($items{src}[0]);
  223. if (! $config{rebuild}) {
  224. $pagesources{$page}=$items{src}[0];
  225. $pagemtime{$page}=$items{mtime}[0];
  226. $oldlinks{$page}=[@{$items{link}}];
  227. $links{$page}=[@{$items{link}}];
  228. $depends{$page}=$items{depends}[0] if exists $items{depends};
  229. $destsources{$_}=$page foreach @{$items{dest}};
  230. $renderedfiles{$page}=[@{$items{dest}}];
  231. $pagecase{lc $page}=$page;
  232. foreach my $k (grep /_/, keys %items) {
  233. my ($id, $key)=split(/_/, $k, 2);
  234. $pagestate{$page}{decode_entities($id)}{decode_entities($key)}=$items{$k}[0];
  235. }
  236. }
  237. $oldrenderedfiles{$page}=[@{$items{dest}}];
  238. $pagectime{$page}=$items{ctime}[0];
  239. }
  240. # saveindex relies on %hooks being populated, else it won't save
  241. # the page state owned by a given hook. But no plugins are loaded
  242. # by this program, so populate %hooks with all hook ids that
  243. # currently have page state.
  244. foreach my $page (keys %pagemtime) {
  245. foreach my $id (keys %{$pagestate{$page}}) {
  246. $hooks{_dummy}{$id}=1;
  247. }
  248. }
  249. return close($in);
  250. }
  251. # Used to be in IkiWiki/UserInfo, but only used here now.
  252. sub get_banned_users () {
  253. my @ret;
  254. my $userinfo=userinfo_retrieve();
  255. foreach my $user (keys %{$userinfo}) {
  256. push @ret, $user if $userinfo->{$user}->{banned};
  257. }
  258. return @ret;
  259. }
  260. 1