MetastringsTable.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace Elgg\Database;
  3. use Elgg\Cache\Pool;
  4. use Elgg\Database;
  5. /**
  6. * Normalization for strings used in metadata and annoations tables.
  7. *
  8. * WARNING: API IN FLUX. DO NOT USE DIRECTLY.
  9. *
  10. * @access private
  11. *
  12. * @package Elgg.Core
  13. * @subpackage Database
  14. * @since 1.10.0
  15. *
  16. * @access private
  17. */
  18. class MetastringsTable {
  19. /** @var Pool */
  20. private $cache;
  21. /** @var Database */
  22. private $db;
  23. /**
  24. * Constructor
  25. *
  26. * @param Pool $cache A cache for this table.
  27. * @param Database $db The database.
  28. */
  29. public function __construct(Pool $cache, Database $db) {
  30. $this->cache = $cache;
  31. $this->db = $db;
  32. }
  33. /**
  34. * Gets the metastring identifier for a value.
  35. *
  36. * Elgg normalizes the names and values of annotations and metadata. This function
  37. * provides the identifier used as the index in the metastrings table. Plugin
  38. * developers should only use this if denormalizing names/values for performance
  39. * reasons (to avoid multiple joins on the metastrings table).
  40. *
  41. * @param string $string The value
  42. * @param bool $case_sensitive Should the retrieval be case sensitive?
  43. * If not, there may be more than one result
  44. *
  45. * @return int|array metastring id or array of ids
  46. */
  47. function getId($string, $case_sensitive = true) {
  48. if ($case_sensitive) {
  49. return $this->getIdCaseSensitive($string);
  50. } else {
  51. return $this->getIdCaseInsensitive($string);
  52. }
  53. }
  54. /**
  55. * Gets the id associated with this string, case-sensitively.
  56. * Will add the string to the table if not present.
  57. *
  58. * @param string $string The value
  59. *
  60. * @return int
  61. */
  62. private function getIdCaseSensitive($string) {
  63. $string = (string)$string;
  64. return $this->cache->get($string, function() use ($string) {
  65. $escaped_string = $this->db->sanitizeString($string);
  66. $query = "SELECT * FROM {$this->getTableName()} WHERE string = BINARY '$escaped_string' LIMIT 1";
  67. $results = $this->db->getData($query);
  68. if (isset($results[0])) {
  69. return $results[0]->id;
  70. } else {
  71. return $this->add($string);
  72. }
  73. });
  74. }
  75. /**
  76. * Gets all ids associated with this string when taken case-insensitively.
  77. * Will add the string to the table if not present.
  78. *
  79. * @param string $string The value
  80. *
  81. * @return int[]
  82. */
  83. private function getIdCaseInsensitive($string) {
  84. $string = (string)$string;
  85. // caching doesn't work for case insensitive requests
  86. $escaped_string = $this->db->sanitizeString($string);
  87. $query = "SELECT * FROM {$this->getTableName()} WHERE string = '$escaped_string'";
  88. $results = $this->db->getData($query);
  89. $ids = array();
  90. foreach ($results as $result) {
  91. $ids[] = $result->id;
  92. }
  93. if (empty($ids)) {
  94. $ids[] = $this->add($string);
  95. }
  96. return $ids;
  97. }
  98. /**
  99. * Add a metastring.
  100. *
  101. * @warning You should not call this directly. Use elgg_get_metastring_id().
  102. *
  103. * @param string $string The value to be normalized
  104. * @return int The identifier for this string
  105. */
  106. function add($string) {
  107. $escaped_string = $this->db->sanitizeString(trim($string));
  108. return $this->db->insertData("INSERT INTO {$this->getTableName()} (string) VALUES ('$escaped_string')");
  109. }
  110. /**
  111. * The full name of the metastrings table, including prefix.
  112. *
  113. * @return string
  114. */
  115. public function getTableName() {
  116. return $this->db->getTablePrefix() . "metastrings";
  117. }
  118. }