Elgg  Version 1.11
Datalist.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg\Database;
3 
5 use Elgg\Database;
6 use Elgg\Logger;
7 
19 class Datalist {
20 
22  private $cache;
23 
25  private $db;
26 
28  private $logger;
29 
31  private $table;
32 
41  public function __construct(Pool $cache, Database $db, Logger $logger, $table) {
42  $this->cache = $cache;
43  $this->db = $db;
44  $this->logger = $logger;
45  $this->table = $table;
46  }
47 
61  function get($name) {
62  $name = trim($name);
63 
64  // cannot store anything longer than 255 characters in db, so catch here
65  if (elgg_strlen($name) > 255) {
66  $this->logger->error("The name length for configuration variables cannot be greater than 255");
67  return false;
68  }
69 
70  return $this->cache->get($name, function() use ($name) {
71  $escaped_name = $this->db->sanitizeString($name);
72  $result = $this->db->getDataRow("SELECT * FROM {$this->table} WHERE name = '$escaped_name'");
73  return $result ? $result->value : null;
74  });
75  }
76 
93  function set($name, $value) {
94  $name = trim($name);
95 
96  // cannot store anything longer than 255 characters in db, so catch before we set
97  if (elgg_strlen($name) > 255) {
98  $this->logger->error("The name length for configuration variables cannot be greater than 255");
99  return false;
100  }
101 
102 
103  $escaped_name = $this->db->sanitizeString($name);
104  $escaped_value = $this->db->sanitizeString($value);
105  $success = $this->db->insertData("INSERT INTO {$this->table}"
106  . " SET name = '$escaped_name', value = '$escaped_value'"
107  . " ON DUPLICATE KEY UPDATE value = '$escaped_value'");
108 
109  $this->cache->put($name, $value);
110 
111  return $success !== false;
112  }
113 
124  function loadAll() {
125  $result = $this->db->getData("SELECT * FROM {$this->table}");
126  $map = array();
127  if (is_array($result)) {
128  foreach ($result as $row) {
129  $map[$row->name] = $row->value;
130  $this->cache->put($row->name, $row->value);
131  }
132  }
133 
134  return $map;
135  }
136 
164  function runFunctionOnce($functionname, $timelastupdatedcheck = 0) {
165  $lastupdated = $this->get($functionname);
166  if ($lastupdated) {
167  $lastupdated = (int) $lastupdated;
168  } elseif ($lastupdated !== false) {
169  $lastupdated = 0;
170  } else {
171  // unable to check datalist
172  return false;
173  }
174  if (is_callable($functionname) && $lastupdated <= $timelastupdatedcheck) {
175  $functionname();
176  $this->set($functionname, time());
177  return true;
178  } else {
179  return false;
180  }
181  }
182 }
$success
Definition: view.php:29
__construct(Pool $cache, Database $db, Logger $logger, $table)
Constructor.
Definition: Datalist.php:41
if($guid==elgg_get_logged_in_user_guid()) $name
Definition: delete.php:21
$value
Definition: longtext.php:26
elgg_strlen()
Wrapper function for mb_strlen().
Definition: mb_wrapper.php:72
loadAll()
Load entire datalist in memory.
Definition: Datalist.php:124
runFunctionOnce($functionname, $timelastupdatedcheck=0)
Run a function one time per installation.
Definition: Datalist.php:164
$row
table
Definition: admin.php:59