Merge branch '3-move-all-db_query-call-into-separate-functions' into 'master'

Move some queries from Display.php to thier own functions

Not replacing everything, just trying out if this is useful at all.


See merge request !2
This commit is contained in:
Aleksei Miheev
2016-05-03 12:31:58 +07:00
parent ad62c7ca5e
commit 71aee0481e
2 changed files with 122 additions and 82 deletions

View File

@@ -82,73 +82,34 @@ function Display()
// Find the previous or next topic. Make a fuss if there are no more.
if (isset($_REQUEST['prev_next']) && ($_REQUEST['prev_next'] == 'prev' || $_REQUEST['prev_next'] == 'next'))
{
// Just prepare some variables that are used in the query.
$gt_lt = $_REQUEST['prev_next'] == 'prev' ? '>' : '<';
$order = $_REQUEST['prev_next'] == 'prev' ? '' : ' DESC';
$request = db_query("
SELECT t2.ID_TOPIC
FROM {$db_prefix}topics AS t, {$db_prefix}topics AS t2, {$db_prefix}messages AS m, {$db_prefix}messages AS m2
WHERE m.ID_MSG = t.ID_LAST_MSG
AND t.ID_TOPIC = $topic" . (empty($modSettings['enableStickyTopics']) ? "
AND m2.posterTime $gt_lt m.posterTime" : "
AND ((m2.posterTime $gt_lt m.posterTime AND t2.isSticky $gt_lt= t.isSticky) OR t2.isSticky $gt_lt t.isSticky)") . "
AND t2.ID_LAST_MSG = m2.ID_MSG
AND t2.ID_BOARD = $board
ORDER BY" . (empty($modSettings['enableStickyTopics']) ? '' : " t2.isSticky$order,") . " m2.posterTime$order
LIMIT 1", __FILE__, __LINE__);
$topic = getSiblingTopic($topic, $board, $_REQUEST['prev_next']);
// No more left.
if (mysql_num_rows($request) == 0)
if ($topic === NULL)
fatal_lang_error('previous_next_end', false);
// Now you can be sure $topic is the ID_TOPIC to view.
list ($topic) = mysql_fetch_row($request);
mysql_free_result($request);
$context['current_topic'] = $topic;
// Go to the newest message on this topic.
$_REQUEST['start'] = 'new';
}
$request=db_query("
SELECT t.ID_TOPIC AS id, msg.subject AS subj
FROM {$db_prefix}related_topics rt
INNER JOIN {$db_prefix}topics t ON (t.ID_TOPIC=rt.ID_slave)
INNER JOIN {$db_prefix}messages msg ON (msg.ID_MSG=t.ID_FIRST_MSG)
WHERE rt.ID_master={$context['current_topic']}", __FILE__, __LINE__);
if(mysql_num_rows($request)==0) $context['related_topics']=array();
else
while($context['related_topics'][]=mysql_fetch_assoc($request));
$context['related_topics'] = getRelatedTopics($context['current_topic']);
// Add 1 to the number of views of this topic.
if (empty($_SESSION['last_read_topic']) || $_SESSION['last_read_topic'] != $topic)
{
db_query("
UPDATE {$db_prefix}topics
SET numViews = numViews + 1
WHERE ID_TOPIC = $topic
LIMIT 1", __FILE__, __LINE__);
updateTopicViews($topic);
# remember where we were
$_SESSION['last_read_topic'] = $topic;
}
// Get all the important topic info.
$request = db_query("
SELECT
t.numReplies, t.numViews, t.locked, ms.subject, t.isSticky, t.isFirstSticky, t.ID_POLL,
t.ID_MEMBER_STARTED, IFNULL(lt.logTime, 0) AS logTime, t.ID_FIRST_MSG, t.subtitle AS subtitle
FROM ({$db_prefix}topics AS t, {$db_prefix}messages AS ms)
LEFT JOIN {$db_prefix}log_topics AS lt ON (lt.ID_TOPIC = $topic AND lt.ID_MEMBER = $ID_MEMBER)
WHERE t.ID_TOPIC = $topic
AND ms.ID_MSG = t.ID_FIRST_MSG
LIMIT 1", __FILE__, __LINE__);
if (mysql_num_rows($request) == 0)
$topicinfo = getTopicInfo($topic, $ID_MEMBER);
if ($topicinfo === NULL)
fatal_lang_error(472, false);
$topicinfo = mysql_fetch_assoc($request);
mysql_free_result($request);
// The start isn't a number; it's information about what to do, where to go.
if (!is_numeric($_REQUEST['start']))
@@ -161,16 +122,7 @@ function Display()
$logTime = time();
else
{
// Find the earliest unread message in the topic. (the use of topics here is just for both tables.)
$request = db_query("
SELECT IFNULL(lt.logTime, IFNULL(lmr.logTime, 0)) AS logTime
FROM {$db_prefix}topics AS t
LEFT JOIN {$db_prefix}log_topics AS lt ON (lt.ID_TOPIC = $topic AND lt.ID_MEMBER = $ID_MEMBER)
LEFT JOIN {$db_prefix}log_mark_read AS lmr ON (lmr.ID_BOARD = $board AND lmr.ID_MEMBER = $ID_MEMBER)
WHERE t.ID_TOPIC = $topic
LIMIT 1", __FILE__, __LINE__);
list ($logTime) = mysql_fetch_row($request);
mysql_free_result($request);
$logTime = getFirstUnreadPostTimestamp($topic, $board, $ID_MEMBER);
}
// Fall through to the next if statement.

View File

@@ -211,6 +211,94 @@ if (!defined('SMF'))
void getAttachmentFilename(string filename, int ID_ATTACH, bool new = true)
// !!!
*/
// Find the earliest unread message in the topic. (the use of topics here is just for both tables.)
function getFirstUnreadPostTimestamp($topic, $board, $user) {
$request = db_query("
SELECT IFNULL(lt.logTime, IFNULL(lmr.logTime, 0)) AS logTime
FROM {$db_prefix}topics AS t
LEFT JOIN ${db_prefix}log_topics AS lt ON (lt.ID_TOPIC = '${topic}' AND lt.ID_MEMBER = '${user}')
LEFT JOIN ${db_prefix}log_mark_read AS lmr ON (lmr.ID_BOARD = '${board}' AND lmr.ID_MEMBER = '${user}')
WHERE t.ID_TOPIC = '${topic}'
LIMIT 1", __FILE__, __LINE__);
list ($logTime) = mysql_fetch_row($request);
mysql_free_result($request);
return $logTime;
}
# bump number of topic views, if necessary
function updateTopicViews($topic) {
global $db_prefix;
db_query("
UPDATE {$db_prefix}topics
SET numViews = numViews + 1
WHERE ID_TOPIC = $topic
LIMIT 1", __FILE__, __LINE__);
}
function getSiblingTopic($topic, $board, $direction) {
global $db_prefix;
global $modSettings;
// Just prepare some variables that are used in the query.
$gt_lt = $direction == 'prev' ? '>' : '<';
$order = $direction == 'prev' ? 'ASC' : 'DESC';
$request = db_query("
SELECT t2.ID_TOPIC
FROM ${db_prefix}topics AS t, ${db_prefix}topics AS t2, ${db_prefix}messages AS m, ${db_prefix}messages AS m2
WHERE m.ID_MSG = t.ID_LAST_MSG
AND t.ID_TOPIC = ${topic}" . (empty($modSettings['enableStickyTopics']) ? "
AND m2.posterTime ${gt_lt} m.posterTime" : "
AND ((m2.posterTime ${gt_lt} m.posterTime AND t2.isSticky ${gt_lt}= t.isSticky) OR t2.isSticky ${gt_lt} t.isSticky)") . "
AND t2.ID_LAST_MSG = m2.ID_MSG
AND t2.ID_BOARD = ${board}
ORDER BY" . (empty($modSettings['enableStickyTopics']) ? '' : " t2.isSticky ${order},") . " m2.posterTime ${order}
LIMIT 1", __FILE__, __LINE__);
// No more left.
if (mysql_num_rows($request) == 0)
return NULL;
// Now you can be sure $topic is the ID_TOPIC to view.
list ($sibling_topic) = mysql_fetch_row($request);
mysql_free_result($request);
return $sibling_topic;
}
function getRelatedTopics($topic) {
global $db_prefix;
$query=db_query("
SELECT t.ID_TOPIC AS id, msg.subject AS subj
FROM ${db_prefix}related_topics rt
INNER JOIN ${db_prefix}topics t ON (t.ID_TOPIC=rt.ID_slave)
INNER JOIN ${db_prefix}messages msg ON (msg.ID_MSG=t.ID_FIRST_MSG)
WHERE rt.ID_master=${topic}", __FILE__, __LINE__);
// Nothing?
if(mysql_num_rows($query)==0) return array();
// fetch 'em all!
while ($related_topics[] = mysql_fetch_assoc($query));
return $related_topics;
}
function getTopicInfo($topic, $user) {
global $db_prefix;
$query = db_query("
SELECT
t.numReplies, t.numViews, t.locked, ms.subject, t.isSticky, t.isFirstSticky, t.ID_POLL,
t.ID_MEMBER_STARTED, IFNULL(lt.logTime, 0) AS logTime, t.ID_FIRST_MSG, t.subtitle AS subtitle
FROM (${db_prefix}topics AS t, ${db_prefix}messages AS ms)
LEFT JOIN ${db_prefix}log_topics AS lt ON (lt.ID_TOPIC = ${topic} AND lt.ID_MEMBER = ${user})
WHERE t.ID_TOPIC = ${topic}
AND ms.ID_MSG = t.ID_FIRST_MSG
LIMIT 1", __FILE__, __LINE__);
if (mysql_num_rows($query) == 0) return NULL;
return mysql_fetch_assoc($query);
}
// Do a query. Takes care of errors too.
function db_query($db_string, $file, $line)
@@ -329,7 +417,7 @@ function updateLastMessages($setboards)
// Find the latest message on this board. (highest ID_MSG)
$result = db_query("
SELECT ID_BOARD, MAX(ID_MSG) AS ID_MSG
FROM {$db_prefix}messages
FROM ${db_prefix}messages
WHERE ID_BOARD" . (count($setboards) == 1 ? " = $setboards[0]" : ' IN (' . implode(', ', $setboards) . ')') . "
GROUP BY ID_BOARD", __FILE__, __LINE__);
$setboards = array_flip($setboards);
@@ -337,12 +425,12 @@ function updateLastMessages($setboards)
{
// Okay, this board is done ;).
unset($setboards[$row['ID_BOARD']]);
$rPosterTime = db_query("SELECT posterTime FROM {$db_prefix}messages WHERE ID_MSG={$row['ID_MSG']}", __FILE__, __LINE__);
$rPosterTime = db_query("SELECT posterTime FROM ${db_prefix}messages WHERE ID_MSG={$row['ID_MSG']}", __FILE__, __LINE__);
list($posterTime) = mysql_fetch_row($rPosterTime);
// Update the board!
db_query("
UPDATE {$db_prefix}boards
UPDATE ${db_prefix}boards
SET ID_LAST_MSG = $row[ID_MSG], lastUpdated = $posterTime
WHERE ID_BOARD = $row[ID_BOARD]
LIMIT 1", __FILE__, __LINE__);
@@ -362,7 +450,7 @@ function updateLastMessages($setboards)
if (!empty($the_parent_boards))
{
db_query("
UPDATE {$db_prefix}boards
UPDATE ${db_prefix}boards
SET lastUpdated = $posterTime
WHERE ID_BOARD IN (" . implode(',', $the_parent_boards) . ")
AND lastUpdated < $posterTime
@@ -378,7 +466,7 @@ function updateLastMessages($setboards)
if (!empty($setboards))
db_query("
UPDATE {$db_prefix}boards
UPDATE ${db_prefix}boards
SET ID_LAST_MSG = 0
WHERE ID_BOARD IN (" . implode(', ', array_keys($setboards)) . ")
LIMIT 1", __FILE__, __LINE__);
@@ -395,14 +483,14 @@ function updateStats($type, $condition = '1')
// Update the latest member (highest ID_MEMBER) and count.
$result = db_query("
SELECT COUNT(ID_MEMBER), MAX(ID_MEMBER)
FROM {$db_prefix}members", __FILE__, __LINE__);
FROM ${db_prefix}members", __FILE__, __LINE__);
list ($memberCount, $latestmember) = mysql_fetch_row($result);
mysql_free_result($result);
// Get the latest member's display name.
$result = db_query("
SELECT IFNULL(realName, memberName) AS realName
FROM {$db_prefix}members
FROM ${db_prefix}members
WHERE ID_MEMBER = " . (int) $latestmember . "
LIMIT 1", __FILE__, __LINE__);
list ($latestRealName) = mysql_fetch_row($result);
@@ -413,7 +501,7 @@ function updateStats($type, $condition = '1')
{
$result = db_query("
SELECT COUNT(ID_MEMBER)
FROM {$db_prefix}members
FROM ${db_prefix}members
WHERE is_activated = 0
AND validation_code = ''", __FILE__, __LINE__);
list ($unapprovedCount) = mysql_fetch_row($result);
@@ -433,7 +521,7 @@ function updateStats($type, $condition = '1')
// Get the number of messages...
$result = db_query("
SELECT COUNT(ID_MSG) AS totalMessages, MAX(ID_MSG) AS maxMsgID
FROM {$db_prefix}messages", __FILE__, __LINE__);
FROM ${db_prefix}messages", __FILE__, __LINE__);
$row = mysql_fetch_assoc($result);
mysql_free_result($result);
@@ -446,7 +534,7 @@ function updateStats($type, $condition = '1')
// Get the number of topics.
$result = db_query("
SELECT COUNT(ID_TOPIC) AS totalTopics
FROM {$db_prefix}topics", __FILE__, __LINE__);
FROM ${db_prefix}topics", __FILE__, __LINE__);
$row = mysql_fetch_assoc($result);
mysql_free_result($result);
@@ -475,7 +563,7 @@ function updateStats($type, $condition = '1')
// Fetch postgroups.
$request = db_query("
SELECT ID_GROUP, minPosts
FROM {$db_prefix}membergroups
FROM ${db_prefix}membergroups
WHERE minPosts != -1", __FILE__, __LINE__);
$postgroups = array();
while ($row = mysql_fetch_assoc($request))
@@ -500,7 +588,7 @@ function updateStats($type, $condition = '1')
// A big fat CASE WHEN... END is faster than a zillion UPDATE's ;).
db_query("
UPDATE {$db_prefix}members
UPDATE ${db_prefix}members
SET ID_POST_GROUP = CASE$conditions
ELSE 0
END" . ($condition != '1' ? "
@@ -536,7 +624,7 @@ function updateMemberData($members, $data)
LIMIT 1';
db_query("
UPDATE {$db_prefix}members
UPDATE ${db_prefix}members
SET" . substr($setString, 0, -1) . '
WHERE ' . $condition, __FILE__, __LINE__);
@@ -568,7 +656,7 @@ function updateSettings($changeArray)
return;
db_query("
REPLACE INTO {$db_prefix}settings
REPLACE INTO ${db_prefix}settings
(variable, value)
VALUES " . implode(',
', $replaceArray), __FILE__, __LINE__);
@@ -1358,7 +1446,7 @@ function parsesmileys(&$message)
// Load the smileys in reverse order by length so they don't get parsed wrong.
$result = db_query("
SELECT code, filename, description
FROM {$db_prefix}smileys
FROM ${db_prefix}smileys
ORDER BY LENGTH(code) DESC", __FILE__, __LINE__);
$smileysfrom = array();
$smileysto = array();
@@ -1439,11 +1527,11 @@ function writeLog($force = false)
$ip = ip2long($user_info['ip']);
db_query("
DELETE FROM {$db_prefix}log_online
DELETE FROM ${db_prefix}log_online
WHERE logTime < NOW() - INTERVAL " . ($modSettings['lastActive'] * 60) . " SECOND
OR session = '$session_id'" . (empty($ID_MEMBER) ? '' : " OR ID_MEMBER = $ID_MEMBER"), __FILE__, __LINE__);
db_query("
INSERT IGNORE INTO {$db_prefix}log_online
INSERT IGNORE INTO ${db_prefix}log_online
(session, ID_MEMBER, ip, url)
VALUES ('${session_id}', '${ID_MEMBER}', '${ip}', '${serialized}')", __FILE__, __LINE__);
@@ -1683,7 +1771,7 @@ function logAction($action, $extra = array())
if (!empty($modSettings['modlog_enabled']))
{
db_query("
INSERT INTO {$db_prefix}log_actions
INSERT INTO ${db_prefix}log_actions
(logTime, ID_MEMBER, IP, action, extra)
VALUES (" . time() . ", $ID_MEMBER, '$user_info[ip]', '$action',
'" . addslashes(serialize($extra)) . "')", __FILE__, __LINE__);
@@ -1719,14 +1807,14 @@ function trackStats($stats = array())
$date = strftime('%Y%m%d', forum_time(false));
db_query("
UPDATE {$db_prefix}log_activity
UPDATE ${db_prefix}log_activity
SET" . substr($setStringUpdate, 0, -1) . "
WHERE date = $date
LIMIT 1", __FILE__, __LINE__);
if (db_affected_rows() == 0)
{
db_query("
INSERT IGNORE INTO {$db_prefix}log_activity
INSERT IGNORE INTO ${db_prefix}log_activity
(date, " . implode(', ', array_keys($cache_stats)) . ")
VALUES ($date, " . implode(', ', $cache_stats) . ')', __FILE__, __LINE__);
}
@@ -1742,17 +1830,17 @@ function spamProtection($error_type)
// Delete old entries... if you can moderate this board or this is login, override spamWaitTime with 2.
if ($error_type == 'spam' && !allowedTo('moderate_board'))
db_query("
DELETE FROM {$db_prefix}log_floodcontrol
DELETE FROM ${db_prefix}log_floodcontrol
WHERE logTime < " . (time() - $modSettings['spamWaitTime']), __FILE__, __LINE__);
else
db_query("
DELETE FROM {$db_prefix}log_floodcontrol
DELETE FROM ${db_prefix}log_floodcontrol
WHERE (logTime < " . (time() - 2) . " AND ip = '$user_info[ip]')
OR logTime < " . (time() - $modSettings['spamWaitTime']), __FILE__, __LINE__);
// Add a new entry, deleting the old if necessary.
db_query("
REPLACE INTO {$db_prefix}log_floodcontrol
REPLACE INTO ${db_prefix}log_floodcontrol
(ip, logTime)
VALUES ('$user_info[ip]', " . time() . ")", __FILE__, __LINE__);
// If affected is 0 or 2, it was there already.
@@ -1853,7 +1941,7 @@ function setupThemeContext()
{
$result = db_query("
SELECT COUNT(*)
FROM {$db_prefix}postmoderation", __FILE__, __LINE__);
FROM ${db_prefix}postmoderation", __FILE__, __LINE__);
list($modmsg) = mysql_fetch_row($result);
if ($modmsg)
$context['user']['awaiting_mod'] = $modmsg . ' ' . $txt['visual_require_mod'];