forked from leftypol/leftypol
pages.php: use UserPostQueries for mod_page_ip
This commit is contained in:
parent
71416afc75
commit
3c2bc57245
1 changed files with 30 additions and 91 deletions
|
@ -3,13 +3,9 @@
|
|||
* Copyright (c) 2010-2013 Tinyboard Development Group
|
||||
*/
|
||||
use Vichan\Context;
|
||||
use Vichan\Data\ReportQueries;
|
||||
use Vichan\Functions\Format;
|
||||
use Vichan\Data\{UserPostQueries, ReportQueries};
|
||||
use Vichan\Functions\Net;
|
||||
|
||||
use function Vichan\Functions\Net\decode_cursor;
|
||||
use function Vichan\Functions\Net\encode_cursor;
|
||||
|
||||
defined('TINYBOARD') or exit;
|
||||
|
||||
function _link_or_copy(string $target, string $link): bool {
|
||||
|
@ -870,7 +866,7 @@ function mod_ip_remove_note(Context $ctx, $ip, $id) {
|
|||
header('Location: ?/IP/' . $ip . '#notes', true, $config['redirect_http']);
|
||||
}
|
||||
|
||||
function mod_ip(Context $ctx, $ip, string $encoded_cursor = '') {
|
||||
function mod_ip(Context $ctx, $ip, string $encoded_cursor = null) {
|
||||
global $config, $mod;
|
||||
|
||||
if (filter_var($ip, FILTER_VALIDATE_IP) === false)
|
||||
|
@ -924,81 +920,29 @@ function mod_ip(Context $ctx, $ip, string $encoded_cursor = '') {
|
|||
$args['hostname'] = rDNS($ip);
|
||||
}
|
||||
|
||||
// Decode the cursor.
|
||||
list($cursor_type, $board_id_cursor_map) = decode_cursor($encoded_cursor);
|
||||
$post_per_page = $config['mod']['ip_recentposts'];
|
||||
$next_cursor_map = [];
|
||||
$prev_cursor_map = [];
|
||||
|
||||
$boards = listBoards();
|
||||
|
||||
$queryable_uris = [];
|
||||
foreach ($boards as $board) {
|
||||
$uri = $board['uri'];
|
||||
openBoard($uri);
|
||||
if (hasPermission($config['mod']['show_ip'], $uri)) {
|
||||
// Extract the cursor relative to the board.
|
||||
$id_cursor = false;
|
||||
if (isset($board_id_cursor_map[$uri])) {
|
||||
$value = $board_id_cursor_map[$uri];
|
||||
if (is_numeric($value)) {
|
||||
$id_cursor = (int)$value;
|
||||
$queryable_uris[] = $uri;
|
||||
}
|
||||
}
|
||||
|
||||
if ($id_cursor === false) {
|
||||
$query = prepare(sprintf('SELECT * FROM `posts_%s` WHERE `ip` = :ip ORDER BY `sticky` DESC, `id` DESC LIMIT :limit', $uri));
|
||||
$query->bindValue(':ip', $ip);
|
||||
$query->bindValue(':limit', $post_per_page + 1, PDO::PARAM_INT); // Always fetch more.
|
||||
$query->execute();
|
||||
$posts = $query->fetchAll(PDO::FETCH_ASSOC);
|
||||
} elseif ($cursor_type === 'n') {
|
||||
$query = prepare(sprintf('SELECT * FROM `posts_%s` WHERE `ip` = :ip AND `id` <= :start_id ORDER BY `sticky` DESC, `id` DESC LIMIT :limit', $uri));
|
||||
$query->bindValue(':ip', $ip);
|
||||
$query->bindValue(':start_id', $id_cursor, PDO::PARAM_INT);
|
||||
$query->bindValue(':limit', $post_per_page + 2, PDO::PARAM_INT); // Always fetch more.
|
||||
$query->execute();
|
||||
$posts = $query->fetchAll(PDO::FETCH_ASSOC);
|
||||
} elseif ($cursor_type === 'p') {
|
||||
// FIXME
|
||||
$query = prepare(sprintf('SELECT * FROM `posts_%s` WHERE `ip` = :ip AND `id` >= :start_id ORDER BY `sticky` ASC, `id` ASC LIMIT :limit', $uri));
|
||||
$query->bindValue(':ip', $ip);
|
||||
$query->bindValue(':start_id', $id_cursor, PDO::PARAM_INT);
|
||||
$query->bindValue(':limit', $post_per_page + 2, PDO::PARAM_INT); // Always fetch more.
|
||||
$query->execute();
|
||||
$posts = array_reverse($query->fetchAll(PDO::FETCH_ASSOC));
|
||||
} else {
|
||||
throw new RuntimeException("Unknown cursor type '$cursor_type'");
|
||||
}
|
||||
$queries = $ctx->get(UserPostQueries::class);
|
||||
$result = $queries->fetchPaginatedByIp($queryable_uris, $ip, $config['mod']['ip_recentposts'], $encoded_cursor);
|
||||
|
||||
$posts_count = count($posts);
|
||||
$args['cursor_prev'] = $result->cursor_prev;
|
||||
$args['cursor_next'] = $result->cursor_next;
|
||||
|
||||
if ($posts_count === $post_per_page + 2) {
|
||||
$has_extra_prev_post = true;
|
||||
$has_extra_end_post = true;
|
||||
} elseif ($posts_count === $post_per_page + 1) {
|
||||
$has_extra_prev_post = $id_cursor !== false && $posts[0]['id'] == $id_cursor;
|
||||
$has_extra_end_post = !$has_extra_prev_post;
|
||||
} else {
|
||||
$has_extra_prev_post = false;
|
||||
$has_extra_end_post = false;
|
||||
}
|
||||
|
||||
// Get the previous cursor, if any.
|
||||
if ($has_extra_prev_post) {
|
||||
// Select the most recent post.
|
||||
$prev_cursor_map[$uri] = $posts[1]['id'];
|
||||
array_shift($posts);
|
||||
$posts_count--;
|
||||
}
|
||||
// Get the next cursor, if any.
|
||||
if ($has_extra_end_post) {
|
||||
// Since we fetched 1 above the limit, we always know if there are any posts after the current page.
|
||||
// Query orders by DESC, so the SECOND last post has the lowest ID.
|
||||
array_pop($posts);
|
||||
$next_cursor_map[$uri] = $posts[$posts_count - 2]['id'];
|
||||
}
|
||||
foreach($boards as $board) {
|
||||
$uri = $board['uri'];
|
||||
// The Thread and Post classes rely on some implicit board parameter set by openBoard.
|
||||
openBoard($uri);
|
||||
|
||||
// Finally load the post contents and build them.
|
||||
foreach ($posts as $post) {
|
||||
foreach ($result->by_uri[$uri] as $post) {
|
||||
if (!$post['thread']) {
|
||||
$po = new Thread($post, '?/', $mod, false);
|
||||
} else {
|
||||
|
@ -1011,11 +955,6 @@ function mod_ip(Context $ctx, $ip, string $encoded_cursor = '') {
|
|||
$args['posts'][$uri]['posts'][] = $po->build(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Build the cursors.
|
||||
$args['cursor_prev'] = !empty($encoded_cursor) ? encode_cursor('p', $prev_cursor_map) : false;
|
||||
$args['cursor_next'] = !empty($next_cursor_map) ? encode_cursor('n', $next_cursor_map) : false;
|
||||
|
||||
$args['boards'] = $boards;
|
||||
$args['token'] = make_secure_link_token('ban');
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue