4 use lib '.'; # For use in nonstandard directory, munged by Makefile.
10 \[\[(!?) # directive open; 2: optional prefix
12 ( # 4: the parameters (including initial whitespace)
15 (?:[-\w]+=)? # named parameter key?
17 """.*?""" # triple-quoted value
19 "[^"]+" # single-quoted value
21 [^\s\]]+ # unquoted value
23 \s* # whitespace or end
26 *) # 0 or more parameters
27 \]\] # directive closed
30 sub handle_directive {
33 my $directive = shift;
37 return "${escape}[[${prefix}${directive}${args}]]"
39 if ($directive =~ m/^(if|more|table|template|toggleable)$/) {
40 $args =~ s{$regex}{handle_directive($1, $2, $3, $4)}eg;
42 return "[[!${directive}${args}]]"
45 sub prefix_directives {
48 IkiWiki::loadplugins();
49 IkiWiki::checkconfig();
53 error "ikiwiki has not built this wiki yet, cannot transition";
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);
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";
76 if (! IkiWiki::saveindex()) {
77 die "failed to save indexdb\n"
79 if (! IkiWiki::loadindex()) {
80 die "transition failed, cannot load new indexdb\n";
82 if (! unlink("$config{wikistatedir}/index")) {
83 die "unlink failed: $!\n";
90 eval q{use IkiWiki::UserInfo};
91 eval q{use Authen::Passphrase::BlowfishCrypt};
93 error("ikiwiki-transition hashpassword: failed to load Authen::Passphrase, passwords not hashed");
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});
109 sub aggregateinternal {
111 require IkiWiki::Plugin::aggregate;
112 IkiWiki::checkconfig();
113 IkiWiki::Plugin::aggregate::migrate_to_internal();
120 IkiWiki::checkconfig();
122 # unpack old-format wrappers setting into new fields
125 foreach my $wrapper (@{$config{wrappers}}) {
126 if ($wrapper->{cgi}) {
128 die "don't know what to do with second cgi wrapper ".$wrapper->{wrapper}."\n";
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};
136 elsif ($config{rcs}) {
138 die "don't know what to do with second rcs wrapper ".$wrapper->{wrapper}."\n";
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};
147 die "don't know what to do with wrapper ".$wrapper->{wrapper}."\n";
151 IkiWiki::Setup::dump($setup);
158 IkiWiki::checkconfig();
160 eval q{use IkiWiki::UserInfo};
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);
181 my %banned=map { $_ => 1 } @{$config{banned_users}}, IkiWiki::get_banned_users();
182 $config{banned_users}=[sort keys %banned];
184 IkiWiki::Setup::dump($setup);
189 IkiWiki::loadplugins();
190 IkiWiki::checkconfig();
191 IkiWiki::loadindex();
192 foreach my $page (keys %links) {
194 $l{$_}=1 foreach @{$links{$page}};
195 $links{$page}=[keys %l]
197 IkiWiki::saveindex();
201 my $dirorsetup=shift;
203 if (! defined $dirorsetup) {
207 if (-d $dirorsetup) {
208 $config{wikistatedir}=$dirorsetup."/.ikiwiki";
210 elsif (-f $dirorsetup) {
211 loadsetup($dirorsetup);
214 error("ikiwiki-transition: $dirorsetup does not exist");
217 if (! -d $config{wikistatedir}) {
218 error("ikiwiki-transition: $config{wikistatedir} does not exist");
224 if (! defined $setup) {
228 require IkiWiki::Setup;
230 %config = IkiWiki::defaultconfig();
231 IkiWiki::Setup::load($setup);
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";
247 usage() unless @ARGV;
250 if ($mode eq 'prefix_directives') {
251 prefix_directives(@ARGV);
253 elsif ($mode eq 'hashpassword') {
256 elsif ($mode eq 'indexdb') {
259 elsif ($mode eq 'aggregateinternal') {
260 aggregateinternal(@ARGV);
262 elsif ($mode eq 'setupformat') {
265 elsif ($mode eq 'moveprefs') {
268 elsif ($mode eq 'deduplinks') {
277 # A slightly modified version of the old loadindex function.
279 %oldrenderedfiles=%pagectime=();
280 if (! $config{rebuild}) {
281 %pagesources=%pagemtime=%oldlinks=%links=%depends=
282 %destsources=%renderedfiles=%pagecase=%pagestate=();
284 open (my $in, "<", "$config{wikistatedir}/index") || return;
290 foreach my $i (split(/ /, $_)) {
291 my ($item, $val)=split(/=/, $i, 2);
292 push @{$items{$item}}, decode_entities($val);
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];
312 $oldrenderedfiles{$page}=[@{$items{dest}}];
313 $pagectime{$page}=$items{ctime}[0];
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;
329 # Used to be in IkiWiki/UserInfo, but only used here now.
330 sub get_banned_users () {
332 my $userinfo=userinfo_retrieve();
333 foreach my $user (keys %{$userinfo}) {
334 push @ret, $user if $userinfo->{$user}->{banned};
339 # Used to be in IkiWiki, but only used here (to migrate admin prefs into the
341 sub pagespec_merge ($$) {
345 return $a if $a eq $b;
346 return "($a) or ($b)";