summaryrefslogtreecommitdiff
path: root/ikiwiki-transition
blob: b15d9f46b825a50ddd47bacff59ceb85c33edf2e (plain)
  1. #!/usr/bin/perl
  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. my $setup=shift;
  42. if (! defined $setup) {
  43. usage();
  44. }
  45. require IkiWiki::Setup;
  46. require IkiWiki::Plugin::aggregate;
  47. %config = IkiWiki::defaultconfig();
  48. IkiWiki::Setup::load($setup);
  49. IkiWiki::loadplugins();
  50. IkiWiki::checkconfig();
  51. IkiWiki::loadindex();
  52. if (! %pagesources) {
  53. error "ikiwiki has not built this wiki yet, cannot transition";
  54. }
  55. foreach my $page (values %pagesources) {
  56. next unless defined pagetype($page) &&
  57. -f $config{srcdir}."/".$page;
  58. my $content=readfile($config{srcdir}."/".$page);
  59. my $oldcontent=$content;
  60. $content=~s{$regex}{handle_directive($1, $2, $3, $4)}eg;
  61. if ($oldcontent ne $content) {
  62. writefile($page, $config{srcdir}, $content);
  63. }
  64. }
  65. }
  66. sub indexdb {
  67. my $dir=shift;
  68. if (! defined $dir) {
  69. usage();
  70. }
  71. $config{wikistatedir}=$dir."/.ikiwiki";
  72. # Note: No lockwiki here because ikiwiki already locks it
  73. # before calling this.
  74. if (! IkiWiki::oldloadindex()) {
  75. die "failed to load index\n";
  76. }
  77. if (! IkiWiki::saveindex()) {
  78. die "failed to save indexdb\n"
  79. }
  80. if (! IkiWiki::loadindex()) {
  81. die "transition failed, cannot load new indexdb\n";
  82. }
  83. if (! unlink("$config{wikistatedir}/index")) {
  84. die "unlink failed: $!\n";
  85. }
  86. }
  87. sub hashpassword {
  88. my $dir=shift;
  89. if (! defined $dir) {
  90. usage();
  91. }
  92. $config{wikistatedir}=$dir."/.ikiwiki";
  93. eval q{use IkiWiki::UserInfo};
  94. eval q{use Authen::Passphrase::BlowfishCrypt};
  95. if ($@) {
  96. error("ikiwiki-transition hashpassword: failed to load Authen::Passphrase, passwords not hashed");
  97. }
  98. IkiWiki::lockwiki();
  99. IkiWiki::loadplugin("passwordauth");
  100. my $userinfo = IkiWiki::userinfo_retrieve();
  101. foreach my $user (keys %{$userinfo}) {
  102. if (ref $userinfo->{$user} &&
  103. exists $userinfo->{$user}->{password} &&
  104. length $userinfo->{$user}->{password} &&
  105. ! exists $userinfo->{$user}->{cryptpassword}) {
  106. IkiWiki::Plugin::passwordauth::setpassword($user, $userinfo->{$user}->{password});
  107. }
  108. }
  109. }
  110. sub aggregateinternal {
  111. my $setup=shift;
  112. if (! defined $setup) {
  113. usage();
  114. }
  115. require IkiWiki::Setup;
  116. require IkiWiki::Plugin::aggregate;
  117. %config = IkiWiki::defaultconfig();
  118. IkiWiki::Setup::load($setup);
  119. IkiWiki::checkconfig();
  120. IkiWiki::Plugin::aggregate::migrate_to_internal();
  121. }
  122. sub setupformat {
  123. my $setup=shift;
  124. if (! defined $setup) {
  125. usage();
  126. }
  127. require IkiWiki::Setup;
  128. %config = IkiWiki::defaultconfig();
  129. IkiWiki::Setup::load($setup);
  130. IkiWiki::checkconfig();
  131. # unpack old-format wrappers setting into new fields
  132. my $cgi_seen=0;
  133. my $rcs_seen=0;
  134. foreach my $wrapper (@{$config{wrappers}}) {
  135. if ($wrapper->{cgi}) {
  136. if ($cgi_seen) {
  137. die "don't know what to do with second cgi wrapper ".$wrapper->{wrapper}."\n";
  138. }
  139. $cgi_seen++;
  140. print "setting cgi_wrapper to ".$wrapper->{wrapper}."\n";
  141. $config{cgi_wrapper}=$wrapper->{wrapper};
  142. $config{cgi_wrappermode}=$wrapper->{wrappermode}
  143. if exists $wrapper->{wrappermode};
  144. }
  145. elsif ($config{rcs}) {
  146. if ($rcs_seen) {
  147. die "don't know what to do with second rcs wrapper ".$wrapper->{wrapper}."\n";
  148. }
  149. $rcs_seen++;
  150. print "setting $config{rcs}_wrapper to ".$wrapper->{wrapper}."\n";
  151. $config{$config{rcs}."_wrapper"}=$wrapper->{wrapper};
  152. $config{$config{rcs}."_wrappermode"}=$wrapper->{wrappermode}
  153. if exists $wrapper->{wrappermode};
  154. }
  155. else {
  156. die "don't know what to do with wrapper ".$wrapper->{wrapper}."\n";
  157. }
  158. }
  159. IkiWiki::Setup::dump($setup);
  160. }
  161. sub moveprefs {
  162. my $setup=shift;
  163. if (! defined $setup) {
  164. usage();
  165. }
  166. require IkiWiki::Setup;
  167. %config = IkiWiki::defaultconfig();
  168. IkiWiki::Setup::load($setup);
  169. IkiWiki::checkconfig();
  170. eval q{use IkiWiki::UserInfo};
  171. error $@ if $@;
  172. foreach my $field (qw{allowed_attachments locked_pages}) {
  173. my $orig=$config{$field};
  174. foreach my $admin (@{$config{adminuser}}) {
  175. my $a=IkiWiki::userinfo_get($admin, $field);
  176. if (defined $a && length $a &&
  177. $a ne $orig && # might already have been moved
  178. defined $config{$field} &&
  179. length $config{$field}) {
  180. $config{$field}=IkiWiki::pagespec_merge($config{$field}, $a);
  181. }
  182. }
  183. }
  184. my %banned=map { $_ => 1 } @{$config{banned_users}}, IkiWiki::get_banned_users();
  185. $config{banned_users}=[sort keys %banned];
  186. IkiWiki::Setup::dump($setup);
  187. }
  188. sub usage {
  189. print STDERR "Usage: ikiwiki-transition type ...\n";
  190. print STDERR "Currently supported transition subcommands:\n";
  191. print STDERR "\tprefix_directives setupfile ...\n";
  192. print STDERR "\taggregateinternal setupfile\n";
  193. print STDERR "\tsetupformat setupfile\n";
  194. print STDERR "\tmoveprefs setupfile\n";
  195. print STDERR "\thashpassword srcdir\n";
  196. print STDERR "\tindexdb srcdir\n";
  197. exit 1;
  198. }
  199. usage() unless @ARGV;
  200. my $mode=shift;
  201. if ($mode eq 'prefix_directives') {
  202. prefix_directives(@ARGV);
  203. }
  204. elsif ($mode eq 'hashpassword') {
  205. hashpassword(@ARGV);
  206. }
  207. elsif ($mode eq 'indexdb') {
  208. indexdb(@ARGV);
  209. }
  210. elsif ($mode eq 'aggregateinternal') {
  211. aggregateinternal(@ARGV);
  212. }
  213. elsif ($mode eq 'setupformat') {
  214. setupformat(@ARGV);
  215. }
  216. elsif ($mode eq 'moveprefs') {
  217. moveprefs(@ARGV);
  218. }
  219. else {
  220. usage();
  221. }
  222. package IkiWiki;
  223. # A slightly modified version of the old loadindex function.
  224. sub oldloadindex {
  225. %oldrenderedfiles=%pagectime=();
  226. if (! $config{rebuild}) {
  227. %pagesources=%pagemtime=%oldlinks=%links=%depends=
  228. %destsources=%renderedfiles=%pagecase=%pagestate=();
  229. }
  230. open (my $in, "<", "$config{wikistatedir}/index") || return;
  231. while (<$in>) {
  232. chomp;
  233. my %items;
  234. $items{link}=[];
  235. $items{dest}=[];
  236. foreach my $i (split(/ /, $_)) {
  237. my ($item, $val)=split(/=/, $i, 2);
  238. push @{$items{$item}}, decode_entities($val);
  239. }
  240. next unless exists $items{src}; # skip bad lines for now
  241. my $page=pagename($items{src}[0]);
  242. if (! $config{rebuild}) {
  243. $pagesources{$page}=$items{src}[0];
  244. $pagemtime{$page}=$items{mtime}[0];
  245. $oldlinks{$page}=[@{$items{link}}];
  246. $links{$page}=[@{$items{link}}];
  247. $depends{$page}=$items{depends}[0] if exists $items{depends};
  248. $destsources{$_}=$page foreach @{$items{dest}};
  249. $renderedfiles{$page}=[@{$items{dest}}];
  250. $pagecase{lc $page}=$page;
  251. foreach my $k (grep /_/, keys %items) {
  252. my ($id, $key)=split(/_/, $k, 2);
  253. $pagestate{$page}{decode_entities($id)}{decode_entities($key)}=$items{$k}[0];
  254. }
  255. }
  256. $oldrenderedfiles{$page}=[@{$items{dest}}];
  257. $pagectime{$page}=$items{ctime}[0];
  258. }
  259. # saveindex relies on %hooks being populated, else it won't save
  260. # the page state owned by a given hook. But no plugins are loaded
  261. # by this program, so populate %hooks with all hook ids that
  262. # currently have page state.
  263. foreach my $page (keys %pagemtime) {
  264. foreach my $id (keys %{$pagestate{$page}}) {
  265. $hooks{_dummy}{$id}=1;
  266. }
  267. }
  268. return close($in);
  269. }
  270. # Used to be in IkiWiki/UserInfo, but only used here now.
  271. sub get_banned_users () {
  272. my @ret;
  273. my $userinfo=userinfo_retrieve();
  274. foreach my $user (keys %{$userinfo}) {
  275. push @ret, $user if $userinfo->{$user}->{banned};
  276. }
  277. return @ret;
  278. }
  279. 1