Commit | Line | Data |
---|---|---|
d3b026ab | 1 | <?php |
2 | /******************************************************************************** | |
3 | * include/tree.inc.php : thread tree | |
4 | * ----------------------- | |
5 | * | |
6 | * This file is part of the banana distribution | |
7 | * Copyright: See COPYING files that comes with this distribution | |
8 | ********************************************************************************/ | |
9 | ||
10 | ||
e3bc13e0 | 11 | define('BANANA_TREE_VERSION', '0.1.2'); |
d3b026ab | 12 | |
13 | /** | |
14 | * Class representing a thread tree | |
15 | */ | |
16 | class BananaTree | |
17 | { | |
18 | /** Tree cache | |
19 | */ | |
20 | static private $cache = array(); | |
21 | ||
22 | /** Tree format | |
23 | */ | |
24 | public $version; | |
25 | ||
26 | /** Last update timestamp | |
27 | */ | |
28 | public $time = 0; | |
29 | ||
30 | /** Data | |
31 | */ | |
e3bc13e0 | 32 | public $data = array(); |
33 | ||
34 | /** Data caching | |
35 | */ | |
36 | private $urls = array(); | |
37 | private $title = array(); | |
38 | ||
39 | private $displaid = null; | |
d3b026ab | 40 | |
41 | /** Construct a new tree from a given root | |
42 | */ | |
43 | public function __construct(BananaSpoolHead &$root) | |
44 | { | |
45 | if (empty($root->children)) { | |
46 | $this->data = null; | |
47 | } else { | |
e3bc13e0 | 48 | $this->data =& $this->builder($root); |
d3b026ab | 49 | } |
50 | $this->time = time(); | |
51 | $this->version = BANANA_TREE_VERSION; | |
52 | $this->saveToFile($root->id); | |
53 | } | |
54 | ||
55 | private function &builder(BananaSpoolHead &$head) | |
56 | { | |
e3bc13e0 | 57 | $array = array(array($head->id)); |
58 | $this->urls[$head->id] = banana_entities(Banana::$page->makeURL(array('group' => Banana::$group, | |
59 | 'artid' => $head->id))); | |
60 | $this->title[$head->id] = banana_entities($head->name . ', ' . Banana::$spool->formatDate($head)); | |
d3b026ab | 61 | foreach ($head->children as $key=>&$msg) { |
62 | $tree =& $this->builder($msg); | |
63 | $last = $key == count($head->children) - 1; | |
64 | foreach ($tree as $kt=>&$line) { | |
65 | if ($kt === 0 && $key === 0 && !$last) { | |
ee4eff66 | 66 | $array[0] = array_merge($array[0], array(array('+', $msg->id)), $line); |
d3b026ab | 67 | } else if($kt === 0 && $key === 0) { |
ee4eff66 | 68 | $array[0] = array_merge($array[0], array(array('-', $msg->id)), $line); |
d3b026ab | 69 | } else if ($kt === 0 && $last) { |
ee4eff66 | 70 | $array[] = array_merge(array(' ', array('`', $msg->id)), $line); |
d3b026ab | 71 | } else if ($kt === 0) { |
ee4eff66 | 72 | $array[] = array_merge(array(' ', array('t', $msg->id)), $line); |
d3b026ab | 73 | } else if ($last) { |
e3bc13e0 | 74 | $array[] = array_merge(array(' ', ' '), $line); |
d3b026ab | 75 | } else { |
ee4eff66 | 76 | $array[] = array_merge(array(' ', array('|', $head->children[$key+1]->id)), $line); |
d3b026ab | 77 | } |
78 | } | |
79 | unset($tree); | |
80 | } | |
81 | return $array; | |
82 | } | |
83 | ||
84 | /** Save the content of the tree into a file | |
85 | */ | |
86 | private function saveToFile($id) | |
87 | { | |
88 | file_put_contents(BananaTree::filename($id), serialize($this)); | |
89 | } | |
90 | ||
1d521296 | 91 | /** Return html to display the tree |
92 | */ | |
93 | public function &show() | |
94 | { | |
20a3f08d | 95 | if (!is_null($this->displaid) || is_null($this->data)) { |
e3bc13e0 | 96 | return $this->displaid; |
97 | } | |
0fa5e39a | 98 | static $t_e, $tree; |
99 | //$u_h, $u_ht, $u_vt, $u_l, $u_f, $r_h, $r_ht, $r_vt, $r_l, $r_f; | |
e3bc13e0 | 100 | if (!isset($t_e)) { |
0fa5e39a | 101 | $tree = array(); |
e3bc13e0 | 102 | $t_e = Banana::$page->makeImg(Array('img' => 'e', 'alt' => ' ', 'height' => 18, 'width' => 14)); |
fdee5f35 FB |
103 | $tree['+'] = array(Banana::$page->makeImg(Array('img' => 'p2', 'alt' => '-', 'height' => 18, 'width' => 14)), |
104 | Banana::$page->makeImg(Array('img' => 'p2r', 'alt' => '+', 'height' => 18, 'width' => 14))); | |
105 | $tree['-'] = array(Banana::$page->makeImg(Array('img' => 'm2', 'alt' => '-', 'height' => 18, 'width' => 14)), | |
106 | Banana::$page->makeImg(Array('img' => 'm2r', 'alt' => '-', 'height' => 18, 'width' => 14))); | |
0fa5e39a | 107 | $tree['|'] = array(Banana::$page->makeImg(Array('img' => 'l2', 'alt' => '|', 'height' => 18, 'width' => 14)), |
108 | Banana::$page->makeImg(Array('img' => 'l2r', 'alt' => '|', 'height' => 18, 'width' => 14))); | |
fdee5f35 FB |
109 | $tree['`'] = array(Banana::$page->makeImg(Array('img' => 'a2', 'alt' => '`', 'height' => 18, 'width' => 14)), |
110 | Banana::$page->makeImg(Array('img' => 'a2r', 'alt' => '`', 'height' => 18, 'width' => 14))); | |
111 | $tree['t'] = array(Banana::$page->makeImg(Array('img' => 't2', 'alt' => 't', 'height' => 18, 'width' => 14)), | |
112 | Banana::$page->makeImg(Array('img' => 't2r', 'alt' => 't', 'height' => 18, 'width' => 14))); | |
e3bc13e0 | 113 | } |
114 | $text = '<div class="tree">'; | |
115 | foreach ($this->data as &$line) { | |
116 | $text .= '<div style="height: 18px">'; | |
117 | foreach ($line as &$item) { | |
ee4eff66 | 118 | if ($item == ' ') { |
119 | $text .= $t_e; | |
120 | } else if (is_array($item)) { | |
121 | $head =& Banana::$spool->overview[$item[1]]; | |
0fa5e39a | 122 | $text .= $tree[$item[0]][$head->isread ? 1 : 0]; |
ee4eff66 | 123 | } else { |
e3bc13e0 | 124 | $head =& Banana::$spool->overview[$item]; |
0fa5e39a | 125 | $text .= '<span style="background-color: ' . $head->color . '; text-decoration: none" title="' |
126 | . $this->title[$item] . '"><input type="radio" name="banana_tree" value="' . $head->id . '"'; | |
e3bc13e0 | 127 | if (Banana::$msgshow_javascript) { |
128 | $text .= ' onchange="window.location=\'' . $this->urls[$item] . '\'"'; | |
129 | } else { | |
130 | $text .= ' disabled="disabled"'; | |
131 | } | |
132 | if (Banana::$artid == $item) { | |
133 | $text .= ' checked="checked"'; | |
134 | } | |
135 | $text .= '/></span>'; | |
136 | } | |
137 | } | |
138 | $text .= "</div>\n"; | |
139 | } | |
140 | $text .= '</div>'; | |
141 | $this->displaid =& $text; | |
142 | return $text; | |
1d521296 | 143 | } |
144 | ||
d3b026ab | 145 | /** Get filename |
146 | */ | |
147 | static private function filename($id) | |
148 | { | |
abaec820 | 149 | static $host; |
150 | if (!isset($host)) { | |
151 | $host = parse_url(Banana::$page->makeURL(array()), PHP_URL_HOST); | |
152 | } | |
153 | return BananaSpool::getPath('tree_' . $id . '_' . $host); | |
d3b026ab | 154 | } |
155 | ||
156 | /** Read a tree from a file | |
157 | */ | |
158 | static private function &readFromFile($id) | |
159 | { | |
160 | $tree = null; | |
161 | $file = BananaTree::filename($id); | |
162 | if (!file_exists($file)) { | |
163 | return $tree; | |
164 | } | |
165 | $tree = unserialize(file_get_contents($file)); | |
166 | if ($tree->version != BANANA_TREE_VERSION) { | |
167 | $tree = null; | |
168 | } | |
169 | return $tree; | |
170 | } | |
171 | ||
172 | /** Build a tree for the given id | |
173 | */ | |
174 | static public function &build($id) | |
175 | { | |
176 | $root =& Banana::$spool->root($id); | |
177 | if (!isset(BananaTree::$cache[$root->id])) { | |
178 | $tree =& BananaTree::readFromFile($root->id); | |
179 | if (is_null($tree) || $tree->time < $root->time) { | |
180 | $tree = new BananaTree($root); | |
181 | } | |
182 | BananaTree::$cache[$root->id] =& $tree; | |
183 | } | |
184 | return BananaTree::$cache[$root->id]; | |
185 | } | |
186 | ||
187 | /** Kill the file associated to the given id | |
188 | */ | |
189 | static public function kill($id) | |
190 | { | |
191 | @unlink(BananaTree::filename($id)); | |
192 | } | |
193 | } | |
194 | // vim:set et sw=4 sts=4 ts=4 enc=utf-8: | |
195 | ?> |