Add catalog for Ukko

This commit is contained in:
Matěj Grabovský 2015-06-02 15:28:06 +02:00 committed by -
parent 013bf68d26
commit c2367e7c03
6 changed files with 205 additions and 102 deletions

View File

@ -12,47 +12,51 @@
* $config['additional_javascript'][] = 'js/catalog-link.js'; * $config['additional_javascript'][] = 'js/catalog-link.js';
*/ */
function catalog() { (function($) {
var board = $("input[name='board']"); var catalog = function() {
var board = $('input[name="board"]');
var catalog_url = configRoot + board.first().val() + "/catalog.html"; if (!board.length) {
return;
}
var pages = document.getElementsByClassName('pages')[0]; var catalog_url = configRoot + board.first().val() + '/catalog.html';
var bottom = document.getElementsByClassName('boardlist bottom')[0]
var subtitle = document.getElementsByClassName('subtitle')[0];
var link = document.createElement('a'); var pages = $('.pages');
link.href = catalog_url; var bottom = $('.boardlist.bottom');
var subtitle = $('.subtitle');
if (pages) { var link = $('<a class="catalog" />')
link.textContent = _('Catalog'); .attr('href', catalog_url);
link.style.color = '#F10000';
link.style.padding = '4px';
link.style.paddingLeft = '9px';
link.style.borderLeft = '1px solid'
link.style.borderLeftColor = '#A8A8A8';
link.style.textDecoration = "underline";
pages.appendChild(link) if (pages.length) {
} link.text(_('Catalog'))
else { .css({
link.textContent = '['+_('Catalog')+']'; color: '#F10000',
link.style.paddingLeft = '10px'; padding: '4px',
link.style.textDecoration = "underline"; textDecoration: 'underline',
document.body.insertBefore(link, bottom); display: 'table-cell'
});
link.insertAfter(pages);
} else if (bottom.length) {
link.text('['+_('Catalog')+']')
.css({
paddingLeft: '10px',
textDecoration: 'underline'
});
link.insertBefore(bottom);
}
if (subtitle.length) {
subtitle.append('<br />');
$('<a class="catalog" />')
.text(_('Catalog'))
.attr('href', catalog_url)
.appendTo(subtitle);
}
} }
if (subtitle) { if (active_page == 'thread' || active_page == 'index' || active_page == 'ukko') {
var link2 = document.createElement('a');
link2.textContent = _('Catalog');
link2.href = catalog_url;
var br = document.createElement('br');
subtitle.appendChild(br);
subtitle.appendChild(link2);
}
}
if (active_page == 'thread' || active_page == 'index') {
$(document).ready(catalog); $(document).ready(catalog);
} }
})(jQuery);

View File

@ -36,11 +36,12 @@
{% for post in recent_posts %} {% for post in recent_posts %}
<div class="mix" <div class="mix"
data-reply="{{ post.reply_count }}" data-reply="{{ post.reply_count }}"
data-bump="{{ post.bump }}" data-bump="{{ post.bump }}"
data-time="{{ post.time }}" data-time="{{ post.time }}"
data-id="{{ post.id }}" data-board="{{ post.board }}"
data-sticky="{% if post.sticky %}true{% else %}false{% endif %}" data-id="{{ post.id }}"
data-locked="{% if post.locked %}true{% else %}false{% endif %}" data-sticky="{% if post.sticky %}true{% else %}false{% endif %}"
data-locked="{% if post.locked %}true{% else %}false{% endif %}"
> >
<div class="thread grid-li grid-size-small"> <div class="thread grid-li grid-size-small">
<a href="{{post.link}}"> <a href="{{post.link}}">

View File

@ -1,27 +1,27 @@
<?php <?php
$theme = array(); $theme = array();
// Theme name // Theme name
$theme['name'] = 'Catalog'; $theme['name'] = 'Catalog';
// Description (you can use Tinyboard markup here) // Description (you can use Tinyboard markup here)
$theme['description'] = 'Show a post catalog.'; $theme['description'] = 'Show a post catalog.';
$theme['version'] = 'v0.2'; $theme['version'] = 'v0.2.1';
// Theme configuration // Theme configuration
$theme['config'] = Array(); $theme['config'] = Array();
$theme['config'][] = Array( $theme['config'][] = Array(
'title' => 'Title', 'title' => 'Title',
'name' => 'title', 'name' => 'title',
'type' => 'text', 'type' => 'text',
'default' => 'Catalog' 'default' => 'Catalog'
); );
$__boards = listBoards(); $__boards = listBoards();
$__default_boards = Array(); $__default_boards = Array();
foreach ($__boards as $__board) foreach ($__boards as $__board)
$__default_boards[] = $__board['uri']; $__default_boards[] = $__board['uri'];
$theme['config'][] = Array( $theme['config'][] = Array(
'title' => 'Included boards', 'title' => 'Included boards',
'name' => 'boards', 'name' => 'boards',
@ -29,7 +29,7 @@
'comment' => '(space seperated)', 'comment' => '(space seperated)',
'default' => implode(' ', $__default_boards) 'default' => implode(' ', $__default_boards)
); );
$theme['config'][] = Array( $theme['config'][] = Array(
'title' => 'Update on new posts', 'title' => 'Update on new posts',
'name' => 'update_on_posts', 'name' => 'update_on_posts',
@ -38,14 +38,22 @@
'comment' => 'Without this checked, the catalog only updates on new threads.' 'comment' => 'Without this checked, the catalog only updates on new threads.'
); );
$theme['config'][] = Array( $theme['config'][] = Array(
'title' => 'Use tooltipster', 'title' => 'Enable Ukko catalog',
'name' => 'use_tooltipster', 'name' => 'enable_ukko',
'type' => 'checkbox', 'type' => 'checkbox',
'default' => true, 'default' => false,
'comment' => 'Check this if you wish to show a nice tooltip with info about the thread on mouse over.' 'comment' => 'Enable catalog for the Ukko theme. This requires the Ukko theme to be enabled.'
); );
$theme['config'][] = Array(
'title' => 'Use tooltipster',
'name' => 'use_tooltipster',
'type' => 'checkbox',
'default' => true,
'comment' => 'Check this if you wish to show a nice tooltip with info about the thread on mouse over.'
);
// Unique function name for building everything // Unique function name for building everything
$theme['build_function'] = 'catalog_build'; $theme['build_function'] = 'catalog_build';

