5 use FindBin; use lib $FindBin::Bin; # For use in nonstandard directory, munged by Makefile.
11 \[\[(!?) # directive open; 2: optional prefix
13 ( # 4: the parameters (including initial whitespace)
16 (?:[-\w]+=)? # named parameter key?
18 """.*?""" # triple-quoted value
20 "[^"]+" # single-quoted value
22 [^\s\]]+ # unquoted value
24 \s* # whitespace or end
27 *) # 0 or more parameters
28 \]\] # directive closed
31 sub handle_directive {
34 my $directive = shift;
38 return "${escape}[[${prefix}${directive}${args}]]"
40 if ($directive =~ m/^(if|more|table|template|toggleable)$/) {
41 $args =~ s{$regex}{handle_directive($1, $2, $3, $4)}eg;
43 return "[[!${directive}${args}]]"
46 sub prefix_directives {
49 IkiWiki::loadplugins();
50 IkiWiki::checkconfig();
54 error "ikiwiki has not built this wiki yet, cannot transition";
57 foreach my $page (values %pagesources) {
58 next unless defined pagetype($page) &&
59 -f $config{srcdir}."/".$page;
60 my $content=readfile($config{srcdir}."/".$page);
61 my $oldcontent=$content;
62 $content=~s{$regex}{handle_directive($1, $2, $3, $4)}eg;
63 if ($oldcontent ne $content) {
64 writefile($page, $config{srcdir}, $content);
72 # Note: No lockwiki here because ikiwiki already locks it
73 # before calling this.
74 if (! IkiWiki::oldloadindex()) {
75 die "failed to load index\n";
77 if (! IkiWiki::saveindex()) {
78 die "failed to save indexdb\n"
80 if (! IkiWiki::loadindex()) {
81 die "transition failed, cannot load new indexdb\n";
83 if (! unlink("$config{wikistatedir}/index")) {
84 die "unlink failed: $!\n";
91 eval q{use IkiWiki::UserInfo};
92 eval q{use Authen::Passphrase::BlowfishCrypt};
94 error("ikiwiki-transition hashpassword: failed to load Authen::Passphrase, passwords not hashed");
98 IkiWiki::loadplugin("passwordauth");
99 my $userinfo = IkiWiki::userinfo_retrieve();
100 foreach my $user (keys %{$userinfo}) {
101 if (ref $userinfo->{$user} &&
102 exists $userinfo->{$user}->{password} &&
103 length $userinfo->{$user}->{password} &&
104 ! exists $userinfo->{$user}->{cryptpassword}) {
105 IkiWiki::Plugin::passwordauth::setpassword($user, $userinfo->{$user}->{password});
110 sub aggregateinternal {
112 require IkiWiki::Plugin::aggregate;
113 IkiWiki::checkconfig();
114 IkiWiki::Plugin::aggregate::migrate_to_internal();
121 IkiWiki::checkconfig();
123 # unpack old-format wrappers setting into new fields
126 foreach my $wrapper (@{$config{wrappers}}) {
127 if ($wrapper->{cgi}) {
129 die "don't know what to do with second cgi wrapper ".$wrapper->{wrapper}."\n";
132 print "setting cgi_wrapper to ".$wrapper->{wrapper}."\n";
133 $config{cgi_wrapper}=$wrapper->{wrapper};
134 $config{cgi_wrappermode}=$wrapper->{wrappermode}
135 if exists $wrapper->{wrappermode};
137 elsif ($config{rcs}) {
139 die "don't know what to do with second rcs wrapper ".$wrapper->{wrapper}."\n";
142 print "setting $config{rcs}_wrapper to ".$wrapper->{wrapper}."\n";
143 $config{$config{rcs}."_wrapper"}=$wrapper->{wrapper};
144 $config{$config{rcs}."_wrappermode"}=$wrapper->{wrappermode}
145 if exists $wrapper->{wrappermode};
148 die "don't know what to do with wrapper ".$wrapper->{wrapper}."\n";
152 IkiWiki::Setup::dump($setup);
159 IkiWiki::checkconfig();
161 eval q{use IkiWiki::UserInfo};
164 foreach my $field (qw{allowed_attachments locked_pages}) {
165 my $orig=$config{$field};
166 foreach my $admin (@{$config{adminuser}}) {
167 my $a=IkiWiki::userinfo_get($admin, $field);
168 if (defined $a && length $a &&
169 # might already have been moved
170 (! defined $orig || $a ne $orig)) {
171 if (defined $config{$field} &&
172 length $config{$field}) {
173 $config{$field}=IkiWiki::pagespec_merge($config{$field}, $a);
182 my %banned=map { $_ => 1 } @{$config{banned_users}}, IkiWiki::get_banned_users();
183 $config{banned_users}=[sort keys %banned];
185 IkiWiki::Setup::dump($setup);
190 IkiWiki::loadplugins();
191 IkiWiki::checkconfig();
192 IkiWiki::loadindex();
193 foreach my $page (keys %links) {
195 $l{$_}=1 foreach @{$links{$page}};
196 $links{$page}=[keys %l]
198 IkiWiki::saveindex();
202 my $dirorsetup=shift;
204 if (! defined $dirorsetup) {
208 if (-d $dirorsetup) {
209 $config{wikistatedir}=$dirorsetup."/.ikiwiki";
211 elsif (-f $dirorsetup) {
212 loadsetup($dirorsetup);
215 error("ikiwiki-transition: $dirorsetup does not exist");
218 if (! -d $config{wikistatedir}) {
219 error("ikiwiki-transition: $config{wikistatedir} does not exist");
225 if (! defined $setup) {
229 require IkiWiki::Setup;
231 %config = IkiWiki::defaultconfig();
232 IkiWiki::Setup::load($setup);
236 print STDERR "Usage: ikiwiki-transition type ...\n";
237 print STDERR "Currently supported transition subcommands:\n";
238 print STDERR "\tprefix_directives setupfile ...\n";
239 print STDERR "\taggregateinternal setupfile\n";
240 print STDERR "\tsetupformat setupfile\n";
241 print STDERR "\tmoveprefs setupfile\n";
242 print STDERR "\thashpassword setupfile|srcdir\n";
243 print STDERR "\tindexdb setupfile|srcdir\n";
244 print STDERR "\tdeduplinks setupfile\n";
248 usage() unless @ARGV;
251 if ($mode eq 'prefix_directives') {
252 prefix_directives(@ARGV);
254 elsif ($mode eq 'hashpassword') {
257 elsif ($mode eq 'indexdb') {
260 elsif ($mode eq 'aggregateinternal') {
261 aggregateinternal(@ARGV);
263 elsif ($mode eq 'setupformat') {
266 elsif ($mode eq 'moveprefs') {
269 elsif ($mode eq 'deduplinks') {
278 # A slightly modified version of the old loadindex function.
280 %oldrenderedfiles=%pagectime=();
281 if (! $config{rebuild}) {
282 %pagesources=%pagemtime=%oldlinks=%links=%depends=
283 %destsources=%renderedfiles=%pagecase=%pagestate=();
285 open (my $in, "<", "$config{wikistatedir}/index") || return;
291 foreach my $i (split(/ /, $_)) {
292 my ($item, $val)=split(/=/, $i, 2);
293 push @{$items{$item}}, decode_entities($val);
296 next unless exists $items{src}; # skip bad lines for now
298 my $page=pagename($items{src}[0]);
299 if (! $config{rebuild}) {
300 $pagesources{$page}=$items{src}[0];
301 $pagemtime{$page}=$items{mtime}[0];
302 $oldlinks{$page}=[@{$items{link}}];
303 $links{$page}=[@{$items{link}}];
304 $depends{$page}={ $items{depends}[0] => $IkiWiki::DEPEND_CONTENT } if exists $items{depends};
305 $destsources{$_}=$page foreach @{$items{dest}};
306 $renderedfiles{$page}=[@{$items{dest}}];
307 $pagecase{lc $page}=$page;
308 foreach my $k (grep /_/, keys %items) {
309 my ($id, $key)=split(/_/, $k, 2);
310 $pagestate{$page}{decode_entities($id)}{decode_entities($key)}=$items{$k}[0];
313 $oldrenderedfiles{$page}=[@{$items{dest}}];
314 $pagectime{$page}=$items{ctime}[0];
317 # saveindex relies on %hooks being populated, else it won't save
318 # the page state owned by a given hook. But no plugins are loaded
319 # by this program, so populate %hooks with all hook ids that
320 # currently have page state.
321 foreach my $page (keys %pagemtime) {
322 foreach my $id (keys %{$pagestate{$page}}) {
323 $hooks{_dummy}{$id}=1;
330 # Used to be in IkiWiki/UserInfo, but only used here now.
331 sub get_banned_users () {
333 my $userinfo=userinfo_retrieve();
334 foreach my $user (keys %{$userinfo}) {
335 push @ret, $user if $userinfo->{$user}->{banned};
340 # Used to be in IkiWiki, but only used here (to migrate admin prefs into the
342 sub pagespec_merge ($$) {
346 return $a if $a eq $b;
347 return "($a) or ($b)";