lainchan/templates/themes/catalog/theme.php

268 lines
8.0 KiB
PHP
Raw Normal View History

2013-07-18 11:30:00 -04:00
<?php
require 'info.php';
2015-06-02 09:28:06 -04:00
function catalog_build($action, $settings, $board) {
global $config;
2015-06-02 09:28:06 -04:00
$b = new Catalog($settings);
$boards = explode(' ', $settings['boards']);
2013-07-18 11:30:00 -04:00
// Possible values for $action:
// - all (rebuild everything, initialization)
// - news (news has been updated)
// - boards (board list changed)
// - post (a reply has been made)
// - post-thread (a thread has been made)
2015-06-02 09:28:06 -04:00
if ($action === 'all') {
2013-07-18 11:30:00 -04:00
foreach ($boards as $board) {
$b = new Catalog();
$action = generation_strategy("sb_catalog", array($board));
if ($action == 'delete') {
2015-04-02 14:54:28 -04:00
file_unlink($config['dir']['home'] . $board . '/catalog.html');
file_unlink($config['dir']['home'] . $board . '/index.rss');
2015-04-02 14:54:28 -04:00
}
elseif ($action == 'rebuild') {
2015-04-02 14:54:28 -04:00
$b->build($settings, $board);
}
2013-07-18 11:30:00 -04:00
}
} elseif ($action == 'post-thread' || ($settings['update_on_posts'] && $action == 'post') || ($settings['update_on_posts'] && $action == 'post-delete') && in_array($board, $boards)) {
$b = new Catalog();
2015-04-02 14:54:28 -04:00
$action = generation_strategy("sb_catalog", array($board));
if ($action == 'delete') {
2015-04-02 14:54:28 -04:00
file_unlink($config['dir']['home'] . $board . '/catalog.html');
file_unlink($config['dir']['home'] . $board . '/index.rss');
2015-04-02 14:54:28 -04:00
}
elseif ($action == 'rebuild') {
2015-04-02 14:54:28 -04:00
$b->build($settings, $board);
}
}
2015-06-02 09:28:06 -04:00
// FIXME: Check that Ukko is actually enabled
if ($settings['enable_ukko'] && (
$action === 'all' || $action === 'post' ||
$action === 'post-thread' || $action === 'post-delete'))
{
$b->buildUkko();
2013-07-18 11:30:00 -04:00
}
// FIXME: Check that Rand is actually enabled
if ($settings['enable_rand'] && (
$action === 'all' || $action === 'post' ||
$action === 'post-thread' || $action === 'post-delete'))
{
$b->buildRand();
}
}
2015-06-02 09:28:06 -04:00
// Wrap functions in a class so they don't interfere with normal Tinyboard operations
class Catalog {
2015-06-02 09:28:06 -04:00
private $settings;
// Cache for threads from boards that have already been processed
private $threadsCache = array();
2015-04-02 14:54:28 -04:00
2015-06-02 09:28:06 -04:00
public function __construct($settings) {
$this->settings = $settings;
}
/**
* Build and save the HTML of the catalog for the Ukko theme
*/
public function buildUkko() {
global $config;
$ukkoSettings = themeSettings('ukko');
$queries = array();
$threads = array();
$exclusions = explode(' ', $ukkoSettings['exclude']);
$boards = array_diff(listBoards(true), $exclusions);
foreach ($boards as $b) {
if (array_key_exists($b, $this->threadsCache)) {
$threads = array_merge($threads, $this->threadsCache[$b]);
} else {
$queries[] = $this->buildThreadsQuery($b);
2015-04-02 14:54:28 -04:00
}
}
2015-06-02 09:28:06 -04:00
// Fetch threads from boards that haven't beenp processed yet
if (!empty($queries)) {
$sql = implode(' UNION ALL ', $queries);
$res = query($sql) or error(db_error());
$threads = array_merge($threads, $res->fetchAll(PDO::FETCH_ASSOC));
}
// Sort in bump order
usort($threads, function($a, $b) {
return strcmp($b['bump'], $a['bump']);
});
// Generate data for the template
$recent_posts = $this->generateRecentPosts($threads);
$this->saveForBoard($ukkoSettings['uri'], $recent_posts,
$config['root'] . $ukkoSettings['uri']);
}
/**
* Build and save the HTML of the catalog for the Rand theme
*/
public function buildRand() {
global $config;
$randSettings = themeSettings('rand');
$queries = array();
$threads = array();
$exclusions = explode(' ', $randSettings['exclude']);
$boards = array_diff(listBoards(true), $exclusions);
foreach ($boards as $b) {
if (array_key_exists($b, $this->threadsCache)) {
$threads = array_merge($threads, $this->threadsCache[$b]);
} else {
$queries[] = $this->buildThreadsQuery($b);
}
}
// Fetch threads from boards that haven't beenp processed yet
if (!empty($queries)) {
$sql = implode(' UNION ALL ', $queries);
$res = query($sql) or error(db_error());
$threads = array_merge($threads, $res->fetchAll(PDO::FETCH_ASSOC));
}
// Sort in bump order
usort($threads, function($a, $b) {
return strcmp($b['bump'], $a['bump']);
});
// Generate data for the template
$recent_posts = $this->generateRecentPosts($threads);
$this->saveForBoard($randSettings['uri'], $recent_posts,
$config['root'] . $randSettings['uri']);
}
2015-06-02 09:28:06 -04:00
/**
* Build and save the HTML of the catalog for the given board
*/
public function build($board_name) {
if (!openBoard($board_name)) {
error(sprintf(_("Board %s doesn't exist"), $post['board']));
}
if (array_key_exists($board_name, $this->threadsCache)) {
$threads = $this->threadsCache[$board_name];
} else {
$sql = $this->buildThreadsQuery($board_name);
$query = query($sql . ' ORDER BY `bump` DESC') or error(db_error());
$threads = $query->fetchAll(PDO::FETCH_ASSOC);
// Save for posterity
$this->threadsCache[$board_name] = $threads;
}
// Generate data for the template
$recent_posts = $this->generateRecentPosts($threads);
$this->saveForBoard($board_name, $recent_posts);
}
private function buildThreadsQuery($board) {
$sql = "SELECT *, `id` AS `thread_id`, " .
"(SELECT COUNT(`id`) FROM ``posts_$board`` WHERE `thread` = `thread_id`) AS `reply_count`, " .
"(SELECT SUM(`num_files`) FROM ``posts_$board`` WHERE `thread` = `thread_id` AND `num_files` IS NOT NULL) AS `image_count`, " .
"'$board' AS `board` FROM ``posts_$board`` WHERE `thread` IS NULL";
return $sql;
}
private function generateRecentPosts($threads) {
global $config, $board;
$posts = array();
foreach ($threads as $post) {
if ($board['uri'] !== $post['board']) {
openBoard($post['board']);
}
2015-03-10 08:23:40 -04:00
$post['link'] = $config['root'] . $board['dir'] . $config['dir']['res'] . link_for($post);
2013-07-18 11:30:00 -04:00
$post['board_name'] = $board['name'];
if ($post['embed'] && preg_match('/^https?:\/\/(\w+\.)?(?:youtube\.com\/watch\?v=|youtu\.be\/)([a-zA-Z0-9\-_]{10,11})(&.+)?$/i', $post['embed'], $matches)) {
$post['youtube'] = $matches[2];
2015-06-02 09:28:06 -04:00
}
if (isset($post['files']) && $post['files']) {
2014-04-27 09:48:47 -04:00
$files = json_decode($post['files']);
if ($files[0]) {
if ($files[0]->file == 'deleted') {
if (count($files) > 1) {
foreach ($files as $file) {
2015-06-02 09:28:06 -04:00
if (($file == $files[0]) || ($file->file == 'deleted'))
continue;
$post['file'] = $config['uri_thumb'] . $file->thumb;
}
2015-06-02 09:28:06 -04:00
if (empty($post['file']))
$post['file'] = $config['image_deleted'];
} else {
$post['file'] = $config['image_deleted'];
}
2015-06-02 09:28:06 -04:00
} else if($files[0]->thumb == 'spoiler') {
$post['file'] = '/' . $config['spoiler_image'];
2015-06-02 09:28:06 -04:00
} else {
$post['file'] = $config['uri_thumb'] . $files[0]->thumb;
}
}
} else {
$post['file'] = $config['root'] . $config['image_deleted'];
}
2015-06-02 09:28:06 -04:00
if (empty($post['image_count']))
$post['image_count'] = 0;
2015-03-09 05:31:08 -04:00
$post['pubdate'] = date('r', $post['time']);
2015-06-02 09:28:06 -04:00
$posts[] = $post;
}
return $posts;
}
private function saveForBoard($board_name, $recent_posts, $board_link = null) {
global $board, $config;
if ($board_link === null) {
$board_link = $config['root'] . $board['dir'];
2013-07-18 11:30:00 -04:00
}
2014-06-12 13:26:21 -04:00
2015-06-02 09:28:06 -04:00
$required_scripts = array('js/jquery.min.js', 'js/jquery.mixitup.min.js',
'js/catalog.js');
// Include scripts that haven't been yet included
2014-06-12 13:26:21 -04:00
foreach($required_scripts as $i => $s) {
if (!in_array($s, $config['additional_javascript']))
$config['additional_javascript'][] = $s;
}
file_write($config['dir']['home'] . $board_name . '/catalog.html', Element('themes/catalog/catalog.html', Array(
2015-06-02 09:28:06 -04:00
'settings' => $this->settings,
2013-07-18 11:30:00 -04:00
'config' => $config,
'boardlist' => createBoardlist(),
2015-06-02 09:28:06 -04:00
'recent_images' => array(),
2013-07-18 11:30:00 -04:00
'recent_posts' => $recent_posts,
2015-06-02 09:28:06 -04:00
'stats' => array(),
2013-07-18 11:30:00 -04:00
'board' => $board_name,
2015-06-02 09:28:06 -04:00
'link' => $board_link
)));
2015-03-09 05:31:08 -04:00
file_write($config['dir']['home'] . $board_name . '/index.rss', Element('themes/catalog/index.rss', Array(
'config' => $config,
'recent_posts' => $recent_posts,
'board' => $board
)));
2013-07-18 11:30:00 -04:00
}
2015-06-02 09:28:06 -04:00
}