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