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]]
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".
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.
12 >> I thought about this briefly, and got about that far.. glad you got
13 >> further. :-) --[[Joey]]
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:
21 >> `bugs/* and !*/Discussion and !link(bugs/* and !*/Discussion and !link(done))`
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
28 >> Also, it may cause ambiguities with page names that contain parens
29 >> (though some such ambigutities already exist with the pagespec syntax).
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.
42 > Does that seem like a reasonable first approach?
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?
50 >> Example: `define(~bugs, bugs/* and !*/Discussion) and define(~openbugs, ~bugs and !link(done)) and ~openbugs and !link(~openbugs)`
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.
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.
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.
59 >> So, equivilant example: `define(bugs, bugs/* and !*/Discussion) and define(openbugs, bugs and !link(done)) and openbugs and !link(openbugs)`
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.
65 >>> If you memoize the outcomes of the named pagespecs you can make in O(N.X), no?
68 >>>> Yeah, guess that'd work. :-)
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.
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.
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:
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)"]]
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.
90 foreach my $nextpage (keys %IkiWiki::pagesources)
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]].
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]]
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]]
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]]
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.)
111 The following three inlines work for me with this patch:
115 [ [!inline pages="define(~bugs, bugs/* and ! */Discussion) and ~bugs" archive="yes"]]
119 [ [!inline pages="define(~bugs, bugs/* and ! */Discussion) and define(~openbugs,~bugs and !link(done)) and ~openbugs" archive="yes"]]
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"]]
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]]
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.
136 >> What do you think is best to do about `is_globlist()`? At the moment it requires that the 'second word', as
137 >> 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).
138 >> My thought was just to search for 'and' or 'or' as words anywhere in the pagespec. Thoughts?
140 >> Oh, one more thing. In pagespec_translate (now pagespec_makeperl), there is a part of the regular expression for `# any other text`.
141 >> This contained `()`, which has no effect. I replaced that with `\(\)`, but that is a change in the definition of pagespecs unrelated to the
142 >> rest of this patch. In a related change, commands were not able to contain `)` in their parameters. I've extended that so the cannot
143 >> contain `(` or `)`. -- [[Will]]
147 diff --git a/IkiWiki.pm b/IkiWiki.pm
148 index e476521..07b71d7 100644
151 @@ -1524,7 +1524,7 @@ sub globlist_to_pagespec ($) { #{{{
153 sub is_globlist ($) { #{{{
155 - return ( $s =~ /[^\s]+\s+([^\s]+)/ && $1 ne "and" && $1 ne "or" );
156 + return 0; #( $s =~ /[^\s]+\s+([^\s]+)/ && $1 ne "and" && $1 ne "or" );
159 sub safequote ($) { #{{{
160 @@ -1605,7 +1605,7 @@ sub pagespec_merge ($$) { #{{{
161 return "($a) or ($b)";
164 -sub pagespec_translate ($) { #{{{
165 +sub pagespec_makeperl ($) { #{{{
168 # Support for old-style GlobLists.
169 @@ -1624,9 +1624,11 @@ sub pagespec_translate ($) { #{{{
173 - \w+\([^\)]*\) # command(params)
174 + define\(\s*~\w+\s*,((\([^\(\)]*\)) | ([^\(\)]+))+\) # define(~specName, spec) - spec can contain parens 1 deep
176 + \w+\([^\(\)]*\) # command(params) - params cannot contain parens
178 - [^\s()]+ # any other text
179 + [^\s\(\)]+ # any other text
181 \s* # ignore whitespace
183 @@ -1640,16 +1642,23 @@ sub pagespec_translate ($) { #{{{
184 elsif ($word eq "(" || $word eq ")" || $word eq "!") {
187 + elsif ($word =~ /^define\(\s*~(\w+)\s*,(.*)\)$/) {
188 + $code .= " (\$specFuncsRef->{$1}=";
189 + #$code .= "memoize(";
190 + $code .= pagespec_makeperl($2);
194 elsif ($word =~ /^(\w+)\((.*)\)$/) {
195 if (exists $IkiWiki::PageSpec::{"match_$1"}) {
196 - $code.="IkiWiki::PageSpec::match_$1(\$page, ".safequote($2).", \@_)";
197 + $code.="IkiWiki::PageSpec::match_$1(\$page, \$specFuncsRef, ".safequote($2).", \@_)";
204 - $code.=" IkiWiki::PageSpec::match_glob(\$page, ".safequote($word).", \@_)";
205 + $code.=" IkiWiki::PageSpec::match_glob(\$page, \$specFuncsRef, ".safequote($word).", \@_)";
209 @@ -1657,8 +1666,18 @@ sub pagespec_translate ($) { #{{{
213 + return 'sub { my $page=shift; my $specFuncsRef=shift; '.$code.' }';
216 +sub pagespec_translate ($) { #{{{
219 + my $code = pagespec_makeperl($spec);
221 + print "Spec '$spec' led to perl '$code'\n";
224 - return eval 'sub { my $page=shift; '.$code.' }';
228 sub pagespec_match ($$;@) { #{{{
229 @@ -1673,7 +1692,7 @@ sub pagespec_match ($$;@) { #{{{
231 my $sub=pagespec_translate($spec);
232 return IkiWiki::FailReason->new("syntax error in pagespec \"$spec\"") if $@;
233 - return $sub->($page, @params);
234 + return $sub->($page, {}, @params);
237 sub pagespec_valid ($) { #{{{
238 @@ -1722,13 +1741,77 @@ sub new { #{{{
240 package IkiWiki::PageSpec;
242 -sub match_glob ($$;@) { #{{{
243 +sub check_named_spec($$$;@) {
245 + my $specFuncsRef=shift;
246 + my $specName=shift;
249 + return IkiWiki::FailReason->new("Named page spec '$specName' is not valid")
250 + unless (substr($specName, 0, 1) eq '~');
252 + $specName = substr($specName, 1);
253 + print "Checking pagespec named $specName against page $page\n";
254 + if (exists $specFuncsRef->{$specName}) {
255 + # remove the named spec from the spec refs
256 + # when we recurse to avoid infinite recursion
257 + my $sub = $specFuncsRef->{$specName};
258 + $specFuncsRef->{$specName} = undef;
259 + my $result = $sub->($page, $specFuncsRef, %params);
260 + $specFuncsRef->{$specName} = $sub;
263 + print "Couldn't find pagespec\n";
264 + return IkiWiki::FailReason->new("Page spec $specName does not exist");
268 +sub check_named_spec_existential($$$$;@) {
270 + my $specFuncsRef=shift;
271 + my $specName=shift;
275 + return IkiWiki::FailReason->new("Named page spec '$specName' is not valid")
276 + unless (substr($specName, 0, 1) eq '~');
277 + $specName = substr($specName, 1);
279 + print "Checking (existential) pagespec named $specName against page $page\n";
281 + if (exists $specFuncsRef->{$specName}) {
282 + # remove the named spec from the spec refs
283 + # when we recurse to avoid infinite recursion
284 + my $sub = $specFuncsRef->{$specName};
285 + $specFuncsRef->{$specName} = undef;
287 + foreach my $nextpage (keys %IkiWiki::pagesources) {
288 + print "Checking $nextpage against $specName\n";
289 + if ($sub->($nextpage, $specFuncsRef, %params)) {
290 + print "Match! Checking spec $nextpage against function from original page $page\n";
291 + my $tempResult = $funcref->($page, $specFuncsRef, $nextpage, %params);
292 + return $tempResult if ($tempResult);
296 + $specFuncsRef->{$specName} = $sub;
297 + return IkiWiki::FailReason->new("No page in spec $specName was successfully matched");
299 + print "Couldn't find pagespec\n";
300 + return IkiWiki::FailReason->new("Page spec $specName does not exist");
304 +sub match_glob ($$$;@) { #{{{
306 + my $specFuncsRef=shift;
310 my $from=exists $params{location} ? $params{location} : '';
312 + print "Matching glob $glob \n";
315 if ($glob =~ m!^\./!) {
317 @@ -1736,6 +1819,10 @@ sub match_glob ($$;@) { #{{{
318 $glob="$from/$glob" if length $from;
321 + if (substr($glob, 0, 1) eq '~') {
322 + return check_named_spec($page, $specFuncsRef, $glob);
325 my $regexp=IkiWiki::glob2re($glob);
326 if ($page=~/^$regexp$/i) {
327 if (! IkiWiki::isinternal($page) || $params{internal}) {
328 @@ -1750,17 +1837,29 @@ sub match_glob ($$;@) { #{{{
332 -sub match_internal ($$;@) { #{{{
333 - return match_glob($_[0], $_[1], @_, internal => 1)
334 +sub match_internal ($$$;@) { #{{{
336 + my $specFuncsRef=shift;
339 + return match_glob($page, $specFuncsRef, $glob, @_, internal => 1)
342 -sub match_link ($$;@) { #{{{
343 +sub match_link ($$$;@) { #{{{
345 - my $link=lc(shift);
346 + my $specFuncsRef=shift;
347 + my $fulllink=shift;
348 + my $link=lc($fulllink);
351 + print "Matching link $fulllink \n";
353 my $from=exists $params{location} ? $params{location} : '';
355 + if (substr($fulllink, 0, 1) eq '~') {
356 + return check_named_spec_existential($page, $specFuncsRef, $fulllink, \&match_link);
360 if ($link =~ m!^\.! && defined $from) {
362 @@ -1784,12 +1883,16 @@ sub match_link ($$;@) { #{{{
363 return IkiWiki::FailReason->new("$page does not link to $link");
366 -sub match_backlink ($$;@) { #{{{
367 - return match_link($_[1], $_[0], @_);
368 +sub match_backlink ($$$;@) { #{{{
370 + my $specFuncsRef=shift;
371 + my $backlink=shift;
372 + return match_link($backlink, $specFuncsRef, $page, @_);
375 -sub match_created_before ($$;@) { #{{{
376 +sub match_created_before ($$$;@) { #{{{
378 + my $specFuncsRef=shift;
381 if (exists $IkiWiki::pagectime{$testpage}) {
382 @@ -1805,8 +1908,9 @@ sub match_created_before ($$;@) { #{{{
386 -sub match_created_after ($$;@) { #{{{
387 +sub match_created_after ($$$;@) { #{{{
389 + my $specFuncsRef=shift;
392 if (exists $IkiWiki::pagectime{$testpage}) {
393 @@ -1822,8 +1926,12 @@ sub match_created_after ($$;@) { #{{{
397 -sub match_creation_day ($$;@) { #{{{
398 - if ((gmtime($IkiWiki::pagectime{shift()}))[3] == shift) {
399 +sub match_creation_day ($$$;@) { #{{{
404 + if ((gmtime($IkiWiki::pagectime{$page}))[3] == $time) {
405 return IkiWiki::SuccessReason->new('creation_day matched');
408 @@ -1831,8 +1939,12 @@ sub match_creation_day ($$;@) { #{{{
412 -sub match_creation_month ($$;@) { #{{{
413 - if ((gmtime($IkiWiki::pagectime{shift()}))[4] + 1 == shift) {
414 +sub match_creation_month ($$$;@) { #{{{
419 + if ((gmtime($IkiWiki::pagectime{$page}))[4] + 1 == $time) {
420 return IkiWiki::SuccessReason->new('creation_month matched');
423 @@ -1840,8 +1952,12 @@ sub match_creation_month ($$;@) { #{{{
427 -sub match_creation_year ($$;@) { #{{{
428 - if ((gmtime($IkiWiki::pagectime{shift()}))[5] + 1900 == shift) {
429 +sub match_creation_year ($$$;@) { #{{{
434 + if ((gmtime($IkiWiki::pagectime{$page}))[5] + 1900 == $time) {
435 return IkiWiki::SuccessReason->new('creation_year matched');
438 diff --git a/IkiWiki/Plugin/attachment.pm b/IkiWiki/Plugin/attachment.pm
439 index f1f792a..a410e48 100644
440 --- a/IkiWiki/Plugin/attachment.pm
441 +++ b/IkiWiki/Plugin/attachment.pm
442 @@ -291,7 +291,8 @@ sub attachment_list ($) { #{{{
444 package IkiWiki::PageSpec;
446 -sub match_user ($$;@) { #{{{
447 +sub match_user ($$$;@) { #{{{
452 @@ -311,7 +312,8 @@ sub match_user ($$;@) { #{{{
456 -sub match_ip ($$;@) { #{{{
457 +sub match_ip ($$$;@) { #{{{
462 diff --git a/IkiWiki/Plugin/conditional.pm b/IkiWiki/Plugin/conditional.pm
463 index 7716fce..2110ca0 100644
464 --- a/IkiWiki/Plugin/conditional.pm
465 +++ b/IkiWiki/Plugin/conditional.pm
466 @@ -70,7 +70,8 @@ sub preprocess_if (@) { #{{{
468 package IkiWiki::PageSpec;
470 -sub match_enabled ($$;@) { #{{{
471 +sub match_enabled ($$$;@) { #{{{
476 @@ -83,13 +84,14 @@ sub match_enabled ($$;@) { #{{{
480 -sub match_sourcepage ($$;@) { #{{{
481 +sub match_sourcepage ($$$;@) { #{{{
483 + my $specFuncsRef=shift;
487 return IkiWiki::FailReason->new("cannot match sourcepage") unless exists $params{sourcepage};
488 - if (match_glob($params{sourcepage}, $glob, @_)) {
489 + if (match_glob($params{sourcepage}, $specFuncsRef, $glob, @_)) {
490 return IkiWiki::SuccessReason->new("sourcepage matches $glob");
493 @@ -97,13 +99,14 @@ sub match_sourcepage ($$;@) { #{{{
497 -sub match_destpage ($$;@) { #{{{
498 +sub match_destpage ($$$;@) { #{{{
500 + my $specFuncsRef=shift;
504 return IkiWiki::FailReason->new("cannot match destpage") unless exists $params{destpage};
505 - if (match_glob($params{destpage}, $glob, @_)) {
506 + if (match_glob($params{destpage}, $specFuncsRef, $glob, @_)) {
507 return IkiWiki::SuccessReason->new("destpage matches $glob");
510 @@ -111,7 +114,8 @@ sub match_destpage ($$;@) { #{{{
514 -sub match_included ($$;@) { #{{{
515 +sub match_included ($$$;@) { #{{{
520 diff --git a/IkiWiki/Plugin/filecheck.pm b/IkiWiki/Plugin/filecheck.pm
521 index 6f71be3..bf472c5 100644
522 --- a/IkiWiki/Plugin/filecheck.pm
523 +++ b/IkiWiki/Plugin/filecheck.pm
524 @@ -66,8 +66,9 @@ sub humansize ($) { #{{{
526 package IkiWiki::PageSpec;
528 -sub match_maxsize ($$;@) { #{{{
529 +sub match_maxsize ($$$;@) { #{{{
532 my $maxsize=eval{IkiWiki::Plugin::attachment::parsesize(shift)};
534 return IkiWiki::FailReason->new("unable to parse maxsize (or number too large)");
535 @@ -87,8 +88,9 @@ sub match_maxsize ($$;@) { #{{{
539 -sub match_minsize ($$;@) { #{{{
540 +sub match_minsize ($$$;@) { #{{{
543 my $minsize=eval{IkiWiki::Plugin::attachment::parsesize(shift)};
545 return IkiWiki::FailReason->new("unable to parse minsize (or number too large)");
546 @@ -108,8 +110,9 @@ sub match_minsize ($$;@) { #{{{
550 -sub match_mimetype ($$;@) { #{{{
551 +sub match_mimetype ($$$;@) { #{{{
557 @@ -138,8 +141,9 @@ sub match_mimetype ($$;@) { #{{{
561 -sub match_virusfree ($$;@) { #{{{
562 +sub match_virusfree ($$$;@) { #{{{
568 @@ -180,7 +184,7 @@ sub match_virusfree ($$;@) { #{{{
572 -sub match_ispage ($$;@) { #{{{
573 +sub match_ispage ($$$;@) { #{{{
576 if (defined IkiWiki::pagetype($filename)) {
577 diff --git a/IkiWiki/Plugin/meta.pm b/IkiWiki/Plugin/meta.pm
578 index b2c85c8..1ee6a69 100644
579 --- a/IkiWiki/Plugin/meta.pm
580 +++ b/IkiWiki/Plugin/meta.pm
581 @@ -264,6 +264,7 @@ sub pagetemplate (@) { #{{{
587 # turn glob into a safe regexp
588 my $re=IkiWiki::glob2re(shift);
589 @@ -291,23 +292,23 @@ sub match { #{{{
591 package IkiWiki::PageSpec;
593 -sub match_title ($$;@) { #{{{
594 +sub match_title ($$$;@) { #{{{
595 IkiWiki::Plugin::meta::match("title", @_);
598 -sub match_author ($$;@) { #{{{
599 +sub match_author ($$$;@) { #{{{
600 IkiWiki::Plugin::meta::match("author", @_);
603 -sub match_authorurl ($$;@) { #{{{
604 +sub match_authorurl ($$$;@) { #{{{
605 IkiWiki::Plugin::meta::match("authorurl", @_);
608 -sub match_license ($$;@) { #{{{
609 +sub match_license ($$$;@) { #{{{
610 IkiWiki::Plugin::meta::match("license", @_);
613 -sub match_copyright ($$;@) { #{{{
614 +sub match_copyright ($$$;@) { #{{{
615 IkiWiki::Plugin::meta::match("copyright", @_);