| 1 | <?php |
| 2 | |
| 3 | require_once('lib/generator/common/db.php'); |
| 4 | |
| 5 | final class Freezer |
| 6 | { |
| 7 | private $namespace = ''; |
| 8 | |
| 9 | public static function clean($directory, $namespace) { |
| 10 | db_query("TRUNCATE freezer"); |
| 11 | } |
| 12 | |
| 13 | function __construct($directory, $namespace) { |
| 14 | $this->namespace = $namespace; |
| 15 | } |
| 16 | |
| 17 | public function ids($flush = TRUE) { |
| 18 | $ids = array(); |
| 19 | |
| 20 | $query = db_query("SELECT id FROM freezer WHERE namespace = '%s'", $this->namespace); |
| 21 | while ($row = db_fetch_assoc($query)) { |
| 22 | $ids[] = $row['id']; |
| 23 | } |
| 24 | |
| 25 | return $ids; |
| 26 | } |
| 27 | |
| 28 | public function open($key, $default) { |
| 29 | $query = db_query("SELECT value FROM freezer WHERE namespace = '%s' AND id = '%s' AND BINARY id = '%s'", $this->namespace, $key, $key); |
| 30 | if ($row = db_fetch_assoc($query)) { |
| 31 | return unserialize($row['value']); |
| 32 | } |
| 33 | return $default; |
| 34 | } |
| 35 | |
| 36 | public function save($key, $content) { |
| 37 | $content = mysql_escape_string(serialize($content)); |
| 38 | |
| 39 | $query = db_query("SELECT 1 FROM freezer WHERE namespace = '%s' AND id = '%s' AND BINARY id = '%s'", $this->namespace, $key, $key); |
| 40 | if (db_fetch_assoc($query)) { |
| 41 | db_query("UPDATE freezer SET value = '%s' WHERE namespace = '%s' AND id = '%s' AND BINARY id = '%s'", $content, $this->namespace, $key, $key); |
| 42 | } |
| 43 | else { |
| 44 | db_query("INSERT INTO freezer (namespace, id, value) VALUES ('%s', '%s', '%s')", $this->namespace, $key, $content); |
| 45 | } |
| 46 | } |
| 47 | } |