| 1 | <?php |
| 2 | |
| 3 | require_once('lib/generator/common/db.php'); |
| 4 | require_once('lib/generator/common/AbstractSerializer.php'); |
| 5 | |
| 6 | abstract class Serializer extends AbstractSerializer |
| 7 | { |
| 8 | private $namespace; |
| 9 | private $file_location; |
| 10 | |
| 11 | private $file; |
| 12 | private $length = 9999; |
| 13 | |
| 14 | private $queue; |
| 15 | private $limit = 50; |
| 16 | |
| 17 | public static function clean($directory, $namespace, $filename='api') { |
| 18 | db_query("TRUNCATE freezer"); |
| 19 | } |
| 20 | |
| 21 | public function __construct($directory, $suffix, $filename='api') { |
| 22 | $this->file_location = $directory . '/' . $filename . '.' . $suffix; |
| 23 | $this->namespace = $filename . '.' . $suffix; |
| 24 | } |
| 25 | |
| 26 | public function __destruct() { |
| 27 | $tmp = fopen($this->file_location . '_tmp', 'w'); |
| 28 | |
| 29 | foreach ($this->header as $header_line) { |
| 30 | fwrite($tmp, $header_line . "\n"); |
| 31 | } |
| 32 | foreach ($this->ids() as $id) { |
| 33 | foreach (explode("\n", $this->getString($id)) as $line) { |
| 34 | if ($line) { |
| 35 | fwrite($tmp, $this->indent . $line . "\n"); |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | foreach ($this->footer as $footer_line) { |
| 40 | fwrite($tmp, $footer_line . "\n"); |
| 41 | } |
| 42 | |
| 43 | fclose($tmp); |
| 44 | |
| 45 | if (file_exists($this->file_location)) { |
| 46 | unlink($this->file_location); |
| 47 | } |
| 48 | |
| 49 | rename($this->file_location . '_tmp', $this->file_location); |
| 50 | } |
| 51 | |
| 52 | public function ids() { |
| 53 | $ids = array(); |
| 54 | |
| 55 | $query = db_query("SELECT id FROM freezer WHERE namespace = '%s'", $this->namespace); |
| 56 | while ($row = db_fetch_assoc($query)) { |
| 57 | $ids[] = $row['id']; |
| 58 | } |
| 59 | |
| 60 | return $ids; |
| 61 | } |
| 62 | |
| 63 | protected function getString($id) { |
| 64 | $query = db_query("SELECT value FROM freezer WHERE namespace = '%s' AND id = '%s' AND BINARY id = '%s'", $this->namespace, $id, $id); |
| 65 | if ($result = db_fetch_assoc($query)) { |
| 66 | return $result['value']; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | public function set($id, $value) { |
| 71 | if (!$id) { |
| 72 | debug_print_backtrace(); |
| 73 | die("Called set without an ID\n"); |
| 74 | } |
| 75 | |
| 76 | $content = mysql_escape_string($this->toString($value, $id)); |
| 77 | |
| 78 | $query = db_query("SELECT 1 FROM freezer WHERE namespace = '%s' AND id = '%s' AND BINARY id = '%s'", $this->namespace, $id, $id); |
| 79 | if (db_fetch_assoc($query)) { |
| 80 | db_query("UPDATE freezer SET value = '%s' WHERE namespace = '%s' AND id = '%s' AND BINARY id = '%s'", $content, $this->namespace, $id, $id); |
| 81 | } |
| 82 | else { |
| 83 | db_query("INSERT INTO freezer (namespace, id, value) VALUES ('%s', '%s', '%s')", $this->namespace, $id, $content); |
| 84 | } |
| 85 | } |
| 86 | } |