From df5a47fec0db813ffbd7d8e456930db517d014e9 Mon Sep 17 00:00:00 2001
From: Olav Morken <olav.morken@uninett.no>
Date: Tue, 31 Mar 2009 06:58:47 +0000
Subject: [PATCH] metarefresh: Add support for 'serialize' metadata format.

git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@1453 44740490-163a-0410-bde0-09ae8108e29a
---
 .../config-templates/config-metarefresh.php   |  6 +++
 modules/metarefresh/hooks/hook_cron.php       | 11 ++++-
 modules/metarefresh/lib/MetaLoader.php        | 43 +++++++++++++++++++
 3 files changed, 59 insertions(+), 1 deletion(-)

diff --git a/modules/metarefresh/config-templates/config-metarefresh.php b/modules/metarefresh/config-templates/config-metarefresh.php
index 0fc750338..1efee0efd 100644
--- a/modules/metarefresh/config-templates/config-metarefresh.php
+++ b/modules/metarefresh/config-templates/config-metarefresh.php
@@ -20,6 +20,12 @@ $config = array(
 			),
 			'expireAfter' 		=> 60*60*24*4, // Maximum 4 days cache time.
 			'outputDir' 	=> 'metadata/metadata-kalmar-consuming/',
+
+			/*
+			 * Which output format the metadata should be saved as.
+			 * Can be 'flatfile' or 'serialize'. 'flatfile' is the default.
+			 */
+			'outputFormat' => 'flatfile',
 		),
 	),
 );
diff --git a/modules/metarefresh/hooks/hook_cron.php b/modules/metarefresh/hooks/hook_cron.php
index 375cdf744..bf91de660 100644
--- a/modules/metarefresh/hooks/hook_cron.php
+++ b/modules/metarefresh/hooks/hook_cron.php
@@ -41,7 +41,16 @@ function metarefresh_hook_cron(&$croninfo) {
 			$outputDir = $set->getString('outputDir');
 			$outputDir = $config->resolvePath($outputDir);
 
-			$metaloader->writeMetadataFiles($outputDir);
+			$outputFormat = $set->getValueValidate('outputFormat', array('flatfile', 'serialize'), 'flatfile');
+			switch ($outputFormat) {
+			case 'flatfile':
+				$metaloader->writeMetadataFiles($outputDir);
+				break;
+			case 'serialize':
+				$metaloader->writeMetadataSerialize($outputDir);
+				break;
+			}
+
 		}
 
 	} catch (Exception $e) {
diff --git a/modules/metarefresh/lib/MetaLoader.php b/modules/metarefresh/lib/MetaLoader.php
index 26067a7d8..b06ff2f67 100644
--- a/modules/metarefresh/lib/MetaLoader.php
+++ b/modules/metarefresh/lib/MetaLoader.php
@@ -184,6 +184,49 @@ class sspmod_metarefresh_MetaLoader {
 		}
 	}
 
+
+	/**
+	 * Save metadata for loading with the 'serialize' metadata loader.
+	 *
+	 * @param string $outputDir  The directory we should save the metadata to.
+	 */
+	public function writeMetadataSerialize($outputDir) {
+		assert('is_string($outputDir)');
+
+		$metaHandler = new SimpleSAML_Metadata_MetaDataStorageHandlerSerialize(array('directory' => $outputDir));
+
+		/* First we add all the metadata entries to the metadata handler. */
+		foreach ($this->metadata as $set => $elements) {
+			foreach ($elements as $m) {
+				$entityId = $m['metadata']['entityid'];
+
+				SimpleSAML_Logger::debug('metarefresh: Add metadata entry ' .
+					var_export($entityId, TRUE) . ' in set ' . var_export($set, TRUE) . '.');
+				$metaHandler->saveMetadata($entityId, $set, $m['metadata']);
+			}
+		}
+
+		/* Then we delete old entries which should no longer exist. */
+		$ct = time();
+		foreach ($metaHandler->getMetadataSets() as $set) {
+			foreach ($metaHandler->getMetadataSet($set) as $entityId => $metadata) {
+				if (!array_key_exists('expire', $metadata)) {
+					SimpleSAML_Logger::warning('metarefresh: Metadata entry without expire ' .
+						'timestamp: ' . var_export($entityId, TRUE) . ' in set ' .
+						var_export($set, TRUE) . '.');
+				}
+				if ($metadata['expire'] > $ct) {
+					continue;
+				}
+
+				SimpleSAML_Logger::debug('metarefresh: Delete expired metadata entry ' .
+					var_export($entityId, TRUE) . ' in set ' . var_export($set, TRUE) . '.');
+				$metaHandler->deleteMetadata($entityId, $set);
+			}
+		}
+	}
+
+
 	private function getTime() {
 		/* The current date, as a string. */
 		date_default_timezone_set('UTC');
-- 
GitLab