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]]
145 >>> Updated patch. Moved the specFuncsRef to the front of the arg list. Still haven't thought through the security implications of
146 >>> having it in `%params`. I've also removed all the debugging `print` statements. And I've updated the `is_globlist()` function.
147 >>> I think this is ready for people other than me to have a play. It is not well enough tested to commit just yet.
152 diff --git a/IkiWiki.pm b/IkiWiki.pm
153 index e476521..532aaf5 100644
156 @@ -1524,7 +1524,16 @@ sub globlist_to_pagespec ($) { #{{{
158 sub is_globlist ($) { #{{{
160 - return ( $s =~ /[^\s]+\s+([^\s]+)/ && $1 ne "and" && $1 ne "or" );
163 + [^\s\(]+ # single item
164 + (\( # possibly with parens after it
165 + ([^\)]* # with stuff inside those parens
166 + (\([^\)]*\))*)* # maybe even nested parens
169 + (\s and \s) | (\s or \s) # or we find 'and' or 'or' somewhere
173 sub safequote ($) { #{{{
174 @@ -1605,7 +1614,7 @@ sub pagespec_merge ($$) { #{{{
175 return "($a) or ($b)";
178 -sub pagespec_translate ($) { #{{{
179 +sub pagespec_makeperl ($) { #{{{
182 # Support for old-style GlobLists.
183 @@ -1624,9 +1633,11 @@ sub pagespec_translate ($) { #{{{
187 - \w+\([^\)]*\) # command(params)
188 + define\(\s*~\w+\s*,((\([^\(\)]*\)) | ([^\(\)]+))+\) # define(~specName, spec) - spec can contain parens 1 deep
190 + \w+\([^\(\)]*\) # command(params) - params cannot contain parens
192 - [^\s()]+ # any other text
193 + [^\s\(\)]+ # any other text
195 \s* # ignore whitespace
197 @@ -1640,16 +1651,23 @@ sub pagespec_translate ($) { #{{{
198 elsif ($word eq "(" || $word eq ")" || $word eq "!") {
201 + elsif ($word =~ /^define\(\s*~(\w+)\s*,(.*)\)$/) {
202 + $code .= " (\$specFuncsRef->{$1}=";
203 + $code .= "memoize(";
204 + $code .= &pagespec_makeperl($2);
208 elsif ($word =~ /^(\w+)\((.*)\)$/) {
209 if (exists $IkiWiki::PageSpec::{"match_$1"}) {
210 - $code.="IkiWiki::PageSpec::match_$1(\$page, ".safequote($2).", \@_)";
211 + $code.="IkiWiki::PageSpec::match_$1(\$specFuncsRef, \$page, ".safequote($2).", \@_)";
218 - $code.=" IkiWiki::PageSpec::match_glob(\$page, ".safequote($word).", \@_)";
219 + $code.=" IkiWiki::PageSpec::match_glob(\$specFuncsRef, \$page, ".safequote($word).", \@_)";
223 @@ -1657,8 +1675,16 @@ sub pagespec_translate ($) { #{{{
227 + return 'sub { my $specFuncsRef=shift; my $page=shift; '.$code.' }';
230 +sub pagespec_translate ($) { #{{{
233 + my $code = pagespec_makeperl($spec);
236 - return eval 'sub { my $page=shift; '.$code.' }';
240 sub pagespec_match ($$;@) { #{{{
241 @@ -1673,7 +1699,7 @@ sub pagespec_match ($$;@) { #{{{
243 my $sub=pagespec_translate($spec);
244 return IkiWiki::FailReason->new("syntax error in pagespec \"$spec\"") if $@;
245 - return $sub->($page, @params);
246 + return $sub->({}, $page, @params);
249 sub pagespec_valid ($) { #{{{
250 @@ -1722,11 +1748,71 @@ sub new { #{{{
252 package IkiWiki::PageSpec;
254 -sub match_glob ($$;@) { #{{{
255 +sub check_named_spec($$$;@) {
256 + my $specFuncsRef=shift;
258 + my $specName=shift;
261 + return IkiWiki::FailReason->new("Named page spec '$specName' is not valid")
262 + unless (substr($specName, 0, 1) eq '~');
264 + $specName = substr($specName, 1);
266 + if (exists $specFuncsRef->{$specName}) {
267 + # remove the named spec from the spec refs
268 + # when we recurse to avoid infinite recursion
269 + my $sub = $specFuncsRef->{$specName};
270 + $specFuncsRef->{$specName} = undef;
271 + my $result = $sub->($specFuncsRef, $page, %params);
272 + $specFuncsRef->{$specName} = $sub;
275 + return IkiWiki::FailReason->new("Page spec '$specName' does not exist");
279 +sub check_named_spec_existential($$$$;@) {
280 + my $specFuncsRef=shift;
282 + my $specName=shift;
286 + return IkiWiki::FailReason->new("Named page spec '$specName' is not valid")
287 + unless (substr($specName, 0, 1) eq '~');
288 + $specName = substr($specName, 1);
290 + if (exists $specFuncsRef->{$specName}) {
291 + # remove the named spec from the spec refs
292 + # when we recurse to avoid infinite recursion
293 + my $sub = $specFuncsRef->{$specName};
294 + $specFuncsRef->{$specName} = undef;
296 + foreach my $nextpage (keys %IkiWiki::pagesources) {
297 + if ($sub->($specFuncsRef, $nextpage, %params)) {
298 + my $tempResult = $funcref->($specFuncsRef, $page, $nextpage, %params);
299 + return $tempResult if ($tempResult);
303 + $specFuncsRef->{$specName} = $sub;
304 + return IkiWiki::FailReason->new("No page in spec '$specName' was successfully matched");
306 + return IkiWiki::FailReason->new("Named page spec '$specName' does not exist");
310 +sub match_glob ($$$;@) { #{{{
311 + my $specFuncsRef=shift;
316 + if (substr($glob, 0, 1) eq '~') {
317 + return check_named_spec($specFuncsRef, $page, $glob);
320 my $from=exists $params{location} ? $params{location} : '';
323 @@ -1750,17 +1836,23 @@ sub match_glob ($$;@) { #{{{
327 -sub match_internal ($$;@) { #{{{
328 - return match_glob($_[0], $_[1], @_, internal => 1)
329 +sub match_internal ($$$;@) { #{{{
330 + return match_glob(shift, shift, shift, @_, internal => 1)
333 -sub match_link ($$;@) { #{{{
334 +sub match_link ($$$;@) { #{{{
335 + my $specFuncsRef=shift;
337 - my $link=lc(shift);
338 + my $fulllink=shift;
339 + my $link=lc($fulllink);
342 - my $from=exists $params{location} ? $params{location} : '';
343 + if (substr($fulllink, 0, 1) eq '~') {
344 + return check_named_spec_existential($specFuncsRef, $page, $fulllink, \&match_link);
347 + my $from=exists $params{location} ? $params{location} : '';
350 if ($link =~ m!^\.! && defined $from) {
352 @@ -1778,17 +1870,21 @@ sub match_link ($$;@) { #{{{
355 return IkiWiki::SuccessReason->new("$page links to page $p matching $link")
356 - if match_glob($p, $link, %params);
357 + if match_glob($specFuncsRef, $p, $link, %params);
360 return IkiWiki::FailReason->new("$page does not link to $link");
363 -sub match_backlink ($$;@) { #{{{
364 - return match_link($_[1], $_[0], @_);
365 +sub match_backlink ($$$;@) { #{{{
366 + my $specFuncsRef=shift;
368 + my $backlink=shift;
369 + return match_link($specFuncsRef, $backlink, $page, @_);
372 -sub match_created_before ($$;@) { #{{{
373 +sub match_created_before ($$$;@) { #{{{
374 + my $specFuncsRef=shift;
378 @@ -1805,7 +1901,8 @@ sub match_created_before ($$;@) { #{{{
382 -sub match_created_after ($$;@) { #{{{
383 +sub match_created_after ($$$;@) { #{{{
384 + my $specFuncsRef=shift;
388 @@ -1822,8 +1919,12 @@ sub match_created_after ($$;@) { #{{{
392 -sub match_creation_day ($$;@) { #{{{
393 - if ((gmtime($IkiWiki::pagectime{shift()}))[3] == shift) {
394 +sub match_creation_day ($$$;@) { #{{{
399 + if ((gmtime($IkiWiki::pagectime{$page}))[3] == $time) {
400 return IkiWiki::SuccessReason->new('creation_day matched');
403 @@ -1831,8 +1932,12 @@ sub match_creation_day ($$;@) { #{{{
407 -sub match_creation_month ($$;@) { #{{{
408 - if ((gmtime($IkiWiki::pagectime{shift()}))[4] + 1 == shift) {
409 +sub match_creation_month ($$$;@) { #{{{
414 + if ((gmtime($IkiWiki::pagectime{$page}))[4] + 1 == $time) {
415 return IkiWiki::SuccessReason->new('creation_month matched');
418 @@ -1840,8 +1945,12 @@ sub match_creation_month ($$;@) { #{{{
422 -sub match_creation_year ($$;@) { #{{{
423 - if ((gmtime($IkiWiki::pagectime{shift()}))[5] + 1900 == shift) {
424 +sub match_creation_year ($$$;@) { #{{{
429 + if ((gmtime($IkiWiki::pagectime{$page}))[5] + 1900 == $time) {
430 return IkiWiki::SuccessReason->new('creation_year matched');
433 diff --git a/IkiWiki/Plugin/attachment.pm b/IkiWiki/Plugin/attachment.pm
434 index f1f792a..a410e48 100644
435 --- a/IkiWiki/Plugin/attachment.pm
436 +++ b/IkiWiki/Plugin/attachment.pm
437 @@ -291,7 +291,8 @@ sub attachment_list ($) { #{{{
439 package IkiWiki::PageSpec;
441 -sub match_user ($$;@) { #{{{
442 +sub match_user ($$$;@) { #{{{
447 @@ -311,7 +312,8 @@ sub match_user ($$;@) { #{{{
451 -sub match_ip ($$;@) { #{{{
452 +sub match_ip ($$$;@) { #{{{
457 diff --git a/IkiWiki/Plugin/conditional.pm b/IkiWiki/Plugin/conditional.pm
458 index 7716fce..c0dbb50 100644
459 --- a/IkiWiki/Plugin/conditional.pm
460 +++ b/IkiWiki/Plugin/conditional.pm
461 @@ -70,7 +70,8 @@ sub preprocess_if (@) { #{{{
463 package IkiWiki::PageSpec;
465 -sub match_enabled ($$;@) { #{{{
466 +sub match_enabled ($$$;@) { #{{{
471 @@ -83,13 +84,14 @@ sub match_enabled ($$;@) { #{{{
475 -sub match_sourcepage ($$;@) { #{{{
476 +sub match_sourcepage ($$$;@) { #{{{
477 + my $specFuncsRef=shift;
482 return IkiWiki::FailReason->new("cannot match sourcepage") unless exists $params{sourcepage};
483 - if (match_glob($params{sourcepage}, $glob, @_)) {
484 + if (match_glob($specFuncsRef, $params{sourcepage}, $glob, @_)) {
485 return IkiWiki::SuccessReason->new("sourcepage matches $glob");
488 @@ -97,13 +99,14 @@ sub match_sourcepage ($$;@) { #{{{
492 -sub match_destpage ($$;@) { #{{{
493 +sub match_destpage ($$$;@) { #{{{
494 + my $specFuncsRef=shift;
499 return IkiWiki::FailReason->new("cannot match destpage") unless exists $params{destpage};
500 - if (match_glob($params{destpage}, $glob, @_)) {
501 + if (match_glob($specFuncsRef, $params{destpage}, $glob, @_)) {
502 return IkiWiki::SuccessReason->new("destpage matches $glob");
505 @@ -111,7 +114,8 @@ sub match_destpage ($$;@) { #{{{
509 -sub match_included ($$;@) { #{{{
510 +sub match_included ($$$;@) { #{{{
515 diff --git a/IkiWiki/Plugin/filecheck.pm b/IkiWiki/Plugin/filecheck.pm
516 index 6f71be3..3592342 100644
517 --- a/IkiWiki/Plugin/filecheck.pm
518 +++ b/IkiWiki/Plugin/filecheck.pm
519 @@ -66,7 +66,8 @@ sub humansize ($) { #{{{
521 package IkiWiki::PageSpec;
523 -sub match_maxsize ($$;@) { #{{{
524 +sub match_maxsize ($$$;@) { #{{{
527 my $maxsize=eval{IkiWiki::Plugin::attachment::parsesize(shift)};
529 @@ -87,7 +88,8 @@ sub match_maxsize ($$;@) { #{{{
533 -sub match_minsize ($$;@) { #{{{
534 +sub match_minsize ($$$;@) { #{{{
537 my $minsize=eval{IkiWiki::Plugin::attachment::parsesize(shift)};
539 @@ -108,7 +110,8 @@ sub match_minsize ($$;@) { #{{{
543 -sub match_mimetype ($$;@) { #{{{
544 +sub match_mimetype ($$$;@) { #{{{
549 @@ -138,7 +141,8 @@ sub match_mimetype ($$;@) { #{{{
553 -sub match_virusfree ($$;@) { #{{{
554 +sub match_virusfree ($$$;@) { #{{{
559 @@ -180,7 +184,7 @@ sub match_virusfree ($$;@) { #{{{
563 -sub match_ispage ($$;@) { #{{{
564 +sub match_ispage ($$$;@) { #{{{
567 if (defined IkiWiki::pagetype($filename)) {
568 diff --git a/IkiWiki/Plugin/meta.pm b/IkiWiki/Plugin/meta.pm
569 index b2c85c8..788f248 100644
570 --- a/IkiWiki/Plugin/meta.pm
571 +++ b/IkiWiki/Plugin/meta.pm
572 @@ -263,6 +263,7 @@ sub pagetemplate (@) { #{{{
579 # turn glob into a safe regexp
580 @@ -291,23 +292,23 @@ sub match { #{{{
582 package IkiWiki::PageSpec;
584 -sub match_title ($$;@) { #{{{
585 +sub match_title ($$$;@) { #{{{
586 IkiWiki::Plugin::meta::match("title", @_);
589 -sub match_author ($$;@) { #{{{
590 +sub match_author ($$$;@) { #{{{
591 IkiWiki::Plugin::meta::match("author", @_);
594 -sub match_authorurl ($$;@) { #{{{
595 +sub match_authorurl ($$$;@) { #{{{
596 IkiWiki::Plugin::meta::match("authorurl", @_);
599 -sub match_license ($$;@) { #{{{
600 +sub match_license ($$$;@) { #{{{
601 IkiWiki::Plugin::meta::match("license", @_);
604 -sub match_copyright ($$;@) { #{{{
605 +sub match_copyright ($$$;@) { #{{{
606 IkiWiki::Plugin::meta::match("copyright", @_);