public static function delete_old_posts() { global $CFG, $DB; // Check if deletion is turned off if (empty($CFG->forumng_permanentdeletion)) { return; } mtrace('Beginning forum deleted/edit message cleanup...'); // Work out how long ago things have to have been 'deleted' before we // permanently delete them $deletebefore = time() - $CFG->forumng_permanentdeletion; // Handle all posts which were deleted (that long ago) or which are in // discussions which were deleted (that long ago) $mainparams = array(); $mainquery = "\nFROM\n {forumng_posts} fp\n INNER JOIN {forumng_discussions} fd ON fd.id = fp.discussionid\n INNER JOIN {forumng} f ON fd.forumngid = f.id\n INNER JOIN {course_modules} cm ON cm.instance = f.id AND cm.module =\n (SELECT id FROM {modules} WHERE name='forumng')\n INNER JOIN {context} x on x.instanceid=cm.id AND x.contextlevel=?\nWHERE\n (fp.deleted<>0 AND fp.deleted<? AND\n NOT EXISTS (SELECT id from {forumng_posts} where parentpostid = fp.id and deleted = 0))\n OR (fp.oldversion<>0 AND fp.modified<?)\n OR (fd.deleted<>0 AND fd.deleted<?)"; $idquery = "SELECT fp.id {$mainquery} "; $mainparams[] = CONTEXT_MODULE; $mainparams[] = $deletebefore; $mainparams[] = $deletebefore; $mainparams[] = $deletebefore; $before = microtime(true); mtrace('Message search: ', ''); $count = $DB->count_records_sql("SELECT COUNT(1) {$mainquery}", $mainparams); mtrace(round(microtime(true) - $before, 1) . 's'); if ($count == 0) { mtrace("No old deleted / edited messages to clean up."); } else { mtrace("Permanently deleting {$count} old deleted / edited messages."); } if ($count) { $before = microtime(true); mtrace('Database post deletion: ', ''); $transaction = $DB->start_delegated_transaction(); // Delete all ratings $DB->execute("DELETE FROM {forumng_ratings} WHERE postid IN ({$idquery})", $mainparams); // Delete all read post records. $DB->execute("DELETE FROM {forumng_read_posts} WHERE postid IN ({$idquery})", $mainparams); // Find all messages... $rs = $DB->get_recordset_sql("\nSELECT\n fp.id AS postid, x.id AS contextid, fp.attachments\n{$mainquery}", $mainparams); // Delete all attachments - this is very very slow (at least 1 query // per post being deleted), it could be done faster in the database // but I thought it might be safer to use the API $fs = get_file_storage(); foreach ($rs as $attachmentrecord) { // Delete both attachments & message attachments if ($attachmentrecord->attachments) { // Only if marked as existing $fs->delete_area_files($attachmentrecord->contextid, 'mod_forumng', 'attachment', $attachmentrecord->postid); } // Always (there is no marker for this one) $fs->delete_area_files($attachmentrecord->contextid, 'mod_forumng', 'message', $attachmentrecord->postid); } $rs->close(); // Delete all posts mod_forumng_utils::update_with_subquery_grrr_mysql("DELETE FROM {forumng_posts} WHERE id %'IN'%", $idquery, $mainparams); // Now delete all discussions $DB->execute("DELETE FROM {forumng_discussions} WHERE deleted<>0 AND deleted<?", array($deletebefore)); $transaction->allow_commit(); mtrace(round(microtime(true) - $before, 1) . 's'); } }
private function mark_mailed($time) { global $DB; list($wheresql, $whereparams) = $this->get_query_where($time, 'forumng_posts'); $querychunk = $this->get_query_from() . $wheresql; $before = microtime(true); mtrace('Marking processed posts: ', ''); mod_forumng_utils::update_with_subquery_grrr_mysql("\nUPDATE\n {forumng_posts}\nSET\n mailstate = " . $this->get_target_mail_state() . "\nWHERE\n id %'IN'%", "SELECT fp.id {$querychunk}", $whereparams); mtrace(round(microtime(true) - $before, 1) . 's.'); unset_config($this->get_pending_flag_name(), 'forumng'); }