Skip to content

Commit

Permalink
Added new script to upgrade the forum records (it has a pretty progre…
Browse files Browse the repository at this point in the history
…ss bar

and it means the main upgrade isn't held up.
  • Loading branch information
moodler committed Apr 23, 2005
1 parent 18cfcc1 commit 3a70cb6
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 122 deletions.
6 changes: 3 additions & 3 deletions admin/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,8 @@
$table->tablealign = "right";
$table->align = array ("right", "left");
$table->wrap = array ("nowrap", "nowrap");
$table->cellpadding = 4;
$table->cellspacing = 3;
$table->cellpadding = 5;
$table->cellspacing = 0;
$table->width = "40%";

$configdata = "<font size=\"+1\">&nbsp;</font><a href=\"config.php\">".get_string("configvariables", 'admin')."</a> - <font size=\"1\">".
Expand Down Expand Up @@ -400,7 +400,7 @@
//////////////////////////////////////////////////////////////////////////////////////////////////


echo '<table width="100%"><tr>';
echo '<table width="100%" cellspacing="0"><tr>';
echo '<td align="center" width="33%">';
print_single_button($CFG->wwwroot.'/doc/', NULL, get_string('documentation'));
echo '</td>';
Expand Down
156 changes: 156 additions & 0 deletions admin/upgradeforumread.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<?PHP //$Id$

require("../config.php");
require("$CFG->dirroot/mod/forum/lib.php");

optional_variable($confirm);

require_login();

if (!isadmin()) {
error("You must be an admin to use this script");
}

if ($CFG->version < 2005042300) {
error("This script does not work with this old version of Moodle");
}

if (!$site = get_site()) {
redirect("index.php");
}


/// Print header

$stradministration = get_string("administration");
$strupgradingdata = get_string("upgradingdata", "admin");

print_header("$site->shortname: $stradministration: $strupgradingdata", "$site->fullname",
"<a href=\"index.php\">$stradministration</a> -> $strupgradingdata");

if (empty($_GET['confirm'])) {
notice_yesno(get_string("upgradeforumreadinfo", "admin"),
"upgradeforumread.php?confirm=true&sesskey=$USER->sesskey",
"index.php");
print_footer();
exit;
} else if (!confirm_sesskey()) {
error(get_string('confirmsesskeybad', 'error'));
}


/// Turn off time limits, sometimes upgrades can be slow.

@set_time_limit(0);
@ob_implicit_flush(true);
@ob_end_flush();

execute_sql('TRUNCATE TABLE '.$CFG->prefix.'forum_read;', false); // Trash all old entries

/// Enter initial read records for all posts older than 1 day.

/// Timestamp for old posts (and therefore considered read).
$dateafter = time() - ($CFG->forum_oldpostdays*24*60*60);

/// Timestamp for one day ago.
$onedayago = time() - (24*60*60);


/// Get all discussions that have had posts since the old post date.
if ($discussions = get_records_select('forum_discussions', 'timemodified > '.$dateafter,
'course', 'id,course,forum,groupid,userid')) {
$dtotal = count($discussions);
notify('Updating forum read/unread records for '.$dtotal.' discussions...');

$groups = array();

$currcourse = 0;
$users = 0;
$count = 0;
$dcount = 0;
print_progress($dcount, $dtotal);

foreach ($discussions as $discussion) {
$dcount++;
if ($discussion->course != $currcourse) {
/// Discussions are ordered by course, so we only need to get any course's users once.
$currcourse = $discussion->course;
$users = get_course_users($currcourse, '', '', 'u.id,u.confirmed');
}
/// If this course has users, and posts more than a day old, mark them for each user.
if ($users &&
($posts = get_records_select('forum_posts', 'discussion = '.$discussion->id.
' AND '.$dateafter.' < modified AND modified < '.$onedayago,
'', 'id,discussion,modified'))) {
foreach ($users as $user) {
/// If its a group discussion, make sure the user is in the group.
if ($discussion->groupid) {
if (!isset($groups[$discussion->groupid][$user->id])) {
$groups[$discussion->groupid][$user->id] = ismember($discussion->groupid, $user->id);
}
}
if (!$discussion->groupid || !empty($groups[$discussion->groupid][$user->id])) {
foreach ($posts as $post) {
print_progress($dcount, $dtotal);
forum_tp_mark_post_read($user->id, $post, $discussion->forum);
}
}
}
}
}
print_progress($dcount, $dtotal, 0);
}


delete_records('config', 'name', 'upgrade', 'value', 'forumread');

notify('Log upgrading was successful!');

print_footer();

exit;



function print_progress($done, $total, $updatetime=3, $sleeptime=1) {
static $count;
static $starttime;
static $lasttime;

if (empty($starttime)) {
$starttime = $lasttime = time();
$lasttime = $starttime - $updatetime;
echo '<table width="500" cellpadding="0" cellspacing="0" align="center"><tr><td width="500">';
echo '<div id="bar" style="border-style:solid;border-width:1px;width:500px;height:50px;">';
echo '<div id="slider" style="border-style:solid;border-width:1px;height:48px;width:10px;background-color:green;"></div>';
echo '</div>';
echo '<div id="text" align="center" style="width:500px;"></div>';
echo '</td></tr></table>';
echo '</div>';
}

if (!isset($count)) {
$count = 0;
}

$count++;

$now = time();

if (($now - $lasttime) >= $updatetime) {
$elapsedtime = $now - $starttime;
$projectedtime = (int)(((float)$total / (float)$done) * $elapsedtime) - $elapsedtime;
$percentage = format_float((float)$done / (float)$total, 2);
$width = (int)(500 * $percentage);

echo '<script>';
echo 'document.getElementById("text").innerHTML = "'.$count.' done. Ending in: '.format_time($projectedtime).'";'."\n";
echo 'document.getElementById("slider").style.width = \''.$width.'px\';'."\n";
echo '</script>';

$lasttime = $now;
sleep($sleeptime);
}
}

?>
3 changes: 3 additions & 0 deletions lang/en/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,15 @@
$string['timezoneforced'] = 'This is forced by the site administrator';
$string['timezoneisforcedto'] = 'Force all users to use';
$string['therewereerrors'] = 'There were errors in your data';
$string['upgradeforumread'] = 'A new feature has been added in Moodle 1.5 to track read/unread forum posts.<br />To use this functionality you need to <a href=\"$a\">update your tables</a>.';
$string['upgradeforumreadinfo'] = 'A new feature has been added in Moodle 1.5 to track read/unread forum posts. To use this functionality you need to update your tables with all the tracking information for existing posts. Depending on the size of your site this can take a long time (hours) and can be quite taxing on the database, so it\'s best to do it during a quiet period. However, your site will continue functioning during this upgrade and users won\'t be affected. Once you start this process you should let it finish (keep your browser window open). However, if you stop the process by closing the window: don\'t worry, you can start over.<br /><br />Do you want to start the upgrading process now?';
$string['upgradelogs'] = 'For full functionality, your old logs need to be upgraded. <a href=\"$a\">More information</a>';
$string['upgradelogsinfo'] = 'Some changes have recently been made in the way logs are stored. To be able to view all of your old logs on a per-activity basis, your old logs need to be upgraded. Depending on your site this can take a long time (eg several hours) and can be quite taxing on the database for large sites. Once you start this process you should let it finish (by keeping the browser window open). Don\'t worry - your site will work fine for other people while the logs are being upgraded.<br /><br />Do you want to upgrade your logs now?';
$string['upgradesure'] = 'Your Moodle files have been changed, and you are about to automatically upgrade your server to this version:
<p><b>$a</b></p>
<p>Once you do this you can not go back again.</p>
<p>Are you sure you want to upgrade this server to this version?</p>';
$string['upgradinglogs'] = 'Upgrading logs';
$string['upgradingdata'] = 'Upgrading data';

?>
68 changes: 9 additions & 59 deletions mod/forum/db/mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,72 +174,22 @@ function forum_upgrade($oldversion) {
KEY `prefix_forum_user_post_idx` (`userid`,`postid`)
) COMMENT=\'Tracks each users read posts\';');

