summaryrefslogtreecommitdiff
path: root/ikiwiki-transition
blob: 599261a093d798f818942e90577ff07a86d09a44 (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. # might already have been moved
  178. (! defined $orig || $a ne $orig)) {
  179. if (defined $config{$field} &&
  180. length $config{$field}) {
  181. $config{$field}=IkiWiki::pagespec_merge($config{$field}, $a);
  182. }
  183. else {
  184. $config{$field}=$a;
  185. }
  186. }
  187. }
  188. }
  189. my %banned=map { $_ => 1 } @{$config{banned_users}}, IkiWiki::get_banned_users();
  190. $config{banned_users}=[sort keys %banned];
  191. IkiWiki::Setup::dump($setup);
  192. }
  193. sub usage {
  194. print STDERR "Usage: ikiwiki-transition type ...\n";
  195. print STDERR "Currently supported transition subcommands:\n";
  196. print STDERR "\tprefix_directives setupfile ...\n";
  197. print STDERR "\taggregateinternal setupfile\n";
  198. print STDERR "\tsetupformat setupfile\n";
  199. print STDERR "\tmoveprefs setupfile\n";
  200. print STDERR "\thashpassword srcdir\n";
  201. print STDERR "\tindexdb srcdir\n";
  202. exit 1;
  203. }
  204. usage() unless @ARGV;
  205. my $mode=shift;
  206. if ($mode eq 'prefix_directives') {
  207. prefix_directives(@ARGV);
  208. }
  209. elsif ($mode eq 'hashpassword') {
  210. hashpassword(@ARGV);
  211. }
  212. elsif ($mode eq 'indexdb') {
  213. indexdb(@ARGV);
  214. }
  215. elsif ($mode eq 'aggregateinternal') {
  216. aggregateinternal(@ARGV);
  217. }
  218. elsif ($mode eq 'setupformat') {
  219. setupformat(@ARGV);
  220. }
  221. elsif ($mode eq 'moveprefs') {
  222. moveprefs(@ARGV);
  223. }
  224. else {
  225. usage();
  226. }
  227. package IkiWiki;
  228. # A slightly modified version of the old loadindex function.
  229. sub oldloadindex {
  230. %oldrenderedfiles=%pagectime=();
  231. if (! $config{rebuild}) {
  232. %pagesources=%pagemtime=%oldlinks=%links=%depends=
  233. %destsources=%renderedfiles=%pagecase=%pagestate=();
  234. }
  235. open (my $in, "<", "$config{wikistatedir}/index") || return;
  236. while (<$in>) {
  237. chomp;
  238. my %items;
  239. $items{link}=[];
  240. $items{dest}=[];
  241. foreach my $i (split(/ /, $_)) {
  242. my ($item, $val)=split(/=/, $i, 2);
  243. push @{$items{$item}}, decode_entities($val);
  244. }
  245. next unless exists $items{src}; # skip bad lines for now
  246. my $page=pagename($items{src}[0]);
  247. if (! $config{rebuild}) {
  248. $pagesources{$page}=$items{src}[0];
  249. $pagemtime{$page}=$items{mtime}[0];
  250. $oldlinks{$page}=[@{$items{link}}];
  251. $links{$page}=[@{$items{link}}];
  252. $depends{$page}=$items{depends}[0] if exists $items{depends};
  253. $destsources{$_}=$page foreach @{$items{dest}};
  254. $renderedfiles{$page}=[@{$items{dest}}];
  255. $pagecase{lc $page}=$page;
  256. foreach my $k (grep /_/, keys %items) {
  257. my ($id, $key)=split(/_/, $k, 2);
  258. $pagestate{$page}{decode_entities($id)}{decode_entities($key)}=$items{$k}[0];
  259. }
  260. }
  261. $oldrenderedfiles{$page}=[@{$items{dest}}];
  262. $pagectime{$page}=$items{ctime}[0];
  263. }
  264. # saveindex relies on %hooks being populated, else it won't save
  265. # the page state owned by a given hook. But no plugins are loaded
  266. # by this program, so populate %hooks with all hook ids that
  267. # currently have page state.
  268. foreach my $page (keys %pagemtime) {
  269. foreach my $id (keys %{$pagestate{$page}}) {
  270. $hooks{_dummy}{$id}=1;
  271. }
  272. }
  273. return close($in);
  274. }
  275. # Used to be in IkiWiki/UserInfo, but only used here now.
  276. sub get_banned_users () {
  277. my @ret;
  278. my $userinfo=userinfo_retrieve();
  279. foreach my $user (keys %{$userinfo}) {
  280. push @ret, $user if $userinfo->{$user}->{banned};
  281. }
  282. return @ret;
  283. }
  284. 1