]> sipb.mit.edu Git - ikiwiki.git/blob - ikiwiki-transition
a257347a1ad1da259ecb23ebc9d56f3269f62c5b
[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         print "... now add aggregateinternal => 1 to your .setup file\n";
118 }
119
120 sub setupformat {
121         my $setup=shift;
122         if (! defined $setup) {
123                 usage();
124         }
125
126         require IkiWiki::Setup;
127
128         %config = IkiWiki::defaultconfig();
129         IkiWiki::Setup::load($setup);
130         IkiWiki::checkconfig();
131         
132         # unpack old-format wrappers setting into new fields
133         foreach my $wrapper (@{$config{wrappers}}) {
134                 if ($wrapper->{cgi}) {
135                         print "setting cgi_wrapper to ".$wrapper->{wrapper}."\n";
136                         $config{cgi_wrapper}=$wrapper->{wrapper};
137                         $config{cgi_wrappermode}=$wrapper->{wrappermode}
138                                 if exists $wrapper->{wrappermode};
139                 }
140                 elsif ($config{rcs}) {
141                         print "setting $config{rcs}_wrapper to ".$wrapper->{wrapper}."\n";
142                         $config{$config{rcs}."_wrapper"}=$wrapper->{wrapper};
143                         $config{$config{rcs}."_wrappermode"}=$wrapper->{wrappermode}
144                                 if exists $wrapper->{wrappermode};
145                 }
146                 else {
147                         die "don't know what to do with wrapper ".$wrapper->{wrapper}."\n";
148                 }
149         }
150
151         IkiWiki::Setup::dump($setup);
152 }
153
154 sub usage {
155         print STDERR "Usage: ikiwiki-transition type ...\n";
156         print STDERR "Currently supported transition subcommands:\n";
157         print STDERR "\tprefix_directives file\n";
158         print STDERR "\tindexdb srcdir\n";
159         print STDERR "\thashpassword srcdir\n";
160         print STDERR "\taggregateinternal setupfile\n";
161         print STDERR "\tsetupformat setupfile\n";
162         exit 1;
163 }
164
165 usage() unless @ARGV;
166
167 my $mode=shift;
168 if ($mode eq 'prefix_directives') {
169         prefix_directives(@ARGV);
170 }
171 elsif ($mode eq 'hashpassword') {
172         hashpassword(@ARGV);
173 }
174 elsif ($mode eq 'indexdb') {
175         indexdb(@ARGV);
176 }
177 elsif ($mode eq 'aggregateinternal') {
178         aggregateinternal(@ARGV);
179 }
180 elsif ($mode eq 'setupformat') {
181         setupformat(@ARGV);
182 }
183 else {
184         usage();
185 }
186
187 package IkiWiki;
188
189 # A slightly modified version of the old loadindex function.
190 sub oldloadindex {
191         %oldrenderedfiles=%pagectime=();
192         if (! $config{rebuild}) {
193                 %pagesources=%pagemtime=%oldlinks=%links=%depends=
194                         %destsources=%renderedfiles=%pagecase=%pagestate=();
195         }
196         open (my $in, "<", "$config{wikistatedir}/index") || return;
197         while (<$in>) {
198                 chomp;
199                 my %items;
200                 $items{link}=[];
201                 $items{dest}=[];
202                 foreach my $i (split(/ /, $_)) {
203                         my ($item, $val)=split(/=/, $i, 2);
204                         push @{$items{$item}}, decode_entities($val);
205                 }
206
207                 next unless exists $items{src}; # skip bad lines for now
208
209                 my $page=pagename($items{src}[0]);
210                 if (! $config{rebuild}) {
211                         $pagesources{$page}=$items{src}[0];
212                         $pagemtime{$page}=$items{mtime}[0];
213                         $oldlinks{$page}=[@{$items{link}}];
214                         $links{$page}=[@{$items{link}}];
215                         $depends{$page}=$items{depends}[0] if exists $items{depends};
216                         $destsources{$_}=$page foreach @{$items{dest}};
217                         $renderedfiles{$page}=[@{$items{dest}}];
218                         $pagecase{lc $page}=$page;
219                         foreach my $k (grep /_/, keys %items) {
220                                 my ($id, $key)=split(/_/, $k, 2);
221                                 $pagestate{$page}{decode_entities($id)}{decode_entities($key)}=$items{$k}[0];
222                         }
223                 }
224                 $oldrenderedfiles{$page}=[@{$items{dest}}];
225                 $pagectime{$page}=$items{ctime}[0];
226         }
227
228         # saveindex relies on %hooks being populated, else it won't save
229         # the page state owned by a given hook. But no plugins are loaded
230         # by this program, so populate %hooks with all hook ids that
231         # currently have page state.
232         foreach my $page (keys %pagemtime) {
233                 foreach my $id (keys %{$pagestate{$page}}) {
234                         $hooks{_dummy}{$id}=1;
235                 }
236         }
237         
238         return close($in);
239 }