View File

@ -1,21 +1,21 @@
<?php <?php
require 'info.php'; require 'info.php';
function catalog_build($action, $settings, $board) { function catalog_build($action, $settings, $board) {
global $config; global $config;
$b = new Catalog($settings);
$boards = explode(' ', $settings['boards']);
// Possible values for $action: // Possible values for $action:
// - all (rebuild everything, initialization) // - all (rebuild everything, initialization)
// - news (news has been updated) // - news (news has been updated)
// - boards (board list changed) // - boards (board list changed)
// - post (a reply has been made) // - post (a reply has been made)
// - post-thread (a thread has been made) // - post-thread (a thread has been made)
if ($action === 'all') {
$boards = explode(' ', $settings['boards']);
if ($action == 'all') {
foreach ($boards as $board) { foreach ($boards as $board) {
$b = new Catalog(); $b = new Catalog($settings);
$action = generation_strategy("sb_catalog", array($board)); $action = generation_strategy("sb_catalog", array($board));
if ($action == 'delete') { if ($action == 'delete') {
@ -23,11 +23,11 @@
file_unlink($config['dir']['home'] . $board . '/index.rss'); file_unlink($config['dir']['home'] . $board . '/index.rss');
} }
elseif ($action == 'rebuild') { elseif ($action == 'rebuild') {
$b->build($settings, $board); $b->build($board);
} }
} }
} elseif ($action == 'post-thread' || ($settings['update_on_posts'] && $action == 'post') || ($settings['update_on_posts'] && $action == 'post-delete') && in_array($board, $boards)) { } elseif ($action == 'post-thread' || ($settings['update_on_posts'] && $action == 'post') || ($settings['update_on_posts'] && $action == 'post-delete') && in_array($board, $boards)) {
$b = new Catalog(); $b = new Catalog($settings);
$action = generation_strategy("sb_catalog", array($board)); $action = generation_strategy("sb_catalog", array($board));
if ($action == 'delete') { if ($action == 'delete') {
@ -35,39 +35,116 @@
file_unlink($config['dir']['home'] . $board . '/index.rss'); file_unlink($config['dir']['home'] . $board . '/index.rss');
} }
elseif ($action == 'rebuild') { elseif ($action == 'rebuild') {
$b->build($settings, $board); $b->build($board);
} }
} }
// FIXME: Check that Ukko is actually enabled
if ($settings['enable_ukko'] && (
$action === 'all' || $action === 'post' ||
$action === 'post-thread' || $action === 'post-delete'))
{
$b->buildUkko();
}
} }
// Wrap functions in a class so they don't interfere with normal Tinyboard operations // Wrap functions in a class so they don't interfere with normal Tinyboard operations
class Catalog { class Catalog {
public function build($settings, $board_name) { private $settings;
global $config, $board; // Cache for threads from boards that have already been processed
private $threadsCache = array();
if (!isset($board) || $board['uri'] != $board_name) { public function __construct($settings) {
if (!openBoard($board_name)) { $this->settings = $settings;
error(sprintf(_("Board %s doesn't exist"), $board_name)); }
/**
* 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);
} }
} }
$recent_images = array(); // Fetch threads from boards that haven't beenp processed yet
$recent_posts = array(); if (!empty($queries)) {
$stats = array(); $sql = implode(' UNION ALL ', $queries);
$res = query($sql) or error(db_error());
$query = query(sprintf("SELECT *, `id` AS `thread_id`, $threads = array_merge($threads, $res->fetchAll(PDO::FETCH_ASSOC));
(SELECT COUNT(`id`) FROM ``posts_%s`` WHERE `thread` = `thread_id`) AS `reply_count`, }
(SELECT SUM(`num_files`) FROM ``posts_%s`` WHERE `thread` = `thread_id` AND `num_files` IS NOT NULL) AS `image_count`,
'%s' AS `board` FROM ``posts_%s`` WHERE `thread` IS NULL ORDER BY `bump` DESC", // Sort in bump order
$board_name, $board_name, $board_name, $board_name, $board_name)) or error(db_error()); usort($threads, function($a, $b) {
return strcmp($b['bump'], $a['bump']);
while ($post = $query->fetch(PDO::FETCH_ASSOC)) { });
// 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 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']);
}
$post['link'] = $config['root'] . $board['dir'] . $config['dir']['res'] . link_for($post); $post['link'] = $config['root'] . $board['dir'] . $config['dir']['res'] . link_for($post);
$post['board_name'] = $board['name']; $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)) { 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]; $post['youtube'] = $matches[2];
} }
if (isset($post['files']) && $post['files']) { if (isset($post['files']) && $post['files']) {
$files = json_decode($post['files']); $files = json_decode($post['files']);
@ -76,20 +153,19 @@
if ($files[0]->file == 'deleted') { if ($files[0]->file == 'deleted') {
if (count($files) > 1) { if (count($files) > 1) {
foreach ($files as $file) { foreach ($files as $file) {
if (($file == $files[0]) || ($file->file == 'deleted')) continue; if (($file == $files[0]) || ($file->file == 'deleted'))
continue;
$post['file'] = $config['uri_thumb'] . $file->thumb; $post['file'] = $config['uri_thumb'] . $file->thumb;
} }
if (empty($post['file'])) $post['file'] = $config['image_deleted']; if (empty($post['file']))
} $post['file'] = $config['image_deleted'];
else { } else {
$post['file'] = $config['image_deleted']; $post['file'] = $config['image_deleted'];
} }
} } else if($files[0]->thumb == 'spoiler') {
else if($files[0]->thumb == 'spoiler') {
$post['file'] = '/' . $config['spoiler_image']; $post['file'] = '/' . $config['spoiler_image'];
} } else {
else {
$post['file'] = $config['uri_thumb'] . $files[0]->thumb; $post['file'] = $config['uri_thumb'] . $files[0]->thumb;
} }
} }
@ -97,27 +173,40 @@
$post['file'] = $config['root'] . $config['image_deleted']; $post['file'] = $config['root'] . $config['image_deleted'];
} }
if (empty($post['image_count'])) $post['image_count'] = 0; if (empty($post['image_count']))
$post['image_count'] = 0;
$post['pubdate'] = date('r', $post['time']); $post['pubdate'] = date('r', $post['time']);
$recent_posts[] = $post; $posts[] = $post;
} }
$required_scripts = array('js/jquery.min.js', 'js/jquery.mixitup.min.js', 'js/catalog.js');
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'];
}
$required_scripts = array('js/jquery.min.js', 'js/jquery.mixitup.min.js',
'js/catalog.js');
// Include scripts that haven't been yet included
foreach($required_scripts as $i => $s) { foreach($required_scripts as $i => $s) {
if (!in_array($s, $config['additional_javascript'])) if (!in_array($s, $config['additional_javascript']))
$config['additional_javascript'][] = $s; $config['additional_javascript'][] = $s;
} }
file_write($config['dir']['home'] . $board_name . '/catalog.html', Element('themes/catalog/catalog.html', Array( file_write($config['dir']['home'] . $board_name . '/catalog.html', Element('themes/catalog/catalog.html', Array(
'settings' => $settings, 'settings' => $this->settings,
'config' => $config, 'config' => $config,
'boardlist' => createBoardlist(), 'boardlist' => createBoardlist(),
'recent_images' => $recent_images, 'recent_images' => array(),
'recent_posts' => $recent_posts, 'recent_posts' => $recent_posts,
'stats' => $stats, 'stats' => array(),
'board' => $board_name, 'board' => $board_name,
'link' => $config['root'] . $board['dir'] 'link' => $board_link
))); )));
file_write($config['dir']['home'] . $board_name . '/index.rss', Element('themes/catalog/index.rss', Array( file_write($config['dir']['home'] . $board_name . '/index.rss', Element('themes/catalog/index.rss', Array(
@ -126,4 +215,4 @@
'board' => $board 'board' => $board
))); )));
} }
}; }

View File

@ -53,7 +53,7 @@
), ),
array( array(
'title' => 'Bumped threads', 'title' => 'Bumped threads',
'name' => 'bumped_count', 'name' => 'recent_count',
'comment' => '(number of consecutive recent threads)', 'comment' => '(number of consecutive recent threads)',
'type' => 'text', 'type' => 'text',
'default' => '1', 'default' => '1',

View File

@ -30,6 +30,7 @@
$body = ''; $body = '';
$overflow = array(); $overflow = array();
$board = array( $board = array(
'uri' => $this->settings['uri'],
'url' => $this->settings['uri'], 'url' => $this->settings['uri'],
'title' => $this->settings['title'], 'title' => $this->settings['title'],
'subtitle' => sprintf($this->settings['subtitle'], $this->settings['thread_limit']) 'subtitle' => sprintf($this->settings['subtitle'], $this->settings['thread_limit'])