Elgg  Version 2.2
 All Classes Namespaces Files Functions Variables Pages
MetastringsTable.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg\Database;
3 
5 use Elgg\Database;
6 
7 
22 
24  private $cache;
25 
27  private $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  $set = implode(',', $set_element);
88 
89  $query = "SELECT * FROM {$this->getTableName()} WHERE string IN ($set)";
90  $ret = [];
91 
92  foreach ($this->db->getData($query) as $row) {
93  $ret[$row->string] = (int)$row->id;
94  unset($missing[$row->string]);
95  }
96  foreach (array_keys($missing) as $string) {
97  $ret[$string] = $this->getIdCaseSensitive($string);
98  }
99 
100  return $ret;
101  }
102 
111  private function getIdCaseSensitive($string) {
112  $string = (string)$string;
113  return $this->cache->get($string, function() use ($string) {
114  $escaped_string = $this->db->sanitizeString($string);
115  $query = "SELECT id FROM {$this->getTableName()} WHERE string = BINARY '$escaped_string' LIMIT 1";
116  $results = $this->db->getData($query);
117  if (isset($results[0])) {
118  return $results[0]->id;
119  } else {
120  return $this->add($string);
121  }
122  });
123  }
124 
133  private function getIdCaseInsensitive($string) {
134  $string = (string)$string;
135  // caching doesn't work for case insensitive requests
136  $escaped_string = $this->db->sanitizeString($string);
137  $query = "SELECT id FROM {$this->getTableName()} WHERE string = '$escaped_string'";
138  $results = $this->db->getData($query);
139  $ids = array();
140  foreach ($results as $result) {
141  $ids[] = $result->id;
142  }
143  if (empty($ids)) {
144  $ids[] = $this->add($string);
145  }
146  return $ids;
147  }
148 
157  function add($string) {
158  $escaped_string = $this->db->sanitizeString(trim($string));
159 
160  return $this->db->insertData("INSERT INTO {$this->getTableName()} (string) VALUES ('$escaped_string')");
161  }
162 
168  public function getTableName() {
169  return $this->db->getTablePrefix() . "metastrings";
170  }
171 }
getId($string, $case_sensitive=true)
Gets the metastring identifier for a value.
$string
__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
getMap(array $string_keys)
Get a map of strings to their metastring identifiers (case sensitive matches)