summaryrefslogtreecommitdiff
path: root/inc/functions.php
diff options
context:
space:
mode:
authorMichael Foster <savetheinternet@tinyboard.org>2013-08-29 13:31:02 +1000
committerMichael Foster <savetheinternet@tinyboard.org>2013-08-29 13:31:02 +1000
commit3545e2406edc441123d0796ffff12fc871bf7ec1 (patch)
tree5285be3c19c228c5821d877577204aea253f2dad /inc/functions.php
parent577a8b991d10b898e0025f5f45bb38cafff5080e (diff)
Perforamnce: Much more efficient >>X and >>>/board/X
Diffstat (limited to 'inc/functions.php')
-rw-r--r--inc/functions.php109
1 files changed, 81 insertions, 28 deletions
diff --git a/inc/functions.php b/inc/functions.php
index 321c65d0..f260a0ce 100644
--- a/inc/functions.php
+++ b/inc/functions.php
@@ -1623,8 +1623,66 @@ function markup(&$body, $track_cites = false) {
$skip_chars = 0;
$body_tmp = $body;
-
- // TODO: Make this use fewer queries, similar to local board cites.
+
+ if (isset($cited_posts)) {
+ // Carry found posts from local board >>X links
+ foreach ($cited_posts as $cite => $thread) {
+ $cited_posts[$cite] = $config['root'] . $board['dir'] . $config['dir']['res'] .
+ ($thread ? $thread : $cite) . '.html#' . $cite;
+ }
+
+ $cited_posts = array(
+ $board['uri'] => $cited_posts
+ );
+ } else
+ $cited_posts = array();
+
+ $crossboard_indexes = array();
+ $search_cites_boards = array();
+
+ foreach ($cites as $matches) {
+ $_board = $matches[2][0];
+ $cite = @$matches[3][0];
+
+ if (!isset($search_cites_boards[$_board]))
+ $search_cites_boards[$_board] = array();
+ $search_cites_boards[$_board][] = $cite;
+ }
+
+ $tmp_board = $board['uri'];
+
+ foreach ($search_cites_boards as $_board => $search_cites) {
+ $clauses = array();
+ foreach ($search_cites as $cite) {
+ if (!$cite || isset($cited_posts[$_board][$cite]))
+ continue;
+ $clauses[] = '`id` = ' . $cite;
+ }
+ $clauses = array_unique($clauses);
+
+ if ($board['uri'] != $_board) {
+ if (!openBoard($_board))
+ continue; // Unknown board
+ }
+
+ if (!empty($clauses)) {
+ $cited_posts[$_board] = array();
+
+ $query = query(sprintf('SELECT `thread`, `id` FROM ``posts_%s`` WHERE ' .
+ implode(' OR ', $clauses), $board['uri'])) or error(db_error());
+
+ while ($cite = $query->fetch(PDO::FETCH_ASSOC)) {
+ $cited_posts[$_board][$cite['id']] = $config['root'] . $board['dir'] . $config['dir']['res'] .
+ ($cite['thread'] ? $cite['thread'] : $cite['id']) . '.html#' . $cite['id'];
+ }
+ }
+
+ $crossboard_indexes[$_board] = $config['root'] . $board['dir'] . $config['file_index'];
+ }
+
+ // Restore old board
+ if ($board['uri'] != $tmp_board)
+ openBoard($tmp_board);
foreach ($cites as $matches) {
$_board = $matches[2][0];
@@ -1638,39 +1696,34 @@ function markup(&$body, $track_cites = false) {
// Temporarily store board information because it will be overwritten
$tmp_board = $board['uri'];
- // Check if the board exists, and load settings
- if (openBoard($_board)) {
- if ($cite) {
- $query = prepare(sprintf("SELECT `thread`,`id` FROM ``posts_%s`` WHERE `id` = :id LIMIT 1", $board['uri']));
- $query->bindValue(':id', $cite);
- $query->execute() or error(db_error($query));
+ if ($cite) {
+ if (isset($cited_posts[$_board][$cite])) {
+ $link = $cited_posts[$_board][$cite];
+
+ $replacement = '<a ' .
+ ($_board == $board['uri'] ?
+ 'onclick="highlightReply(\''.$cite.'\');" '
+ : '') . 'href="' . $link . '">' .
+ '&gt;&gt;&gt;/' . $_board . '/' . $cite .
+ '</a>';
- if ($post = $query->fetch(PDO::FETCH_ASSOC)) {
- $replacement = '<a onclick="highlightReply(\''.$cite.'\');" href="' .
- $config['root'] . $board['dir'] . $config['dir']['res'] . ($post['thread']?$post['thread']:$post['id']) . '.html#' . $cite . '">' .
- '&gt;&gt;&gt;/' . $_board . '/' . $cite .
- '</a>';
-
- $body = mb_substr_replace($body, $matches[1][0] . $replacement . $matches[4][0], $matches[0][1] + $skip_chars, mb_strlen($matches[0][0]));
- $skip_chars += mb_strlen($matches[1][0] . $replacement . $matches[4][0]) - mb_strlen($matches[0][0]);
-
- if ($track_cites && $config['track_cites'])
- $tracked_cites[] = array($board['uri'], $post['id']);
- }
- } else {
- $replacement = '<a href="' .
- $config['root'] . $board['dir'] . $config['file_index'] . '">' .
- '&gt;&gt;&gt;/' . $_board . '/' .
- '</a>';
$body = mb_substr_replace($body, $matches[1][0] . $replacement . $matches[4][0], $matches[0][1] + $skip_chars, mb_strlen($matches[0][0]));
$skip_chars += mb_strlen($matches[1][0] . $replacement . $matches[4][0]) - mb_strlen($matches[0][0]);
+
+ if ($track_cites && $config['track_cites'])
+ $tracked_cites[] = array($_board, $cite);
}
+ } elseif(isset($crossboard_indexes[$_board])) {
+ $replacement = '<a href="' . $crossboard_indexes[$_board] . '">' .
+ '&gt;&gt;&gt;/' . $_board . '/' .
+ '</a>';
+ $body = mb_substr_replace($body, $matches[1][0] . $replacement . $matches[4][0], $matches[0][1] + $skip_chars, mb_strlen($matches[0][0]));
+ $skip_chars += mb_strlen($matches[1][0] . $replacement . $matches[4][0]) - mb_strlen($matches[0][0]);
}
-
- // Restore main board settings
- openBoard($tmp_board);
}
}
+
+ $tracked_cites = array_unique($tracked_cites, SORT_REGULAR);
$body = preg_replace("/^\s*&gt;.*$/m", '<span class="quote">$0</span>', $body);