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