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