Fixes vim mode line.
[banana.git] / banana / tree.inc.php
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
11 define('BANANA_TREE_VERSION', '0.1.2');
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 */
32 public $data = array();
33
34 /** Data caching
35 */
36 private $urls = array();
37 private $title = array();
38
39 private $displaid = null;
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 {
48 $this->data =& $this->builder($root);
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 {
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));
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) {
66 $array[0] = array_merge($array[0], array(array('+', $msg->id)), $line);
67 } else if($kt === 0 && $key === 0) {
68 $array[0] = array_merge($array[0], array(array('-', $msg->id)), $line);
69 } else if ($kt === 0 && $last) {
70 $array[] = array_merge(array(' ', array('`', $msg->id)), $line);
71 } else if ($kt === 0) {
72 $array[] = array_merge(array(' ', array('t', $msg->id)), $line);
73 } else if ($last) {
74 $array[] = array_merge(array(' ', ' '), $line);
75 } else {
76 $array[] = array_merge(array(' ', array('|', $head->children[$key+1]->id)), $line);
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
91 /** Create a reference to a tree image.
92 */
93 static private function makeTreeImg($img, $alt)
94 {
95 return Banana::$page->makeImg(Array('img' => $img, 'alt' => $alt, 'height' => 18, 'width' => 14));
96 }
97
98 /** Add an entry to the static tree association table.
99 */
100 static private function addTreeKind(array& $tree, $ascii, $img)
101 {
102 $tree[$ascii] = array(self::makeTreeImg($img . Banana::$tree_unread, $ascii),
103 self::makeTreeImg($img . Banana::$tree_read, $ascii));
104 }
105
106 /** Return html to display the tree
107 */
108 public function &show()
109 {
110 if (!is_null($this->displaid) || is_null($this->data)) {
111 return $this->displaid;
112 }
113 static $t_e, $tree;
114 //$u_h, $u_ht, $u_vt, $u_l, $u_f, $r_h, $r_ht, $r_vt, $r_l, $r_f;
115 if (!isset($t_e)) {
116 $t_e = self::makeTreeImg('e', ' ');
117 $tree = array();
118 self::addTreeKind($tree, '+', 'p2');
119 self::addTreeKind($tree, '-', 'm2');
120 self::addTreeKind($tree, '|', 'l2');
121 self::addTreeKind($tree, '`', 'a2');
122 self::addTreeKind($tree, 't', 't2');
123 }
124 $text = '<div class="tree">';
125 foreach ($this->data as &$line) {
126 $text .= '<div style="height: 18px">';
127 unset($head);
128 foreach ($line as &$item) {
129 if ($item == ' ') {
130 $text .= $t_e;
131 } else if (is_array($item)) {
132 $head =& Banana::$spool->overview[$item[1]];
133 $text .= $tree[$item[0]][$head->isread ? 1 : 0];
134 } else {
135 $head =& Banana::$spool->overview[$item];
136 $text .= '<span style="background-color: ' . $head->color . '; text-decoration: none" title="'
137 . $this->title[$item] . '"><input type="radio" name="banana_tree" value="' . $head->id . '"';
138 if (Banana::$msgshow_javascript) {
139 $text .= ' onchange="window.location=\'' . $this->urls[$item] . '\'"';
140 } else {
141 $text .= ' disabled="disabled"';
142 }
143 if (Banana::$artid == $item) {
144 $text .= ' checked="checked"';
145 }
146 $last_title = $head->subject;
147 $text .= '/></span>';
148 }
149 }
150 if (function_exists('hook_spoolTagBranch') && isset($head)) {
151 $text .= hook_spoolTagBranch($head);
152 }
153 $text .= "</div>\n";
154 }
155 $text .= '</div>';
156 $this->displaid =& $text;
157 return $text;
158 }
159
160 /** Get filename
161 */
162 static private function filename($id)
163 {
164 static $host;
165 if (!isset($host)) {
166 $host = parse_url(Banana::$page->makeURL(array()), PHP_URL_HOST);
167 }
168 return BananaSpool::getPath('tree_' . $id . '_' . $host);
169 }
170
171 /** Read a tree from a file
172 */
173 static private function &readFromFile($id)
174 {
175 $tree = null;
176 $file = BananaTree::filename($id);
177 if (!file_exists($file)) {
178 return $tree;
179 }
180 $tree = unserialize(file_get_contents($file));
181 if ($tree->version != BANANA_TREE_VERSION) {
182 $tree = null;
183 }
184 return $tree;
185 }
186
187 /** Build a tree for the given id
188 */
189 static public function &build($id)
190 {
191 $root =& Banana::$spool->root($id);
192 if (!isset(BananaTree::$cache[$root->id])) {
193 $tree =& BananaTree::readFromFile($root->id);
194 if (is_null($tree) || $tree->time < $root->time) {
195 $tree = new BananaTree($root);
196 }
197 BananaTree::$cache[$root->id] =& $tree;
198 }
199 return BananaTree::$cache[$root->id];
200 }
201
202 /** Kill the file associated to the given id
203 */
204 static public function kill($id)
205 {
206 @unlink(BananaTree::filename($id));
207 }
208 }
209 // vim:set et sw=4 sts=4 ts=4 fenc=utf-8:
210 ?>