Elgg  Version 1.12
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  $ar = array_unique($ar);
28  return $ar;
29 }
30 
78 function elgg_get_tags(array $options = array()) {
80 
81  $defaults = array(
82  'threshold' => 1,
83  'tag_names' => array(),
84  'limit' => elgg_get_config('default_limit'),
85 
86  'types' => ELGG_ENTITIES_ANY_VALUE,
87  'subtypes' => ELGG_ENTITIES_ANY_VALUE,
88  'type_subtype_pairs' => ELGG_ENTITIES_ANY_VALUE,
89 
90  'owner_guids' => ELGG_ENTITIES_ANY_VALUE,
91  'container_guids' => ELGG_ENTITIES_ANY_VALUE,
92  'site_guids' => $CONFIG->site_guid,
93 
94  'modified_time_lower' => ELGG_ENTITIES_ANY_VALUE,
95  'modified_time_upper' => ELGG_ENTITIES_ANY_VALUE,
96  'created_time_lower' => ELGG_ENTITIES_ANY_VALUE,
97  'created_time_upper' => ELGG_ENTITIES_ANY_VALUE,
98 
99  'joins' => array(),
100  'wheres' => array(),
101  );
102 
103 
104  $options = array_merge($defaults, $options);
105 
106  $singulars = array('type', 'subtype', 'owner_guid', 'container_guid', 'site_guid', 'tag_name');
108 
109  $registered_tags = elgg_get_registered_tag_metadata_names();
110 
111  if (!is_array($options['tag_names'])) {
112  return false;
113  }
114 
115  // empty array so use all registered tag names
116  if (count($options['tag_names']) == 0) {
117  $options['tag_names'] = $registered_tags;
118  }
119 
120  $diff = array_diff($options['tag_names'], $registered_tags);
121  if (count($diff) > 0) {
122  elgg_deprecated_notice('Tag metadata names must be registered by elgg_register_tag_metadata_name()', 1.7);
123  // return false;
124  }
125 
126 
127  $wheres = $options['wheres'];
128 
129  // catch for tags that were spaces
130  $wheres[] = "msv.string != ''";
131 
132  $sanitised_tags = array();
133  foreach ($options['tag_names'] as $tag) {
134  $sanitised_tags[] = '"' . sanitise_string($tag) . '"';
135  }
136  $tags_in = implode(',', $sanitised_tags);
137  $wheres[] = "(msn.string IN ($tags_in))";
138 
139  $wheres[] = _elgg_get_entity_type_subtype_where_sql('e', $options['types'],
140  $options['subtypes'], $options['type_subtype_pairs']);
141  $wheres[] = _elgg_get_guid_based_where_sql('e.site_guid', $options['site_guids']);
142  $wheres[] = _elgg_get_guid_based_where_sql('e.owner_guid', $options['owner_guids']);
143  $wheres[] = _elgg_get_guid_based_where_sql('e.container_guid', $options['container_guids']);
144  $wheres[] = _elgg_get_entity_time_where_sql('e', $options['created_time_upper'],
145  $options['created_time_lower'], $options['modified_time_upper'], $options['modified_time_lower']);
146 
147  // see if any functions failed
148  // remove empty strings on successful functions
149  foreach ($wheres as $i => $where) {
150  if ($where === false) {
151  return false;
152  } elseif (empty($where)) {
153  unset($wheres[$i]);
154  }
155  }
156 
157  // remove identical where clauses
158  $wheres = array_unique($wheres);
159 
160  $joins = $options['joins'];
161 
162  $joins[] = "JOIN {$CONFIG->dbprefix}metadata md on md.entity_guid = e.guid";
163  $joins[] = "JOIN {$CONFIG->dbprefix}metastrings msv on msv.id = md.value_id";
164  $joins[] = "JOIN {$CONFIG->dbprefix}metastrings msn on md.name_id = msn.id";
165 
166  // remove identical join clauses
167  $joins = array_unique($joins);
168 
169  foreach ($joins as $i => $join) {
170  if ($join === false) {
171  return false;
172  } elseif (empty($join)) {
173  unset($joins[$i]);
174  }
175  }
176 
177 
178  $query = "SELECT msv.string as tag, count(msv.id) as total ";
179  $query .= "FROM {$CONFIG->dbprefix}entities e ";
180 
181  // add joins
182  foreach ($joins as $j) {
183  $query .= " $j ";
184  }
185 
186  // add wheres
187  $query .= ' WHERE ';
188 
189  foreach ($wheres as $w) {
190  $query .= " $w AND ";
191  }
192 
193  // Add access controls
194  $query .= _elgg_get_access_where_sql();
195 
196  $threshold = sanitise_int($options['threshold']);
197  $query .= " GROUP BY msv.string HAVING total >= {$threshold} ";
198  $query .= " ORDER BY total DESC ";
199 
200  $limit = sanitise_int($options['limit']);
201  $query .= " LIMIT {$limit} ";
202 
203  return get_data($query);
204 }
205 
217  global $CONFIG;
218 
219  if (!isset($CONFIG->registered_tag_metadata_names)) {
220  $CONFIG->registered_tag_metadata_names = array();
221  }
222 
223  if (!in_array($name, $CONFIG->registered_tag_metadata_names)) {
224  $CONFIG->registered_tag_metadata_names[] = $name;
225  }
226 
227  return true;
228 }
229 
237  global $CONFIG;
238 
239  $names = (isset($CONFIG->registered_tag_metadata_names)) ? $CONFIG->registered_tag_metadata_names : array();
240 
241  return $names;
242 }
243 
247 function _elgg_tags_init() {
248  // register the standard tags metadata name
250 }
251 
252 return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
253  $events->registerHandler('init', 'system', '_elgg_tags_init');
254 };
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:236
elgg_get_tags(array $options=array())
Get popular tags and their frequencies.
Definition: tags.php:78
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:539
_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:557
$string
$options
Definition: index.php:14
$limit
Definition: userpicker.php:38
global $CONFIG
sanitise_string($string)
Wrapper function for alternate English spelling (.
Definition: database.php:150
const ELGG_ENTITIES_ANY_VALUE
Definition: elgglib.php:2006
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Log a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:1031
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:216
_elgg_tags_init()
private
Definition: tags.php:247
_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:524
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:1401
_elgg_get_access_where_sql(array $options=array())
Returns the SQL where clause for enforcing read access to data.
Definition: access.php:216