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