Add very preliminary patch
[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 > Another option would be go with a more functional syntax.  The concept here would
13 > be to allow a pagespec to appear in a 'pagespec function' anywhere a page can.  e.g.
14 > I could pass a pagespec to `link()` and that would return true if there is a link to any
15 > page matching the pagespec.  This makes the variables and existential quantification
16 > implicit.  It would allow the example requested above:
17 >
18 >> `bugs/* and !*/Discussion and !link(bugs/* and !*/Discussion and !link(done))`
19 >
20 > Unfortunately, this is also going to make the pagespec parsing more complex because
21 > we now need to parse nested sets of parentheses to know when the nested pagespec
22 > ends, and that isn't a regular language (we can't use regular expression matching for
23 > easy parsing).
24 >
25 > One simplification of that would be to introduce some pagespec [[shortcuts]].  We could
26 > then allow pagespec functions to take either pages, or named pagespec shortcuts.  The
27 > pagespec shortcuts would just be listed on a special page, like current [[shortcuts]].
28 > (It would probably be a good idea to require that shortcuts on that page can only refer
29 > to named pagespecs higher up that page than themselves.  That would stop some
30 > looping issues...)  These shortcuts would be used as follows: when trying to match
31 > a page (without globs) you look to see if the page exists.  If it does then you have a
32 > match.  If it doesn't, then you look to see if a similarly named pagespec shortcut
33 > exists.  If it does, then you check that pagespec recursively to see if you have a match.
34 > The ordering requirement on named pagespecs stops infinite recursion.
35 >
36 > Does that seem like a reasonable first approach?
37 >
38 > -- [[Will]]
39
40 > One quick further thought.  All the above discussion assumes that 'dependency' is the
41 > same as 'links to', which is not really true.  For example, you'd like to be able to say
42 > "This bug does not depend upon [ [ link to other bug ] ]" and not have a dependency.
43 > Without having different types of links, I don't see how this would be possible.
44 >
45 > -- [[Will]]
46
47 Okie - I've had a quick attempt at this.  Initial patch attached.  This one doesn't quite work.
48 And there is still a lot of debugging stuff in there.
49
50 At the moment I've added a new preprocessor plugin, `definepagespec`, which is like
51 shortcut for pagespecs.  To reference a named pagespec, use `~` like this:
52
53     [ [!definepagespec name="bugs" spec="bugs/* and !*/Discussion"]]
54     [ [!definepagespec name="openbugs" spec="~bugs and !link(done)"]]
55     [ [!definepagespec name="readybugs" spec="~openbugs and !link(~openbugs)"]]
56
57 At the moment the problem is in `match_link()` when we're trying to find a sub-page that
58 matches the appropriate page spec.  There is no good list of pages available to iterate over.
59
60     foreach my $nextpage (keys %IkiWiki::pagesources)
61
62 does not give me a good list of pages.  I found the same thing when I was working on
63 this todo [[todo/Add_a_plugin_to_list_available_pre-processor_commands]].
64
65 Immediately below is a patch for IkiWiki.pm.  Below that is a new plugin `definepagespec `
66 which behaves like `shortcut` for pagespecs.
67
68 ----
69
70 diff --git a/IkiWiki.pm b/IkiWiki.pm
71 index e476521..1d2d48c 100644
72 --- a/IkiWiki.pm
73 +++ b/IkiWiki.pm
74 @@ -14,7 +14,7 @@ use open qw{:utf8 :std};
75  use vars qw{%config %links %oldlinks %pagemtime %pagectime %pagecase
76             %pagestate %renderedfiles %oldrenderedfiles %pagesources
77             %destsources %depends %hooks %forcerebuild $gettext_obj
78 -           %loaded_plugins};
79 +           %loaded_plugins %named_pagespec};
80  
81  use Exporter q{import};
82  our @EXPORT = qw(hook debug error template htmlpage add_depends pagespec_match
83 @@ -22,7 +22,7 @@ our @EXPORT = qw(hook debug error template htmlpage add_depends pagespec_match
84                   displaytime will_render gettext urlto targetpage
85                  add_underlay
86                   %config %links %pagestate %renderedfiles
87 -                 %pagesources %destsources);
88 +                 %pagesources %destsources %named_pagespec);
89  our $VERSION = 2.00; # plugin interface version, next is ikiwiki version
90  our $version='unknown'; # VERSION_AUTOREPLACE done by Makefile, DNE
91  my $installdir=''; # INSTALLDIR_AUTOREPLACE done by Makefile, DNE
92 @@ -1271,7 +1271,7 @@ sub loadindex () { #{{{
93         %oldrenderedfiles=%pagectime=();
94         if (! $config{rebuild}) {
95                 %pagesources=%pagemtime=%oldlinks=%links=%depends=
96 -               %destsources=%renderedfiles=%pagecase=%pagestate=();
97 +               %destsources=%renderedfiles=%pagecase=%pagestate=%named_pagespec=();
98         }
99         my $in;
100         if (! open ($in, "<", "$config{wikistatedir}/indexdb")) {
101 @@ -1729,6 +1729,8 @@ sub match_glob ($$;@) { #{{{
102         
103         my $from=exists $params{location} ? $params{location} : '';
104         
105 +       print "Matching glob $glob \n";
106 +       
107         # relative matching
108         if ($glob =~ m!^\./!) {
109                 $from=~s#/?[^/]+$##;
110 @@ -1736,6 +1738,18 @@ sub match_glob ($$;@) { #{{{
111                 $glob="$from/$glob" if length $from;
112         }
113  
114 +       if (substr($glob, 0, 1) eq '~') {
115 +               my $specname = substr($glob, 1);
116 +               print "Checking for pagespec named $specname \n";
117 +               if (exists $IkiWiki::named_pagespec{$specname}) {
118 +                       my $spec = $IkiWiki::named_pagespec{$specname};
119 +                       return IkiWiki::pagespec_match($page, $spec, %params);
120 +               } else {
121 +                       print "Couldn't find pagespec\n";
122 +                       return IkiWiki::FailReason->new("Page spec $specname referenced on page $page does not exist");
123 +               }
124 +       }
125 +
126         my $regexp=IkiWiki::glob2re($glob);
127         if ($page=~/^$regexp$/i) {
128                 if (! IkiWiki::isinternal($page) || $params{internal}) {
129 @@ -1756,11 +1770,36 @@ sub match_internal ($$;@) { #{{{
130  
131  sub match_link ($$;@) { #{{{
132         my $page=shift;
133 -       my $link=lc(shift);
134 +       my $fulllink=shift;
135 +       my $link=lc($fulllink);
136         my %params=@_;
137  
138 +       print "Matching link $fulllink \n";
139 +       
140         my $from=exists $params{location} ? $params{location} : '';
141  
142 +       if (substr($fulllink, 0, 1) eq '~') {
143 +               my $specname = substr($fulllink, 1);
144 +               print "Checking link pagespec $specname \n";
145 +               if (exists $IkiWiki::named_pagespec{$specname}) {
146 +                       my $spec = $IkiWiki::named_pagespec{$specname};
147 +                       
148 +                       print "Checking all pages against $spec\n";
149 +                       
150 +                       foreach my $nextpage (keys %IkiWiki::pagesources) {
151 +                               print "Checking $nextpage against $spec\n";
152 +                               if (pagespec_match($nextpage, $spec, %params) && IkiWiki::PageSpec::match_link($page, $nextpage, %params)) {
153 +                                       return IkiWiki::SuccessReason->new("$page links to page $nextpage matching $link")
154 +                               }
155 +                       }
156 +                       
157 +                       return IkiWiki::FailReason->new("$page has no links to any pages that match $spec");
158 +               } else {
159 +                       print "Pagespec $specname not found\n";
160 +                       return IkiWiki::FailReason->new("$page cannot link to nonexistent spec name $specname");
161 +               }
162 +       }
163 +
164         # relative matching
165         if ($link =~ m!^\.! && defined $from) {
166                 $from=~s#/?[^/]+$##;
167
168 ----
169
170 #!/usr/bin/perl
171 package IkiWiki::Plugin::definepagespec;
172
173 use warnings;
174 use strict;
175 use IkiWiki 2.00;
176
177 sub import { #{{{
178         hook(type => "getsetup", id => "definepagespec", call => \&getsetup);
179         hook(type => "refresh", id => "definepagespec", call => \&refresh);
180         hook(type => "preprocess", id => "definepagespec", call => \&preprocess);
181 } #}}}
182
183 sub getsetup () { #{{{
184         return
185                 plugin => {
186                         safe => 1,
187                         rebuild => undef,
188                 },
189 } #}}}
190
191 sub refresh () { #{{{
192         # Preprocess the shortcuts page to get all the available shortcuts
193         # defined before other pages are rendered.
194         my $srcfile=srcfile("pagespecs.mdwn", 1);
195         if (! defined $srcfile) {
196                 error(gettext("definepagespec plugin will not work without a pagespecs.mdwn"));
197         }
198         IkiWiki::preprocess("pagespecs", "pagespecs", readfile($srcfile));
199 } # }}}
200
201 sub preprocess (@) { #{{{
202         my %params=@_;
203
204         if (! defined $params{name} || ! defined $params{spec}) {
205                 error gettext("missing name or spec parameter");
206         }
207
208         $IkiWiki::named_pagespec{$params{name}} = $params{spec};
209
210         #translators: This is used to display what shortcuts are defined.
211         #translators: First parameter is the name of the shortcut, the second
212         #translators: is an URL.
213         return sprintf(gettext("pagespec %s refers to <i>%s</i>"), $params{name}, $params{spec});
214 } # }}}
215
216 1