From 6d1eb9961d67f2d0fbb3fc3c427fb2803c0b61d4 Mon Sep 17 00:00:00 2001 From: 8n-tech <8n-tech@users.noreply.github.com> Date: Tue, 14 Apr 2015 01:40:45 +1000 Subject: [PATCH] - board-search.php can now act as an include as well as a direct request. - boards.php completely rewritten to work with new board-search.php. Functions (sort-of) without JavaScript. - inc/functions.php fetchBoardActivity() now pulls total board counts with a more reliable and faster method. - inc/functions.php fetchBoardActivity() & fetchBoardTags() now expect db prefixes. - stylesheets/stylesheet.css now handles title search bar. - boards-tags.html completely thrown out for flat tag list styling. - boards-search.html added for the new page layout. - boards-table.html added for the contents. Signed-off-by: 8n-tech <8n-tech@users.noreply.github.com> --- board-search.php | 108 ++++++++++------- boards.php | 187 ++++++++++------------------- inc/functions.php | 25 +++- stylesheets/style.css | 3 +- templates/8chan/boards-search.html | 78 ++++++++++++ templates/8chan/boards-table.html | 11 ++ templates/8chan/boards-tags.html | 95 +-------------- 7 files changed, 243 insertions(+), 264 deletions(-) create mode 100644 templates/8chan/boards-search.html create mode 100644 templates/8chan/boards-table.html diff --git a/board-search.php b/board-search.php index d9fb3ec2..044aaad7 100644 --- a/board-search.php +++ b/board-search.php @@ -1,6 +1,11 @@ false, - 'nsfw' => true, - 'tags' => false, + 'lang' => false, + 'nsfw' => true, + 'tags' => false, + 'titles' => false, ); // Include NSFW boards? @@ -96,67 +102,83 @@ foreach ($response['boards'] as $boardUri => &$board) { /* Activity Fetching */ $boardActivity = fetchBoardActivity( array_keys( $response['boards'] ) ); +$response['tags'] = array(); // Loop through each board and record activity to it. // We will also be weighing and building a tag list. foreach ($response['boards'] as $boardUri => &$board) { $board['active'] = (int) $boardActivity['active'][ $boardUri ]; - $board['pph'] = (int) $boardActivity['average'][ $boardUri ]; + $board['posts'] = (int) $boardActivity['posts'][ $boardUri ]; + $board['pph'] = (int) $boardActivity['average'][ $boardUri ]; if (isset($board['tags']) && count($board['tags']) > 0) { foreach ($board['tags'] as $tag) { - if (isset($response['tag'][$tag])) { - $response['tag'][$tag] += $board['active']; + if (isset($response['tags'][$tag])) { + $response['tags'][$tag] += $board['active']; } else { - $response['tag'][$tag] = $board['active']; + $response['tags'][$tag] = $board['active']; } } } } +// Sort boards by their popularity, then by their total posts. +$boardActivityValues = array(); + +foreach ($response['boards'] as $boardUri => $board) { + $boardActivityValues[$boardUri] = "{$board['active']}.{$board['posts']}"; +} + +array_multisort($boardActivityValues, SORT_DESC, $response['boards']); + // Get the top most popular tags. -if (count($response['tag']) > 0) { +if (count($response['tags']) > 0) { // Sort by most active tags. - arsort( $response['tag'] ); + arsort( $response['tags'] ); // Get the first n most active tags. - $response['tag'] = array_splice( $response['tag'], 0, 200 ); + $response['tags'] = array_splice( $response['tags'], 0, 200 ); - $tagLightest = end( array_keys( $response['tag'] ) ); + // $tagLightest = end( array_keys( $response['tag'] ) ); } /* (Please) Respond */ -$json = json_encode( $response ); +if (!$Included) { + $json = json_encode( $response ); -// Error Handling -switch (json_last_error()) { - case JSON_ERROR_NONE: - $jsonError = false; - break; - case JSON_ERROR_DEPTH: - $jsonError = 'Maximum stack depth exceeded'; - break; - case JSON_ERROR_STATE_MISMATCH: - $jsonError = 'Underflow or the modes mismatch'; - break; - case JSON_ERROR_CTRL_CHAR: - $jsonError = 'Unexpected control character found'; - break; - case JSON_ERROR_SYNTAX: - $jsonError = 'Syntax error, malformed JSON'; - break; - case JSON_ERROR_UTF8: - $jsonError = 'Malformed UTF-8 characters, possibly incorrectly encoded'; - break; - default: - $jsonError = 'Unknown error'; - break; + // Error Handling + switch (json_last_error()) { + case JSON_ERROR_NONE: + $jsonError = false; + break; + case JSON_ERROR_DEPTH: + $jsonError = 'Maximum stack depth exceeded'; + break; + case JSON_ERROR_STATE_MISMATCH: + $jsonError = 'Underflow or the modes mismatch'; + break; + case JSON_ERROR_CTRL_CHAR: + $jsonError = 'Unexpected control character found'; + break; + case JSON_ERROR_SYNTAX: + $jsonError = 'Syntax error, malformed JSON'; + break; + case JSON_ERROR_UTF8: + $jsonError = 'Malformed UTF-8 characters, possibly incorrectly encoded'; + break; + default: + $jsonError = 'Unknown error'; + break; + } + + if ($jsonError) { + $json = "{\"error\":\"{$jsonError}\"}"; + } + + // Successful output + echo $json; } - -if ($jsonError) { - $json = "{\"error\":\"{$jsonError}\"}"; -} - -// Successful output -echo $json; \ No newline at end of file +else { + return $response; +} \ No newline at end of file diff --git a/boards.php b/boards.php index a1cb260b..afd96841 100644 --- a/boards.php +++ b/boards.php @@ -1,6 +1,6 @@ $board) { - $query = prepare(sprintf(" +/* $query = prepare(sprintf(" SELECT IFNULL(MAX(id),0) max, (SELECT COUNT(*) FROM ``posts_%s`` WHERE FROM_UNIXTIME(time) > DATE_SUB(NOW(), INTERVAL 1 HOUR)) pph, (SELECT COUNT(DISTINCT ip) FROM ``posts_%s`` WHERE FROM_UNIXTIME(time) > DATE_SUB(NOW(), INTERVAL 3 DAY)) uniq_ip FROM ``posts_%s`` ", $board['uri'], $board['uri'], $board['uri'], $board['uri'], $board['uri'])); $query->execute() or error(db_error($query)); - $r = $query->fetch(PDO::FETCH_ASSOC); + $r = $query->fetch(PDO::FETCH_ASSOC); */ - $tquery = prepare("SELECT `tag` FROM ``board_tags`` WHERE `uri` = :uri"); - $tquery->execute([":uri" => $board['uri']]) or error(db_error($tquery)); - $r2 = $tquery->fetchAll(PDO::FETCH_ASSOC); +$boardQuery = prepare("SELECT COUNT(1) AS 'boards_total', COUNT(indexed) AS 'boards_public' FROM ``boards``"); +$boardQuery->execute() or error(db_error($tagQuery)); +$boardResult = $boardQuery->fetchAll(PDO::FETCH_ASSOC)[0]; - $tags = array(); - if ($r2) { - foreach ($r2 as $ii => $t) { - $tag=to_tag($t['tag']); - $tags[] = $tag; - if (!isset($all_tags[$tag])) { - $all_tags[$tag] = (int)$r['uniq_ip']; - } else { - $all_tags[$tag] += $r['uniq_ip']; - } - } - } +$boards_total = $boardResult['boards_total']; +$boards_public = $boardResult['boards_public']; +$boards_hidden = $boardResult['boards_total'] - $boardResult['boards_public']; - $pph = $r['pph']; +$posts_hour = 0; +$posts_total = 0; - $total_posts_hour += $pph; - $total_posts += $r['max']; +/* Create and distribute page */ +$boardsHTML = Element("8chan/boards-table.html", array( + "config" => $config, + "boards" => $boards, + ) +); - $boards[$i]['pph'] = $pph; - $boards[$i]['ppd'] = $pph*24; - $boards[$i]['max'] = $r['max']; - $boards[$i]['uniq_ip'] = $r['uniq_ip']; - $boards[$i]['tags'] = $tags; +$tagsHTML = Element("8chan/boards-tags.html", array( + "config" => $config, + "tags" => $tags, + ) +); - if ($write_maxes) fwrite($fp, $board['uri'] . ':' . $boards[$i]['max'] . "\n"); -} -if ($write_maxes) fclose($fp); +$searchHTML = Element("8chan/boards-search.html", array( + "config" => $config, + + "boards_total" => $boards_total, + "boards_public" => $boards_public, + "boards_hidden" => $boards_hidden, + + "posts_hour" => $posts_hour, + "posts_total" => $posts_total, + + "page_updated" => date('r'), + "uptime" => shell_exec('uptime -p'), + + "html_boards" => $boardsHTML, + "html_tags" => $tagsHTML + ) +); -usort($boards, -function ($a, $b) { - $x = $b['uniq_ip'] - $a['uniq_ip']; - if ($x) { return $x; - //} else { return strcmp($a['uri'], $b['uri']); } - } else { return $b['max'] - $a['max']; } -}); +$config['additional_javascript'] = array( + 'js/jquery.min.js', + 'js/board-directory.js' +); -$hidden_boards_total = 0; -$rows = array(); -foreach ($boards as $i => &$board) { - $board_config = @file_get_contents($board['uri'].'/config.php'); - $boardCONFIG = array(); - if ($board_config && $board['uri'] !== 'int') { - $board_config = str_replace('$config', '$boardCONFIG', $board_config); - $board_config = str_replace(' $config, + "body" => $searchHTML, + "title" => "Boards on ∞chan" + ) +); - $board['title'] = utf8tohtml($board['title']); - $locale_arr = explode('_', $locale); - $locale_short = isset($locale_arr[1]) ? strtolower($locale_arr[1]) : strtolower($locale_arr[0]); - $locale_short = str_replace('.utf-8', '', $locale_short); - $country = get_country($locale_short); - if ($board['uri'] === 'int') {$locale_short = 'eo'; $locale = 'eo'; $country = 'Esperanto';} - - $board['img'] = "\"$country\""; - - if ($showboard || $admin) { - if (!$showboard) { - $lock = ' '; - } else { - $lock = ''; - } - $board['ago'] = human_time_diff(strtotime($board['time'])); - } else { - unset($boards[$i]); - $hidden_boards_total += 1; - } -} - -$n_boards = sizeof($boards); -$t_boards = $hidden_boards_total + $n_boards; - -$boards = array_values($boards); -arsort($all_tags); - -$config['additional_javascript'] = array('js/jquery.min.js', 'js/jquery.tablesorter.min.js'); -$body = Element("8chan/boards-tags.html", array("config" => $config, "n_boards" => $n_boards, "t_boards" => $t_boards, "hidden_boards_total" => $hidden_boards_total, "total_posts" => $total_posts, "total_posts_hour" => $total_posts_hour, "boards" => $boards, "last_update" => date('r'), "uptime_p" => shell_exec('uptime -p'), 'tags' => $all_tags, 'top2k' => false)); - -$html = Element("page.html", array("config" => $config, "body" => $body, "title" => "Boards on ∞chan")); -$boards_top2k = $boards; -array_splice($boards_top2k, 100); -$boards_top2k = array_values($boards_top2k); -$body = Element("8chan/boards-tags.html", array("config" => $config, "n_boards" => $n_boards, "t_boards" => $t_boards, "hidden_boards_total" => $hidden_boards_total, "total_posts" => $total_posts, "total_posts_hour" => $total_posts_hour, "boards" => $boards_top2k, "last_update" => date('r'), "uptime_p" => shell_exec('uptime -p'), 'tags' => $all_tags, 'top2k' => true)); -$html_top2k = Element("page.html", array("config" => $config, "body" => $body, "title" => "Boards on ∞chan")); - -if ($admin) { - echo $html; -} else { - foreach ($boards as $i => &$b) { unset($b['img']); } - file_write("boards.json", json_encode($boards)); - file_write("tags.json", json_encode($all_tags)); - foreach ($boards as $i => $b) {/* - if (in_array($b['uri'], $config['no_top_bar_boards'])) { - unset($boards[$i]); - }*/ - unset($boards[$i]['img']); - } - - array_splice($boards, 48); - - $boards = array_values($boards); - - file_write("boards-top20.json", json_encode($boards)); - file_write("boards.html", $html_top2k); - file_write("boards_full.html", $html); - echo $html; -} \ No newline at end of file +file_write("boards.html", $pageHTML); +echo $pageHTML; \ No newline at end of file diff --git a/inc/functions.php b/inc/functions.php index 1e2b9be7..cb2dc364 100755 --- a/inc/functions.php +++ b/inc/functions.php @@ -820,11 +820,14 @@ function loadBoardConfig( $uri ) { } function fetchBoardActivity( $uris ) { + global $config; + $boardActivity = array(); - /* - $uris = "\"" . implode( (array) $uris, "\",\"" ) . "\""; + $tablePrefix = "{$config['db']['prefix']}posts_"; + $uris = "\"{$tablePrefix}" . implode( (array) $uris, "\",\"{$tablePrefix}" ) . "\""; + /* $tagQuery = prepare("SELECT * FROM ``board_tags`` WHERE `uri` IN ({$uris})"); $tagQuery->execute() or error(db_error($tagQuery)); $tagResult = $tagQuery->fetchAll(PDO::FETCH_ASSOC); @@ -845,20 +848,30 @@ function fetchBoardActivity( $uris ) { } */ - foreach( (array) $uris as $uri ) { + $aiQuery = prepare("SELECT `TABLE_NAME`, `AUTO_INCREMENT` FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = \"{$config['db']['database']}\" AND TABLE_NAME IN ({$uris})"); + $aiQuery->execute() or error(db_error($aiQuery)); + $aiResult = $aiQuery->fetchAll(PDO::FETCH_ASSOC); + + foreach ($aiResult as $aiRow) { + $uri = str_replace( $tablePrefix, "", $aiRow['TABLE_NAME'] ); + $posts = $aiRow['AUTO_INCREMENT'] - 1; + $random = rand( -1000, 1000 ); if( $random < 0 ) $random = 0; - $boardActivity['active'][ $uri ] = $random; - $boardActivity['average'][ $uri ] = $random * 72; + $boardActivity['active'][ $uri ] = $random; + $boardActivity['average'][ $uri ] = ($random * 72) / 72; + $boardActivity['posts'][ $uri ] = $posts; } return $boardActivity; } function fetchBoardTags( $uris ) { + global $config; + $boardTags = array(); - $uris = "\"" . implode( (array) $uris, "\",\"" ) . "\""; + $uris = "\"{$config['db']['prefix']}" . implode( (array) $uris, "\",\"" ) . "\""; $tagQuery = prepare("SELECT * FROM ``board_tags`` WHERE `uri` IN ({$uris})"); $tagQuery->execute() or error(db_error($tagQuery)); diff --git a/stylesheets/style.css b/stylesheets/style.css index e862b9f6..916067fc 100644 --- a/stylesheets/style.css +++ b/stylesheets/style.css @@ -1394,10 +1394,10 @@ aside.search-container .box { margin: 8px 0; } .search-sfw { + display: block; cursor: pointer; font-size: 110%; line-height: 120%; - vertical-align: bottom; } #search-sfw-input { margin: 0; @@ -1405,6 +1405,7 @@ aside.search-container .box { transform: scale(1.20); } #search-lang-input, +#search-title-input, #search-tag-input { box-sizing: border-box; font-size: 110%; diff --git a/templates/8chan/boards-search.html b/templates/8chan/boards-search.html new file mode 100644 index 00000000..1aff831c --- /dev/null +++ b/templates/8chan/boards-search.html @@ -0,0 +1,78 @@ +
+
+