modify_database('','TRUNCATE TABLE prefix_forum_read;');

/// Enter initial read records for all posts older than 1 day.

require $CFG->dirroot.'/mod/forum/lib.php';
/// Timestamp for old posts (and therefore considered read).
$dateafter = time() - ($CFG->forum_oldpostdays*24*60*60);
/// Timestamp for one day ago.
$onedayago = time() - (24*60*60);

/// Get all discussions that have had posts since the old post date.
if ($discussions = get_records_select('forum_discussions', 'timemodified > '.$dateafter,
'course', 'id,course,forum,groupid,userid')) {
$roughposts = count_records_select('forum_posts', 'modified < '.$onedayago.' AND modified > '.$dateafter);
notify('Updating forum read/unread records for approx '.$roughposts.' posts...');

$db->debug = false; // Too much to look at!
$currcourse = 0;
$users = 0;
$count = 0;
foreach ($discussions as $discussion) {
if ($discussion->course != $currcourse) {
/// Discussions are ordered by course, so we only need to get any course's users once.
$currcourse = $discussion->course;
$users = get_course_users($currcourse, '', '', 'id,id');
}
/// If this course has users, and posts more than a day old, mark them for each user.
if ($users &&
($posts = get_records_select('forum_posts', 'discussion = '.$discussion->id.
' AND modified < '.$onedayago.' AND modified > '.$dateafter,
'', 'id,discussion,modified'))) {
foreach ($users as $user) {
/// If its a group discussion, make sure the user is in the group.
if (!$discussion->groupid ||
$discussion->userid == $user->id ||
ismember($discussion->groupid, $user->id)) {
foreach ($posts as $post) {
$count++;
if ($count % 100 == 0) {
echo $count.'<br />';
} else {
echo '.';
}
forum_tp_mark_post_read($user->id, $post, $discussion->forum);
}
}
}
}
}
$db->debug = true;
}
set_config('upgrade', 'forumread'); // The upgrade of this table will be done later by admin/upgradeforumread.php
}

