]> sipb.mit.edu Git - ikiwiki.git/blob - doc/todo/tracking_bugs_with_dependencies.mdwn
long-delayed code review
[ikiwiki.git] / doc / todo / tracking_bugs_with_dependencies.mdwn
1 I like the idea of [[tips/integrated_issue_tracking_with_ikiwiki]], and I do so on several wikis.  However, as far as I can tell, ikiwiki has no functionality which can represent dependencies between bugs and allow pagespecs to select based on dependencies.  For instance, I can't write a pagespec which selects all bugs with no dependencies on bugs not marked as done.  --[[JoshTriplett]]
2
3 > I started having a think about this.  I'm going to start with the idea that expanding
4 > the pagespec syntax is the way to attack this.  It seems that any pagespec that is going
5 > to represent "all bugs with no dependencies on bugs not marked as done" is going to
6 > need some way to represent "bugs not marked as done" as a collection of pages, and
7 > then represent "bugs which do not link to pages in the previous collection".
8 >
9 > One way to do this would be to introduce variables into the pagespec, along with
10 > universal and/or existential [[!wikipedia Quantification]].  That looks quite complex.
11 >
12 >> I thought about this briefly, and got about that far.. glad you got
13 >> further. :-) --[[Joey]]
14
15 > Another option would be go with a more functional syntax.  The concept here would
16 > be to allow a pagespec to appear in a 'pagespec function' anywhere a page can.  e.g.
17 > I could pass a pagespec to `link()` and that would return true if there is a link to any
18 > page matching the pagespec.  This makes the variables and existential quantification
19 > implicit.  It would allow the example requested above:
20 >
21 >> `bugs/* and !*/Discussion and !link(bugs/* and !*/Discussion and !link(done))`
22 >
23 > Unfortunately, this is also going to make the pagespec parsing more complex because
24 > we now need to parse nested sets of parentheses to know when the nested pagespec
25 > ends, and that isn't a regular language (we can't use regular expression matching for
26 > easy parsing).
27 >
28 >> Also, it may cause ambiguities with page names that contain parens
29 >> (though some such ambigutities already exist with the pagespec syntax).
30 >
31 > One simplification of that would be to introduce some pagespec [[shortcuts]].  We could
32 > then allow pagespec functions to take either pages, or named pagespec shortcuts.  The
33 > pagespec shortcuts would just be listed on a special page, like current [[shortcuts]].
34 > (It would probably be a good idea to require that shortcuts on that page can only refer
35 > to named pagespecs higher up that page than themselves.  That would stop some
36 > looping issues...)  These shortcuts would be used as follows: when trying to match
37 > a page (without globs) you look to see if the page exists.  If it does then you have a
38 > match.  If it doesn't, then you look to see if a similarly named pagespec shortcut
39 > exists.  If it does, then you check that pagespec recursively to see if you have a match.
40 > The ordering requirement on named pagespecs stops infinite recursion.
41 >
42 > Does that seem like a reasonable first approach?
43 >
44 > -- [[Will]]
45
46 >> Having a separate page for the shortcuts feels unwieldly.. perhaps
47 >> instead the shortcut could be defined earlier in the scope of the same
48 >> pagespec that uses it?
49 >> 
50 >> Example: `define(~bugs, bugs/* and !*/Discussion) and define(~openbugs, ~bugs and !link(done)) and ~openbugs and !link(~openbugs)`
51
52 >>> That could work.  parens are only ever nested 1 deep in that grammar so it is regular and the current parsing would be ok.
53
54 >> Note that I made the "~" explicit, not implicit, so it could be left out. In the case of ambiguity between
55 >> a definition and a page name, the definition would win.
56
57 >>> That was my initial thought too :), but when implementing it I decided that requiring the ~ made things easier.  I'll probably require the ~ for the first pass at least.
58
59 >> So, equivilant example: `define(bugs, bugs/* and !*/Discussion) and define(openbugs, bugs and !link(done)) and openbugs and !link(openbugs)`
60 >> 
61 >> Re recursion, it is avoided.. but building a pagespec that is O(N^X) where N is the
62 >> number of pages in the wiki is not avoided. Probably need to add DOS prevention.
63 >>  --[[Joey]]
64
65 >>> If you memoize the outcomes of the named pagespecs you can make in O(N.X), no?
66 >>> -- [[Will]]
67
68 >>>> Yeah, guess that'd work. :-)
69
70 > One quick further thought.  All the above discussion assumes that 'dependency' is the
71 > same as 'links to', which is not really true.  For example, you'd like to be able to say
72 > "This bug does not depend upon [ [ link to other bug ] ]" and not have a dependency.
73 > Without having different types of links, I don't see how this would be possible.
74 >
75 > -- [[Will]]
76
77 Okie - I've had a quick attempt at this.  Initial patch attached.  This one doesn't quite work.
78 And there is still a lot of debugging stuff in there.
79
80 At the moment I've added a new preprocessor plugin, `definepagespec`, which is like
81 shortcut for pagespecs.  To reference a named pagespec, use `~` like this:
82
83     [ [!definepagespec name="bugs" spec="bugs/* and !*/Discussion"]]
84     [ [!definepagespec name="openbugs" spec="~bugs and !link(done)"]]
85     [ [!definepagespec name="readybugs" spec="~openbugs and !link(~openbugs)"]]
86
87 At the moment the problem is in `match_link()` when we're trying to find a sub-page that
88 matches the appropriate page spec.  There is no good list of pages available to iterate over.
89
90     foreach my $nextpage (keys %IkiWiki::pagesources)
91
92 does not give me a good list of pages.  I found the same thing when I was working on
93 this todo [[todo/Add_a_plugin_to_list_available_pre-processor_commands]].
94
95 > I'm not sure why iterating over `%pagesources` wouldn't work here, it's the same method
96 > used by anything that needs to match a pagespec against all pages..? --[[Joey]]
97
98 >> My uchecked hypothesis is that %pagesources is created after the refresh hook.
99 >> I've also been concerned about how globally defined pagespec shortcuts would interact with
100 >> the page dependancy system.  Your idea of internally defined shortcuts should fix that. -- [[Will]]
101
102 >>> You're correct, the refresh hook is run very early, before pagesources
103 >>> is populated. (It will be partially populated on a refresh, but will
104 >>> not be updated to reflect new pages.) Agree that internally defined
105 >>> seems the way to go. --[[Joey]]
106
107 Immediately below is a patch which seems to basically work.  Lots of debugging code is still there
108 and it needs a cleanup, but I thought it worth posting at this point.  (I was having problems
109 with old style glob lists, so i just switched them off for the moment.)
110
111 The following three inlines work for me with this patch:
112
113     Bugs:
114     
115     [ [!inline pages="define(~bugs, bugs/* and ! */Discussion) and ~bugs" archive="yes"]]
116     
117     OpenBugs:
118     
119     [ [!inline pages="define(~bugs, bugs/* and ! */Discussion) and define(~openbugs,~bugs and !link(done)) and ~openbugs" archive="yes"]]
120     
121     ReadyBugs:
122     
123     [ [!inline pages="define(~bugs, bugs/* and ! */Discussion) and define(~openbugs,~bugs and !link(done)) and define(~readybugs,~openbugs and !link(~openbugs)) and ~readybugs" archive="yes"]]
124
125 > Nice! Could the specfuncsref be passed in %params? I'd like to avoid
126 > needing to change the prototype of every pagespec function, since several
127 > plugins define them too. --[[Joey]]
128
129 >> Maybe - it needs more thought.  I also considered it when I was going though changing all those plugins :).
130 >> My concern was that `%params` can contain other user-defined parameters,
131 >> e.g. `link(target, otherparameter)`, and that means that the specFuncs could be clobbered by a user (or other
132 >> weird security hole).  I thought it better to separate it, but I didn't think about it too hard.  I might move it to
133 >> the first parameter rather than the second.  Ikiwiki is my first real perl hacking and I'm still discovering
134 >> good ways to write things in perl.
135 >>
136 >>>> `%params` contains the parameters passed to `pagespec_match`, not
137 >>>> user-supplied parameters. The user-supplied parameter to a function
138 >>>> like `match_glob()` or `match_link()` is passed in the second positional parameter. --[[Joey]]
139
140 >>>>> OK.  That seems reasonable then.  The only problem is that my PERLfu is not strong enough to make it
141 >>>>> work.  I really have to wonder what substance was influencing the designers of PERL...
142 >>>>> I can't figure out how to use the %params.  And I'm pissed off enough with PERL that I'm not going
143 >>>>> to try and figure it out any more.  There are two patches below now.  The first one uses an extra
144 >>>>> argument and works.  The second one tries to use %params and doesn't - take your pick :-). -- [[Will]]
145
146 >> What do you think is best to do about `is_globlist()`?  At the moment it requires that the 'second word', as
147 >> delimited by a space and ignoring parens, is 'and' or 'or'.  This doesn't hold in the above example pagespecs (so I just hard wired it to 0 to test my patch).
148 >> My thought was just to search for 'and' or 'or' as words anywhere in the pagespec.  Thoughts?
149
150 >>> Dunno, we could just finish deprecating it. Or change the regexp to
151 >>> skip over spaces in parens. (`/[^\s]+\s+([^)]+)/`) --[[Joey]]
152
153 >>>> I think I have a working regexp now.
154
155 >> Oh, one more thing.  In pagespec_translate (now pagespec_makeperl), there is a part of the regular expression for `# any other text`.
156 >> This contained `()`, which has no effect.  I replaced that with `\(\)`, but that is a change in the definition of pagespecs unrelated to the
157 >> rest of this patch. In a related change, commands were not able to contain `)` in their parameters.  I've extended that so the cannot
158 >> contain `(` or `)`.  -- [[Will]]
159
160 >>> `[^\s()]+` is a character class matching all characters not spaces or
161 >>> parens. Since the pervious terminals in the regexp consume most
162 >>> occurances of an open paren or close paren, it's unlikely for one to
163 >>> get through to that part of the regexp. For example, "foo()" will be
164 >>> matched by the command matcher; "(foo)" will be matched by the open
165 >>> paren literal terminal. "foo(" and "foo)" can get through to the
166 >>> end, and would be matched as a page name, if it didn't exclude parens.
167 >>>
168 >>> So why exclude them? Well, consider "foo and(bar and baz)". We don't
169 >>> want it to match "and(" as a page name!
170 >>> 
171 >>> Escaping the parens in the character class actually changes nothing; the
172 >>> changed character class still matches all characters not spaces or
173 >>> parens. (Try it!).
174 >>> 
175 >>> Re commands containing '(', I don't really see any reason not to
176 >>> allow that, unless it breaks something. --[[Joey]]
177
178 >>>> Oh, I didn't realise you didn't need to escape parens inside [].  All else I
179 >>>> I understood.  I have stopped commands from containing parens because
180 >>>> once you allow that then you might have a extra level of depth in the parsing
181 >>>> of define() statements. -- [[Will]]
182
183 >>> Updated patch.  Moved the specFuncsRef to the front of the arg list.  Still haven't thought through the security implications of
184 >>> having it in `%params`.  I've also removed all the debugging `print` statements.  And I've updated the `is_globlist()` function.
185 >>> I think this is ready for people other than me to have a play.  It is not well enough tested to commit just yet.
186 >>> -- [[Will]]
187
188 I've lost track of the indent level, so I'm going back to not indented - I think this is a working [[patch]] taking into
189 account all comments above (which doesn't mean it is above reproach :) ).  --[[Will]]
190
191 > Very belated code review of last version of the patch:
192
193 > * `is_globlist` is no longer needed
194 > * `pagespec_translate` is already memoized, so the explicit call
195 >   to memoize when handling a define seems unnecessary?
196 > * I don't understand why the pagespec match regexp is changed
197 >   from having flags `igx` to `ixgs`. Don't see why you
198 >   want `.` to match '\n` in it, and don't see any `.` in the regexp 
199 >   anyway?
200 > * Some changes of `@_` to `%params` in `pagespec_makeperl` do not
201 >   make sense to me. I don't see where \%params is defined and populated,
202 >   except with `\$params{specFunc}`.
203 > * Seems that the only reason `match_glob` has to check for `~` is
204 >   because when a named spec appears in a pagespec, it is translated
205 >   to `match_glob("~foo")`. If, instead, `pagespec_makeperl` checked
206 >   for named specs, it could convert them into `check_named_spec("foo")`
207 >   and avoid that ugliness.
208 > * The changes to `match_link` seem either unecessary, or incomplete.
209 >   Shouldn't it check for named specs and call
210 >   `check_named_spec_existential`?
211 > * Generally, the need to modify `match_*` functions so that they
212 >   check for and handle named pagespecs seems suboptimal, if
213 >   only because there might be others people may want to use named
214 >   pagespecs with. It would be possible to move this check
215 >   to `pagespec_makeperl`, by having it check if the parameter
216 >   passed to a pagespec function looked like a named pagespec.
217 >   The only issue is that some pagespec functions take a parameter
218 >   that is not a page name at all, and it could be weird
219 >   if such a parameter were accidentially interpreted as a named
220 >   pagespec. (But, that seems unlikely to happen.)
221 > * I need to check if your trick to avoid infinite recursion
222 >   works if there are two named specs that recursively
223 >   call one-another. I suspect it does, but will test this
224 >   myself..
225 >  
226 > --[[Joey]] 
227
228 ----
229
230     diff --git a/IkiWiki.pm b/IkiWiki.pm
231     index 4e4da11..8b3cdfe 100644
232     --- a/IkiWiki.pm
233     +++ b/IkiWiki.pm
234     @@ -1550,7 +1550,16 @@ sub globlist_to_pagespec ($) {
235      
236      sub is_globlist ($) {
237         my $s=shift;
238     -   return ( $s =~ /[^\s]+\s+([^\s]+)/ && $1 ne "and" && $1 ne "or" );
239     +   return ! ($s =~ /
240     +                   (^\s*
241     +                           [^\s(]+         # single item
242     +                                   (\(                     # possibly with parens after it
243     +                                           ([^)]*  # with stuff inside those parens
244     +                                           (\([^)]*\))*)*  # maybe even nested parens
245     +                                   \))?\s*$
246     +                   ) |
247     +                           (\s and \s) | (\s or \s)        # or we find 'and' or 'or' somewhere
248     +                   /xs);
249      }
250      
251      sub safequote ($) {
252     @@ -1631,7 +1640,7 @@ sub pagespec_merge ($$) {
253         return "($a) or ($b)";
254      }
255      
256     -sub pagespec_translate ($) {
257     +sub pagespec_makeperl ($) {
258         my $spec=shift;
259      
260         # Support for old-style GlobLists.
261     @@ -1650,12 +1659,14 @@ sub pagespec_translate ($) {
262                 |
263                         \)              # )
264                 |
265     -                   \w+\([^\)]*\)   # command(params)
266     +                   define\(\s*~\w+\s*,((\([^()]*\)) | ([^()]+))+\) # define(~specName, spec) - spec can contain parens 1 deep
267     +           |
268     +                   \w+\([^()]*\)   # command(params) - params cannot contain parens
269                 |
270                         [^\s()]+        # any other text
271                 )
272                 \s*             # ignore whitespace
273     -   }igx) {
274     +   }igxs) {
275                 my $word=$1;
276                 if (lc $word eq 'and') {
277                         $code.=' &&';
278     @@ -1666,16 +1677,23 @@ sub pagespec_translate ($) {
279                 elsif ($word eq "(" || $word eq ")" || $word eq "!") {
280                         $code.=' '.$word;
281                 }
282     -           elsif ($word =~ /^(\w+)\((.*)\)$/) {
283     +           elsif ($word =~ /^define\(\s*~(\w+)\s*,(.*)\)$/s) {
284     +                   $code .= " (\$params{specFuncs}->{$1}=";        # (exists \$params{specFuncs}) && 
285     +                   $code .= "memoize(";
286     +                   $code .= &pagespec_makeperl($2);
287     +                   $code .= ")";
288     +                   $code .= ") ";
289     +           }
290     +           elsif ($word =~ /^(\w+)\((.*)\)$/s) {
291                         if (exists $IkiWiki::PageSpec::{"match_$1"}) {
292     -                           $code.="IkiWiki::PageSpec::match_$1(\$page, ".safequote($2).", \@_)";
293     +                           $code.="IkiWiki::PageSpec::match_$1(\$page, ".safequote($2).", \%params)";
294                         }
295                         else {
296                                 $code.=' 0';
297                         }
298                 }
299                 else {
300     -                   $code.=" IkiWiki::PageSpec::match_glob(\$page, ".safequote($word).", \@_)";
301     +                   $code.=" IkiWiki::PageSpec::match_glob(\$page, ".safequote($word).", \%params)";
302                 }
303         }
304      
305     @@ -1683,8 +1701,18 @@ sub pagespec_translate ($) {
306                 $code=0;
307         }
308      
309     +   return 'sub { my $page=shift; my %params = @_; '.$code.' }';
310     +}
311     +
312     +sub pagespec_translate ($) {
313     +   my $spec=shift;
314     +
315     +   my $code = pagespec_makeperl($spec);
316     +
317     +   # print STDERR "Spec '$spec' generated code '$code'\n";
318     +
319         no warnings;
320     -   return eval 'sub { my $page=shift; '.$code.' }';
321     +   return eval $code;
322      }
323      
324      sub pagespec_match ($$;@) {
325     @@ -1699,7 +1727,7 @@ sub pagespec_match ($$;@) {
326      
327         my $sub=pagespec_translate($spec);
328         return IkiWiki::FailReason->new("syntax error in pagespec \"$spec\"") if $@;
329     -   return $sub->($page, @params);
330     +   return $sub->($page, @params, specFuncs => {});
331      }
332      
333      sub pagespec_valid ($) {
334     @@ -1748,11 +1776,78 @@ sub new {
335      
336      package IkiWiki::PageSpec;
337      
338     +sub check_named_spec($$;@) {
339     +   my $page=shift;
340     +   my $specName=shift;
341     +   my %params=@_;
342     +   
343     +   error("Unable to find specFuncs in params to check_named_spec()!") unless exists $params{specFuncs};
344     +
345     +   my $specFuncsRef=$params{specFuncs};
346     +   
347     +   return IkiWiki::FailReason->new("Named page spec '$specName' is not valid")
348     +           unless (substr($specName, 0, 1) eq '~');
349     +   
350     +   $specName = substr($specName, 1);
351     +
352     +   if (exists $specFuncsRef->{$specName}) {
353     +           # remove the named spec from the spec refs
354     +           # when we recurse to avoid infinite recursion
355     +           my $sub = $specFuncsRef->{$specName};
356     +           delete $specFuncsRef->{$specName};
357     +           my $result = $sub->($page, %params);
358     +           $specFuncsRef->{$specName} = $sub;
359     +           return $result;
360     +   } else {
361     +           return IkiWiki::FailReason->new("Page spec '$specName' does not exist");
362     +   }
363     +}
364     +
365     +sub check_named_spec_existential($$$;@) {
366     +   my $page=shift;
367     +   my $specName=shift;
368     +   my $funcref=shift;
369     +   my %params=@_;
370     +   
371     +   error("Unable to find specFuncs in params to check_named_spec_existential()!") unless exists $params{specFuncs};
372     +   my $specFuncsRef=$params{specFuncs};
373     +   
374     +   return IkiWiki::FailReason->new("Named page spec '$specName' is not valid")
375     +           unless (substr($specName, 0, 1) eq '~');
376     +   $specName = substr($specName, 1);
377     +   
378     +   if (exists $specFuncsRef->{$specName}) {
379     +           # remove the named spec from the spec refs
380     +           # when we recurse to avoid infinite recursion
381     +           my $sub = $specFuncsRef->{$specName};
382     +           delete $specFuncsRef->{$specName};
383     +           
384     +           foreach my $nextpage (keys %IkiWiki::pagesources) {
385     +                   if ($sub->($nextpage, %params)) {
386     +                           my $tempResult = $funcref->($page, $nextpage, %params);
387     +                           if ($tempResult) {
388     +                                   $specFuncsRef->{$specName} = $sub;
389     +                                   return $tempResult;
390     +                           }
391     +                   }
392     +           }
393     +           
394     +           $specFuncsRef->{$specName} = $sub;
395     +           return IkiWiki::FailReason->new("No page in spec '$specName' was successfully matched");
396     +   } else {
397     +           return IkiWiki::FailReason->new("Named page spec '$specName' does not exist");
398     +   }
399     +}
400     +
401      sub match_glob ($$;@) {
402         my $page=shift;
403         my $glob=shift;
404         my %params=@_;
405         
406     +   if (substr($glob, 0, 1) eq '~') {
407     +           return check_named_spec($page, $glob, %params);
408     +   }
409     +
410         my $from=exists $params{location} ? $params{location} : '';
411         
412         # relative matching
413     @@ -1782,11 +1877,12 @@ sub match_internal ($$;@) {
414      
415      sub match_link ($$;@) {
416         my $page=shift;
417     -   my $link=lc(shift);
418     +   my $fulllink=shift;
419         my %params=@_;
420     +   my $link=lc($fulllink);
421      
422         my $from=exists $params{location} ? $params{location} : '';
423     -
424     +   
425         # relative matching
426         if ($link =~ m!^\.! && defined $from) {
427                 $from=~s#/?[^/]+$##;
428     @@ -1804,19 +1900,32 @@ sub match_link ($$;@) {
429                 }
430                 else {
431                         return IkiWiki::SuccessReason->new("$page links to page $p matching $link")
432     -                           if match_glob($p, $link, %params);
433     +                           if match_glob($p, $fulllink, %params);
434                 }
435         }
436         return IkiWiki::FailReason->new("$page does not link to $link");
437      }
438      
439      sub match_backlink ($$;@) {
440     -   return match_link($_[1], $_[0], @_);
441     +   my $page=shift;
442     +   my $backlink=shift;
443     +   my @params=@_;
444     +
445     +   if (substr($backlink, 0, 1) eq '~') {
446     +           return check_named_spec_existential($page, $backlink, \&match_backlink, @params);
447     +   }
448     +
449     +   return match_link($backlink, $page, @params);
450      }
451      
452      sub match_created_before ($$;@) {
453         my $page=shift;
454         my $testpage=shift;
455     +   my @params=@_;
456     +
457     +   if (substr($testpage, 0, 1) eq '~') {
458     +           return check_named_spec_existential($page, $testpage, \&match_created_before, @params);
459     +   }
460      
461         if (exists $IkiWiki::pagectime{$testpage}) {
462                 if ($IkiWiki::pagectime{$page} < $IkiWiki::pagectime{$testpage}) {
463     @@ -1834,6 +1943,11 @@ sub match_created_before ($$;@) {
464      sub match_created_after ($$;@) {
465         my $page=shift;
466         my $testpage=shift;
467     +   my @params=@_;
468     +
469     +   if (substr($testpage, 0, 1) eq '~') {
470     +           return check_named_spec_existential($page, $testpage, \&match_created_after, @params);
471     +   }
472      
473         if (exists $IkiWiki::pagectime{$testpage}) {
474                 if ($IkiWiki::pagectime{$page} > $IkiWiki::pagectime{$testpage}) {