Elgg  Version 1.11
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 
54  function getId($string, $case_sensitive = true) {
55  if ($case_sensitive) {
56  return $this->getIdCaseSensitive($string);
57  } else {
58  return $this->getIdCaseInsensitive($string);
59  }
60  }
61 
70  private function getIdCaseSensitive($string) {
71  $string = (string)$string;
72  return $this->cache->get($string, function() use ($string) {
73  $escaped_string = $this->db->sanitizeString($string);
74  $query = "SELECT * FROM {$this->getTableName()} WHERE string = BINARY '$escaped_string' LIMIT 1";
75  $results = $this->db->getData($query);
76  if (isset($results[0])) {
77  return $results[0]->id;
78  } else {
79  return $this->add($string);
80  }
81  });
82  }
83 
92  private function getIdCaseInsensitive($string) {
93  $string = (string)$string;
94  // caching doesn't work for case insensitive requests
95  $escaped_string = $this->db->sanitizeString($string);
96  $query = "SELECT * FROM {$this->getTableName()} WHERE string = '$escaped_string'";
97  $results = $this->db->getData($query);
98  $ids = array();
99  foreach ($results as $result) {
100  $ids[] = $result->id;
101  }
102  if (empty($ids)) {
103  $ids[] = $this->add($string);
104  }
105  return $ids;
106  }
107 
116  function add($string) {
117  $escaped_string = $this->db->sanitizeString(trim($string));
118 
119  return $this->db->insertData("INSERT INTO {$this->getTableName()} (string) VALUES ('$escaped_string')");
120  }
121 
127  public function getTableName() {
128  return $this->db->getTablePrefix() . "metastrings";
129  }
130 }
getId($string, $case_sensitive=true)
Gets the metastring identifier for a value.
$string
__construct(Pool $cache, Database $db)
Constructor.
add($string)
Add a metastring.
getTableName()
The full name of the metastrings table, including prefix.