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