comments
[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 > One quick further thought.  All the above discussion assumes that 'dependency' is the
69 > same as 'links to', which is not really true.  For example, you'd like to be able to say
70 > "This bug does not depend upon [ [ link to other bug ] ]" and not have a dependency.
71 > Without having different types of links, I don't see how this would be possible.
72 >
73 > -- [[Will]]
74
75 Okie - I've had a quick attempt at this.  Initial patch attached.  This one doesn't quite work.
76 And there is still a lot of debugging stuff in there.
77
78 At the moment I've added a new preprocessor plugin, `definepagespec`, which is like
79 shortcut for pagespecs.  To reference a named pagespec, use `~` like this:
80
81     [ [!definepagespec name="bugs" spec="bugs/* and !*/Discussion"]]
82     [ [!definepagespec name="openbugs" spec="~bugs and !link(done)"]]
83     [ [!definepagespec name="readybugs" spec="~openbugs and !link(~openbugs)"]]
84
85 At the moment the problem is in `match_link()` when we're trying to find a sub-page that
86 matches the appropriate page spec.  There is no good list of pages available to iterate over.
87
88     foreach my $nextpage (keys %IkiWiki::pagesources)
89
90 does not give me a good list of pages.  I found the same thing when I was working on
91 this todo [[todo/Add_a_plugin_to_list_available_pre-processor_commands]].
92
93 > I'm not sure why iterating over `%pagesources` wouldn't work here, it's the same method
94 > used by anything that needs to match a pagespec against all pages..? --[[Joey]]
95
96 >> My uchecked hypothesis is that %pagesources is created after the refresh hook.
97 >> I've also been concerned about how globally defined pagespec shortcuts would interact with
98 >> the page dependancy system.  Your idea of internally defined shortcuts should fix that. -- [[Will]]
99
100 Immediately below is a patch for IkiWiki.pm.  Below that is a new plugin `definepagespec `
101 which behaves like `shortcut` for pagespecs.
102
103 ----
104
105 diff --git a/IkiWiki.pm b/IkiWiki.pm
106 index e476521..1d2d48c 100644
107 --- a/IkiWiki.pm
108 +++ b/IkiWiki.pm
109 @@ -14,7 +14,7 @@ use open qw{:utf8 :std};
110  use vars qw{%config %links %oldlinks %pagemtime %pagectime %pagecase
111             %pagestate %renderedfiles %oldrenderedfiles %pagesources
112             %destsources %depends %hooks %forcerebuild $gettext_obj
113 -           %loaded_plugins};
114 +           %loaded_plugins %named_pagespec};
115  
116  use Exporter q{import};
117  our @EXPORT = qw(hook debug error template htmlpage add_depends pagespec_match
118 @@ -22,7 +22,7 @@ our @EXPORT = qw(hook debug error template htmlpage add_depends pagespec_match
119                   displaytime will_render gettext urlto targetpage
120                  add_underlay
121                   %config %links %pagestate %renderedfiles
122 -                 %pagesources %destsources);
123 +                 %pagesources %destsources %named_pagespec);
124  our $VERSION = 2.00; # plugin interface version, next is ikiwiki version
125  our $version='unknown'; # VERSION_AUTOREPLACE done by Makefile, DNE
126  my $installdir=''; # INSTALLDIR_AUTOREPLACE done by Makefile, DNE
127 @@ -1271,7 +1271,7 @@ sub loadindex () { #{{{
128         %oldrenderedfiles=%pagectime=();
129         if (! $config{rebuild}) {
130                 %pagesources=%pagemtime=%oldlinks=%links=%depends=
131 -               %destsources=%renderedfiles=%pagecase=%pagestate=();
132 +               %destsources=%renderedfiles=%pagecase=%pagestate=%named_pagespec=();
133         }
134         my $in;
135         if (! open ($in, "<", "$config{wikistatedir}/indexdb")) {
136 @@ -1729,6 +1729,8 @@ sub match_glob ($$;@) { #{{{
137         
138         my $from=exists $params{location} ? $params{location} : '';
139         
140 +       print "Matching glob $glob \n";
141 +       
142         # relative matching
143         if ($glob =~ m!^\./!) {
144                 $from=~s#/?[^/]+$##;
145 @@ -1736,6 +1738,18 @@ sub match_glob ($$;@) { #{{{
146                 $glob="$from/$glob" if length $from;
147         }
148  
149 +       if (substr($glob, 0, 1) eq '~') {
150 +               my $specname = substr($glob, 1);
151 +               print "Checking for pagespec named $specname \n";
152 +               if (exists $IkiWiki::named_pagespec{$specname}) {
153 +                       my $spec = $IkiWiki::named_pagespec{$specname};
154 +                       return IkiWiki::pagespec_match($page, $spec, %params);
155 +               } else {
156 +                       print "Couldn't find pagespec\n";
157 +                       return IkiWiki::FailReason->new("Page spec $specname referenced on page $page does not exist");
158 +               }
159 +       }
160 +
161         my $regexp=IkiWiki::glob2re($glob);
162         if ($page=~/^$regexp$/i) {
163                 if (! IkiWiki::isinternal($page) || $params{internal}) {
164 @@ -1756,11 +1770,36 @@ sub match_internal ($$;@) { #{{{
165  
166  sub match_link ($$;@) { #{{{
167         my $page=shift;
168 -       my $link=lc(shift);
169 +       my $fulllink=shift;
170 +       my $link=lc($fulllink);
171         my %params=@_;
172  
173 +       print "Matching link $fulllink \n";
174 +       
175         my $from=exists $params{location} ? $params{location} : '';
176  
177 +       if (substr($fulllink, 0, 1) eq '~') {
178 +               my $specname = substr($fulllink, 1);
179 +               print "Checking link pagespec $specname \n";
180 +               if (exists $IkiWiki::named_pagespec{$specname}) {
181 +                       my $spec = $IkiWiki::named_pagespec{$specname};
182 +                       
183 +                       print "Checking all pages against $spec\n";
184 +                       
185 +                       foreach my $nextpage (keys %IkiWiki::pagesources) {
186 +                               print "Checking $nextpage against $spec\n";
187 +                               if (pagespec_match($nextpage, $spec, %params) && IkiWiki::PageSpec::match_link($page, $nextpage, %params)) {
188 +                                       return IkiWiki::SuccessReason->new("$page links to page $nextpage matching $link")
189 +                               }
190 +                       }
191 +                       
192 +                       return IkiWiki::FailReason->new("$page has no links to any pages that match $spec");
193 +               } else {
194 +                       print "Pagespec $specname not found\n";
195 +                       return IkiWiki::FailReason->new("$page cannot link to nonexistent spec name $specname");
196 +               }
197 +       }
198 +
199         # relative matching
200         if ($link =~ m!^\.! && defined $from) {
201                 $from=~s#/?[^/]+$##;
202
203 ----
204
205 #!/usr/bin/perl
206 package IkiWiki::Plugin::definepagespec;
207
208 use warnings;
209 use strict;
210 use IkiWiki 2.00;
211
212 sub import { #{{{
213         hook(type => "getsetup", id => "definepagespec", call => \&getsetup);
214         hook(type => "refresh", id => "definepagespec", call => \&refresh);
215         hook(type => "preprocess", id => "definepagespec", call => \&preprocess);
216 } #}}}
217
218 sub getsetup () { #{{{
219         return
220                 plugin => {
221                         safe => 1,
222                         rebuild => undef,
223                 },
224 } #}}}
225
226 sub refresh () { #{{{
227         # Preprocess the shortcuts page to get all the available shortcuts
228         # defined before other pages are rendered.
229         my $srcfile=srcfile("pagespecs.mdwn", 1);
230         if (! defined $srcfile) {
231                 error(gettext("definepagespec plugin will not work without a pagespecs.mdwn"));
232         }
233         IkiWiki::preprocess("pagespecs", "pagespecs", readfile($srcfile));
234 } # }}}
235
236 sub preprocess (@) { #{{{
237         my %params=@_;
238
239         if (! defined $params{name} || ! defined $params{spec}) {
240                 error gettext("missing name or spec parameter");
241         }
242
243         $IkiWiki::named_pagespec{$params{name}} = $params{spec};
244
245         #translators: This is used to display what shortcuts are defined.
246         #translators: First parameter is the name of the shortcut, the second
247         #translators: is an URL.
248         return sprintf(gettext("pagespec %s refers to <i>%s</i>"), $params{name}, $params{spec});
249 } # }}}
250
251 1