From 5f5dbbd1631fcfa49df8113d3782c0b32b09a45a Mon Sep 17 00:00:00 2001 From: 8n-tech <8n-tech@users.noreply.github.com> Date: Sun, 19 Apr 2015 22:11:56 +1000 Subject: [PATCH] #456 - boards.php no longer generates a cache file or forbids direct access if a $_GET is specified. #456 - boards.php now generates a boards.json and boards-top20.json when accessed directly. #456 - Added API compatibility to board-search.php ('max' and 'ppd') #456 - Fixed an issue with listBoards() that caused an SQL error if you only asked for indexed. fuck I hope this works Signed-off-by: 8n-tech <8n-tech@users.noreply.github.com> --- board-search.php | 21 ++++++++++++++++----- boards.php | 14 ++++++++++++-- inc/functions.php | 31 ++++++++++++++++++++++++++----- 3 files changed, 54 insertions(+), 12 deletions(-) diff --git a/board-search.php b/board-search.php index a02c89bf..b573edd1 100644 --- a/board-search.php +++ b/board-search.php @@ -125,7 +125,6 @@ foreach ($boards as $board) { unset( $boards ); - /* Tag Fetching */ // (We have do this even if we're not filtering by tags so that we know what each board's tags are) @@ -137,6 +136,7 @@ foreach ($response['boards'] as $boardUri => &$board) { // If we are filtering by tag and there is no match, remove from the response. if ( $search['tags'] !== false && ( !isset( $boardTags[ $boardUri ] ) || count(array_intersect($search['tags'], $boardTags[ $boardUri ])) !== count($search['tags']) ) ) { unset( $response['boards'][$boardUri] ); + continue; } // If we aren't filtering / there is a match AND we have tags, set the tags. else if ( isset( $boardTags[ $boardUri ] ) && $boardTags[ $boardUri ] ) { @@ -146,6 +146,9 @@ foreach ($response['boards'] as $boardUri => &$board) { else { $board['tags'] = array(); } + + // Legacy support for API readers. + $board['max'] = &$board['posts_total']; } unset( $boardTags ); @@ -159,6 +162,7 @@ $boardActivity = fetchBoardActivity( array_keys( $response['boards'] ), $search[ foreach ($response['boards'] as $boardUri => &$board) { $board['active'] = 0; $board['pph'] = 0; + $board['ppd'] = 0; if (isset($boardActivity['active'][ $boardUri ])) { $board['active'] = (int) $boardActivity['active'][ $boardUri ]; @@ -171,6 +175,7 @@ foreach ($response['boards'] as $boardUri => &$board) { } $board['pph'] = round( $boardActivity['average'][ $boardUri ], 2 ); + $board['ppd'] = round( $boardActivity['today'][ $boardUri ], 2 ); unset( $precision ); } @@ -194,11 +199,17 @@ array_multisort( $response['boards'] ); -$boardLimit = $search['index'] ? 50 : 100; +if (php_sapi_name() == 'cli') { + $boardLimit = $search['index'] ? 50 : 100; + + $response['omitted'] = count( $response['boards'] ) - $boardLimit; + $response['omitted'] = $response['omitted'] < 0 ? 0 : $response['omitted']; + $response['boards'] = array_splice( $response['boards'], $search['page'], $boardLimit ); +} +else { + $response['omitted'] = 0; +} -$response['omitted'] = count( $response['boards'] ) - $boardLimit; -$response['omitted'] = $response['omitted'] < 0 ? 0 : $response['omitted']; -$response['boards'] = array_splice( $response['boards'], $search['page'], $boardLimit ); $response['order'] = array_keys( $response['boards'] ); diff --git a/boards.php b/boards.php index 0acdfaf5..600951d4 100644 --- a/boards.php +++ b/boards.php @@ -5,7 +5,7 @@ include "inc/functions.php"; $admin = isset($mod["type"]) && $mod["type"]<=30; $founding_date = "October 23, 2013"; -if (php_sapi_name() == 'fpm-fcgi' && !$admin) { +if (php_sapi_name() == 'fpm-fcgi' && !$admin && count($_GET) == 0) { error('Cannot be run directly.'); } @@ -92,5 +92,15 @@ $pageHTML = Element("page.html", array( ) ); -file_write("boards.html", $pageHTML); +// We only want to cache if this is not a dynamic form request. +// Otherwise, our information will be skewed by the search criteria. +if (count($_GET) == 0) { + // Preserves the JSON output format of [{board},{board}]. + $nonAssociativeBoardList = array_values($boards); + + file_write("boards.html", $pageHTML); + file_write("boards.json", json_encode($nonAssociativeBoardList)); + file_write("boards-top20.json", json_encode(array_splice($nonAssociativeBoardList, 0, 48))); +} + echo $pageHTML; \ No newline at end of file diff --git a/inc/functions.php b/inc/functions.php index 8a279b41..1d5f8216 100755 --- a/inc/functions.php +++ b/inc/functions.php @@ -795,10 +795,11 @@ function listBoards($just_uri = false, $indexed_only = false) { ``boards``.`indexed` indexed, ``boards``.`sfw` sfw, ``boards``.`posts_total` posts_total - FROM ``boards``" . ( $indexed_only ? " WHERE `indexed` = 1 " : "" ) . - "LEFT JOIN ``board_create`` - ON ``boards``.`uri` = ``board_create``.`uri` - ORDER BY ``boards``.`uri`") or error(db_error()); + FROM ``boards`` + LEFT JOIN ``board_create`` + ON ``boards``.`uri` = ``board_create``.`uri`" . + ( $indexed_only ? " WHERE `indexed` = 1 " : "" ) . + "ORDER BY ``boards``.`uri`") or error(db_error()); $boards = $query->fetchAll(PDO::FETCH_ASSOC); } @@ -840,12 +841,17 @@ function fetchBoardActivity( array $uris = array(), $forTime = false, $detailed if (!is_integer($forTime)) { $forTime = time(); } + // Get the last hour for this timestamp. $nowHour = ( (int)( time() / 3600 ) * 3600 ); + // Get the hour before. This is what we actually use for pulling data. $forHour = ( (int)( $forTime / 3600 ) * 3600 ) - 3600; + // Get the hour from yesterday to calculate posts per day. + $yesterHour = $forHour - ( 3600 * 23 ); $boardActivity = array( 'active' => array(), + 'today' => array(), 'average' => array(), ); @@ -867,10 +873,21 @@ function fetchBoardActivity( array $uris = array(), $forTime = false, $detailed // Format the results. foreach ($bsResult as $bsRow) { + // Do we need to define the arrays for this URI? if (!isset($boardActivity['active'][$bsRow['stat_uri']])) { if ($bsRow['stat_hour'] == $forHour) { $boardActivity['active'][$bsRow['stat_uri']] = unserialize( $bsRow['author_ip_array'] ); } + else { + $boardActivity['active'][$bsRow['stat_uri']] = array(); + } + + if ($bsRow['stat_hour'] <= $forHour && $bsRow['stat_hour'] >= $yesterHour) { + $boardActivity['today'][$bsRow['stat_uri']] = $bsRow['post_count']; + } + else { + $boardActivity['today'][$bsRow['stat_uri']] = 0; + } $boardActivity['average'][$bsRow['stat_uri']] = $bsRow['post_count']; } @@ -879,7 +896,11 @@ function fetchBoardActivity( array $uris = array(), $forTime = false, $detailed $boardActivity['active'][$bsRow['stat_uri']] = array_merge( $boardActivity['active'][$bsRow['stat_uri']], unserialize( $bsRow['author_ip_array'] ) ); } - $boardActivity['average'][$bsRow['stat_uri']] = $bsRow['post_count']; + if ($bsRow['stat_hour'] <= $forHour && $bsRow['stat_hour'] >= $yesterHour) { + $boardActivity['today'][$bsRow['stat_uri']] += $bsRow['post_count']; + } + + $boardActivity['average'][$bsRow['stat_uri']] += $bsRow['post_count']; } }