Elgg  Version 2.3
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  $wheres = $options['wheres'];
121 
122  // catch for tags that were spaces
123  $wheres[] = "msv.string != ''";
124 
125  $sanitised_tags = array();
126  foreach ($options['tag_names'] as $tag) {
127  $sanitised_tags[] = '"' . sanitise_string($tag) . '"';
128  }
129  $tags_in = implode(',', $sanitised_tags);
130  $wheres[] = "(msn.string IN ($tags_in))";
131 
132  $wheres[] = _elgg_services()->entityTable->getEntityTypeSubtypeWhereSql('e', $options['types'],
133  $options['subtypes'], $options['type_subtype_pairs']);
134  $wheres[] = _elgg_get_guid_based_where_sql('e.site_guid', $options['site_guids']);
135  $wheres[] = _elgg_get_guid_based_where_sql('e.owner_guid', $options['owner_guids']);
136  $wheres[] = _elgg_get_guid_based_where_sql('e.container_guid', $options['container_guids']);
137  $wheres[] = _elgg_get_entity_time_where_sql('e', $options['created_time_upper'],
138  $options['created_time_lower'], $options['modified_time_upper'], $options['modified_time_lower']);
139 
140  // see if any functions failed
141  // remove empty strings on successful functions
142  foreach ($wheres as $i => $where) {
143  if ($where === false) {
144  return false;
145  } elseif (empty($where)) {
146  unset($wheres[$i]);
147  }
148  }
149 
150  // remove identical where clauses
151  $wheres = array_unique($wheres);
152 
153  $joins = $options['joins'];
154 
155  $joins[] = "JOIN {$CONFIG->dbprefix}metadata md on md.entity_guid = e.guid";
156  $joins[] = "JOIN {$CONFIG->dbprefix}metastrings msv on msv.id = md.value_id";
157  $joins[] = "JOIN {$CONFIG->dbprefix}metastrings msn on md.name_id = msn.id";
158 
159  // remove identical join clauses
160  $joins = array_unique($joins);
161 
162  foreach ($joins as $i => $join) {
163  if ($join === false) {
164  return false;
165  } elseif (empty($join)) {
166  unset($joins[$i]);
167  }
168  }
169 
170 
171  $query = "SELECT msv.string as tag, count(msv.id) as total ";
172  $query .= "FROM {$CONFIG->dbprefix}entities e ";
173 
174  // add joins
175  foreach ($joins as $j) {
176  $query .= " $j ";
177  }
178 
179  // add wheres
180  $query .= ' WHERE ';
181 
182  foreach ($wheres as $w) {
183  $query .= " $w AND ";
184  }
185 
186  // Add access controls
187  $query .= _elgg_get_access_where_sql();
188 
189  $threshold = sanitise_int($options['threshold']);
190  $query .= " GROUP BY msv.string HAVING total >= {$threshold} ";
191  $query .= " ORDER BY total DESC ";
192 
193  $limit = sanitise_int($options['limit']);
194  $query .= " LIMIT {$limit} ";
195 
196  return get_data($query);
197 }
198 
210  if (!isset($GLOBALS['_ELGG']->registered_tag_metadata_names)) {
211  $GLOBALS['_ELGG']->registered_tag_metadata_names = array();
212  }
213 
214  if (!in_array($name, $GLOBALS['_ELGG']->registered_tag_metadata_names)) {
215  $GLOBALS['_ELGG']->registered_tag_metadata_names[] = $name;
216  }
217 
218  return true;
219 }
220 
228  $names = (isset($GLOBALS['_ELGG']->registered_tag_metadata_names)) ? $GLOBALS['_ELGG']->registered_tag_metadata_names : array();
229 
230  return $names;
231 }
232 
236 function _elgg_tags_init() {
237  // register the standard tags metadata name
239 }
240 
241 return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
242  $events->registerHandler('init', 'system', '_elgg_tags_init');
243 };
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:227
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:341
_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:359
$options
Elgg admin footer.
Definition: footer.php:6
$string
$limit
Definition: userpicker.php:38
global $CONFIG
sanitise_string($string)
Alias of sanitize_string.
Definition: database.php:166
const ELGG_ENTITIES_ANY_VALUE
Definition: elgglib.php:2095
elgg global
Pointer to the global context.
Definition: elgglib.js:12
get_data($query, $callback=null, array $params=[])
Retrieve rows from the database.
Definition: database.php:55
_elgg_services(\Elgg\Di\ServiceProvider $services=null)
Get the global service provider.
Definition: autoloader.php:17
elgg_register_tag_metadata_name($name)
Registers a metadata name as containing tags for an entity.
Definition: tags.php:209
_elgg_tags_init()
private
Definition: tags.php:236
$_ELGG registered_tag_metadata_names
An array of metadata names to be used as tags.
Definition: config.php:345
sanitise_int($int, $signed=true)
Alias of sanitize_int.
Definition: database.php:194
$defaults
Definition: tags.php:14
_elgg_normalize_plural_options_array($options, $singulars)
Normalise the singular keys in an options array to plural keys.
Definition: elgglib.php:1528
_elgg_get_access_where_sql(array $options=array())
Returns the SQL where clause for enforcing read access to data.
Definition: access.php:214