Elgg  Version 2.3
Datalist.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Database;
4 
8 
20 class Datalist {
21 
22  const MAX_NAME_LENGTH = 255;
23 
25  protected $cache;
26 
28  protected $db;
29 
31  protected $logger;
32 
34  protected $table;
35 
45  $this->cache = $cache;
46  $this->db = $db;
47  $this->logger = $logger;
48  $this->table = $table;
49  }
50 
60  public function setCache(Pool $pool) {
61  $this->cache = $pool;
62  }
63 
77  public function get($name) {
78  $name = trim($name);
79  if (!$this->validateName($name)) {
80  return false;
81  }
82 
83  return $this->cache->get($name, function() use ($name) {
84 
85  $sql = "SELECT * FROM {$this->table} WHERE name = :name";
86  $params = [
87  ':name' => $name,
88  ];
89  $result = $this->db->getDataRow($sql, null, $params);
90  return $result ? $result->value : null;
91  });
92  }
93 
110  public function set($name, $value) {
111  $name = trim($name);
112  if (!$this->validateName($name)) {
113  return false;
114  }
115 
116  $sql = "
117  INSERT INTO {$this->table}
118  SET name = :name, value = :value
119  ON DUPLICATE KEY UPDATE value = :value
120  ";
121 
122  $params = [
123  ':name' => $name,
124  ':value' => $value,
125  ];
126 
127  $success = $this->db->insertData($sql, $params);
128 
129  $this->cache->put($name, $value);
130 
131  return $success !== false;
132  }
133 
161  public function runFunctionOnce($functionname, $timelastupdatedcheck = 0) {
162  $lastupdated = $this->get($functionname);
163  if ($lastupdated) {
164  $lastupdated = (int) $lastupdated;
165  } elseif ($lastupdated !== false) {
166  $lastupdated = 0;
167  } else {
168  // unable to check datalist
169  return false;
170  }
171  if (is_callable($functionname) && $lastupdated <= $timelastupdatedcheck) {
172  $functionname();
173  $this->set($functionname, time());
174  return true;
175  } else {
176  return false;
177  }
178  }
179 
187  protected function validateName($name) {
188 
189  $max = self::MAX_NAME_LENGTH;
190 
191  // Can't use elgg_strlen() because not available until core loaded.
192  if (is_callable('mb_strlen')) {
193  $is_valid = (mb_strlen($name) <= $max);
194  } else {
195  $is_valid = (strlen($name) <= $max);
196  }
197 
198  if (!$is_valid) {
199  $this->logger->error("The name length for configuration variables cannot be greater than $max");
200  }
201 
202  return $is_valid;
203  }
204 
205 }
__construct(Pool $cache, Database $db, Logger $logger, $table)
Constructor.
Definition: Datalist.php:44
validateName($name)
Verify a datalist name is valid.
Definition: Datalist.php:187
if($guid==elgg_get_logged_in_user_guid()) $name
Definition: delete.php:21
The Elgg database.
Definition: Database.php:17
$value
Definition: longtext.php:42
$params
Definition: login.php:72
if($prev_offset< 1) if($current_page==1) if(1< $start_page) if(1< ($start_page-2)) elseif($start_page==3) $max
Definition: pagination.php:95
table
Definition: admin.css.php:59
elgg subtext time
runFunctionOnce($functionname, $timelastupdatedcheck=0)
Run a function one time per installation.
Definition: Datalist.php:161
setCache(Pool $pool)
Set cache.
Definition: Datalist.php:60
http free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:5