confirmation
[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 > 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 for IkiWiki.pm.  Below that is a new plugin `definepagespec `
108 which behaves like `shortcut` for pagespecs.
109
110 ----
111
112 diff --git a/IkiWiki.pm b/IkiWiki.pm
113 index e476521..1d2d48c 100644
114 --- a/IkiWiki.pm
115 +++ b/IkiWiki.pm
116 @@ -14,7 +14,7 @@ use open qw{:utf8 :std};
117  use vars qw{%config %links %oldlinks %pagemtime %pagectime %pagecase
118             %pagestate %renderedfiles %oldrenderedfiles %pagesources
119             %destsources %depends %hooks %forcerebuild $gettext_obj
120 -           %loaded_plugins};
121 +           %loaded_plugins %named_pagespec};
122  
123  use Exporter q{import};
124  our @EXPORT = qw(hook debug error template htmlpage add_depends pagespec_match
125 @@ -22,7 +22,7 @@ our @EXPORT = qw(hook debug error template htmlpage add_depends pagespec_match
126                   displaytime will_render gettext urlto targetpage
127                  add_underlay
128                   %config %links %pagestate %renderedfiles
129 -                 %pagesources %destsources);
130 +                 %pagesources %destsources %named_pagespec);
131  our $VERSION = 2.00; # plugin interface version, next is ikiwiki version
132  our $version='unknown'; # VERSION_AUTOREPLACE done by Makefile, DNE
133  my $installdir=''; # INSTALLDIR_AUTOREPLACE done by Makefile, DNE
134 @@ -1271,7 +1271,7 @@ sub loadindex () { #{{{
135         %oldrenderedfiles=%pagectime=();
136         if (! $config{rebuild}) {
137                 %pagesources=%pagemtime=%oldlinks=%links=%depends=
138 -               %destsources=%renderedfiles=%pagecase=%pagestate=();
139 +               %destsources=%renderedfiles=%pagecase=%pagestate=%named_pagespec=();
140         }
141         my $in;
142         if (! open ($in, "<", "$config{wikistatedir}/indexdb")) {
143 @@ -1729,6 +1729,8 @@ sub match_glob ($$;@) { #{{{
144         
145         my $from=exists $params{location} ? $params{location} : '';
146         
147 +       print "Matching glob $glob \n";
148 +       
149         # relative matching
150         if ($glob =~ m!^\./!) {
151                 $from=~s#/?[^/]+$##;
152 @@ -1736,6 +1738,18 @@ sub match_glob ($$;@) { #{{{
153                 $glob="$from/$glob" if length $from;
154         }
155  
156 +       if (substr($glob, 0, 1) eq '~') {
157 +               my $specname = substr($glob, 1);
158 +               print "Checking for pagespec named $specname \n";
159 +               if (exists $IkiWiki::named_pagespec{$specname}) {
160 +                       my $spec = $IkiWiki::named_pagespec{$specname};
161 +                       return IkiWiki::pagespec_match($page, $spec, %params);
162 +               } else {
163 +                       print "Couldn't find pagespec\n";
164 +                       return IkiWiki::FailReason->new("Page spec $specname referenced on page $page does not exist");
165 +               }
166 +       }
167 +
168         my $regexp=IkiWiki::glob2re($glob);
169         if ($page=~/^$regexp$/i) {
170                 if (! IkiWiki::isinternal($page) || $params{internal}) {
171 @@ -1756,11 +1770,36 @@ sub match_internal ($$;@) { #{{{
172  
173  sub match_link ($$;@) { #{{{
174         my $page=shift;
175 -       my $link=lc(shift);
176 +       my $fulllink=shift;
177 +       my $link=lc($fulllink);
178         my %params=@_;
179  
180 +       print "Matching link $fulllink \n";
181 +       
182         my $from=exists $params{location} ? $params{location} : '';
183  
184 +       if (substr($fulllink, 0, 1) eq '~') {
185 +               my $specname = substr($fulllink, 1);
186 +               print "Checking link pagespec $specname \n";
187 +               if (exists $IkiWiki::named_pagespec{$specname}) {
188 +                       my $spec = $IkiWiki::named_pagespec{$specname};
189 +                       
190 +                       print "Checking all pages against $spec\n";
191 +                       
192 +                       foreach my $nextpage (keys %IkiWiki::pagesources) {
193 +                               print "Checking $nextpage against $spec\n";
194 +                               if (pagespec_match($nextpage, $spec, %params) && IkiWiki::PageSpec::match_link($page, $nextpage, %params)) {
195 +                                       return IkiWiki::SuccessReason->new("$page links to page $nextpage matching $link")
196 +                               }
197 +                       }
198 +                       
199 +                       return IkiWiki::FailReason->new("$page has no links to any pages that match $spec");
200 +               } else {
201 +                       print "Pagespec $specname not found\n";
202 +                       return IkiWiki::FailReason->new("$page cannot link to nonexistent spec name $specname");
203 +               }
204 +       }
205 +
206         # relative matching
207         if ($link =~ m!^\.! && defined $from) {
208                 $from=~s#/?[^/]+$##;
209
210 ----
211
212 #!/usr/bin/perl
213 package IkiWiki::Plugin::definepagespec;
214
215 use warnings;
216 use strict;
217 use IkiWiki 2.00;
218
219 sub import { #{{{
220         hook(type => "getsetup", id => "definepagespec", call => \&getsetup);
221         hook(type => "refresh", id => "definepagespec", call => \&refresh);
222         hook(type => "preprocess", id => "definepagespec", call => \&preprocess);
223 } #}}}
224
225 sub getsetup () { #{{{
226         return
227                 plugin => {
228                         safe => 1,
229                         rebuild => undef,
230                 },
231 } #}}}
232
233 sub refresh () { #{{{
234         # Preprocess the shortcuts page to get all the available shortcuts
235         # defined before other pages are rendered.
236         my $srcfile=srcfile("pagespecs.mdwn", 1);
237         if (! defined $srcfile) {
238                 error(gettext("definepagespec plugin will not work without a pagespecs.mdwn"));
239         }
240         IkiWiki::preprocess("pagespecs", "pagespecs", readfile($srcfile));
241 } # }}}
242
243 sub preprocess (@) { #{{{
244         my %params=@_;
245
246         if (! defined $params{name} || ! defined $params{spec}) {
247                 error gettext("missing name or spec parameter");
248         }
249
250         $IkiWiki::named_pagespec{$params{name}} = $params{spec};
251
252         #translators: This is used to display what shortcuts are defined.
253         #translators: First parameter is the name of the shortcut, the second
254         #translators: is an URL.
255         return sprintf(gettext("pagespec %s refers to <i>%s</i>"), $params{name}, $params{spec});
256 } # }}}
257
258 1