Closed pjc09h closed 1 year ago
A good example of "user query glitch art":
$app->dbOld->prepared_query("
(SELECT
SQL_CALC_FOUND_ROWS
s.`Page`,
s.`PageID`,
lr.`PostID`,
null AS ForumID,
null AS ForumName,
IF(s.`Page` = 'artist', a.`Name`, co.`Name`) AS Name,
c.`ID` AS LastPost,
c.`AddedTime` AS LastPostTime,
c_lr.`Body` AS LastReadBody,
c_lr.`EditedTime` AS LastReadEditedTime,
um.`ID` AS LastReadUserID,
um.`Username` AS LastReadUsername,
ui.`Avatar` AS LastReadAvatar,
c_lr.`EditedUserID` AS LastReadEditedUserID
FROM `users_subscriptions_comments` AS s
LEFT JOIN `users_comments_last_read` AS lr ON lr.`UserID` = {$app->user->core['id']} AND lr.`Page` = s.`Page` AND lr.`PageID` = s.`PageID`
LEFT JOIN `artists_group` AS a ON s.`Page` = 'artist' AND a.`ArtistID` = s.`PageID`
LEFT JOIN `collages` AS co ON s.`Page` = 'collages' AND co.`ID` = s.`PageID`
LEFT JOIN `comments` AS c ON c.`ID` = (
SELECT MAX(`ID`)
FROM `comments`
WHERE `Page` = s.`Page`
AND `PageID` = s.`PageID`
)
LEFT JOIN `comments` AS c_lr ON c_lr.`ID` = lr.`PostID`
LEFT JOIN `users_main` AS um ON um.`ID` = c_lr.`AuthorID`
LEFT JOIN `users_info` AS ui ON ui.`UserID` = um.`ID`
WHERE s.`UserID` = {$app->user->core['id']} AND s.`Page` IN ('artist', 'collages', 'requests', 'torrents') AND (s.`Page` != 'collages' OR co.`Deleted` = '0')" . ($ShowUnread ? ' AND c.`ID` > IF(lr.`PostID` IS NULL, 0, lr.`PostID`)' : '') . "
GROUP BY s.`PageID`)
UNION ALL
(SELECT 'forums', s.`TopicID`, lr.`PostID`, f.`ID`, f.`Name`, t.`Title`, p.`ID`, p.`AddedTime`, p_lr.`Body`, p_lr.`EditedTime`, um.`ID`, um.`Username`, ui.`Avatar`, p_lr.`EditedUserID`
FROM `users_subscriptions` AS s
LEFT JOIN `forums_last_read_topics` AS lr ON lr.`UserID` = {$app->user->core['id']} AND s.`TopicID` = lr.`TopicID`
LEFT JOIN `forums_topics` AS t ON t.`ID` = s.`TopicID`
LEFT JOIN `forums` AS f ON f.`ID` = t.`ForumID`
LEFT JOIN `forums_posts` AS p ON p.`ID` = (
SELECT MAX(`ID`)
FROM `forums_posts`
WHERE `TopicID` = s.`TopicID`
)
LEFT JOIN `forums_posts` AS p_lr ON p_lr.`ID` = lr.`PostID`
LEFT JOIN `users_main` AS um ON um.`ID` = p_lr.`AuthorID`
LEFT JOIN `users_info` AS ui ON ui.`UserID` = um.`ID`
WHERE s.`UserID` = {$app->user->core['id']}" .
($ShowUnread ? " AND p.`ID` > IF(t.`IsLocked` = '1' AND t.`IsSticky` = '0'" . ", p.`ID`, IF(lr.`PostID` IS NULL, 0, lr.`PostID`))" : '') .
' AND ' . Forums::user_forums_sql() . "
GROUP BY t.`ID`)
ORDER BY `LastPostTime` DESC
LIMIT $Limit");
All that remains is to do the necessary database surgery (quicker without a migration) and do a quick end-to-end test of the features on a new account.
Thanks wgm! Actions items based on an email conversation:
User
class are defined as$foo ??= null
before trying to use them (500 error on user stats).select where users.id = ?
butjoin users_main on users_main.userId
.[!!] Actually, there's a better way to do this for now. The codebase references
users_main
355 times in 140 files. Many of these are legacy "glitch art" queries, either massive or programmatically determined. I need to remove theauto_increment
and primary key properties ofusers_main.id
and manually set it tousers.id
. Then the existing queries forusers_main.id
should work long enough for them to be rewritten.