Wildcard attachexists condition

List of attachments to the Mini page :

(:attachtable Mini list=all:)
7zip.png 1622008-11-01 14:03:56
8z.jpg 1,8642008-11-01 14:03:58
bitterlemon.png 250,1422009-04-22 22:37:28
Bulgaria-locator-template.svg 80,1652008-11-01 14:03:59
inja.png 131,5252008-11-01 14:03:57
inja2.jpg 5,3242008-11-01 14:03:58
r.doc 16,8962008-11-01 14:03:59
Test.jpg 2,6712008-11-01 14:03:59
th.jpg 15,4312008-11-01 14:03:59
th00---7zip.png.jpg 1,2062011-03-28 19:57:33
th00---bitterlemon.png.jpg 3,3392009-05-09 15:01:31
th00---inja.png.jpg 5,9242011-03-28 19:57:33
th00---inja2.jpg.jpg 5,8282011-03-28 19:57:33
th00---Test.jpg.jpg 3,1882011-03-28 19:57:33

Conditionals :

(:if attachexists Mini/*.jpg:)
* attachexists *.jpg
(:if attachexists Mini/*.png:)
* attachexists *.png
(:if attachexists Mini/*.avi:)
* attachexists *.avi
(:if attachexists Mini/Test.jpg:)
* attachexists Test.jpg
(:if attachexists Mini/te*.jpg:)
* attachexists te*.jpg (case insensitive)
(:if attachexists Mini/Test.html:)
* attachexists Test.html
(:if attachexists Mini/th*---*.jpg:)
* Mini or Thumblist thumbnails exist
(:if attachexists *:)
* current page has an attachment
(:ifend:)
  • attachexists *.jpg
  • attachexists *.png
  • attachexists Test.jpg
  • attachexists te*.jpg (case insensitive)
  • Mini or Thumblist thumbnails exist

In a pagelist :

(:pagelist group=Cookbook if="attachexists {=$FullName}/*" fmt=#simplename:)

Multiple file matches. Note that this may seem counter-intuitive, but works exactly the same way as the pagelist name=*Page1,*Page2,-A* parameter.

(:if attachexists *.ogg,*.mp3,*.wav,*.au:)
* at least one audio file (not all types)
(:if attachexists Mini/*.png,*.avi:)
* either *.png, or *.avi exist (not both)
(:if attachexists Mini/*,-*.avi,-*.zip:)
* there are attachments other than *.avi, or *.zip
(:if [ attachexists Mini/*.png  && attachexists Mini/*.jpg ]:)
* both *.png, and *.jpg exist
(:ifend:)
  • either *.png, or *.avi exist (not both)
  • there are attachments other than *.avi, or *.zip
  • both *.png, and *.jpg exist

Code to add in config.php :

$Conditions['attachexists'] = 'WildcardAttachExists($pagename, $condparm)';
function WildcardAttachExists($PN, $path='*')
{
	global $UploadFileFmt;
	if (preg_match('!^(.*)/([^/]+)$!', $path, $m)) {
		$path = $m[2]; $PN = MakePageName($PN, $m[1]);
	}
	$uploaddir = FmtPageName($UploadFileFmt, $PN);
	$flist = array();
	if(preg_match("/(^|,)[!-]|[\\*\\?]/", $path)){
		if($dirp=@opendir($uploaddir)){
			while (($f=readdir($dirp))!==false)
				if($f{0}!='.' && ! preg_match("/,\\d+$/", $f) )
					$flist[$f]=$f;
			closedir($dirp);
		}
		$flist = MatchPageNames($flist,  array($path));
		return count($flist);
	}
	$upname = MakeUploadName($PN,$path);
	$filepath = FmtPageName("$UploadFileFmt/$upname", $PN);
	return file_exists($filepath);
}
 0: 00.00 MarkupToHTML begin
 1: 00.00 MarkupToHTML begin
 2: 00.07 MarkupToHTML end
 3: 00.07 MarkupToHTML begin
 4: 00.08 MarkupToHTML end
 5: 00.08 MarkupToHTML begin
 6: 00.08 FPLTemplate: Chain begin
 7: 00.08 FPLTemplate: FPLTemplateLoad
 8: 00.09 FPLTemplate: FPLTemplateDefaults
 9: 00.09 FPLTemplate: FPLTemplatePageList
10: 00.09 MakePageList pre
11: 00.09 PageListSources begin
12: 00.09 PageStore::ls begin SQLite
13: 00.09 PageStore::ls merge SQLite
14: 00.09 PageStore::ls end SQLite
15: 00.09 PageStore::ls begin $FarmD/shared.d/$FullName
16: 00.09 PageStore::ls end $FarmD/shared.d/$FullName
17: 00.09 PageStore::ls begin $FarmD/wikilib.d/$FullName
18: 00.09 PageStore::ls merge $FarmD/wikilib.d/$FullName
19: 00.09 PageStore::ls end $FarmD/wikilib.d/$FullName
20: 00.09 PageListSources end count=56
21: 00.09 PageListSort pre ret=4 order=name
22: 00.09 MakePageList items count=56, filters=PageListIf
23: 00.76 MakePageList post count=13, readc=0
24: 00.76 PageListSort begin
25: 00.76 PageListSort sort
26: 00.76 PageListSort end
27: 00.76 MakePageList end
28: 00.76 FPLTemplate: FPLTemplateSliceList
29: 00.76 FPLTemplate: FPLTemplateFormat
30: 00.76 MarkupToHTML begin
31: 00.78 MarkupToHTML end
32: 00.78 FPLTemplate: Chain end
33: 00.78 MarkupToHTML end
34: 00.78 MarkupToHTML begin
35: 00.79 MarkupToHTML end
36: 00.80 MarkupToHTML end
37: 00.81 MarkupToHTML begin
38: 00.83 MarkupToHTML end
39: 00.83 MarkupToHTML begin
40: 00.84 MarkupToHTML end
41: 00.84 now