]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - ikiwiki-transition.in
Fixed upstream => done.
[git.ikiwiki.info.git] / ikiwiki-transition.in
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4 use lib '.'; # For use in nonstandard directory, munged by Makefile.
5 use IkiWiki;
6 use HTML::Entities;
8 my $regex = qr{
9         (\\?)           # 1: escape?
10         \[\[(!?)        # directive open; 2: optional prefix
11         ([-\w]+)        # 3: command
12         (               # 4: the parameters (including initial whitespace)
13         \s+
14                 (?:
15                         (?:[-\w]+=)?            # named parameter key?
16                         (?:
17                                 """.*?"""       # triple-quoted value
18                                 |
19                                 "[^"]+"         # single-quoted value
20                                 |
21                                 [^\s\]]+        # unquoted value
22                         )
23                         \s*                     # whitespace or end
24                                                 # of directive
25                 )
26         *)              # 0 or more parameters
27         \]\]            # directive closed
28 }sx;
30 sub handle_directive {
31         my $escape = shift;
32         my $prefix = shift;
33         my $directive = shift;
34         my $args = shift;
36         if (length $escape) {
37                 return "${escape}[[${prefix}${directive}${args}]]"
38         }
39         if ($directive =~ m/^(if|more|table|template|toggleable)$/) {
40                 $args =~ s{$regex}{handle_directive($1, $2, $3, $4)}eg;
41         }
42         return "[[!${directive}${args}]]"
43 }
45 sub prefix_directives {
46         loadsetup(shift);
48         IkiWiki::loadplugins();
49         IkiWiki::checkconfig();
50         IkiWiki::loadindex();
52         if (! %pagesources) {
53                 error "ikiwiki has not built this wiki yet, cannot transition";
54         }
56         foreach my $page (values %pagesources) {
57                 next unless defined pagetype($page) &&
58                             -f $config{srcdir}."/".$page;
59                 my $content=readfile($config{srcdir}."/".$page);
60                 my $oldcontent=$content;
61                 $content=~s{$regex}{handle_directive($1, $2, $3, $4)}eg;
62                 if ($oldcontent ne $content) {
63                         writefile($page, $config{srcdir}, $content);
64                 }
65         }
66 }
68 sub indexdb {
69         setstatedir(shift);
71         # Note: No lockwiki here because ikiwiki already locks it
72         # before calling this.  
73         if (! IkiWiki::oldloadindex()) {
74                 die "failed to load index\n";
75         }
76         if (! IkiWiki::saveindex()) {
77                 die "failed to save indexdb\n"
78         }
79         if (! IkiWiki::loadindex()) {
80                 die "transition failed, cannot load new indexdb\n";
81         }
82         if (! unlink("$config{wikistatedir}/index")) {
83                 die "unlink failed: $!\n";
84         }
85 }
87 sub hashpassword {
88         setstatedir(shift);
90         eval q{use IkiWiki::UserInfo};
91         eval q{use Authen::Passphrase::BlowfishCrypt};
92         if ($@) {
93                 error("ikiwiki-transition hashpassword: failed to load Authen::Passphrase, passwords not hashed");
94         }
96         IkiWiki::lockwiki();
97         IkiWiki::loadplugin("passwordauth");
98         my $userinfo = IkiWiki::userinfo_retrieve();
99         foreach my $user (keys %{$userinfo}) {
100                 if (ref $userinfo->{$user} &&
101                     exists $userinfo->{$user}->{password} &&
102                     length $userinfo->{$user}->{password} &&
103                     ! exists $userinfo->{$user}->{cryptpassword}) {
104                         IkiWiki::Plugin::passwordauth::setpassword($user, $userinfo->{$user}->{password});
105                 }
106         }
109 sub aggregateinternal {
110         loadsetup(shift);
111         require IkiWiki::Plugin::aggregate;
112         IkiWiki::checkconfig();
113         IkiWiki::Plugin::aggregate::migrate_to_internal();
116 sub setupformat {
117         my $setup=shift;
119         loadsetup($setup);
120         IkiWiki::checkconfig();
121         
122         # unpack old-format wrappers setting into new fields
123         my $cgi_seen=0;
124         my $rcs_seen=0;
125         foreach my $wrapper (@{$config{wrappers}}) {
126                 if ($wrapper->{cgi}) {
127                         if ($cgi_seen) {
128                                 die "don't know what to do with second cgi wrapper ".$wrapper->{wrapper}."\n";
129                         }
130                         $cgi_seen++;
131                         print "setting cgi_wrapper to ".$wrapper->{wrapper}."\n";
132                         $config{cgi_wrapper}=$wrapper->{wrapper};
133                         $config{cgi_wrappermode}=$wrapper->{wrappermode}
134                                 if exists $wrapper->{wrappermode};
135                 }
136                 elsif ($config{rcs}) {
137                         if ($rcs_seen) {
138                                 die "don't know what to do with second rcs wrapper ".$wrapper->{wrapper}."\n";
139                         }
140                         $rcs_seen++;
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         }
151         IkiWiki::Setup::dump($setup);
154 sub moveprefs {
155         my $setup=shift;
157         loadsetup($setup);
158         IkiWiki::checkconfig();
160         eval q{use IkiWiki::UserInfo};
161         error $@ if $@;
163         foreach my $field (qw{allowed_attachments locked_pages}) {
164                 my $orig=$config{$field};
165                 foreach my $admin (@{$config{adminuser}}) {
166                         my $a=IkiWiki::userinfo_get($admin, $field);
167                         if (defined $a && length $a &&
168                             # might already have been moved
169                             (! defined $orig || $a ne $orig)) {
170                                 if (defined $config{$field} &&
171                                     length $config{$field}) {
172                                         $config{$field}=IkiWiki::pagespec_merge($config{$field}, $a);
173                                 }
174                                 else {
175                                         $config{$field}=$a;
176                                 }
177                         }
178                 }
179         }
181         my %banned=map { $_ => 1 } @{$config{banned_users}}, IkiWiki::get_banned_users();
182         $config{banned_users}=[sort keys %banned];
184         IkiWiki::Setup::dump($setup);
187 sub deduplinks {
188         loadsetup(shift);
189         IkiWiki::loadplugins();
190         IkiWiki::checkconfig();
191         IkiWiki::loadindex();
192         foreach my $page (keys %links) {
193                 my %l;
194                 $l{$_}=1 foreach @{$links{$page}};
195                 $links{$page}=[keys %l]
196         }
197         IkiWiki::saveindex();
200 sub setstatedir {
201         my $dirorsetup=shift;
203         if (! defined $dirorsetup) {
204                 usage();                
205         }
207         if (-d $dirorsetup) {
208                 $config{wikistatedir}=$dirorsetup."/.ikiwiki";
209         }
210         elsif (-f $dirorsetup) {
211                 loadsetup($dirorsetup);
212         }
213         else {
214                 error("ikiwiki-transition: $dirorsetup does not exist");
215         }
217         if (! -d $config{wikistatedir}) {
218                 error("ikiwiki-transition: $config{wikistatedir} does not exist");
219         }
221         
222 sub loadsetup {
223         my $setup=shift;
224         if (! defined $setup) {
225                 usage();
226         }
228         require IkiWiki::Setup;
230         %config = IkiWiki::defaultconfig();
231         IkiWiki::Setup::load($setup);
234 sub usage {
235         print STDERR "Usage: ikiwiki-transition type ...\n";
236         print STDERR "Currently supported transition subcommands:\n";
237         print STDERR "\tprefix_directives setupfile ...\n";
238         print STDERR "\taggregateinternal setupfile\n";
239         print STDERR "\tsetupformat setupfile\n";
240         print STDERR "\tmoveprefs setupfile\n";
241         print STDERR "\thashpassword setupfile|srcdir\n";
242         print STDERR "\tindexdb setupfile|srcdir\n";
243         print STDERR "\tdeduplinks setupfile\n";
244         exit 1;
247 usage() unless @ARGV;
249 my $mode=shift;
250 if ($mode eq 'prefix_directives') {
251         prefix_directives(@ARGV);
253 elsif ($mode eq 'hashpassword') {
254         hashpassword(@ARGV);
256 elsif ($mode eq 'indexdb') {
257         indexdb(@ARGV);
259 elsif ($mode eq 'aggregateinternal') {
260         aggregateinternal(@ARGV);
262 elsif ($mode eq 'setupformat') {
263         setupformat(@ARGV);
265 elsif ($mode eq 'moveprefs') {
266         moveprefs(@ARGV);
268 elsif ($mode eq 'deduplinks') {
269         deduplinks(@ARGV);
271 else {
272         usage();
275 package IkiWiki;
277 # A slightly modified version of the old loadindex function.
278 sub oldloadindex {
279         %oldrenderedfiles=%pagectime=();
280         if (! $config{rebuild}) {
281                 %pagesources=%pagemtime=%oldlinks=%links=%depends=
282                         %destsources=%renderedfiles=%pagecase=%pagestate=();
283         }
284         open (my $in, "<", "$config{wikistatedir}/index") || return;
285         while (<$in>) {
286                 chomp;
287                 my %items;
288                 $items{link}=[];
289                 $items{dest}=[];
290                 foreach my $i (split(/ /, $_)) {
291                         my ($item, $val)=split(/=/, $i, 2);
292                         push @{$items{$item}}, decode_entities($val);
293                 }
295                 next unless exists $items{src}; # skip bad lines for now
297                 my $page=pagename($items{src}[0]);
298                 if (! $config{rebuild}) {
299                         $pagesources{$page}=$items{src}[0];
300                         $pagemtime{$page}=$items{mtime}[0];
301                         $oldlinks{$page}=[@{$items{link}}];
302                         $links{$page}=[@{$items{link}}];
303                         $depends{$page}={ $items{depends}[0] => $IkiWiki::DEPEND_CONTENT } if exists $items{depends};
304                         $destsources{$_}=$page foreach @{$items{dest}};
305                         $renderedfiles{$page}=[@{$items{dest}}];
306                         $pagecase{lc $page}=$page;
307                         foreach my $k (grep /_/, keys %items) {
308                                 my ($id, $key)=split(/_/, $k, 2);
309                                 $pagestate{$page}{decode_entities($id)}{decode_entities($key)}=$items{$k}[0];
310                         }
311                 }
312                 $oldrenderedfiles{$page}=[@{$items{dest}}];
313                 $pagectime{$page}=$items{ctime}[0];
314         }
316         # saveindex relies on %hooks being populated, else it won't save
317         # the page state owned by a given hook. But no plugins are loaded
318         # by this program, so populate %hooks with all hook ids that
319         # currently have page state.
320         foreach my $page (keys %pagemtime) {
321                 foreach my $id (keys %{$pagestate{$page}}) {
322                         $hooks{_dummy}{$id}=1;
323                 }
324         }
325         
326         return close($in);
329 # Used to be in IkiWiki/UserInfo, but only used here now.
330 sub get_banned_users () {
331         my @ret;
332         my $userinfo=userinfo_retrieve();
333         foreach my $user (keys %{$userinfo}) {
334                 push @ret, $user if $userinfo->{$user}->{banned};
335         }
336         return @ret;
339 # Used to be in IkiWiki, but only used here (to migrate admin prefs into the
340 # setup file) now.
341 sub pagespec_merge ($$) {
342         my $a=shift;
343         my $b=shift;
345         return $a if $a eq $b;
346         return "($a) or ($b)";