Global Statistics

+

{% trans %}There are currently {{boards_public}} public boards, {{boards_total}} total. Site-wide, {{posts_hour}} posts have been made in the last hour, with {{posts_total}} being made on all active boards since {{founding_date}}.{% endtrans %}

+ {% if uptime %}

{{uptime}} without interruption

{% endif %} +

This page last updated {{page_updated}}.

+
+ +
+ + +
+ + + + + + + + + + + + + + + + + + + + + + + {{html_boards}} +
{% trans %}Board{% endtrans %}{% trans %}Title{% endtrans %}{% trans %}PPH{% endtrans %}{% trans %}Total posts{% endtrans %}{% trans %}Active users{% endtrans %}{% trans %}Tags{% endtrans %}
+
+
+
\ No newline at end of file diff --git a/templates/8chan/boards-table.html b/templates/8chan/boards-table.html new file mode 100644 index 00000000..6682ce6c --- /dev/null +++ b/templates/8chan/boards-table.html @@ -0,0 +1,11 @@ +{% for board in boards %} + + {{ board.img|raw }} {% if board['sfw'] %}{% else %}{% endif %} +
/{{board['uri']}}/{{lock|raw}}
+
{{ board['title'] }}
+
{{board['pph']}} +
{{board['posts']}} +
{{board['active']}} +
{% for tag in board.tags %}{{ tag }} {% endfor %}
+ +{% endfor %} \ No newline at end of file diff --git a/templates/8chan/boards-tags.html b/templates/8chan/boards-tags.html index 71515f1e..f569c339 100644 --- a/templates/8chan/boards-tags.html +++ b/templates/8chan/boards-tags.html @@ -1,90 +1,5 @@ -
-
-

Global Statistics

-

{% trans %}There are currently {{t_boards}} total boards, {{hidden_boards_total}} of which are unindexed. Site-wide, {{total_posts_hour}} posts have been made in the last hour, with {{total_posts}} being made on all active boards since October 23, 2013.{% endtrans %}

- {% if uptime %}

{{uptime_p}} without interruption

{% endif %} -

This page last updated .

-
- -
- - -
- - - - - - - - - - - - - - - - - - - - - - - - {% for board in boards %} - - - - - - - - - - {% endfor %} - -
{% trans %}Board{% endtrans %}{% trans %}Title{% endtrans %}{% trans %}PPH{% endtrans %}{% trans %}Total posts{% endtrans %}{% trans %}Active users{% endtrans %}{% trans %}Tags{% endtrans %}
{{ board.img|raw }} {% if board['sfw'] %}{% else %}{% endif %}
/{{board['uri']}}/{{lock|raw}}
{{ board['title'] }}
{{board['pph']}}
{{board['max']}}
{{board['uniq_ip']}}
{% for tag in board.tags %}{{ tag }} {% endfor %}
-
-
-
\ No newline at end of file +{% for tag, weight in tags %} +
  • + {{tag}} +
  • +{% endfor %} \ No newline at end of file