]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - ikiwiki-transition
add plugin safe/rebuild info (part 1 of 2)
[git.ikiwiki.info.git] / ikiwiki-transition
1 #!/usr/bin/perl -i
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         $/=undef; # process whole files at once
46         
47         while (<>) {
48                 s{$regex}{handle_directive($1, $2, $3, $4)}eg;
49                 print;
50         }
51 }
53 sub indexdb {
54         $config{wikistatedir}=shift()."/.ikiwiki";
56         if (! defined $config{wikistatedir}) {
57                 usage();                
58         }
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 }
76 sub hashpassword {
77         $config{wikistatedir}=shift()."/.ikiwiki";
79         if (! defined $config{wikistatedir}) {
80                 usage();                
81         }
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         }
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         }
102 sub aggregateinternal {
103         require IkiWiki::Setup;
104         require IkiWiki::Plugin::aggregate;
106         %config = (IkiWiki::defaultconfig(), IkiWiki::Setup::load(shift));
107         IkiWiki::checkconfig();
109         IkiWiki::Plugin::aggregate::migrate_to_internal();
111         print "... now add aggregateinternal => 1 to your .setup file\n";
114 sub usage {
115         print STDERR "Usage: ikiwiki-transition type ...\n";
116         print STDERR "Currently supported transition subcommands:\n";
117         print STDERR "  prefix_directives file\n";
118         print STDERR "  indexdb srcdir\n";
119         print STDERR "  hashpassword srcdir\n";
120         print STDERR "  aggregateinternal setupfile\n";
121         exit 1;
124 usage() unless @ARGV;
126 my $mode=shift;
127 if ($mode eq 'prefix_directives') {
128         prefix_directives(@ARGV);
130 elsif ($mode eq 'hashpassword') {
131         hashpassword(@ARGV);
133 elsif ($mode eq 'indexdb') {
134         indexdb(@ARGV);
136 elsif ($mode eq 'aggregateinternal') {
137         aggregateinternal(@ARGV);
139 else {
140         usage();
143 package IkiWiki;
145 # A slightly modified version of the old loadindex function.
146 sub oldloadindex {
147         %oldrenderedfiles=%pagectime=();
148         if (! $config{rebuild}) {
149                 %pagesources=%pagemtime=%oldlinks=%links=%depends=
150                         %destsources=%renderedfiles=%pagecase=%pagestate=();
151         }
152         open (my $in, "<", "$config{wikistatedir}/index") || return;
153         while (<$in>) {
154                 chomp;
155                 my %items;
156                 $items{link}=[];
157                 $items{dest}=[];
158                 foreach my $i (split(/ /, $_)) {
159                         my ($item, $val)=split(/=/, $i, 2);
160                         push @{$items{$item}}, decode_entities($val);
161                 }
163                 next unless exists $items{src}; # skip bad lines for now
165                 my $page=pagename($items{src}[0]);
166                 if (! $config{rebuild}) {
167                         $pagesources{$page}=$items{src}[0];
168                         $pagemtime{$page}=$items{mtime}[0];
169                         $oldlinks{$page}=[@{$items{link}}];
170                         $links{$page}=[@{$items{link}}];
171                         $depends{$page}=$items{depends}[0] if exists $items{depends};
172                         $destsources{$_}=$page foreach @{$items{dest}};
173                         $renderedfiles{$page}=[@{$items{dest}}];
174                         $pagecase{lc $page}=$page;
175                         foreach my $k (grep /_/, keys %items) {
176                                 my ($id, $key)=split(/_/, $k, 2);
177                                 $pagestate{$page}{decode_entities($id)}{decode_entities($key)}=$items{$k}[0];
178                         }
179                 }
180                 $oldrenderedfiles{$page}=[@{$items{dest}}];
181                 $pagectime{$page}=$items{ctime}[0];
182         }
184         # saveindex relies on %hooks being populated, else it won't save
185         # the page state owned by a given hook. But no plugins are loaded
186         # by this program, so populate %hooks with all hook ids that
187         # currently have page state.
188         foreach my $page (keys %pagemtime) {
189                 foreach my $id (keys %{$pagestate{$page}}) {
190                         $hooks{_dummy}{$id}=1;
191                 }
192         }
193         
194         return close($in);