]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - ikiwiki-transition
Merge branch 'master' into next
[git.ikiwiki.info.git] / ikiwiki-transition
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4 use IkiWiki;
5 use HTML::Entities;
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;
29 sub handle_directive {
30         my $escape = shift;
31         my $prefix = shift;
32         my $directive = shift;
33         my $args = shift;
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 }
44 sub prefix_directives {
45         my $setup=shift;
46         if (! defined $setup) {
47                 usage();
48         }
50         require IkiWiki::Setup;
51         require IkiWiki::Plugin::aggregate;
53         %config = IkiWiki::defaultconfig();
54         IkiWiki::Setup::load($setup);
55         IkiWiki::loadplugins();
56         IkiWiki::checkconfig();
57         IkiWiki::loadindex();
59         if (! %pagesources) {
60                 error "ikiwiki has not built this wiki yet, cannot transition";
61         }
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 }
75 sub indexdb {
76         my $dir=shift;
77         if (! defined $dir) {
78                 usage();                
79         }
80         $config{wikistatedir}=$dir."/.ikiwiki";
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 }
98 sub hashpassword {
99         my $dir=shift;
100         if (! defined $dir) {
101                 usage();                
102         }
103         $config{wikistatedir}=$dir."/.ikiwiki";
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         }
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         }
124 sub aggregateinternal {
125         my $setup=shift;
126         if (! defined $setup) {
127                 usage();
128         }
130         require IkiWiki::Setup;
131         require IkiWiki::Plugin::aggregate;
133         %config = IkiWiki::defaultconfig();
134         IkiWiki::Setup::load($setup);
135         IkiWiki::checkconfig();
137         IkiWiki::Plugin::aggregate::migrate_to_internal();
140 sub setupformat {
141         my $setup=shift;
142         if (! defined $setup) {
143                 usage();
144         }
146         require IkiWiki::Setup;
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         }
181         IkiWiki::Setup::dump($setup);
184 sub moveprefs {
185         my $setup=shift;
186         if (! defined $setup) {
187                 usage();
188         }
190         require IkiWiki::Setup;
192         %config = IkiWiki::defaultconfig();
193         IkiWiki::Setup::load($setup);
194         IkiWiki::checkconfig();
196         eval q{use IkiWiki::UserInfo};
197         error $@ if $@;
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                             $a ne $orig && # might already have been moved
205                             defined $config{$field} &&
206                             length $config{$field}) {
207                                  $config{$field}=IkiWiki::pagespec_merge($config{$field}, $a);
208                         }
209                 }
210         }
212         my %banned=map { $_ => 1 } @{$config{banned_users}}, IkiWiki::get_banned_users();
213         $config{banned_users}=[sort keys %banned];
215         IkiWiki::Setup::dump($setup);
218 sub usage {
219         print STDERR "Usage: ikiwiki-transition type ...\n";
220         print STDERR "Currently supported transition subcommands:\n";
221         print STDERR "\tprefix_directives setupfile ...\n";
222         print STDERR "\taggregateinternal setupfile\n";
223         print STDERR "\tsetupformat setupfile\n";
224         print STDERR "\tmoveprefs setupfile\n";
225         print STDERR "\thashpassword srcdir\n";
226         print STDERR "\tindexdb srcdir\n";
227         exit 1;
230 usage() unless @ARGV;
232 my $mode=shift;
233 if ($mode eq 'prefix_directives') {
234         prefix_directives(@ARGV);
236 elsif ($mode eq 'hashpassword') {
237         hashpassword(@ARGV);
239 elsif ($mode eq 'indexdb') {
240         indexdb(@ARGV);
242 elsif ($mode eq 'aggregateinternal') {
243         aggregateinternal(@ARGV);
245 elsif ($mode eq 'setupformat') {
246         setupformat(@ARGV);
248 elsif ($mode eq 'moveprefs') {
249         moveprefs(@ARGV);
251 else {
252         usage();
255 package IkiWiki;
257 # A slightly modified version of the old loadindex function.
258 sub oldloadindex {
259         %oldrenderedfiles=%pagectime=();
260         if (! $config{rebuild}) {
261                 %pagesources=%pagemtime=%oldlinks=%links=%depends=
262                         %destsources=%renderedfiles=%pagecase=%pagestate=();
263         }
264         open (my $in, "<", "$config{wikistatedir}/index") || return;
265         while (<$in>) {
266                 chomp;
267                 my %items;
268                 $items{link}=[];
269                 $items{dest}=[];
270                 foreach my $i (split(/ /, $_)) {
271                         my ($item, $val)=split(/=/, $i, 2);
272                         push @{$items{$item}}, decode_entities($val);
273                 }
275                 next unless exists $items{src}; # skip bad lines for now
277                 my $page=pagename($items{src}[0]);
278                 if (! $config{rebuild}) {
279                         $pagesources{$page}=$items{src}[0];
280                         $pagemtime{$page}=$items{mtime}[0];
281                         $oldlinks{$page}=[@{$items{link}}];
282                         $links{$page}=[@{$items{link}}];
283                         $depends{$page}=$items{depends}[0] if exists $items{depends};
284                         $destsources{$_}=$page foreach @{$items{dest}};
285                         $renderedfiles{$page}=[@{$items{dest}}];
286                         $pagecase{lc $page}=$page;
287                         foreach my $k (grep /_/, keys %items) {
288                                 my ($id, $key)=split(/_/, $k, 2);
289                                 $pagestate{$page}{decode_entities($id)}{decode_entities($key)}=$items{$k}[0];
290                         }
291                 }
292                 $oldrenderedfiles{$page}=[@{$items{dest}}];
293                 $pagectime{$page}=$items{ctime}[0];
294         }
296         # saveindex relies on %hooks being populated, else it won't save
297         # the page state owned by a given hook. But no plugins are loaded
298         # by this program, so populate %hooks with all hook ids that
299         # currently have page state.
300         foreach my $page (keys %pagemtime) {
301                 foreach my $id (keys %{$pagestate{$page}}) {
302                         $hooks{_dummy}{$id}=1;
303                 }
304         }
305         
306         return close($in);
309 # Used to be in IkiWiki/UserInfo, but only used here now.
310 sub get_banned_users () {
311         my @ret;
312         my $userinfo=userinfo_retrieve();
313         foreach my $user (keys %{$userinfo}) {
314                 push @ret, $user if $userinfo->{$user}->{banned};
315         }
316         return @ret;