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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
<?php
require 'info.php';
/**
* Generate the board's HTML and move it and its JavaScript in place, whence
* it's served
*/
function semirand_build($action, $settings) {
global $config;
if ($action !== 'all' && $action !== 'post' && $action !== 'post-thread' &&
$action !== 'post-delete')
{
return;
}
if ($config['smart_build']) {
file_unlink($settings['uri'] . '/index.html');
} else {
$semirand = new semirand($settings);
// Copy the generated board HTML to its place
file_write($settings['uri'] . '/index.html', $semirand->build());
file_write($settings['uri'] . '/semirand.js',
Element('themes/semirand/semirand.js', array()));
}
}
/**
* Encapsulation of the theme's internals
*/
class semirand {
private $settings;
function __construct($settings) {
$this->settings = $this->parseSettings($settings);
}
/**
* Parse and validate configuration parameters passed from the UI
*/
private function parseSettings($settings) {
if (!is_numeric($settings['thread_limit']) ||
!is_numeric($settings['random_count']) ||
!is_numeric($settings['recent_count']))
{
error('Invalid configuration parameters.', true);
}
$settings['exclude'] = explode(' ', $settings['exclude']);
$settings['thread_limit'] = intval($settings['thread_limit']);
$settings['random_count'] = intval($settings['random_count']);
$settings['recent_count'] = intval($settings['recent_count']);
if ($settings['thread_limit'] < 1 ||
$settings['random_count'] < 1 ||
$settings['recent_count'] < 1)
{
error('Invalid configuration parameters.', true);
}
return $settings;
}
/**
* Obtain list of all threads from all non-excluded boards
*/
private function fetchThreads() {
$query = '';
$boards = listBoards(true);
foreach ($boards as $b) {
if (in_array($b, $this->settings['exclude']))
continue;
// Threads are those posts that have no parent thread
$query .= "SELECT *, '$b' AS `board` FROM ``posts_$b`` " .
"WHERE `thread` IS NULL UNION ALL ";
}
$query = preg_replace('/UNION ALL $/', 'ORDER BY `bump` DESC', $query);
$result = query($query) or error(db_error());
return $result->fetchAll(PDO::FETCH_ASSOC);
}
/**
* Retrieve all replies to a given thread
*/
private function fetchReplies($board, $thread_id) {
$query = prepare("SELECT * FROM ``posts_$board`` WHERE `thread` = :id");
$query->bindValue(':id', $thread_id, PDO::PARAM_INT);
$query->execute() or error(db_error($query));
return $query->fetchAll(PDO::FETCH_ASSOC);
}
/**
* Intersperse random threads between those in bump order
*/
private function shuffleThreads($threads) {
$random_count = $this->settings['random_count'];
$recent_count = $this->settings['recent_count'];
$total = count($threads);
// Storage for threads that will be randomly interspersed
$shuffled = array();
// Ratio of bumped / all threads
$topRatio = $recent_count / ($recent_count + $random_count);
// Shuffle the bottom half of all threads
$random = array_splice($threads, floor($total * $topRatio));
shuffle($random);
// Merge the random and sorted threads into one sequence. The pattern
// starts at random threads
while (!empty($threads)) {
$shuffled = array_merge($shuffled,
array_splice($random, 0, $random_count),
array_splice($threads, 0, $recent_count));
}
return $shuffled;
}
/**
* Build the HTML of a single thread in the catalog
*/
private function buildOne($post, $mod = false) {
global $config;
openBoard($post['board']);
$thread = new Thread($post, $mod ? '?/' : $config['root'], $mod);
$replies = $this->fetchReplies($post['board'], $post['id']);
// Number of replies to a thread that are displayed beneath it
$preview_count = $post['sticky'] ? $config['threads_preview_sticky'] :
$config['threads_preview'];
// Chomp the last few replies
$disp_replies = array_splice($replies, 0, $preview_count);
$disp_img_count = 0;
foreach ($disp_replies as $reply) {
if ($reply['files'] !== '')
++$disp_img_count;
// Append the reply to the thread as it's being built
$thread->add(new Post($reply, $mod ? '?/' : $config['root'], $mod));
}
// Count the number of omitted image replies
$omitted_img_count = count(array_filter($replies, function($p) {
return $p['files'] !== '';
}));
// Set the corresponding omitted numbers on the thread
if (!empty($replies)) {
$thread->omitted = count($replies);
$thread->omitted_images = $omitted_img_count;
}
// Board name and link
$html = '<h2><a href="' . $config['root'] . $post['board'] . '/">/' .
$post['board'] . '/</a></h2>';
// The thread itself
$html .= $thread->build(true);
return $html;
}
/**
* Query the required information and generate the HTML
*/
public function build($mod = false) {
if (!isset($this->settings)) {
error('Theme is not configured properly.');
}
global $config;
$html = '';
$overflow = array();
// Fetch threads from all boards and chomp the first 'n' posts, depending
// on the setting
$threads = $this->shuffleThreads($this->fetchThreads());
$total_count = count($threads);
// Top threads displayed on load
$top_threads = array_splice($threads, 0, $this->settings['thread_limit']);
// Number of processed threads by board
$counts = array();
// Output threads up to the specified limit
foreach ($top_threads as $post) {
if (array_key_exists($post['board'], $counts)) {
++$counts[$post['board']];
} else {
$counts[$post['board']] = 1;
}
$html .= $this->buildOne($post, $mod);
}
foreach ($threads as $post) {
if (array_key_exists($post['board'], $counts)) {
++$counts[$post['board']];
} else {
$counts[$post['board']] = 1;
}
$page = 'index';
$board_page = floor($counts[$post['board']] / $config['threads_per_page']);
if ($board_page > 0) {
$page = $board_page + 1;
}
$overflow[] = array(
'id' => $post['id'],
'board' => $post['board'],
'page' => $page . '.html'
);
}
$html .= '<script>var ukko_overflow = ' . json_encode($overflow) . '</script>';
$html .= '<script type="text/javascript" src="/'.$this->settings['uri'].'/semirand.js"></script>';
return Element('index.html', array(
'config' => $config,
'board' => array(
'dir' => $this->settings['uri'] . "/",
'url' => $this->settings['uri'],
'title' => $this->settings['title'],
'subtitle' => str_replace('%s', $this->settings['thread_limit'],
strval(min($this->settings['subtitle'], $total_count))),
),
'no_post_form' => true,
'body' => $html,
'mod' => $mod,
'boardlist' => createBoardlist($mod),
));
}
};
if (!function_exists('array_column')) {
/**
* Pick out values from subarrays by given key
*/
function array_column($array, $key) {
$result = [];
foreach ($array as $val) {
$result[] = $val[$key];
}
return $result;
}
}
|