-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathTagadelicCloud.php
155 lines (142 loc) · 3.98 KB
/
TagadelicCloud.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
/**
* class TagadelicCloud
* TagadelicCloud, contains a list of tags and methods to manipulate
* this set of tags.
* It can operate on the list of tags.
*/
class TagadelicCloud {
private $id = ""; # An identifier for this cloud. Must be unique.
private $tags = array(); # List of the tags in this cloud.
private $steps = 6; #Amount of steps to weight the cloud in. Defaults to 6. Means: 6 different sized tags.
private $needs_recalc = true;
public $fuck_locale = "";
/**
* Initalize the cloud
*
* @param id Integer, identifies this cloud; used for caching and
* re-fetching of previously built clouds.
* @param tags Array, provide tags on building. Tags can be added
* later on, using `add_tag()` method.
* @return TagadelicCloud.
*/
function __construct($id, $tags = array()) {
$this->id = $id;
$this->tags = $tags;
}
/**
* Getter for id
* @ingroup getters
* @returns Integer id of this cloud
*/
public function get_id() {
return $this->id;
}
/**
* Getter for tags
* @ingroup getters
* @returns Array list of tags
*/
public function get_tags() {
$this->recalculate();
return $this->tags;
}
/**
* Add a new tag to the cloud
* @param $tag TagadelicTag
* instance of TagadelicTag.
*
* return $this, for chaining.
*/
public function add_tag($tag) {
$this->tags[] = $tag;
return $this;
}
/**
* setter for drupal(wrapper). Mostly for testability
* Operates on $this
* Returns $this
*/
public function set_drupal($drupal) {
$this->drupal = $drupal;
return $this;
}
/**
* Getter for drupal
* @return DrupalWrapper value in $this::$drupal.
*/
public function drupal() {
if (empty($this->drupal)) {
$this->drupal = new TagadelicDrupalWrapper();
}
return $this->drupal;
}
/**
* Instantiate $this from cache
* Optionally pass $drupal, a Drupalwrapper along, mostly for testing.
* Returns this
*/
public static function from_cache($id, $drupal) {
$cache_id = "tagadelic_cloud_{$id}";
return $drupal->cache_get($cache_id);
}
/**
* Writes the cloud to cache. Will recalculate if needed.
* @return $this; for chaining.
*/
public function to_cache() {
$cache_id = "tagadelic_cloud_{$this->id}";
$this->drupal()->cache_set($cache_id, $this);
return $this;
}
/**
* Sorts the tags by given property.
* @return $this; for chaining.
*/
public function sort($by_property) {
if ($by_property == "random") {
$this->drupal()->shuffle($this->tags);
}
else {
//Bug in PHP https://bugs.php.net/bug.php?id=50688, lets supress the error.
@usort($this->tags, array($this, "cb_sort_by_{$by_property}"));
}
return $this;
}
/**
* (Re)calculates the weights on the tags.
* @param $recalculate. Optional flag to enfore recalculation of the weights for the tags in this cloud.
* defaults to FALSE, meaning the value will be calculated once per cloud.
* @return $this; for chaining
*/
private function recalculate() {
$tags = array();
// Find minimum and maximum log-count.
$min = 1e9;
$max = -1e9;
foreach ($this->tags as $id => $tag) {
$min = min($min, $tag->distributed());
$max = max($max, $tag->distributed());
$tags[$id] = $tag;
}
// Note: we need to ensure the range is slightly too large to make sure even
// the largest element is rounded down.
$range = max(.01, $max - $min) * 1.0001;
foreach ($tags as $id => $tag) {
$this->tags[$id]->set_weight(1 + floor($this->steps * ($tag->distributed() - $min) / $range));
}
return $this;
}
private function cb_sort_by_name($a, $b) {
return strcoll($a->get_name(), $b->get_name());
}
private function cb_sort_by_count($a, $b) {
$ac = $a->get_count();
$bc = $b->get_count();
if ($ac == $bc) {
return 0;
}
//Highest first, High to low
return ($ac < $bc) ? +1 : -1;
}
}