]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - ikiwiki-transition
a development on the topic of defining classes of pages
[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         setstatedir(shift);
78         # Note: No lockwiki here because ikiwiki already locks it
79         # before calling this.  
80         if (! IkiWiki::oldloadindex()) {
81                 die "failed to load index\n";
82         }
83         if (! IkiWiki::saveindex()) {
84                 die "failed to save indexdb\n"
85         }
86         if (! IkiWiki::loadindex()) {
87                 die "transition failed, cannot load new indexdb\n";
88         }
89         if (! unlink("$config{wikistatedir}/index")) {
90                 die "unlink failed: $!\n";
91         }
92 }
94 sub hashpassword {
95         setstatedir(shift);
97         eval q{use IkiWiki::UserInfo};
98         eval q{use Authen::Passphrase::BlowfishCrypt};
99         if ($@) {
100                 error("ikiwiki-transition hashpassword: failed to load Authen::Passphrase, passwords not hashed");
101         }
103         IkiWiki::lockwiki();
104         IkiWiki::loadplugin("passwordauth");
105         my $userinfo = IkiWiki::userinfo_retrieve();
106         foreach my $user (keys %{$userinfo}) {
107                 if (ref $userinfo->{$user} &&
108                     exists $userinfo->{$user}->{password} &&
109                     length $userinfo->{$user}->{password} &&
110                     ! exists $userinfo->{$user}->{cryptpassword}) {
111                         IkiWiki::Plugin::passwordauth::setpassword($user, $userinfo->{$user}->{password});
112                 }
113         }
116 sub aggregateinternal {
117         my $setup=shift;
118         if (! defined $setup) {
119                 usage();
120         }
122         require IkiWiki::Setup;
123         require IkiWiki::Plugin::aggregate;
125         %config = IkiWiki::defaultconfig();
126         IkiWiki::Setup::load($setup);
127         IkiWiki::checkconfig();
129         IkiWiki::Plugin::aggregate::migrate_to_internal();
132 sub setupformat {
133         my $setup=shift;
134         if (! defined $setup) {
135                 usage();
136         }
138         require IkiWiki::Setup;
140         %config = IkiWiki::defaultconfig();
141         IkiWiki::Setup::load($setup);
142         IkiWiki::checkconfig();
143         
144         # unpack old-format wrappers setting into new fields
145         my $cgi_seen=0;
146         my $rcs_seen=0;
147         foreach my $wrapper (@{$config{wrappers}}) {
148                 if ($wrapper->{cgi}) {
149                         if ($cgi_seen) {
150                                 die "don't know what to do with second cgi wrapper ".$wrapper->{wrapper}."\n";
151                         }
152                         $cgi_seen++;
153                         print "setting cgi_wrapper to ".$wrapper->{wrapper}."\n";
154                         $config{cgi_wrapper}=$wrapper->{wrapper};
155                         $config{cgi_wrappermode}=$wrapper->{wrappermode}
156                                 if exists $wrapper->{wrappermode};
157                 }
158                 elsif ($config{rcs}) {
159                         if ($rcs_seen) {
160                                 die "don't know what to do with second rcs wrapper ".$wrapper->{wrapper}."\n";
161                         }
162                         $rcs_seen++;
163                         print "setting $config{rcs}_wrapper to ".$wrapper->{wrapper}."\n";
164                         $config{$config{rcs}."_wrapper"}=$wrapper->{wrapper};
165                         $config{$config{rcs}."_wrappermode"}=$wrapper->{wrappermode}
166                                 if exists $wrapper->{wrappermode};
167                 }
168                 else {
169                         die "don't know what to do with wrapper ".$wrapper->{wrapper}."\n";
170                 }
171         }
173         IkiWiki::Setup::dump($setup);
176 sub moveprefs {
177         my $setup=shift;
178         if (! defined $setup) {
179                 usage();
180         }
182         require IkiWiki::Setup;
184         %config = IkiWiki::defaultconfig();
185         IkiWiki::Setup::load($setup);
186         IkiWiki::checkconfig();
188         eval q{use IkiWiki::UserInfo};
189         error $@ if $@;
191         foreach my $field (qw{allowed_attachments locked_pages}) {
192                 my $orig=$config{$field};
193                 foreach my $admin (@{$config{adminuser}}) {
194                         my $a=IkiWiki::userinfo_get($admin, $field);
195                         if (defined $a && length $a &&
196                             # might already have been moved
197                             (! defined $orig || $a ne $orig)) {
198                                 if (defined $config{$field} &&
199                                     length $config{$field}) {
200                                         $config{$field}=IkiWiki::pagespec_merge($config{$field}, $a);
201                                 }
202                                 else {
203                                         $config{$field}=$a;
204                                 }
205                         }
206                 }
207         }
209         my %banned=map { $_ => 1 } @{$config{banned_users}}, IkiWiki::get_banned_users();
210         $config{banned_users}=[sort keys %banned];
212         IkiWiki::Setup::dump($setup);
215 sub deduplinks {
216         setstatdir(shift);
217         IkiWiki::loadindex();
218         foreach my $page (keys %links) {
219                 my %l;
220                 $l{$_}=1 foreach @{$links{$page}};
221                 $links{$page}=[keys %l]
222         }
223         IkiWiki::saveindex();
226 sub setstatedir {
227         my $dir=shift;
229         if (! defined $dir) {
230                 usage();                
231         }
233         if (! -d $dir) {
234                 error("ikiwiki-transition: $dir does not exist");
235         }
237         $config{wikistatedir}=$dir."/.ikiwiki";
239         if (! -d $config{wikistatedir}) {
240                 error("ikiwiki-transition: $config{wikistatedir} does not exist");
241         }
244 sub usage {
245         print STDERR "Usage: ikiwiki-transition type ...\n";
246         print STDERR "Currently supported transition subcommands:\n";
247         print STDERR "\tprefix_directives setupfile ...\n";
248         print STDERR "\taggregateinternal setupfile\n";
249         print STDERR "\tsetupformat setupfile\n";
250         print STDERR "\tmoveprefs setupfile\n";
251         print STDERR "\thashpassword srcdir\n";
252         print STDERR "\tindexdb srcdir\n";
253         print STDERR "\tdeduplinks srcdir\n";
254         exit 1;
257 usage() unless @ARGV;
259 my $mode=shift;
260 if ($mode eq 'prefix_directives') {
261         prefix_directives(@ARGV);
263 elsif ($mode eq 'hashpassword') {
264         hashpassword(@ARGV);
266 elsif ($mode eq 'indexdb') {
267         indexdb(@ARGV);
269 elsif ($mode eq 'aggregateinternal') {
270         aggregateinternal(@ARGV);
272 elsif ($mode eq 'setupformat') {
273         setupformat(@ARGV);
275 elsif ($mode eq 'moveprefs') {
276         moveprefs(@ARGV);
278 elsif ($mode eq 'deduplinks') {
279         deduplinks(@ARGV);
281 else {
282         usage();
285 package IkiWiki;
287 # A slightly modified version of the old loadindex function.
288 sub oldloadindex {
289         %oldrenderedfiles=%pagectime=();
290         if (! $config{rebuild}) {
291                 %pagesources=%pagemtime=%oldlinks=%links=%depends=
292                         %destsources=%renderedfiles=%pagecase=%pagestate=();
293         }
294         open (my $in, "<", "$config{wikistatedir}/index") || return;
295         while (<$in>) {
296                 chomp;
297                 my %items;
298                 $items{link}=[];
299                 $items{dest}=[];
300                 foreach my $i (split(/ /, $_)) {
301                         my ($item, $val)=split(/=/, $i, 2);
302                         push @{$items{$item}}, decode_entities($val);
303                 }
305                 next unless exists $items{src}; # skip bad lines for now
307                 my $page=pagename($items{src}[0]);
308                 if (! $config{rebuild}) {
309                         $pagesources{$page}=$items{src}[0];
310                         $pagemtime{$page}=$items{mtime}[0];
311                         $oldlinks{$page}=[@{$items{link}}];
312                         $links{$page}=[@{$items{link}}];
313                         $depends{$page}=$items{depends}[0] if exists $items{depends};
314                         $destsources{$_}=$page foreach @{$items{dest}};
315                         $renderedfiles{$page}=[@{$items{dest}}];
316                         $pagecase{lc $page}=$page;
317                         foreach my $k (grep /_/, keys %items) {
318                                 my ($id, $key)=split(/_/, $k, 2);
319                                 $pagestate{$page}{decode_entities($id)}{decode_entities($key)}=$items{$k}[0];
320                         }
321                 }
322                 $oldrenderedfiles{$page}=[@{$items{dest}}];
323                 $pagectime{$page}=$items{ctime}[0];
324         }
326         # saveindex relies on %hooks being populated, else it won't save
327         # the page state owned by a given hook. But no plugins are loaded
328         # by this program, so populate %hooks with all hook ids that
329         # currently have page state.
330         foreach my $page (keys %pagemtime) {
331                 foreach my $id (keys %{$pagestate{$page}}) {
332                         $hooks{_dummy}{$id}=1;
333                 }
334         }
335         
336         return close($in);
339 # Used to be in IkiWiki/UserInfo, but only used here now.
340 sub get_banned_users () {
341         my @ret;
342         my $userinfo=userinfo_retrieve();
343         foreach my $user (keys %{$userinfo}) {
344                 push @ret, $user if $userinfo->{$user}->{banned};
345         }
346         return @ret;