add sorting tables to the pcu view.
[monitor.git] / www / database.php
1
2 <?php 
3
4 # TODO: clean up this aweful hack.
5 system("/usr/share/monitor-server/phpconfig.py > /var/www/cgi-bin/monitor/monitorconfig.php");
6 include 'monitorconfig.php';
7 define("PICKLE_PATH", MONITOR_DATA_ROOT);
8
9 class Pickle
10 {
11         public function load($name)
12         {
13                 if ( ! $this->exists("production." . $name) )
14                 {
15                         print "Exception: No such file %s" . $name . "\n";
16                         return NULL;
17                 }
18                 $name = "production." . $name;
19                 $fname = $this->__file($name);
20                 $o = unserialize(file_get_contents($fname));
21
22                 return $o;
23         }
24         public function dump($name, $obj)
25         {
26                 if ( ! file_exists(PICKLE_PATH) )
27                 {
28                         if ( ! mkdir(PICKLE_PATH, 0777, True) )
29                         {
30                                 print "Exception: Unable to create directory :" . PICKLE_PATH . "\n";
31                         }
32                 }
33                 $name = "production." . $name;
34                 $fname = $this->__file($name);
35
36                 return file_put_contents($fname, serialize($obj));
37         }
38         private function __file($name)
39         {
40                 return sprintf("%s/%s.phpserial", PICKLE_PATH, $name);
41         }
42
43         public function exists($name)
44         {
45                 return file_exists($this->__file($name));
46         }
47
48         public function remove($name)
49         {
50                 return unlink($this->__file($name));
51         }
52
53         public function if_cached_else($cond, $name, $function)
54         {
55                 if ( $cond and $this->exists("production.%s" % $name) )
56                 {
57                         $o = $this->load($name);
58                 } else {
59                         $o = $function();
60                         if ($cond)
61                         {
62                                 $this->dump($name, $o); # cache the object using 'name'
63                         }
64                 }
65                 return o;
66         }
67 }
68
69 ?>