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