]> sipb.mit.edu Git - ikiwiki.git/blobdiff - IkiWiki/Plugin/po.pm
po: initial infrastructure to deal with renamed master page
[ikiwiki.git] / IkiWiki / Plugin / po.pm
index 7039f3033f709dcc36ad771012de3298f591ebc7..d4280853feeb7793236b2ca11fea87f2afe671f0 100644 (file)
@@ -35,6 +35,8 @@ sub import { #{{{
        hook(type => "filter", id => "po", call => \&filter);
        hook(type => "htmlize", id => "po", call => \&htmlize);
        hook(type => "pagetemplate", id => "po", call => \&pagetemplate, last => 1);
+       hook(type => "renamepage", id => "po", call => \&renamepage);
+       hook(type => "delete", id => "po", call => \&mydelete);
        hook(type => "change", id => "po", call => \&change);
        hook(type => "editcontent", id => "po", call => \&editcontent);
 
@@ -325,30 +327,65 @@ sub pagetemplate (@) { #{{{
        }
 } # }}}
 
+# Save information about master page rename, so that:
+# - our delete hook can ignore the translations not renamed already
+# - our change hook can rename the translations accordingly.
+#
+# FIXME:
+# This hook is called once per page linking to the old page, which
+# means our delete hook won't know it should not delete a renamed orphan
+# page's translation.
+#
+# Moreover, we can't recognize such pages at delete stage:
+# existing links are fixed in the renaming process, so every
+# renamed page's old location will be an orphan anyway at this time.
+sub renamepage(@) { #{{{
+       my %params=@_;
+       my $oldpage=$params{oldpage};
+       my $newpage=$params{newpage};
+
+       setrenamed($oldpage, $newpage) if istranslatable($oldpage);
+       return $params{content};
+} #}}}
+
+sub mydelete(@) { #{{{
+       my @deleted=@_;
+
+       map {
+               deletetranslations($_);
+       } grep { istranslatablefile($_) && ! renamed(pagename($_))} @deleted;
+} #}}}
+
 sub change(@) { #{{{
        my @rendered=@_;
 
+       my $eachrenamed=eachrenamed();
+       while (my ($oldpage, $newpage) = $eachrenamed->()) {
+               renametranslations($oldpage, $newpage);
+       }
+
        my $updated_po_files=0;
 
        # Refresh/create POT and PO files as needed.
-       foreach my $page (map pagename($_), @rendered) {
-               next unless istranslatable($page);
-               my $file=srcfile($pagesources{$page});
+       foreach my $file (@rendered) {
+               next unless istranslatablefile($file);
+               my $page=pagename($file);
+               my $masterfile=srcfile($file);
                my $updated_pot_file=0;
                # Only refresh Pot file if it does not exist, or if
                # $pagesources{$page} was changed: don't if only the HTML was
                # refreshed, e.g. because of a dependency.
                if ((grep { $_ eq $pagesources{$page} } @origneedsbuild)
-                   || ! -e potfile($file)) {
-                       refreshpot($file);
+                   || ! -e potfile($masterfile)) {
+                       refreshpot($masterfile);
                        $updated_pot_file=1;
                }
                my @pofiles;
                map {
                        push @pofiles, $_ if ($updated_pot_file || ! -e $_);
-               } (pofiles($file));
+               } (pofiles($masterfile));
                if (@pofiles) {
-                       refreshpofiles($file, @pofiles);
+                       refreshpofiles($masterfile, @pofiles);
                        map { IkiWiki::rcs_add($_); } @pofiles if ($config{rcs});
                        $updated_po_files=1;
                }
@@ -365,6 +402,7 @@ sub change(@) { #{{{
                }
                # Reinitialize module's private variables.
                resetalreadyfiltered();
+               resetrenamed();
                resettranslationscache();
                flushmemoizecache();
                # Trigger a wiki refresh.
@@ -488,6 +526,34 @@ sub myurlto ($$;$) { #{{{
        } #}}}
 }
 
+{
+       my %renamed;
+
+       sub renamed ($) { #{{{
+               my $page=shift;
+
+               if (exists $renamed{$page} &&
+                   defined $renamed{$page}) {
+                       return $renamed{$page};
+               }
+               return;
+       } #}}}
+
+       sub setrenamed ($$) { #{{{
+               my $oldpage=shift;
+               my $newpage=shift;
+
+               $renamed{$oldpage}=$newpage;
+       } #}}}
+
+       sub resetrenamed () { #{{{
+               undef %renamed;
+       } #}}}
+
+       sub eachrenamed () { #{{{
+               return sub { each %renamed };
+       } #}}}
+}
 
 # ,----
 # | Helper functions
@@ -501,16 +567,21 @@ sub maybe_add_leading_slash ($;$) { #{{{
        return $str;
 } #}}}
 
-sub istranslatable ($) { #{{{
-       my $page=shift;
-
-       $page=~s#^/##;
-       my $file=$pagesources{$page};
+sub istranslatablefile ($) { #{{{
+       my $file=shift;
 
        return 0 unless defined $file;
        return 0 if (defined pagetype($file) && pagetype($file) eq 'po');
        return 0 if $file =~ /\.pot$/;
-       return 1 if  pagespec_match($page, $config{po_translatable_pages});
+       return 1 if pagespec_match(pagename($file), $config{po_translatable_pages});
+       return;
+} #}}}
+
+sub istranslatable ($) { #{{{
+       my $page=shift;
+
+       $page=~s#^/##;
+       return 1 if istranslatablefile($pagesources{$page});
        return;
 } #}}}
 
@@ -751,6 +822,22 @@ sub homepageurl (;$) { #{{{
        return urlto('', $page);
 } #}}}
 
+# - do *not* implement this until the renamepage hook works
+# - do *not* delete translations of pages that were orphans
+#   before being renamed (see renamepage hook comments above)
+sub deletetranslations ($) { #{{{
+       my $deletedmasterfile=shift;
+
+       debug "po(deletetranslations): TODO: delete translations of $deletedmasterfile";
+} #}}}
+
+sub renametranslations (@) { #{{{
+       my ($oldpage, $newpage)=shift;
+
+       debug "po(renametranslations): TODO: rename translations of $oldpage to $newpage";
+} #}}}
+
+
 # ,----
 # | PageSpec's
 # `----