if ($oldversion < 2005032900) {
modify_database('','ALTER TABLE prefix_forum_posts ADD INDEX prefix_form_posts_created_idx (created);');
modify_database('','ALTER TABLE prefix_forum_posts ADD INDEX prefix_form_posts_mailed_idx (mailed);');
}

if ($oldversion < 2005041100) { // replace wiki-like with markdown
include_once( "$CFG->dirroot/lib/wiki_to_markdown.php" );
$wtm = new WikiToMarkdown();
$sql = "select course from {$CFG->prefix}forum_discussions, {$CFG->prefix}forum_posts ";
$sql .= "where {$CFG->prefix}forum_posts.discussion = {$CFG->prefix}forum_discussions.id ";
$sql .= "and {$CFG->prefix}forum_posts.id = ";
$wtm->update( 'forum_posts','message','format',$sql );
}
if ($oldversion < 2005041100) { // replace wiki-like with markdown
include_once( "$CFG->dirroot/lib/wiki_to_markdown.php" );
$wtm = new WikiToMarkdown();
$sql = "select course from {$CFG->prefix}forum_discussions, {$CFG->prefix}forum_posts ";
$sql .= "where {$CFG->prefix}forum_posts.discussion = {$CFG->prefix}forum_discussions.id ";
$sql .= "and {$CFG->prefix}forum_posts.id = ";
$wtm->update( 'forum_posts','message','format',$sql );
}

return true;

