Elgg  Version 1.11
tags.php
Go to the documentation of this file.
1 <?php
19  if (!is_string($string)) {
20  return $string;
21  }
22 
23  $ar = explode(",", $string);
24  $ar = array_map('trim', $ar);
25  $ar = array_filter($ar, 'is_not_null');
26  $ar = array_map('strip_tags', $ar);
27  return $ar;
28 }
29 
72 function elgg_get_tags(array $options = array()) {
74 
75  $defaults = array(
76  'threshold' => 1,
77  'tag_names' => array(),
78  'limit' => elgg_get_config('default_limit'),
79 
80  'types' => ELGG_ENTITIES_ANY_VALUE,
81  'subtypes' => ELGG_ENTITIES_ANY_VALUE,
82  'type_subtype_pairs' => ELGG_ENTITIES_ANY_VALUE,
83 
84  'owner_guids' => ELGG_ENTITIES_ANY_VALUE,
85  'container_guids' => ELGG_ENTITIES_ANY_VALUE,
86  'site_guids' => $CONFIG->site_guid,
87 
88  'modified_time_lower' => ELGG_ENTITIES_ANY_VALUE,
89  'modified_time_upper' => ELGG_ENTITIES_ANY_VALUE,
90  'created_time_lower' => ELGG_ENTITIES_ANY_VALUE,
91  'created_time_upper' => ELGG_ENTITIES_ANY_VALUE,
92 
93  'joins' => array(),
94  'wheres' => array(),
95  );
96 
97 
98  $options = array_merge($defaults, $options);
99 
100  $singulars = array('type', 'subtype', 'owner_guid', 'container_guid', 'site_guid', 'tag_name');
102 
103  $registered_tags = elgg_get_registered_tag_metadata_names();
104 
105  if (!is_array($options['tag_names'])) {
106  return false;
107  }
108 
109  // empty array so use all registered tag names
110  if (count($options['tag_names']) == 0) {
111  $options['tag_names'] = $registered_tags;
112  }
113 
114  $diff = array_diff($options['tag_names'], $registered_tags);
115  if (count($diff) > 0) {
116  elgg_deprecated_notice('Tag metadata names must be registered by elgg_register_tag_metadata_name()', 1.7);
117  // return false;
118  }
119 
120 
121  $wheres = $options['wheres'];
122 
123  // catch for tags that were spaces
124  $wheres[] = "msv.string != ''";
125 
126  $sanitised_tags = array();
127  foreach ($options['tag_names'] as $tag) {
128  $sanitised_tags[] = '"' . sanitise_string($tag) . '"';
129  }
130  $tags_in = implode(',', $sanitised_tags);
131  $wheres[] = "(msn.string IN ($tags_in))";
132 
133  $wheres[] = _elgg_get_entity_type_subtype_where_sql('e', $options['types'],
134  $options['subtypes'], $options['type_subtype_pairs']);
135  $wheres[] = _elgg_get_guid_based_where_sql('e.site_guid', $options['site_guids']);
136  $wheres[] = _elgg_get_guid_based_where_sql('e.owner_guid', $options['owner_guids']);
137  $wheres[] = _elgg_get_guid_based_where_sql('e.container_guid', $options['container_guids']);
138  $wheres[] = _elgg_get_entity_time_where_sql('e', $options['created_time_upper'],
139  $options['created_time_lower'], $options['modified_time_upper'], $options['modified_time_lower']);
140 
141  // see if any functions failed
142  // remove empty strings on successful functions
143  foreach ($wheres as $i => $where) {
144  if ($where === false) {
145  return false;
146  } elseif (empty($where)) {
147  unset($wheres[$i]);
148  }
149  }
150 
151  // remove identical where clauses
152  $wheres = array_unique($wheres);
153 
154  $joins = $options['joins'];
155 
156  $joins[] = "JOIN {$CONFIG->dbprefix}metadata md on md.entity_guid = e.guid";
157  $joins[] = "JOIN {$CONFIG->dbprefix}metastrings msv on msv.id = md.value_id";
158  $joins[] = "JOIN {$CONFIG->dbprefix}metastrings msn on md.name_id = msn.id";
159 
160  // remove identical join clauses
161  $joins = array_unique($joins);
162 
163  foreach ($joins as $i => $join) {
164  if ($join === false) {
165  return false;
166  } elseif (empty($join)) {
167  unset($joins[$i]);
168  }
169  }
170 
171 
172  $query = "SELECT msv.string as tag, count(msv.id) as total ";
173  $query .= "FROM {$CONFIG->dbprefix}entities e ";
174 
175  // add joins
176  foreach ($joins as $j) {
177  $query .= " $j ";
178  }
179 
180  // add wheres
181  $query .= ' WHERE ';
182 
183  foreach ($wheres as $w) {
184  $query .= " $w AND ";
185  }
186 
187  // Add access controls
188  $query .= _elgg_get_access_where_sql();
189 
190  $threshold = sanitise_int($options['threshold']);
191  $query .= " GROUP BY msv.string HAVING total >= {$threshold} ";
192  $query .= " ORDER BY total DESC ";
193 
194  $limit = sanitise_int($options['limit']);
195  $query .= " LIMIT {$limit} ";
196 
197  return get_data($query);
198 }
199 
211  global $CONFIG;
212 
213  if (!isset($CONFIG->registered_tag_metadata_names)) {
214  $CONFIG->registered_tag_metadata_names = array();
215  }
216 
217  if (!in_array($name, $CONFIG->registered_tag_metadata_names)) {
218  $CONFIG->registered_tag_metadata_names[] = $name;
219  }
220 
221  return true;
222 }
223 
231  global $CONFIG;
232 
233  $names = (isset($CONFIG->registered_tag_metadata_names)) ? $CONFIG->registered_tag_metadata_names : array();
234 
235  return $names;
236 }
237 
241 function _elgg_tags_init() {
242  // register the standard tags metadata name
244 }
245 
246 return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
247  $events->registerHandler('init', 'system', '_elgg_tags_init');
248 };
elgg_get_config($name, $site_guid=0)
Get an Elgg configuration value.
elgg_get_registered_tag_metadata_names()
Returns an array of valid metadata names for tags.
Definition: tags.php:230
elgg_get_tags(array $options=array())
Get popular tags and their frequencies.
Definition: tags.php:72
if($guid==elgg_get_logged_in_user_guid()) $name
Definition: delete.php:21
string_to_tag_array($string)
Takes in a comma-separated string and returns an array of tags which have been trimmed.
Definition: tags.php:18
_elgg_get_guid_based_where_sql($column, $guids)
Returns SQL where clause for owner and containers.
Definition: entities.php:535
_elgg_get_entity_time_where_sql($table, $time_created_upper=null, $time_created_lower=null, $time_updated_upper=null, $time_updated_lower=null)
Returns SQL where clause for entity time limits.
Definition: entities.php:553
$string
$options
Definition: index.php:14
$limit
Definition: userpicker.php:31
global $CONFIG
sanitise_string($string)
Wrapper function for alternate English spelling (.
Definition: database.php:150
const ELGG_ENTITIES_ANY_VALUE
Definition: elgglib.php:1967
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Log a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:1006
elgg global
Pointer to the global context.
Definition: elgglib.js:12
get_data($query, $callback="")
Retrieve rows from the database.
Definition: database.php:50
elgg_register_tag_metadata_name($name)
Registers a metadata name as containing tags for an entity.
Definition: tags.php:210
_elgg_tags_init()
private
Definition: tags.php:241
_elgg_get_entity_type_subtype_where_sql($table, $types, $subtypes, $pairs)
Returns SQL where clause for type and subtype on main entity table.
Definition: entities.php:520
sanitise_int($int, $signed=true)
Sanitizes an integer for database use.
Definition: database.php:173
$defaults
Definition: tags.php:15
_elgg_normalize_plural_options_array($options, $singulars)
Normalise the singular keys in an options array to plural keys.
Definition: elgglib.php:1376
_elgg_get_access_where_sql(array $options=array())
Returns the SQL where clause for enforcing read access to data.
Definition: access.php:216