checkUpdateNeeded();
$upgradeNeeded = true;
if (!$upgradeNeeded)
{
$mes = e107::getMessage();
$mes -> addInfo("The forum is already at the most recent version, no upgrade is required");
$ns -> tablerender('Forum Upgrade', $mes -> render());
require (e_ADMIN . 'footer.php');
exit ;
}
if (isset($_POST) && count($_POST))
{
if (isset($_POST['skip_attach']))
{
$f -> updateInfo['skip_attach'] = 1;
$f -> updateInfo['currentStep'] = 2;
$f -> setUpdateInfo();
}
if (isset($_POST['nextStep']))
{
$tmp = array_keys($_POST['nextStep']);
$f -> updateInfo['currentStep'] = $tmp[0];
$f -> setUpdateInfo();
}
}
$currentStep = (isset($f -> updateInfo['currentStep']) ? $f -> updateInfo['currentStep'] : 1);
$stepParms = (isset($stepParms) ? $stepParms : '');
//echo "currentStep = $currentStep ";
if (function_exists('step' . $currentStep))
{
$result = call_user_func('step' . $currentStep, $stepParms);
}
require (e_ADMIN . 'footer.php');
exit ;
function step1()
{
global $f;
$f -> updateInfo['currentStep'] = 1;
$f -> setUpdateInfo();
$e107 = e107::getInstance();
$mes = e107::getMessage();
//Check attachment dir permissions
if (!isset($f -> updateInfo['skip_attach']))
{
$f -> checkAttachmentDirs();
if (isset($f -> error['attach']))
{
$text = "
ERROR:
The following errors have occured. These issues must be resolved if you ever want to enable attachment or image uploading in your forums. If you do not ever plan on enabling this setting in your forum, you may click the 'skip' button
";
foreach ($f->error['attach'] as $e)
{
$text .= '** ' . $e . ' ';
}
$text .= "
";
}
else
{
$mes -> addSuccess("Attachment and attachment/thumb directories are writable");
$text = "
";
}
e107::getRender() -> tablerender('Step 1: Attachment directory permissions', $mes -> render() . $text);
}
}
function step2()
{
$e107 = e107::getInstance();
$mes = e107::getMessage();
$ns = e107::getRender();
if (!isset($_POST['create_tables']))
{
$text = "
This step will create the new forum_thread, forum_post, and forum_attach tables. It will also create a forum_new table that will become the 'real' forum table once the data from the current table is migrated.
";
$ns -> tablerender('Step 2: Forum table creation', $text);
return;
}
// FIXME - use db_verify. ??
require_once (e_HANDLER . 'db_table_admin_class.php');
$db = new db_table_admin;
$tabList = array(
'forum' => 'forum_new',
'forum_thread' => '',
'forum_post' => '',
'forum_track' => ''
);
//
$ret = '';
$failed = false;
$text = '';
foreach ($tabList as $name => $rename)
{
$message = 'Creating table ' . ($rename ? $rename : $name);
$result = $db -> createTable(e_PLUGIN . 'forum/forum_sql.php', $name, true, $rename);
if ($result === true)
{
$mes -> addSuccess($message);
// $text .= 'Success ';
}
elseif ($result !== true)
{
// $text .= 'Failed ';
$mes -> addError($message);
$failed = true;
}
}
if ($failed)
{
$mes -> addError("Creation of table(s) failed. You can not continue until these are created successfully!");
}
else
{
$text = "";
}
$ns -> tablerender('Step 2: Forum table creation', $mes -> render() . $text);
}
// FIXME - use e107::getPlugin()->manage_extended_field('add', $name, $attrib,
// $source)
function step3()
{
$ns = e107::getRender();
$mes = e107::getMessage();
$stepCaption = 'Step 3: Extended user field creation';
if (!isset($_POST['create_extended']))
{
$text = "
This step will create the new extended user fields required for the new forum code:
* user_plugin_forum_posts (to track number of posts for each user)
* user_plugin_forum_viewed (to track threads viewed by each user
";
$ns -> tablerender($stepCaption, $text);
return;
}
require_once (e_HANDLER . 'user_extended_class.php');
$ue = new e107_user_extended;
$fieldList = array(
'plugin_forum_posts' => EUF_INTEGER,
'plugin_forum_viewed' => EUF_TEXTAREA
);
$failed = false;
foreach ($fieldList as $fieldName => $fieldType)
{
$result = $ue -> user_extended_add_system($fieldName, $fieldType);
if ($result === true)
{
$mes -> addSuccess('Creating extended user field user_' . $fieldName);
}
else
{
$mes -> addError('Creating extended user field user_' . $fieldName);
$failed = true;
}
}
if ($failed)
{
$mes -> addError("Creation of extended field(s) failed. You can not continue until these are create successfully!");
}
else
{
$text .= "
";
}
$ns -> tablerender($stepCaption, $mes -> render() . $text);
}
function step4()
{
global $pref;
$e107 = e107::getInstance();
$mes = e107::getMessage();
$ns = e107::getRender();
$stepCaption = 'Step 4: Move user specific forum data and forum prefs';
if (!isset($_POST['move_user_data']))
{
$text = "
This step will move the main forum preferences into its own table row. It will also move all user_viewed data from user table into the user extended table.
The user_forum field data will not be moved, as it will be recalculated later.
Depending on the size of your user table, this step could take a while.
Number of viewed data processed: {$result['viewcount']}
Number of tracked records added: {$result['trackcount']}
");
$text = "";
$ns -> tablerender($stepCaption, $mes -> render() . $text);
}
function step5()
{
$e107 = e107::getInstance();
$sql = e107::getDb();
$ns = e107::getRender();
$mes = e107::getMessage();
$stepCaption = 'Step 5: Migrate forum data';
if (!isset($_POST['move_forum_data']))
{
$text = "This step will copy all of your forum configuration from the `forum` table into the `forum_new` table.
Once the information is successfully copied, the existing 1.0 forum table will be renamed `forum_old` and the newly created `forum_new` table will be renamed `forum`.
Number of forum parents processed: {$counts['parents']}
Number of forums processed: {$counts['forums']}
Number of sub forums processed: {$counts['subs']}
");
$result = $sql -> gen('RENAME TABLE `#forum` TO `#forum_old` ') ? e_MESSAGE_SUCCESS : E_MESSAGE_ERROR;
$mes -> add("Renaming forum to forum_old", $result);
$result = $sql -> gen('RENAME TABLE `#forum_new` TO `#forum` ') ? e_MESSAGE_SUCCESS : E_MESSAGE_ERROR;
$mes -> add("Renaming forum_new to forum", $result);
$text = "
";
$ns -> tablerender($stepCaption, $mes -> render() . $text);
}
}
function step6x()
{
global $f;
$e107 = e107::getInstance();
$ns = e107::getRender();
$mes = e107::getMessage();
$sql = e107::getDb();
$stepCaption = 'Step 6: Thread and post data';
$threadLimit = varset($_POST['threadLimit'], 1000);
$lastThread = varset($f -> updateInfo['lastThread'], 0);
$maxTime = ini_get('max_execution_time');
if (!isset($_POST['move_thread_data']))
{
$count = $sql -> count('forum_t', '(*)', "WHERE thread_parent = 0 AND thread_id > {$lastThread}");
$limitDropdown = createThreadLimitDropdown($count);
$text = "This step will copy all of your existing forum threads and posts into the new `forum_thread` and `forum_post` tables.
Depending on your forum size and speed of server, this could take some time. This routine will attempt to do it in steps in order to
reduce the possibility of data loss and server timeouts.
Your current timeout appears to be set at {$maxTime} seconds. This routine will attempt to extend this time in order to process all threads,
success will depend on your server configuration. If you get a timeout while performing this function, return to this page and select fewer threads
to process.
";
$text .= "";
$ns -> tablerender($stepCaption, $mes -> render() . $text);
return;
}
$count = $sql -> count('forum_t', '(*)', "WHERE thread_parent=0 AND thread_id>{$lastThread}");
if ($count === false)
{
echo "error: Unable to determine last thread id";
exit ;
}
$done = false;
$qry = "
SELECT thread_id FROM `#forum_t`
WHERE thread_parent = 0
AND thread_id > {$lastThread}
ORDER BY thread_id ASC
LIMIT 0, {$threadLimit}
";
if ($sql -> gen($qry))
{
$postCount = 0;
$threadList = $sql -> db_getList();
foreach ($threadList as $t)
{
set_time_limit(30);
$id = (int)$t['thread_id'];
$result = $f -> migrateThread($id);
if ($result === false)
{
$mes -> addError("ERROR! Failed to migrate thread id: {$id}");
}
else
{
$postCount += ($result - 1);
$f -> updateInfo['lastThread'] = $id;
$f -> setUpdateInfo();
}
}
$mes -> addSuccess('Successfully converted ' . count($threadList) . " threads and {$postCount} replies");
$mes -> addSuccess("Last thread id = {$t['thread_id']}");
$count = $sql -> count('forum_t', '(*)', "WHERE thread_parent = 0 AND thread_id > {$f->updateInfo['lastThread']}");
if ($count)
{
$limitDropdown = createThreadLimitDropdown($count);
$text .= "
";
$ns -> tablerender($stepCaption, $mes -> render() . $text);
}
else
{
$done = true;
}
}
else
{
$done = true;
}
if ($done)
{
$mes -> addSuccess("Thread migration is complete!!");
$text = "";
$ns -> tablerender($stepCaption, $mes -> render() . $text);
}
}
function step6()
{
$sql = e107::getDb();
$ns = e107::getRender();
$mes = e107::getMessage();
$stepCaption = 'Step 6: Thread and post data';
$_SESSION['forumupdate_thread_total'] = $sql -> count('forum_t', '(*)', "WHERE thread_parent = 0");
$_SESSION['forumupdate_thread_count'] = 0;
$_SESSION['forumupdate_thread_last'] = 0;
$text = "This step will copy all of your existing forum threads and posts into the new `forum_thread` and `forum_post` tables.
Depending on your forum size and speed of server, this could take some time.
";
// $text .= "";
$ns -> tablerender($stepCaption, $mes -> render() . $text);
}
function step6_ajax()
{
global $f;
$sql = e107::getDb();
$lastThread = vartrue($_SESSION['forumupdate_thread_last'], 0);
$qry = "
SELECT thread_id FROM `#forum_t`
WHERE thread_parent = 0
AND thread_id > {$lastThread}
ORDER BY thread_id ASC
LIMIT 0, 300
";
if ($sql -> gen($qry))
{
$threadList = $sql -> db_getList();
foreach ($threadList as $t)
{
$id = (int)$t['thread_id'];
$result = $f -> migrateThread($id);
if ($result === false)
{
echo "Error";
}
else
{
$_SESSION['forumupdate_thread_last'] = $id;
$_SESSION['forumupdate_thread_count']++;
}
}
}
echo round(($_SESSION['forumupdate_thread_count'] / $_SESSION['forumupdate_thread_total']) * 100);
}
function step7()
{
$e107 = e107::getInstance();
$ns = e107::getRender();
$stepCaption = 'Step 7: Calculate user post counts';
if (!isset($_POST['calculate_usercounts']))
{
$text = "
This step will calculate post count information for all users, as well as recount all for thread and reply counts.
";
$ns -> tablerender($stepCaption, $text);
}
function step7_ajax()//TODO
{
}
function step8()
{
$e107 = e107::getInstance();
$stepCaption = 'Step 8: Calculate last post information';
if (!isset($_POST['calculate_lastpost']))
{
$text = "
This step will recalculate all thread and forum lastpost information
";
e107::getRender() -> tablerender($stepCaption, $text);
return;
}
global $forum;
$forum -> forumUpdateLastpost('forum', 'all', true);
// $forum->forumUpdateLastpost('thread', 84867);
$text .= "
Successfully recalculated lastpost information for all forums and threads.
";
e107::getRender() -> tablerender($stepCaption, $text);
}
function step8_ajax()//TODO
{
}
function step9()
{
$e107 = e107::getInstance();
$sql = e107::getDb();
$stepCaption = 'Step 9: Migrate poll information';
if (!isset($_POST['migrate_polls']))
{
$text = "
This step will recalculate all poll information that has been entered in the forums.
";
e107::getRender() -> tablerender($stepCaption, $text);
return;
}
$qry = "
SELECT t.thread_id, p.poll_id FROM `#polls` AS p
LEFT JOIN `#forum_thread` AS t ON t.thread_id = p.poll_datestamp
WHERE t.thread_id IS NOT NULL
";
if ($sql -> gen($qry))
{
while ($row = $sql -> fetch(MYSQL_ASSOC))
{
$threadList[] = $row['thread_id'];
}
foreach ($threadList as $threadId)
{
if ($sql -> select('forum_thread', 'thread_options', 'thread_id = ' . $threadId, 'default'))
{
$row = $sql -> fetch(MYSQL_ASSOC);
if ($row['thread_options'])
{
$opts = unserialize($row['thread_options']);
$opts['poll'] = 1;
}
else
{
$opts = array('poll' => 1);
}
$tmp = array();
$tmp['thread_options'] = serialize($opts);
$tmp['WHERE'] = 'thread_id = ' . $threadId;
// $tmp['_FIELD_TYPES']['thread_options'] = 'escape';
$sql -> update('forum_thread', $tmp);
}
}
}
else
{
$text = 'No threads found! ';
}
$text .= "
Successfully migrated forum poll information for " . count($threadList) . " thread poll(s).
";
e107::getRender() -> tablerender($stepCaption, $text);
}
function step9_ajax()//TODO
{
}
/*
function step10x()
{
$e107 = e107::getInstance();
$sql = e107::getDb();
$ns = e107::getRender();
global $f;
$stepCaption = 'Step 10: Migrate forum attachments';
//FIXME - Files should be moved to e107_media/files/forum/
if(!isset($_POST['migrate_attachments']))
{
$text = "
This step will migrate all forum attachment information.
All files will be moved from the e107_files/public directory into the
".e_MEDIA."plugins/forum/attachment directory and related posts will be updated
accordingly.
";
$ns->tablerender($stepCaption, $text);
return;
}
$qry = "
SELECT post_id, post_entry FROM `#forum_post`
WHERE post_entry REGEXP '_[[:digit:]]+_FT'
";
if($sql->gen($qry))
{
while($row = $sql->fetch(MYSQL_ASSOC))
{
$postList[] = $row;
}
$i = 0;
$pcount = 0;
$f->log("Found ".count($postList). " posts with attachments");
//XXX Run post through $tp->toHtml() and then use $tp->getTag() to find images
// or files.?
foreach($postList as $post)
{
// echo htmlentities($post['post_entry'])." ";
$i++;
// if($pcount++ > 10) { die('here 10'); }
$attachments = array();
$foundFiles = array();
// echo $post['post_entry']."
//Check for attached full-size images
if(preg_match_all('#\[img.*?\]({e_FILE}.*?_FT\d+_.*?)\[/img\]#ms',
$post['post_entry'], $matches, PREG_SET_ORDER))
{
foreach($matches as $match)
{
//Ensure it hasn't already been handled above
if(!in_array($match[1], $foundFiles))
{
$att = array();
$att['thread_id'] = $post['thread_id'];
$att['type'] = 'img';
$att['html'] = $match[0];
$att['name'] = $match[1];
$att['thumb'] = '';
$attachments[] = $att;
}
}
}
if(preg_match_all('#\[img.*?\](\.\./\.\./e107_files/public/.*?_FT\d+_.*?)\[/img\]#ms',
$post['post_entry'], $matches, PREG_SET_ORDER))
{
foreach($matches as $match)
{
//Ensure it hasn't already been handled above
if(!in_array($match[1], $foundFiles))
{
$att = array();
$att['thread_id'] = $post['thread_id'];
$att['type'] = 'img';
$att['html'] = $match[0];
$att['name'] = $match[1];
$att['thumb'] = '';
$attachments[] = $att;
}
}
}
//[file={e_FILE}public/1230090820_1_FT0_julia.zip]julia.zip[/file]
//Check for attached file (non-images)
if(preg_match_all('#\[file=({e_FILE}.*?)\](.*?)\[/file\]#ms',
$post['post_entry'], $matches, PREG_SET_ORDER))
{
foreach($matches as $match)
{
$att = array();
$att['thread_id'] = $post['thread_id'];
$att['type'] = 'file';
$att['html'] = $match[0];
$att['name'] = $match[1];
$att['thumb'] = '';
$attachments[] = $att;
}
}
if(preg_match_all('#\[file=(\.\./\.\./e107_files/public/.*?)\](.*?)\[/file\]#ms',
$post['post_entry'], $matches, PREG_SET_ORDER))
{
foreach($matches as $match)
{
$att = array();
$att['thread_id'] = $post['thread_id'];
$att['type'] = 'file';
$att['html'] = $match[0];
$att['name'] = $match[1];
$att['thumb'] = '';
$attachments[] = $att;
}
}
if(count($attachments))
{
$f->log("found ".count($attachments)." attachments");
$newValues = array();
$info = array();
$info['post_entry'] = $post['post_entry'];
foreach($attachments as $attachment)
{
$error = '';
$f->log($attachment['name']);
if($f->moveAttachment($attachment, $post['post_id'], $error))
{
$fInfo = pathinfo($attachment['name']);
// $_file = split('/', $attachment['name']);
$newval = $attachment['type'].'*'.$fInfo['basename'];
switch($attachment['type'])
{
//If file, add real name to entry
case 'file':
$tmp = explode('_', $fInfo['basename'], 4);
$newval .= '*'.$tmp[3];
break;
//If image and it has a thumb, add thumb filename to entry
case 'img':
if($attachment['thumb'])
{
$fInfo = pathinfo($attachment['thumb']);
$newval .= '*'.$fInfo['basename'];
}
break;
}
$newValues[] = $newval;
// echo "Newval = $newval ";
// echo "Removing from post:".htmlentities($attachment['html'])." ";
$info['post_entry'] = str_replace($attachment['html'], '', $info['post_entry']);
}
else
{
$errorText .= "Failure processing post {$post['post_id']} - file
{$attachment['name']} - {$error} ";
$f->log("Failure processing post {$post['post_id']} - file {$attachment['name']}
- {$error}");
}
}
// echo $errorText." ";
// Did we make any changes at all?
if(count($newValues))
{
$info['WHERE'] = 'post_id = '.$post['post_id'];
$info['post_attachments'] = implode(',', $newValues);
// print_a($info);
$sql->db_Update('forum_post', $info);
}
// echo $post['thread_thread']." ";
// print_a($newValues);
// echo $info['newpost']." -------------------------------------- ";
// Update db values now
}
}
}
else
{
$text = 'No forum attachments found! ';
}
// $forum->forumUpdateLastpost('thread', 84867);
$text .= "
Successfully migrated forum attachment information for ".count($postList)."
post(s).
";
$ns->tablerender($stepCaption, $text);
}
*/
function step10()
{
$e107 = e107::getInstance();
$sql = e107::getDb();
$ns = e107::getRender();
$mes = e107::getMessage();
global $f;
$stepCaption = 'Step 10: Migrate forum attachments';
$_SESSION['forumupdate_attachment_total'] = $sql -> count('forum_post', '(*)', "WHERE post_entry REGEXP '_[[:digit:]]+_FT' ");
$_SESSION['forumupdate_attachment_count'] = 0;
$_SESSION['forumupdate_attachment_last'] = 0;
if ($_SESSION['forumupdate_attachment_total'] == 0)
{
$text .= "
No forum attachments found.
";
$ns -> tablerender($stepCaption, $text);
return;
}
$text = "
This step will migrate the forum attachment information that was found in " . $_SESSION['forumupdate_attachment_total'] . " posts.
All files will be moved from the e107_files/public directory into the " . e_MEDIA . "plugins/forum/ directory and related posts will be updated accordingly.
";
$ns -> tablerender($stepCaption, $text);
return;
}
else
{
$text .= "
There were no orphaned files found
";
$ns -> tablerender($stepCaption, $text);
return;
}
}
function step12()
{
$sql = e107::getDb();
$ns = e107::getRender();
$mes = e107::getMessage();
$f = new forumUpgrade;
$stepCaption = 'Step 12: Delete old forum data';
if (!isset($_POST['delete_old']))
{
$text = "
The forum upgrade should now be complete. During the upgrade process the old forum tables were
retained, it is now time to remove the tables.
We will also be marking the forum upgrade as completed!
";
$ns -> tablerender($stepCaption, $text);
return;
}
$qryArray = array(
"DROP TABLE `#forum_old`",
"DROP TABLE `#forum_t`",
);
// "DELETE FROM `#generic` WHERE gen_type = 'forumUpgrade' "
unset($_SESSION['forumUpgrade']);
foreach ($qryArray as $qry)
{
$sql -> gen($qry);
}
$ret = $f -> setNewVersion();
$mes -> addSuccess("Congratulations, the forum upgrade is now completed!