<?php if (!defined('PmWiki')) exit();
/*
 * Properties - Pages properties for PmWiki 2.0
 * Copyright 2005 by D.Faure (dfaure@cpan.org)
 *
 * You can redistribute it and/or modify it under the terms of the GNU General
 * Public License as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 *
 * See http://www.pmwiki.org/wiki/Cookbook/Properties for info.
 */
SDV($Properties, '_PROPERTIES_');

Markup('properties', '<if',
	"/\\(:(?i:properties)(\\s+.*)?\\s*:\\)/e", "SetProperties(PSS('$1'))");

Markup('property', '>properties',
	"/{\\$(?i:property)\\s+(\\w+)\\s*}/e", "GetProperty('$1')");

$Conditions['property'] = "ExprProperty(\$pagename, \$condparm)";

$Conditions['propertyset'] = "ExprPropertySet(\$pagename, \$condparm)";

function SetProperties($args) {
	global $Properties;
	$opt = ParseArgs($args);
	unset($opt['#'], $opt['+']);
	foreach($opt as $n => $v)
		if($n == '-')
			foreach($opt[$n] as $r)
				unset($GLOBALS[$Properties][$r]);
		else
			$GLOBALS[$Properties][$n] = $v;
}

function GetProperty($n) {
	global $Properties;
	return isset($GLOBALS[$Properties][$n]) ? $GLOBALS[$Properties][$n] : '';
}

function ExprProperty($pagename, $condparm) {
	global $Properties;
	return preg_match('/\\s*(\\w+)\\s+("[^"]*"|\'[^\']*\'|\\S+)\\s*$/',
	                  $condparm, $m) ? (GetProperty($m[1]) == $m[2]) : false;
}

function ExprPropertySet($pagename, $condparm) {
	global $Properties;
	preg_match('/\\s*(\\w+)\\s*$/',
	                  $condparm, $m);
	$p = $m[1];
    return isset($GLOBALS[$Properties][$p]);
}