Elgg  Version 2.3
MetastringsTable.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Database;
4 
7 
22 
24  protected $cache;
25 
27  protected $db;
28 
35  public function __construct(Pool $cache, Database $db) {
36  $this->cache = $cache;
37  $this->db = $db;
38  }
39 
56  function getId($string, $case_sensitive = true) {
57  if ($case_sensitive) {
58  return $this->getIdCaseSensitive($string);
59  } else {
60  return $this->getIdCaseInsensitive($string);
61  }
62  }
63 
73  function getMap(array $string_keys) {
74  if (!$string_keys) {
75  return [];
76  }
77  if (count($string_keys) === 1) {
78  $key = reset($string_keys);
79  return [$key => $this->getIdCaseSensitive($key)];
80  }
81 
82  $missing = array_fill_keys($string_keys, true);
83 
84  $set_element = array_map(function ($string) {
85  return "BINARY '" . $this->db->sanitizeString($string) . "'";
86  }, $string_keys);
87 
88  $set = implode(',', $set_element);
89 
90  $query = "SELECT * FROM {$this->getTableName()} WHERE string IN ($set)";
91  $ret = [];
92 
93  foreach ($this->db->getData($query) as $row) {
94  $ret[$row->string] = (int) $row->id;
95  unset($missing[$row->string]);
96  }
97  foreach (array_keys($missing) as $string) {
98  $ret[$string] = $this->getIdCaseSensitive($string);
99  }
100 
101  return $ret;
102  }
103 
112  private function getIdCaseSensitive($string) {
113  $string = (string) $string;
114  return $this->cache->get($string, function() use ($string) {
115 
116  $memcache = _elgg_get_memcache('metastrings_memcache');
117 
118  // Stash can't handle arbitrary keys
119  $result = $memcache->load(md5($string));
120  if ($result !== false) {
121  return $result;
122  }
123 
124  $query = "SELECT id FROM {$this->getTableName()} WHERE string = BINARY :string";
125  $params = [
126  ':string' => $string,
127  ];
128 
129  $result = $this->db->getDataRow($query, null, $params);
130  if ($result) {
131  $id = $result->id;
132  } else {
133  $id = $this->add($string);
134  }
135 
136  $memcache->save(md5($string), $id);
137 
138  return $id;
139  });
140  }
141 
150  private function getIdCaseInsensitive($string) {
151  $string = (string) $string;
152  // caching doesn't work for case insensitive requests
153  $query = "SELECT id FROM {$this->getTableName()} WHERE string = :string";
154  $params = [
155  ':string' => $string,
156  ];
157  $results = $this->db->getData($query, null, $params);
158  $ids = array();
159  foreach ($results as $result) {
160  $ids[] = $result->id;
161  }
162  if (empty($ids)) {
163  $ids[] = $this->add($string);
164  }
165  return $ids;
166  }
167 
176  public function add($string) {
177  $sql = "INSERT INTO {$this->getTableName()} (string) VALUES (:string)";
178  $params = [
179  ':string' => trim((string) $string),
180  ];
181  return $this->db->insertData($sql, $params);
182  }
183 
189  public function getTableName() {
190  return $this->db->prefix . "metastrings";
191  }
192 
193 }
The Elgg database.
Definition: Database.php:17
_elgg_get_memcache($namespace= 'default')
Get a namespaced ElggMemcache object (if memcache is available) or a null cache.
Definition: memcache.php:40
getId($string, $case_sensitive=true)
Gets the metastring identifier for a value.
$string
$params
Definition: login.php:72
__construct(Pool $cache, Database $db)
Constructor.
add($string)
Add a metastring.
$key
Definition: summary.php:34
getTableName()
The full name of the metastrings table, including prefix.
$row
if(!$collection_name) $id
Definition: add.php:17
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
getMap(array $string_keys)
Get a map of strings to their metastring identifiers (case sensitive matches)