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