Expand Down
69 changes: 9 additions & 60 deletions mod/forum/db/postgres7.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,73 +116,22 @@ function forum_upgrade($oldversion) {
modify_database('','CREATE INDEX prefix_forum_user_discussion_idx ON prefix_forum_read (userid, discussionid);');
modify_database('','CREATE INDEX prefix_forum_user_post_idx ON prefix_forum_read (userid, postid);');

modify_database('','TRUNCATE TABLE prefix_forum_read;'); // Just in case this is being re-run

/// Enter initial read records for all posts older than 1 day.

require $CFG->dirroot.'/mod/forum/lib.php';
/// Timestamp for old posts (and therefore considered read).
$dateafter = time() - ($CFG->forum_oldpostdays*24*60*60);
/// Timestamp for one day ago.
$onedayago = time() - (24*60*60);

/// Get all discussions that have had posts since the old post date.
if ($discussions = get_records_select('forum_discussions', 'timemodified > '.$dateafter,
'course', 'id,course,forum,groupid,userid')) {
$roughposts = count_records_select('forum_posts', 'modified < '.$onedayago.' AND modified > '.$dateafter);
notify('Updating forum read/unread records for approx '.$roughposts.' posts...');

$db->debug = false; // Too much to look at!
$currcourse = 0;
$users = 0;
$count = 0;
foreach ($discussions as $discussion) {
if ($discussion->course != $currcourse) {
/// Discussions are ordered by course, so we only need to get any course's users once.
$currcourse = $discussion->course;
$users = get_course_users($currcourse, '', '', 'id,id');
}
/// If this course has users, and posts more than a day old, mark them for each user.
if ($users &&
($posts = get_records_select('forum_posts', 'discussion = '.$discussion->id.
' AND modified < '.$onedayago.' AND modified > '.$dateafter,
'', 'id,discussion,modified'))) {
foreach ($users as $user) {
/// If its a group discussion, make sure the user is in the group.
if (!$discussion->groupid ||
$discussion->userid == $user->id ||
ismember($discussion->groupid, $user->id)) {
foreach ($posts as $post) {
$count++;
if ($count % 100 == 0) {
echo $count.'<br />';
} else {
echo '.';
}
forum_tp_mark_post_read($user->id, $post, $discussion->forum);
}
}
}
}
}
$db->debug = true;
}
set_config('upgrade', 'forumread'); // The upgrade of this table will be done later by admin/upgradeforumread.php
}

if ($oldversion < 2005032900) {
modify_database('','CREATE INDEX prefix_forum_posts_created_idx ON prefix_forum_posts (created);');
modify_database('','CREATE INDEX prefix_forum_posts_mailed_idx ON prefix_forum_posts (mailed);');

}

if ($oldversion < 2005041100) { // replace wiki-like with markdown
include_once( "$CFG->dirroot/lib/wiki_to_markdown.php" );
$wtm = new WikiToMarkdown();
$sql = "select course from {$CFG->prefix}forum_discussions, {$CFG->prefix}forum_posts ";
$sql .= "where {$CFG->prefix}forum_posts.discussion = {$CFG->prefix}forum_discussions.id ";
$sql .= "and {$CFG->prefix}forum_posts.id = ";
$wtm->update( 'forum_posts','message','format',$sql );
}
if ($oldversion < 2005041100) { // replace wiki-like with markdown
include_once( "$CFG->dirroot/lib/wiki_to_markdown.php" );
$wtm = new WikiToMarkdown();
$sql = "select course from {$CFG->prefix}forum_discussions, {$CFG->prefix}forum_posts ";
$sql .= "where {$CFG->prefix}forum_posts.discussion = {$CFG->prefix}forum_discussions.id ";
$sql .= "and {$CFG->prefix}forum_posts.id = ";
$wtm->update( 'forum_posts','message','format',$sql );
}

return true;

Expand Down
4 changes: 4 additions & 0 deletions mod/forum/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -3135,6 +3135,10 @@ function forum_tp_mark_post_read($userid, &$post, $forumid) {
}
}

function forum_tp_mark_forum_read($userid, $forumid) {
/// We need a proper LEFT JOIN in here to find posts without read records
}

function forum_tp_is_post_read($userid, &$post) {
return (forum_tp_is_post_old($post) ||
(get_record('forum_read', 'userid', $userid, 'postid', $post->id) !== false));
Expand Down

0 comments on commit 3a70cb6

Please sign in to comment.