summaryrefslogtreecommitdiff
path: root/inc
diff options
context:
space:
mode:
authorMichael Foster <savetheinternet@tinyboard.org>2013-08-29 12:52:31 +1000
committerMichael Foster <savetheinternet@tinyboard.org>2013-08-29 12:52:31 +1000
commit577a8b991d10b898e0025f5f45bb38cafff5080e (patch)
tree9be62773ae7718a19142bbf4d27b97c8635b3700 /inc
parentd7fc5adc22c71261dd3f4544a3a5a1816b8d724e (diff)
Performance: Use only one query for validating >>X links in posts
Diffstat (limited to 'inc')
-rw-r--r--inc/functions.php32
1 files changed, 23 insertions, 9 deletions
diff --git a/inc/functions.php b/inc/functions.php
index 1bd185a9..321c65d0 100644
--- a/inc/functions.php
+++ b/inc/functions.php
@@ -1576,29 +1576,41 @@ function markup(&$body, $track_cites = false) {
$skip_chars = 0;
$body_tmp = $body;
-
+
+ $search_cites = array();
+ foreach ($cites as $matches) {
+ $search_cites[] = '`id` = ' . $matches[2][0];
+ }
+ $search_cites = array_unique($search_cites);
+
+ $query = query(sprintf('SELECT `thread`, `id` FROM ``posts_%s`` WHERE ' .
+ implode(' OR ', $search_cites), $board['uri'])) or error(db_error());
+
+ $cited_posts = array();
+ while ($cited = $query->fetch(PDO::FETCH_ASSOC)) {
+ $cited_posts[$cited['id']] = $cited['thread'] ? $cited['thread'] : false;
+ }
+
foreach ($cites as $matches) {
$cite = $matches[2][0];
- $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));
// preg_match_all is not multibyte-safe
foreach ($matches as &$match) {
$match[1] = mb_strlen(substr($body_tmp, 0, $match[1]));
}
- if ($post = $query->fetch(PDO::FETCH_ASSOC)) {
+ if (isset($cited_posts[$cite])) {
$replacement = '<a onclick="highlightReply(\''.$cite.'\');" href="' .
- $config['root'] . $board['dir'] . $config['dir']['res'] . ($post['thread']?$post['thread']:$post['id']) . '.html#' . $cite . '">' .
- '&gt;&gt;' . $cite .
- '</a>';
+ $config['root'] . $board['dir'] . $config['dir']['res'] .
+ ($cited_posts[$cite] ? $cited_posts[$cite] : $cite) . '.html#' . $cite . '">' .
+ '&gt;&gt;' . $cite .
+ '</a>';
$body = mb_substr_replace($body, $matches[1][0] . $replacement . $matches[3][0], $matches[0][1] + $skip_chars, mb_strlen($matches[0][0]));
$skip_chars += mb_strlen($matches[1][0] . $replacement . $matches[3][0]) - mb_strlen($matches[0][0]);
if ($track_cites && $config['track_cites'])
- $tracked_cites[] = array($board['uri'], $post['id']);
+ $tracked_cites[] = array($board['uri'], $cite);
}
}
}
@@ -1612,6 +1624,8 @@ function markup(&$body, $track_cites = false) {
$skip_chars = 0;
$body_tmp = $body;
+ // TODO: Make this use fewer queries, similar to local board cites.
+
foreach ($cites as $matches) {
$_board = $matches[2][0];
$cite = @$matches[3][0];