]> sipb.mit.edu Git - ikiwiki.git/blob - doc/todo/tracking_bugs_with_dependencies.mdwn
fix tag
[ikiwiki.git] / 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 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.)
110
111 The following three inlines work for me with this patch:
112
113     Bugs:
114     
115     [ [!inline pages="define(~bugs, bugs/* and ! */Discussion) and ~bugs" archive="yes"]]
116     
117     OpenBugs:
118     
119     [ [!inline pages="define(~bugs, bugs/* and ! */Discussion) and define(~openbugs,~bugs and !link(done)) and ~openbugs" archive="yes"]]
120     
121     ReadyBugs:
122     
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"]]
124
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]]
128
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.
135 >>
136 >>>> `%params` contains the parameters passed to `pagespec_match`, not
137 >>>> user-supplied parameters. The user-supplied parameter to a function
138 >>>> like `match_glob()` or `match_link()` is passed in the second positional parameter. --[[Joey]]
139
140 >>>>> OK.  That seems reasonable then.  The only problem is that my PERLfu is not strong enough to make it
141 >>>>> work.  I really have to wonder what substance was influencing the designers of PERL...
142 >>>>> I can't figure out how to use the %params.  And I'm pissed off enough with PERL that I'm not going
143 >>>>> to try and figure it out any more.  There are two patches below now.  The first one uses an extra
144 >>>>> argument and works.  The second one tries to use %params and doesn't - take your pick :-). -- [[Will]]
145
146 >> What do you think is best to do about `is_globlist()`?  At the moment it requires that the 'second word', as
147 >> 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).
148 >> My thought was just to search for 'and' or 'or' as words anywhere in the pagespec.  Thoughts?
149
150 >>> Dunno, we could just finish deprecating it. Or change the regexp to
151 >>> skip over spaces in parens. (`/[^\s]+\s+([^)]+)/`) --[[Joey]]
152
153 >>>> I think I have a working regexp now.
154
155 >> Oh, one more thing.  In pagespec_translate (now pagespec_makeperl), there is a part of the regular expression for `# any other text`.
156 >> This contained `()`, which has no effect.  I replaced that with `\(\)`, but that is a change in the definition of pagespecs unrelated to the
157 >> rest of this patch. In a related change, commands were not able to contain `)` in their parameters.  I've extended that so the cannot
158 >> contain `(` or `)`.  -- [[Will]]
159
160 >>> `[^\s()]+` is a character class matching all characters not spaces or
161 >>> parens. Since the pervious terminals in the regexp consume most
162 >>> occurances of an open paren or close paren, it's unlikely for one to
163 >>> get through to that part of the regexp. For example, "foo()" will be
164 >>> matched by the command matcher; "(foo)" will be matched by the open
165 >>> paren literal terminal. "foo(" and "foo)" can get through to the
166 >>> end, and would be matched as a page name, if it didn't exclude parens.
167 >>>
168 >>> So why exclude them? Well, consider "foo and(bar and baz)". We don't
169 >>> want it to match "and(" as a page name!
170 >>> 
171 >>> Escaping the parens in the character class actually changes nothing; the
172 >>> changed character class still matches all characters not spaces or
173 >>> parens. (Try it!).
174 >>> 
175 >>> Re commands containing '(', I don't really see any reason not to
176 >>> allow that, unless it breaks something. --[[Joey]]
177
178 >>>> Oh, I didn't realise you didn't need to escape parens inside [].  All else I
179 >>>> I understood.  I have stopped commands from containing parens because
180 >>>> once you allow that then you might have a extra level of depth in the parsing
181 >>>> of define() statements. -- [[Will]]
182
183 >>> Updated patch.  Moved the specFuncsRef to the front of the arg list.  Still haven't thought through the security implications of
184 >>> having it in `%params`.  I've also removed all the debugging `print` statements.  And I've updated the `is_globlist()` function.
185 >>> I think this is ready for people other than me to have a play.  It is not well enough tested to commit just yet.
186 >>> -- [[Will]]
187
188 ----
189
190 diff --git a/IkiWiki.pm b/IkiWiki.pm
191 index e476521..532aaf5 100644
192 --- a/IkiWiki.pm
193 +++ b/IkiWiki.pm
194 @@ -1524,7 +1524,16 @@ sub globlist_to_pagespec ($) { #{{{
195  
196  sub is_globlist ($) { #{{{
197         my $s=shift;
198 -       return ( $s =~ /[^\s]+\s+([^\s]+)/ && $1 ne "and" && $1 ne "or" );
199 +       return ! ($s =~ /
200 +                       (^\s*
201 +                               [^\s\(]+                # single item
202 +                                       (\(                     # possibly with parens after it
203 +                                               ([^\)]* # with stuff inside those parens
204 +                                               (\([^\)]*\))*)* # maybe even nested parens
205 +                                       \))?\s*$
206 +                       ) |
207 +                               (\s and \s) | (\s or \s)        # or we find 'and' or 'or' somewhere
208 +                       /x);
209  } #}}}
210  
211  sub safequote ($) { #{{{
212 @@ -1605,7 +1614,7 @@ sub pagespec_merge ($$) { #{{{
213         return "($a) or ($b)";
214  } #}}}
215  
216 -sub pagespec_translate ($) { #{{{
217 +sub pagespec_makeperl ($) { #{{{
218         my $spec=shift;
219  
220         # Support for old-style GlobLists.
221 @@ -1624,9 +1633,11 @@ sub pagespec_translate ($) { #{{{
222                 |
223                         \)              # )
224                 |
225 -                       \w+\([^\)]*\)   # command(params)
226 +                       define\(\s*~\w+\s*,((\([^\(\)]*\)) | ([^\(\)]+))+\)     # define(~specName, spec) - spec can contain parens 1 deep
227 +               |
228 +                       \w+\([^\(\)]*\) # command(params) - params cannot contain parens
229                 |
230 -                       [^\s()]+        # any other text
231 +                       [^\s\(\)]+      # any other text
232                 )
233                 \s*             # ignore whitespace
234         }igx) {
235 @@ -1640,16 +1651,23 @@ sub pagespec_translate ($) { #{{{
236                 elsif ($word eq "(" || $word eq ")" || $word eq "!") {
237                         $code.=' '.$word;
238                 }
239 +               elsif ($word =~ /^define\(\s*~(\w+)\s*,(.*)\)$/) {
240 +                       $code .= " (\$specFuncsRef->{$1}=";
241 +                       $code .= "memoize(";
242 +                       $code .= &pagespec_makeperl($2);
243 +                       $code .= ")";
244 +                       $code .= ") ";
245 +               }
246                 elsif ($word =~ /^(\w+)\((.*)\)$/) {
247                         if (exists $IkiWiki::PageSpec::{"match_$1"}) {
248 -                               $code.="IkiWiki::PageSpec::match_$1(\$page, ".safequote($2).", \@_)";
249 +                               $code.="IkiWiki::PageSpec::match_$1(\$specFuncsRef, \$page, ".safequote($2).", \@_)";
250                         }
251                         else {
252                                 $code.=' 0';
253                         }
254                 }
255                 else {
256 -                       $code.=" IkiWiki::PageSpec::match_glob(\$page, ".safequote($word).", \@_)";
257 +                       $code.=" IkiWiki::PageSpec::match_glob(\$specFuncsRef, \$page, ".safequote($word).", \@_)";
258                 }
259         }
260  
261 @@ -1657,8 +1675,16 @@ sub pagespec_translate ($) { #{{{
262                 $code=0;
263         }
264  
265 +       return 'sub { my $specFuncsRef=shift; my $page=shift; '.$code.' }';
266 +} #}}}
267 +
268 +sub pagespec_translate ($) { #{{{
269 +       my $spec=shift;
270 +
271 +       my $code = pagespec_makeperl($spec);
272 +
273         no warnings;
274 -       return eval 'sub { my $page=shift; '.$code.' }';
275 +       return eval $code;
276  } #}}}
277  
278  sub pagespec_match ($$;@) { #{{{
279 @@ -1673,7 +1699,7 @@ sub pagespec_match ($$;@) { #{{{
280  
281         my $sub=pagespec_translate($spec);
282         return IkiWiki::FailReason->new("syntax error in pagespec \"$spec\"") if $@;
283 -       return $sub->($page, @params);
284 +       return $sub->({}, $page, @params);
285  } #}}}
286  
287  sub pagespec_valid ($) { #{{{
288 @@ -1722,11 +1748,71 @@ sub new { #{{{
289  
290  package IkiWiki::PageSpec;
291  
292 -sub match_glob ($$;@) { #{{{
293 +sub check_named_spec($$$;@) {
294 +       my $specFuncsRef=shift;
295 +       my $page=shift;
296 +       my $specName=shift;
297 +       my %params=@_;
298 +       
299 +       return IkiWiki::FailReason->new("Named page spec '$specName' is not valid")
300 +               unless (substr($specName, 0, 1) eq '~');
301 +       
302 +       $specName = substr($specName, 1);
303 +
304 +       if (exists $specFuncsRef->{$specName}) {
305 +               # remove the named spec from the spec refs
306 +               # when we recurse to avoid infinite recursion
307 +               my $sub = $specFuncsRef->{$specName};
308 +               $specFuncsRef->{$specName} = undef;
309 +               my $result = $sub->($specFuncsRef, $page, %params);
310 +               $specFuncsRef->{$specName} = $sub;
311 +               return $result;
312 +       } else {
313 +               return IkiWiki::FailReason->new("Page spec '$specName' does not exist");
314 +       }
315 +}
316 +
317 +sub check_named_spec_existential($$$$;@) {
318 +       my $specFuncsRef=shift;
319 +       my $page=shift;
320 +       my $specName=shift;
321 +       my $funcref=shift;
322 +       my %params=@_;
323 +       
324 +       return IkiWiki::FailReason->new("Named page spec '$specName' is not valid")
325 +               unless (substr($specName, 0, 1) eq '~');
326 +       $specName = substr($specName, 1);
327 +       
328 +       if (exists $specFuncsRef->{$specName}) {
329 +               # remove the named spec from the spec refs
330 +               # when we recurse to avoid infinite recursion
331 +               my $sub = $specFuncsRef->{$specName};
332 +               $specFuncsRef->{$specName} = undef;
333 +               
334 +               foreach my $nextpage (keys %IkiWiki::pagesources) {
335 +                       if ($sub->($specFuncsRef, $nextpage, %params)) {
336 +                               my $tempResult = $funcref->($specFuncsRef, $page, $nextpage, %params);
337 +                               return $tempResult if ($tempResult);
338 +                       }
339 +               }
340 +               
341 +               $specFuncsRef->{$specName} = $sub;
342 +               return IkiWiki::FailReason->new("No page in spec '$specName' was successfully matched");
343 +       } else {
344 +               return IkiWiki::FailReason->new("Named page spec '$specName' does not exist");
345 +       }
346 +}
347 +
348 +sub match_glob ($$$;@) { #{{{
349 +       my $specFuncsRef=shift;
350         my $page=shift;
351         my $glob=shift;
352         my %params=@_;
353         
354 +       if (substr($glob, 0, 1) eq '~') {
355 +               return check_named_spec($specFuncsRef, $page, $glob);
356 +       }
357 +
358         my $from=exists $params{location} ? $params{location} : '';
359         
360         # relative matching
361 @@ -1750,17 +1836,23 @@ sub match_glob ($$;@) { #{{{
362         }
363  } #}}}
364  
365 -sub match_internal ($$;@) { #{{{
366 -       return match_glob($_[0], $_[1], @_, internal => 1)
367 +sub match_internal ($$$;@) { #{{{
368 +       return match_glob(shift, shift, shift, @_, internal => 1)
369  } #}}}
370  
371 -sub match_link ($$;@) { #{{{
372 +sub match_link ($$$;@) { #{{{
373 +       my $specFuncsRef=shift;
374         my $page=shift;
375 -       my $link=lc(shift);
376 +       my $fulllink=shift;
377 +       my $link=lc($fulllink);
378         my %params=@_;
379  
380 -       my $from=exists $params{location} ? $params{location} : '';
381 +       if (substr($fulllink, 0, 1) eq '~') {
382 +               return check_named_spec_existential($specFuncsRef, $page, $fulllink, \&match_link);
383 +       }
384  
385 +       my $from=exists $params{location} ? $params{location} : '';
386 +       
387         # relative matching
388         if ($link =~ m!^\.! && defined $from) {
389                 $from=~s#/?[^/]+$##;
390 @@ -1778,17 +1870,21 @@ sub match_link ($$;@) { #{{{
391                 }
392                 else {
393                         return IkiWiki::SuccessReason->new("$page links to page $p matching $link")
394 -                               if match_glob($p, $link, %params);
395 +                               if match_glob($specFuncsRef, $p, $link, %params);
396                 }
397         }
398         return IkiWiki::FailReason->new("$page does not link to $link");
399  } #}}}
400  
401 -sub match_backlink ($$;@) { #{{{
402 -       return match_link($_[1], $_[0], @_);
403 +sub match_backlink ($$$;@) { #{{{
404 +       my $specFuncsRef=shift;
405 +       my $page=shift;
406 +       my $backlink=shift;
407 +       return match_link($specFuncsRef, $backlink, $page, @_);
408  } #}}}
409  
410 -sub match_created_before ($$;@) { #{{{
411 +sub match_created_before ($$$;@) { #{{{
412 +       my $specFuncsRef=shift;
413         my $page=shift;
414         my $testpage=shift;
415  
416 @@ -1805,7 +1901,8 @@ sub match_created_before ($$;@) { #{{{
417         }
418  } #}}}
419  
420 -sub match_created_after ($$;@) { #{{{
421 +sub match_created_after ($$$;@) { #{{{
422 +       my $specFuncsRef=shift;
423         my $page=shift;
424         my $testpage=shift;
425  
426 @@ -1822,8 +1919,12 @@ sub match_created_after ($$;@) { #{{{
427         }
428  } #}}}
429  
430 -sub match_creation_day ($$;@) { #{{{
431 -       if ((gmtime($IkiWiki::pagectime{shift()}))[3] == shift) {
432 +sub match_creation_day ($$$;@) { #{{{
433 +       shift;
434 +       my $page=shift;
435 +       my $time=shift;
436 +
437 +       if ((gmtime($IkiWiki::pagectime{$page}))[3] == $time) {
438                 return IkiWiki::SuccessReason->new('creation_day matched');
439         }
440         else {
441 @@ -1831,8 +1932,12 @@ sub match_creation_day ($$;@) { #{{{
442         }
443  } #}}}
444  
445 -sub match_creation_month ($$;@) { #{{{
446 -       if ((gmtime($IkiWiki::pagectime{shift()}))[4] + 1 == shift) {
447 +sub match_creation_month ($$$;@) { #{{{
448 +       shift;
449 +       my $page=shift;
450 +       my $time=shift;
451 +
452 +       if ((gmtime($IkiWiki::pagectime{$page}))[4] + 1 == $time) {
453                 return IkiWiki::SuccessReason->new('creation_month matched');
454         }
455         else {
456 @@ -1840,8 +1945,12 @@ sub match_creation_month ($$;@) { #{{{
457         }
458  } #}}}
459  
460 -sub match_creation_year ($$;@) { #{{{
461 -       if ((gmtime($IkiWiki::pagectime{shift()}))[5] + 1900 == shift) {
462 +sub match_creation_year ($$$;@) { #{{{
463 +       shift;
464 +       my $page=shift;
465 +       my $time=shift;
466 +
467 +       if ((gmtime($IkiWiki::pagectime{$page}))[5] + 1900 == $time) {
468                 return IkiWiki::SuccessReason->new('creation_year matched');
469         }
470         else {
471 diff --git a/IkiWiki/Plugin/attachment.pm b/IkiWiki/Plugin/attachment.pm
472 index f1f792a..a410e48 100644
473 --- a/IkiWiki/Plugin/attachment.pm
474 +++ b/IkiWiki/Plugin/attachment.pm
475 @@ -291,7 +291,8 @@ sub attachment_list ($) { #{{{
476  
477  package IkiWiki::PageSpec;
478  
479 -sub match_user ($$;@) { #{{{
480 +sub match_user ($$$;@) { #{{{
481 +       shift;
482         shift;
483         my $user=shift;
484         my %params=@_;
485 @@ -311,7 +312,8 @@ sub match_user ($$;@) { #{{{
486         }
487  } #}}}
488  
489 -sub match_ip ($$;@) { #{{{
490 +sub match_ip ($$$;@) { #{{{
491 +       shift;
492         shift;
493         my $ip=shift;
494         my %params=@_;
495 diff --git a/IkiWiki/Plugin/conditional.pm b/IkiWiki/Plugin/conditional.pm
496 index 7716fce..c0dbb50 100644
497 --- a/IkiWiki/Plugin/conditional.pm
498 +++ b/IkiWiki/Plugin/conditional.pm
499 @@ -70,7 +70,8 @@ sub preprocess_if (@) { #{{{
500  
501  package IkiWiki::PageSpec;
502  
503 -sub match_enabled ($$;@) { #{{{
504 +sub match_enabled ($$$;@) { #{{{
505 +       shift;
506         shift;
507         my $plugin=shift;
508         
509 @@ -83,13 +84,14 @@ sub match_enabled ($$;@) { #{{{
510         }
511  } #}}}
512  
513 -sub match_sourcepage ($$;@) { #{{{
514 +sub match_sourcepage ($$$;@) { #{{{
515 +       my $specFuncsRef=shift;
516         shift;
517         my $glob=shift;
518         my %params=@_;
519  
520         return IkiWiki::FailReason->new("cannot match sourcepage") unless exists $params{sourcepage};
521 -       if (match_glob($params{sourcepage}, $glob, @_)) {
522 +       if (match_glob($specFuncsRef, $params{sourcepage}, $glob, @_)) {
523                 return IkiWiki::SuccessReason->new("sourcepage matches $glob");
524         }
525         else {
526 @@ -97,13 +99,14 @@ sub match_sourcepage ($$;@) { #{{{
527         }
528  } #}}}
529  
530 -sub match_destpage ($$;@) { #{{{
531 +sub match_destpage ($$$;@) { #{{{
532 +       my $specFuncsRef=shift;
533         shift;
534         my $glob=shift;
535         my %params=@_;
536         
537         return IkiWiki::FailReason->new("cannot match destpage") unless exists $params{destpage};
538 -       if (match_glob($params{destpage}, $glob, @_)) {
539 +       if (match_glob($specFuncsRef, $params{destpage}, $glob, @_)) {
540                 return IkiWiki::SuccessReason->new("destpage matches $glob");
541         }
542         else {
543 @@ -111,7 +114,8 @@ sub match_destpage ($$;@) { #{{{
544         }
545  } #}}}
546  
547 -sub match_included ($$;@) { #{{{
548 +sub match_included ($$$;@) { #{{{
549 +       shift;
550         shift;
551         shift;
552         my %params=@_;
553 diff --git a/IkiWiki/Plugin/filecheck.pm b/IkiWiki/Plugin/filecheck.pm
554 index 6f71be3..3592342 100644
555 --- a/IkiWiki/Plugin/filecheck.pm
556 +++ b/IkiWiki/Plugin/filecheck.pm
557 @@ -66,7 +66,8 @@ sub humansize ($) { #{{{
558  
559  package IkiWiki::PageSpec;
560  
561 -sub match_maxsize ($$;@) { #{{{
562 +sub match_maxsize ($$$;@) { #{{{
563 +       shift;
564         my $page=shift;
565         my $maxsize=eval{IkiWiki::Plugin::attachment::parsesize(shift)};
566         if ($@) {
567 @@ -87,7 +88,8 @@ sub match_maxsize ($$;@) { #{{{
568         }
569  } #}}}
570  
571 -sub match_minsize ($$;@) { #{{{
572 +sub match_minsize ($$$;@) { #{{{
573 +       shift;
574         my $page=shift;
575         my $minsize=eval{IkiWiki::Plugin::attachment::parsesize(shift)};
576         if ($@) {
577 @@ -108,7 +110,8 @@ sub match_minsize ($$;@) { #{{{
578         }
579  } #}}}
580  
581 -sub match_mimetype ($$;@) { #{{{
582 +sub match_mimetype ($$$;@) { #{{{
583 +       shift;
584         my $page=shift;
585         my $wanted=shift;
586  
587 @@ -138,7 +141,8 @@ sub match_mimetype ($$;@) { #{{{
588         }
589  } #}}}
590  
591 -sub match_virusfree ($$;@) { #{{{
592 +sub match_virusfree ($$$;@) { #{{{
593 +       shift;
594         my $page=shift;
595         my $wanted=shift;
596  
597 @@ -180,7 +184,7 @@ sub match_virusfree ($$;@) { #{{{
598         }
599  } #}}}
600  
601 -sub match_ispage ($$;@) { #{{{
602 +sub match_ispage ($$$;@) { #{{{
603         my $filename=shift;
604  
605         if (defined IkiWiki::pagetype($filename)) {
606 diff --git a/IkiWiki/Plugin/meta.pm b/IkiWiki/Plugin/meta.pm
607 index b2c85c8..788f248 100644
608 --- a/IkiWiki/Plugin/meta.pm
609 +++ b/IkiWiki/Plugin/meta.pm
610 @@ -263,6 +263,7 @@ sub pagetemplate (@) { #{{{
611  
612  sub match { #{{{
613         my $field=shift;
614 +       shift;
615         my $page=shift;
616         
617         # turn glob into a safe regexp
618 @@ -291,23 +292,23 @@ sub match { #{{{
619  
620  package IkiWiki::PageSpec;
621  
622 -sub match_title ($$;@) { #{{{
623 +sub match_title ($$$;@) { #{{{
624         IkiWiki::Plugin::meta::match("title", @_);      
625  } #}}}
626  
627 -sub match_author ($$;@) { #{{{
628 +sub match_author ($$$;@) { #{{{
629         IkiWiki::Plugin::meta::match("author", @_);
630  } #}}}
631  
632 -sub match_authorurl ($$;@) { #{{{
633 +sub match_authorurl ($$$;@) { #{{{
634         IkiWiki::Plugin::meta::match("authorurl", @_);
635  } #}}}
636  
637 -sub match_license ($$;@) { #{{{
638 +sub match_license ($$$;@) { #{{{
639         IkiWiki::Plugin::meta::match("license", @_);
640  } #}}}
641  
642 -sub match_copyright ($$;@) { #{{{
643 +sub match_copyright ($$$;@) { #{{{
644         IkiWiki::Plugin::meta::match("copyright", @_);
645  } #}}}
646  
647 ----
648
649 diff --git a/IkiWiki.pm b/IkiWiki.pm
650 index e476521..0751d56 100644
651 --- a/IkiWiki.pm
652 +++ b/IkiWiki.pm
653 @@ -1524,7 +1524,16 @@ sub globlist_to_pagespec ($) { #{{{
654  
655  sub is_globlist ($) { #{{{
656         my $s=shift;
657 -       return ( $s =~ /[^\s]+\s+([^\s]+)/ && $1 ne "and" && $1 ne "or" );
658 +       return ! ($s =~ /
659 +                       (^\s*
660 +                               [^\s(]+         # single item
661 +                                       (\(                     # possibly with parens after it
662 +                                               ([^)]*  # with stuff inside those parens
663 +                                               (\([^)]*\))*)*  # maybe even nested parens
664 +                                       \))?\s*$
665 +                       ) |
666 +                               (\s and \s) | (\s or \s)        # or we find 'and' or 'or' somewhere
667 +                       /x);
668  } #}}}
669  
670  sub safequote ($) { #{{{
671 @@ -1605,7 +1614,7 @@ sub pagespec_merge ($$) { #{{{
672         return "($a) or ($b)";
673  } #}}}
674  
675 -sub pagespec_translate ($) { #{{{
676 +sub pagespec_makeperl ($) { #{{{
677         my $spec=shift;
678  
679         # Support for old-style GlobLists.
680 @@ -1624,7 +1633,9 @@ sub pagespec_translate ($) { #{{{
681                 |
682                         \)              # )
683                 |
684 -                       \w+\([^\)]*\)   # command(params)
685 +                       define\(\s*~\w+\s*,((\([^()]*\)) | ([^()]+))+\) # define(~specName, spec) - spec can contain parens 1 deep
686 +               |
687 +                       \w+\([^()]*\)   # command(params) - params cannot contain parens
688                 |
689                         [^\s()]+        # any other text
690                 )
691 @@ -1640,16 +1651,23 @@ sub pagespec_translate ($) { #{{{
692                 elsif ($word eq "(" || $word eq ")" || $word eq "!") {
693                         $code.=' '.$word;
694                 }
695 +               elsif ($word =~ /^define\(\s*~(\w+)\s*,(.*)\)$/) {
696 +                       $code .= " (\$params{specFuncs}->{$1}=";        # (exists \$params{specFuncs}) && 
697 +                       $code .= "memoize(";
698 +                       $code .= &pagespec_makeperl($2);
699 +                       $code .= ")";
700 +                       $code .= ") ";
701 +               }
702                 elsif ($word =~ /^(\w+)\((.*)\)$/) {
703                         if (exists $IkiWiki::PageSpec::{"match_$1"}) {
704 -                               $code.="IkiWiki::PageSpec::match_$1(\$page, ".safequote($2).", \@_)";
705 +                               $code.="IkiWiki::PageSpec::match_$1(\$page, ".safequote($2).", \%params)";
706                         }
707                         else {
708                                 $code.=' 0';
709                         }
710                 }
711                 else {
712 -                       $code.=" IkiWiki::PageSpec::match_glob(\$page, ".safequote($word).", \@_)";
713 +                       $code.=" IkiWiki::PageSpec::match_glob(\$page, ".safequote($word).", \%params)";
714                 }
715         }
716  
717 @@ -1657,23 +1675,36 @@ sub pagespec_translate ($) { #{{{
718                 $code=0;
719         }
720  
721 +       return 'sub { my $page=shift; my %params = @_; '.$code.' }';
722 +} #}}}
723 +
724 +sub pagespec_translate ($) { #{{{
725 +       my $spec=shift;
726 +
727 +       my $code = pagespec_makeperl($spec);
728 +
729 +       print "Spec '$spec' generated code '$code'\n";
730 +
731         no warnings;
732 -       return eval 'sub { my $page=shift; '.$code.' }';
733 +       return eval $code;
734  } #}}}
735  
736  sub pagespec_match ($$;@) { #{{{
737         my $page=shift;
738         my $spec=shift;
739         my @params=@_;
740 +       my %params=@_;
741  
742         # Backwards compatability with old calling convention.
743         if (@params == 1) {
744 -               unshift @params, 'location';
745 +               %params = { location => $params[1] };
746         }
747  
748 +       $params{specFuncs} = {} unless exists $params{specFuncs};
749 +
750         my $sub=pagespec_translate($spec);
751         return IkiWiki::FailReason->new("syntax error in pagespec \"$spec\"") if $@;
752 -       return $sub->($page, @params);
753 +       return $sub->($page, %params);
754  } #}}}
755  
756  sub pagespec_valid ($) { #{{{
757 @@ -1722,11 +1753,84 @@ sub new { #{{{
758  
759  package IkiWiki::PageSpec;
760  
761 +sub check_named_spec($$;@) {
762 +       my $page=shift;
763 +       my $specName=shift;
764 +       my %params=@_;
765 +       
766 +       print "Checking named spec $specName\n";
767 +       
768 +       error("Unable to find specFuncs in params to check_named_spec()!") unless exists $params{specFuncs};
769 +       my $specFuncsRef=$params{specFuncs};
770 +       
771 +       print "A\n";
772 +       
773 +       return IkiWiki::FailReason->new("Named page spec '$specName' is not valid")
774 +               unless (substr($specName, 0, 1) eq '~');
775 +       
776 +       $specName = substr($specName, 1);
777 +
778 +       if (exists $specFuncsRef->{$specName}) {
779 +               # remove the named spec from the spec refs
780 +               # when we recurse to avoid infinite recursion
781 +               my $sub = $specFuncsRef->{$specName};
782 +               $specFuncsRef->{$specName} = undef;
783 +               my $result = $sub->($specFuncsRef, $page, %params);
784 +               $specFuncsRef->{$specName} = $sub;
785 +               return $result;
786 +       } else {
787 +               print "Invalid specname\n";
788 +               return IkiWiki::FailReason->new("Page spec '$specName' does not exist");
789 +       }
790 +}
791 +
792 +sub check_named_spec_existential($$$;@) {
793 +       my $page=shift;
794 +       my $specName=shift;
795 +       my $funcref=shift;
796 +       my %params=@_;
797 +       
798 +       print "(Existential) Checking named spec $specName\n";
799 +       
800 +       error("Unable to find specFuncs in params to check_named_spec()!") unless exists $params{specFuncs};
801 +       my $specFuncsRef=$params{specFuncs};
802 +       
803 +       print "B\n";
804 +       
805 +       return IkiWiki::FailReason->new("Named page spec '$specName' is not valid")
806 +               unless (substr($specName, 0, 1) eq '~');
807 +       $specName = substr($specName, 1);
808 +       
809 +       if (exists $specFuncsRef->{$specName}) {
810 +               # remove the named spec from the spec refs
811 +               # when we recurse to avoid infinite recursion
812 +               my $sub = $specFuncsRef->{$specName};
813 +               $specFuncsRef->{$specName} = undef;
814 +               
815 +               foreach my $nextpage (keys %IkiWiki::pagesources) {
816 +                       if ($sub->($specFuncsRef, $nextpage, %params)) {
817 +                               my $tempResult = $funcref->($page, $nextpage, %params);
818 +                               return $tempResult if ($tempResult);
819 +                       }
820 +               }
821 +               
822 +               $specFuncsRef->{$specName} = $sub;
823 +               return IkiWiki::FailReason->new("No page in spec '$specName' was successfully matched");
824 +       } else {
825 +               print "Invalid specname\n";
826 +               return IkiWiki::FailReason->new("Named page spec '$specName' does not exist");
827 +       }
828 +}
829 +
830  sub match_glob ($$;@) { #{{{
831         my $page=shift;
832         my $glob=shift;
833         my %params=@_;
834         
835 +       if (substr($glob, 0, 1) eq '~') {
836 +               return check_named_spec($page, $glob, %params);
837 +       }
838 +
839         my $from=exists $params{location} ? $params{location} : '';
840         
841         # relative matching
842 @@ -1756,11 +1860,16 @@ sub match_internal ($$;@) { #{{{
843  
844  sub match_link ($$;@) { #{{{
845         my $page=shift;
846 -       my $link=lc(shift);
847 +       my $fulllink=shift;
848 +       my $link=lc($fulllink);
849         my %params=@_;
850  
851 -       my $from=exists $params{location} ? $params{location} : '';
852 +       if (substr($fulllink, 0, 1) eq '~') {
853 +               return check_named_spec_existential($page, $fulllink, \&match_link, %params);
854 +       }
855  
856 +       my $from=exists $params{location} ? $params{location} : '';
857 +       
858         # relative matching
859         if ($link =~ m!^\.! && defined $from) {
860                 $from=~s#/?[^/]+$##;
861 @@ -1785,12 +1894,25 @@ sub match_link ($$;@) { #{{{
862  } #}}}
863  
864  sub match_backlink ($$;@) { #{{{
865 -       return match_link($_[1], $_[0], @_);
866 +       my $page=shift;
867 +       my $backlink=shift;
868 +       my %params=@_;
869 +
870 +       if (substr($backlink, 0, 1) eq '~') {
871 +               return check_named_spec_existential($page, $backlink, \&match_backlink, %params);
872 +       }
873 +
874 +       return match_link($backlink, $page, %params);
875  } #}}}
876  
877  sub match_created_before ($$;@) { #{{{
878         my $page=shift;
879         my $testpage=shift;
880 +       my %params=@_;
881 +
882 +       if (substr($testpage, 0, 1) eq '~') {
883 +               return check_named_spec_existential($page, $testpage, \&match_created_before, %params);
884 +       }
885  
886         if (exists $IkiWiki::pagectime{$testpage}) {
887                 if ($IkiWiki::pagectime{$page} < $IkiWiki::pagectime{$testpage}) {
888 @@ -1808,6 +1930,11 @@ sub match_created_before ($$;@) { #{{{
889  sub match_created_after ($$;@) { #{{{
890         my $page=shift;
891         my $testpage=shift;
892 +       my %params=@_;
893 +
894 +       if (substr($testpage, 0, 1) eq '~') {
895 +               return check_named_spec_existential($page, $testpage, \&match_created_after, %params);
896 +       }
897  
898         if (exists $IkiWiki::pagectime{$testpage}) {
899                 if ($IkiWiki::pagectime{$page} > $IkiWiki::pagectime{$testpage}) {