From a5cef9c87e2efaa786e6c0b4ebe5c3ec8bf911d1 Mon Sep 17 00:00:00 2001 From: skodak Date: Sun, 3 Feb 2008 10:21:27 +0000 Subject: [PATCH] MDL-13305 , MDL-13304 - fixed memory leak in forum cron and saved 10% of queries when printing user pictures in mails; merged from MOODLE_19_STABLE --- mod/forum/lib.php | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/mod/forum/lib.php b/mod/forum/lib.php index 8e20c3203d..0cf1ef0cac 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -288,7 +288,7 @@ function forum_cron() { // caching subscribed users of each forum if (!isset($subscribedusers[$forumid])) { - if ($subusers = forum_subscribed_users($courses[$courseid], $forums[$forumid], 0, true)) { + if ($subusers = forum_subscribed_users($courses[$courseid], $forums[$forumid], 0, false)) { foreach ($subusers as $postuser) { // do not try to mail users with stopped email if ($postuser->emailstop) { @@ -296,10 +296,11 @@ function forum_cron() { continue; } // this user is subscribed to this forum - $subscribedusers[$forumid][] = $postuser->id; + $subscribedusers[$forumid][$postuser->id] = $postuser->id; // this user is a user we have to process later $users[$postuser->id] = $postuser; } + unset($subusers); // release memory } } @@ -327,14 +328,6 @@ function forum_cron() { foreach ($posts as $pid => $post) { - // Get info about the sending user - if (array_key_exists($post->userid, $users)) { // we might know him/her already - $userfrom = $users[$post->userid]; - } else if (!$userfrom = get_record('user', 'id', $post->userid)) { - mtrace('Could not find user '.$post->userid); - continue; - } - // Set up the environment for the post, discussion, forum, course $discussion = $discussions[$post->discussion]; $forum = $forums[$discussion->forum]; @@ -342,10 +335,20 @@ function forum_cron() { $cm = $coursemodules[$forum->id]; // Do some checks to see if we can bail out now - if (empty($subscribedusers[$forum->id]) || !in_array($userto->id, $subscribedusers[$forum->id])) { + if (!isset($subscribedusers[$forum->id][$userto->id])) { continue; // user does not subscribe to this forum } + // Get info about the sending user + if (array_key_exists($post->userid, $users)) { // we might know him/her already + $userfrom = $users[$post->userid]; + } else if ($userfrom = get_record('user', 'id', $post->userid)) { + $users[$userfrom->id] = $userfrom; // fetch only once, we can add it to user list, it will be skipped anyway + } else { + mtrace('Could not find user '.$post->userid); + continue; + } + // setup global $COURSE properly - needed for roles and languages course_setup($course); // More environment @@ -2062,7 +2065,7 @@ function forum_make_mail_post(&$post, $user, $touser, $course, $output = ''; $output .= ''; if ($post->parent) { -- 2.39.5
'; - $output .= print_user_picture($user->id, $course->id, $user->picture, false, true); + $output .= print_user_picture($user, $course->id, $user->picture, false, true); $output .= '