]> sipb.mit.edu Git - ikiwiki.git/blob - doc/todo/missingparents.pm.mdwn
Change Projects link to point to projects DB
[ikiwiki.git] / doc / todo / missingparents.pm.mdwn
1 This is another blogging support thing, and it relies on 
2 [[pagespec_relative_to_a_target]] (but only to figure out whether a given page
3 has a child). Basically, you give it a page called missingparents.mdwn,
4 something like this:
5
6 <pre>
7 [[!missingparents pages="posts/* and !posts/*/*" generate="""[[!template id=year text="$page"]]"""]]
8 [[!missingparents pages="posts/*/* and !posts/*/*/*" generate="""[[!template id=month text="$page"]]"""]]
9 [[!missingparents pages="posts/*/*/* and !posts/*/*/*/*" generate="""[[!template id=day text="$page"]]"""]]
10 </pre>
11
12 And it scans the whole wiki for pages that match the pagespecs but are missing
13 parents. If any are found, they are generated automatically using the text in
14 the "generate" parameter (except $page is substituted for the page title).
15 *These generated pages aren't kept in version control*, but of course they're
16 ordinary wiki pages and can be edited by the web form or otherwise added, at 
17 which point the missingparents plugin lets go of them. (TODO: CGI.pm needs to
18 know to rcs_add these pages if they are edited, and it doesn't.) If all of the
19 children of a missingparent page goes away, the missingparent itself is 
20 unlinked automatically, and all missingparents are deleted on wiki rebuild.
21
22 To implement this, I needed to tell ikiwiki that pages were being added and
23 removed in a non-standard way, and so created functions newpage and delpage
24 in the IkiWiki namespace to do these things. delpage is modeled on the 
25 Render.pm code that deletes pages, so I re-used it in Render.pm. I also
26 needed a way to add files to be deleted on a refresh(), so I added a 
27 needsdelete hook, parallel in form to needsbuild.
28
29 This patch, or one like it, would enable better blogging support, by adding
30 the ability to hierarchically organize blog posts and automatically generate
31 structural pages for year, month, or day. Please apply. --Ethan
32
33 > This looks a lot like [[plugins/autoindex]], except limited to a subset
34 > of pages, and with different templates according to the page it's used
35 > on. Perhaps it could become several enhancements for autoindex? --[[smcv]]
36
37 ----
38
39 <pre>
40 Index: IkiWiki/Render.pm
41 ===================================================================
42 --- IkiWiki/Render.pm   (revision 3926)
43 +++ IkiWiki/Render.pm   (working copy)
44 @@ -322,17 +322,7 @@
45                 if (! $exists{$page}) {
46                         debug(sprintf(gettext("removing old page %s"), $page));
47                         push @del, $pagesources{$page};
48 -                       $links{$page}=[];
49 -                       $renderedfiles{$page}=[];
50 -                       $pagemtime{$page}=0;
51 -                       prune($config{destdir}."/".$_)
52 -                               foreach @{$oldrenderedfiles{$page}};
53 -                       delete $pagesources{$page};
54 -                       foreach (keys %destsources) {
55 -                               if ($destsources{$_} eq $page) {
56 -                                       delete $destsources{$_};
57 -                               }
58 -                       }
59 +                       delpage($page);
60                 }
61         }
62  
63 @@ -377,6 +367,10 @@
64                 }
65         }
66  
67 +       if (@del) {
68 +               run_hooks(needsdelete => sub { shift->(\@del) });
69 +       }
70 +
71         if (%rendered || @del) {
72                 # rebuild dependant pages
73                 foreach my $f (@files) {
74 Index: IkiWiki/Plugin/missingparents.pm
75 ===================================================================
76 --- IkiWiki/Plugin/missingparents.pm    (revision 0)
77 +++ IkiWiki/Plugin/missingparents.pm    (revision 0)
78 @@ -0,0 +1,142 @@
79 +#!/usr/bin/perl
80 +# missingparents plugin: detect missing parents of pages and create them
81 +package IkiWiki::Plugin::missingparents;
82 +
83 +use warnings;
84 +use strict;
85 +use IkiWiki 2.00;
86 +use IkiWiki::Plugin::relative;
87 +
88 +my %ownfiles;
89 +my @pagespecs;
90 +
91 +sub import {
92 +       hook(type => "checkconfig", id => "missingparents", call => \&checkconfig);
93 +       hook(type => "needsdelete", id => "missingparents", call => \&needsdelete);
94 +       hook(type => "needsbuild", id => "missingparents", call => \&needsbuild);
95 +       hook(type => "savestate", id => "missingparents", call => \&savestate);
96 +       hook(type => "preprocess", id => "missingparents", call => \&preprocess_missingparents);
97 +}
98 +
99 +sub checkconfig () {
100 +       IkiWiki::preprocess("missingparents", "missingparents",
101 +               readfile(srcfile("missingparents.mdwn")));
102 +       loadstate();
103 +       if ($config{rebuild}){
104 +               foreach my $file (keys %ownfiles) {
105 +                       unlink $config{srcdir}.'/'.$file;
106 +               }
107 +       }
108 +}
109 +
110 +sub preprocess_missingparents (@) {
111 +       my %params=@_;
112 +
113 +       if (! defined $params{pages} || ! defined $params{generate}) {
114 +               return "[[!missingparents ".gettext("missing pages or generate parameter")."]]";
115 +       }
116 +
117 +       push @pagespecs, \%params;
118 +
119 +       #translators: This is used to display what missingparents are defined.
120 +       #translators: First parameter is a pagespec, the second
121 +       #translators: is text for pages that match that pagespec.
122 +       return sprintf(gettext("missingparents in %s will be %s"), 
123 +                      '`'.$params{pages}.'`', '`\\'.$params{generate}.'`');
124 +}
125 +
126 +my $state_loaded=0;
127 +sub loadstate() {
128 +       my $filename = "$config{wikistatedir}/missingparents";
129 +       if (-e $filename) {
130 +               open (IN, $filename) ||
131 +                     die "$filename: $!";
132 +               while (<IN>) {
133 +                       chomp;
134 +                       $ownfiles{$_} = 1;
135 +               }
136 +
137 +               close IN;
138 +
139 +               $state_loaded=1;
140 +       }
141 +}
142 +
143 +sub savestate() {
144 +       my $filename = "$config{wikistatedir}/missingparents.new";
145 +       my $cleanup = sub { unlink ($filename) };
146 +       open (OUT, ">$filename") || error("open $filename: $!", $cleanup);
147 +       foreach my $data (keys %ownfiles) {
148 +               print OUT "$data\n" if $ownfiles{$data};
149 +       }
150 +       rename($filename, "$config{wikistatedir}/missingparents") ||
151 +               error("rename $filename: $!", $cleanup);
152 +}
153 +
154 +sub needsdelete (@) {
155 +       my $files=shift;
156 +       
157 +       my @mydel;
158 +       my $pruned = 1;
159 +       do {
160 +               $pruned = 0;
161 +               foreach my $file (keys %ownfiles) {
162 +                       my $page = pagename($file);
163 +                       if (! IkiWiki::PageSpec::match_has_child($page, "")) {
164 +                               # No children -- get rid of it
165 +                               push @mydel, $page;
166 +                               delete $ownfiles{$file};
167 +                               IkiWiki::delpage($page);
168 +                               unlink $config{srcdir}."/".$file;
169 +                               $pruned = 1;
170 +                       }
171 +               }
172 +       } while($pruned);
173 +       foreach my $page (@mydel){
174 +               push @{$files}, $page;
175 +       }
176 +}
177 +
178 +sub check_matches($) {
179 +       my $page = shift;
180 +       return if $IkiWiki::pagesources{$page};
181 +
182 +       foreach my $miss (@pagespecs) {
183 +               next unless pagespec_match($page, $miss->{pages});
184 +               my $text = $miss->{generate};
185 +               $text =~ s/\$page/$page/;
186 +               my $output = $page.".mdwn";
187 +               writefile($output, "$config{srcdir}/", $text);
188 +               IkiWiki::newpage($output, $page);
189 +               return $output;
190 +       }
191 +       return "";
192 +}
193 +
194 +sub needsbuild ($) {
195 +       my $files=shift;
196 +       my @new;
197 +
198 +       foreach my $file (@{$files}) {
199 +               if ($ownfiles{$file}) {
200 +                       # someone edited our file, making it the
201 +                       # user's problem
202 +                       delete $ownfiles{$file};
203 +                       next;
204 +               }
205 +               my $page = pagename $file;
206 +               my $newfile = "";
207 +               foreach my $parent (split '/', $page) {
208 +                       $newfile .= $parent;
209 +                       my $output = check_matches($newfile);
210 +                       push @new, $output if $output;
211 +                       $newfile .= "/";
212 +               }
213 +       }
214 +       foreach my $file (@new) {
215 +               $ownfiles{$file} = 1;
216 +               push @{$files}, $file;
217 +       }
218 +}
219 +
220 +1
221 Index: IkiWiki.pm
222 ===================================================================
223 --- IkiWiki.pm  (revision 3926)
224 +++ IkiWiki.pm  (working copy)
225 @@ -16,7 +16,7 @@
226  use Exporter q{import};
227  our @EXPORT = qw(hook debug error template htmlpage add_depends pagespec_match
228                   bestlink htmllink readfile writefile pagetype srcfile pagename
229 -                 displaytime will_render gettext urlto targetpage
230 +                 displaytime will_render gettext urlto targetpage newpage delpage
231                   %config %links %renderedfiles %pagesources %destsources);
232  our $VERSION = 2.00; # plugin interface version, next is ikiwiki version
233  our $version='unknown'; # VERSION_AUTOREPLACE done by Makefile, DNE
234 @@ -330,6 +336,30 @@
235                 error("failed renaming $newfile to $destdir/$file: $!", $cleanup);
236  }
237  
238 +sub newpage($$) {
239 +       my $file=shift;
240 +       my $page=shift;
241 +
242 +       $pagemtime{$page} = $pagectime{$page} = time;
243 +       $pagesources{$page} = $file;
244 +       $pagecase{lc $page} = $page;
245 +}
246 +
247 +sub delpage($) {
248 +       my $page=shift;
249 +       $links{$page}=[];
250 +       $renderedfiles{$page}=[];
251 +       $pagemtime{$page}=0;
252 +       prune($config{destdir}."/".$_)
253 +           foreach @{$oldrenderedfiles{$page}};
254 +       delete $pagesources{$page};
255 +       foreach (keys %destsources) {
256 +               if ($destsources{$_} eq $page) {
257 +                       delete $destsources{$_};
258 +                       }
259 +               }
260 +}
261 +
262  my %cleared;
263  sub will_render ($$;$) {
264         my $page=shift;
265 </pre>
266
267 [[!tag patch patch/core]]