1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Elgg tags |
4
|
|
|
* Functions for managing tags and tag clouds. |
5
|
|
|
* |
6
|
|
|
* @package Elgg.Core |
7
|
|
|
* @subpackage Tags |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Takes in a comma-separated string and returns an array of tags |
12
|
|
|
* which have been trimmed |
13
|
|
|
* |
14
|
|
|
* @param string $string Comma-separated tag string |
15
|
|
|
* |
16
|
|
|
* @return mixed An array of strings or the original data if input was not a string |
17
|
|
|
*/ |
18
|
|
|
function string_to_tag_array($string) { |
19
|
3 |
|
if (!is_string($string)) { |
20
|
1 |
|
return $string; |
21
|
|
|
} |
22
|
|
|
|
23
|
2 |
|
$ar = explode(",", $string); |
24
|
2 |
|
$ar = array_map('trim', $ar); |
25
|
2 |
|
$ar = array_filter($ar, 'is_not_null'); |
26
|
2 |
|
$ar = array_map('strip_tags', $ar); |
27
|
2 |
|
$ar = array_unique($ar); |
28
|
2 |
|
return $ar; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Get popular tags and their frequencies |
33
|
|
|
* |
34
|
|
|
* Accepts all options supported by {@link elgg_get_entities()} |
35
|
|
|
* |
36
|
|
|
* Returns an array of objects that include "tag" and "total" properties |
37
|
|
|
* |
38
|
|
|
* @todo When updating this function for 3.0, I have noticed that docs explicitly mention |
39
|
|
|
* that tags must be registered, but it was not really checked anywhere in code |
40
|
|
|
* So, either update the docs or decide what the behavior should be |
41
|
|
|
* |
42
|
|
|
* @param array $options Options |
43
|
|
|
* |
44
|
|
|
* @option int $threshold Minimum number of tag occurrences |
45
|
|
|
* @option string[] $tag_names Names of registered tag names to include in search |
46
|
|
|
* |
47
|
|
|
* @return object[]|false |
48
|
|
|
* @since 1.7.1 |
49
|
|
|
*/ |
50
|
|
|
function elgg_get_tags(array $options = []) { |
51
|
|
|
$defaults = [ |
52
|
1 |
|
'threshold' => 1, |
53
|
|
|
'tag_names' => [], |
54
|
|
|
]; |
55
|
|
|
|
56
|
1 |
|
$options = array_merge($defaults, $options); |
57
|
|
|
|
58
|
1 |
|
$singulars = ['tag_name']; |
59
|
1 |
|
$options = _elgg_normalize_plural_options_array($options, $singulars); |
60
|
|
|
|
61
|
1 |
|
$tag_names = elgg_extract('tag_names', $options); |
62
|
1 |
|
if (empty($tag_names)) { |
63
|
|
|
$tag_names = elgg_get_registered_tag_metadata_names(); |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
$threshold = elgg_extract('threshold', $options, 1, false); |
67
|
|
|
|
68
|
1 |
|
unset($options['tag_names']); |
69
|
1 |
|
unset($options['threshold']); |
70
|
|
|
|
71
|
1 |
|
$qb = \Elgg\Database\Select::fromTable('metadata', 'md'); |
72
|
1 |
|
$qb->select('md.value AS tag') |
73
|
1 |
|
->addSelect('COUNT(md.id) AS total') |
74
|
1 |
|
->where($qb->compare('md.name', 'IN', $tag_names, ELGG_VALUE_STRING)) |
75
|
1 |
|
->andWhere($qb->compare('md.value', '!=', '', ELGG_VALUE_STRING)) |
76
|
1 |
|
->groupBy('md.value') |
77
|
1 |
|
->having($qb->compare('total', '>=', $threshold, ELGG_VALUE_INTEGER)) |
78
|
1 |
|
->orderBy('total', 'desc'); |
79
|
|
|
|
80
|
1 |
|
$options = new \Elgg\Database\QueryOptions($options); |
81
|
1 |
|
$alias = $qb->joinEntitiesTable('md', 'entity_guid', 'inner', 'e'); |
82
|
1 |
|
$qb->addClause(\Elgg\Database\Clauses\EntityWhereClause::factory($options), $alias); |
83
|
|
|
|
84
|
1 |
|
return _elgg_services()->db->getData($qb); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Registers a metadata name as containing tags for an entity. |
89
|
|
|
* This is required if you are using a non-standard metadata name |
90
|
|
|
* for your tags. |
91
|
|
|
* |
92
|
|
|
* Because tags are simply names of metadata, This is used |
93
|
|
|
* in search to prevent data exposure by searching on |
94
|
|
|
* arbitrary metadata. |
95
|
|
|
* |
96
|
|
|
* @param string $name Tag name |
97
|
|
|
* |
98
|
|
|
* @return bool |
99
|
|
|
* @since 1.7.0 |
100
|
|
|
*/ |
101
|
|
|
function elgg_register_tag_metadata_name($name) { |
102
|
32 |
|
return _elgg_services()->metadataTable->registerTagName($name); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Unregister metadata tag name |
107
|
|
|
* |
108
|
|
|
* @param string $name Tag name |
109
|
|
|
* |
110
|
|
|
* @return bool |
111
|
|
|
* @since 3.0 |
112
|
|
|
*/ |
113
|
|
|
function elgg_unregister_tag_metadata_name($name) { |
114
|
1 |
|
return _elgg_services()->metadataTable->unregisterTagName($name); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Returns an array of valid metadata names for tags. |
119
|
|
|
* |
120
|
|
|
* @return string[] |
121
|
|
|
* @since 1.7.0 |
122
|
|
|
*/ |
123
|
|
|
function elgg_get_registered_tag_metadata_names() { |
124
|
3 |
|
return _elgg_services()->metadataTable->getTagNames(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Tags init |
129
|
|
|
* |
130
|
|
|
* @return void |
131
|
|
|
* |
132
|
|
|
* @access private |
133
|
|
|
*/ |
134
|
|
|
function _elgg_tags_init() { |
135
|
|
|
// register the standard tags metadata name |
136
|
31 |
|
elgg_register_tag_metadata_name('tags'); |
137
|
31 |
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @see \Elgg\Application::loadCore Do not do work here. Just register for events. |
141
|
|
|
*/ |
142
|
|
|
return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) { |
|
|
|
|
143
|
18 |
|
$events->registerHandler('init', 'system', '_elgg_tags_init'); |
144
|
|
|
}; |
145
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.