Elgg  Version 2.2
 All Classes Namespaces Files Functions Variables Pages
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 
57  function setCache(Pool $pool) {
58  $this->cache = $pool;
59  }
60 
74  function get($name) {
75  $name = trim($name);
76  if (!$this->validateName($name)) {
77  return false;
78  }
79 
80  return $this->cache->get($name, function() use ($name) {
81  $escaped_name = $this->db->sanitizeString($name);
82  $result = $this->db->getDataRow("SELECT * FROM {$this->table} WHERE name = '$escaped_name'");
83  return $result ? $result->value : null;
84  });
85  }
86 
103  function set($name, $value) {
104  $name = trim($name);
105  if (!$this->validateName($name)) {
106  return false;
107  }
108 
109  $escaped_name = $this->db->sanitizeString($name);
110  $escaped_value = $this->db->sanitizeString($value);
111  $success = $this->db->insertData("INSERT INTO {$this->table}"
112  . " SET name = '$escaped_name', value = '$escaped_value'"
113  . " ON DUPLICATE KEY UPDATE value = '$escaped_value'");
114 
115  $this->cache->put($name, $value);
116 
117  return $success !== false;
118  }
119 
147  function runFunctionOnce($functionname, $timelastupdatedcheck = 0) {
148  $lastupdated = $this->get($functionname);
149  if ($lastupdated) {
150  $lastupdated = (int) $lastupdated;
151  } elseif ($lastupdated !== false) {
152  $lastupdated = 0;
153  } else {
154  // unable to check datalist
155  return false;
156  }
157  if (is_callable($functionname) && $lastupdated <= $timelastupdatedcheck) {
158  $functionname();
159  $this->set($functionname, time());
160  return true;
161  } else {
162  return false;
163  }
164  }
165 
173  protected function validateName($name) {
174  // Can't use elgg_strlen() because not available until core loaded.
175  if (is_callable('mb_strlen')) {
176  $is_valid = (mb_strlen($name) <= 255);
177  } else {
178  $is_valid = (strlen($name) <= 255);
179  }
180 
181  if (!$is_valid) {
182  $this->logger->error("The name length for configuration variables cannot be greater than 255");
183  }
184 
185  return $is_valid;
186  }
187 }
__construct(Pool $cache, Database $db, Logger $logger, $table)
Constructor.
Definition: Datalist.php:41
validateName($name)
Verify a datalist name is valid.
Definition: Datalist.php:173
if($guid==elgg_get_logged_in_user_guid()) $name
Definition: delete.php:21
$value
Definition: longtext.php:26
set($name, $value)
Set the value for a datalist element.
Definition: Datalist.php:103
table
Definition: admin.css.php:59
runFunctionOnce($functionname, $timelastupdatedcheck=0)
Run a function one time per installation.
Definition: Datalist.php:147
setCache(Pool $pool)
Set cache.
Definition: Datalist.php:57