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