summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Rettberg2017-02-23 14:27:51 +0100
committerSimon Rettberg2017-02-23 14:27:51 +0100
commit8829eb52f03b95a988cd35afe259d3a74f0b18cd (patch)
treede491056a354bc3de4e2719e9bb6e521b3e28371
parent[roomplanner] Make hiding of rooms in api more clever (diff)
downloadslx-admin-8829eb52f03b95a988cd35afe259d3a74f0b18cd.tar.gz
slx-admin-8829eb52f03b95a988cd35afe259d3a74f0b18cd.tar.xz
slx-admin-8829eb52f03b95a988cd35afe259d3a74f0b18cd.zip
[inc/property] Add list-of-values interface
This can be used to store multiple items per key and avoids the race conditions that manually (de)serializing a list and then using get()/set() would cause.
-rw-r--r--apis/cron.inc.php3
-rw-r--r--inc/property.inc.php58
2 files changed, 59 insertions, 2 deletions
diff --git a/apis/cron.inc.php b/apis/cron.inc.php
index 8068eb2e..470c70e6 100644
--- a/apis/cron.inc.php
+++ b/apis/cron.inc.php
@@ -26,6 +26,9 @@ foreach (glob('modules/*/hooks/cron.inc.php') as $file) {
}
switch (mt_rand(1, 10)) {
+case 2:
+ Database::exec("DELETE FROM property_list WHERE dateline <> 0 AND dateline < UNIX_TIMESTAMP()");
+ break;
case 3:
Database::exec("DELETE FROM property WHERE dateline <> 0 AND dateline < UNIX_TIMESTAMP()");
break;
diff --git a/inc/property.inc.php b/inc/property.inc.php
index 9adfbda3..b3d8081a 100644
--- a/inc/property.inc.php
+++ b/inc/property.inc.php
@@ -36,7 +36,7 @@ class Property
* Set value in property store.
*
* @param string $key key of value to set
- * @param type $value the value to store for $key
+ * @param string $value the value to store for $key
* @param int $maxAgeMinutes how long to keep this entry around at least, in minutes. 0 for infinite
*/
public static function set($key, $value, $maxAgeMinutes = 0)
@@ -54,6 +54,61 @@ class Property
}
}
+ /**
+ * Retrieve property list from the store.
+ *
+ * @param string $key Key of list to get all items for
+ * @return array All the items matching the key
+ */
+ public static function getList($key)
+ {
+ $res = Database::simpleQuery("SELECT dateline, value FROM property_list WHERE name = :key", compact('key'));
+ $NOW = time();
+ $return = array();
+ while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
+ if ($row['dateline'] != 0 && $row['dateline'] < $NOW)
+ continue;
+ $return[] = $row['value'];
+ }
+ return $return;
+ }
+
+ /**
+ * Add item to property list.
+ *
+ * @param string $key key of value to set
+ * @param string $value the value to add for $key
+ * @param int $maxAgeMinutes how long to keep this entry around at least, in minutes. 0 for infinite
+ */
+ public static function addToList($key, $value, $maxAgeMinutes = 0)
+ {
+ Database::exec("INSERT INTO property_list (name, value, dateline) VALUES (:key, :value, :dateline)", array(
+ 'key' => $key,
+ 'value' => $value,
+ 'dateline' => ($maxAgeMinutes === 0 ? 0 : time() + ($maxAgeMinutes * 60))
+ ));
+ }
+
+ /**
+ * Remove given item from property list. If the list contains this item
+ * multiple times, they will all be removed.
+ *
+ * @param string $key Key of list
+ * @param string $value item to remove
+ * @return int number of items removed
+ */
+ public static function removeFromList($key, $value)
+ {
+ return Database::exec("DELETE FROM property_list WHERE name = :key AND value = :value", array(
+ 'key' => $key,
+ 'value' => $value,
+ ));
+ }
+
+ /*
+ * Legacy getters/setters
+ */
+
public static function getServerIp()
{
return self::get('server-ip', 'none');
@@ -185,7 +240,6 @@ class Property
return self::get('password-type', 'password');
}
-
public static function getIpxeDefault()
{
return self::get('default-ipxe');