Skip to content

Commit

Permalink
Do not use $_SESSION variable directly (we don't need $_SESSION for c…
Browse files Browse the repository at this point in the history
…ommand line based script)
  • Loading branch information
daris committed Feb 2, 2012
1 parent 50559c7 commit b255e8e
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 70 deletions.
11 changes: 5 additions & 6 deletions converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@
conv_log('Running command line based converter for: '.$forum_config['type'].' ('.gmdate('Y-m-d H:i:s').')');
conv_log('PHP version: '.PHP_VERSION.', OS: '.PHP_OS);

$session = array();

// Create a wrapper for fluxbb (has easy functions for adding users etc.)
require CONV_ROOT.'include/fluxbb.class.php';
$fluxbb = new FluxBB($pun_config);
Expand Down Expand Up @@ -141,7 +143,7 @@

$fluxbb->close_database();

if (!empty($_SESSION['converter']['dupe_users']))
if (!empty($session['dupe_users']))
{
conv_message("\n".'---------------------------'."\n");
conv_message($lang_convert['Username dupes head']);
Expand All @@ -156,10 +158,7 @@
$handle = fopen('php://stdin', 'r');
$line = trim(fgets($handle));
if ($line == 'yes')
{
alert_dupe_users();
unset($_SESSION['converter']['dupe_users']);
}
}

if (!empty($alerts))
Expand All @@ -169,8 +168,8 @@
}

conv_message();
conv_message($lang_convert['Conversion completed in'], round($_SESSION['fluxbb_converter']['time'], 4));
conv_message($lang_convert['Conversion completed in'], round($session['time'], 4));

conv_log('Conversion completed in '.$_SESSION['fluxbb_converter']['time'], false, true);
conv_log('Conversion completed in '.$session['time'], false, true);

exit(0);
8 changes: 5 additions & 3 deletions forums/Merge_FluxBB.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@ class Merge_FluxBB extends Forum

function initialize()
{
global $session;

$this->db->set_names('utf8');

if (!session_id())
session_start();

if (isset($_SESSION['converter']['last_id']))
$this->last_id = $_SESSION['converter']['last_id'];
if (isset($session['last_id']))
$this->last_id = $session['last_id'];
else
{
$check_id = array('groups' => 'g_id', 'users', 'bans', 'categories', 'censoring', 'forums', 'topics', 'posts', 'ranks', 'reports');
Expand All @@ -71,7 +73,7 @@ function initialize()

$this->last_id[$cur_table] = $this->fluxbb->db->num_rows($result) ? $this->db->result($result) : 0;
}
$_SESSION['converter']['last_id'] = $this->last_id;
$session['last_id'] = $this->last_id;
}
}

Expand Down
5 changes: 3 additions & 2 deletions forums/PhpBB_3_0.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,8 @@ function convert_posts($start_at)
'LIMIT' => PER_PAGE,
)) or conv_error('Unable to fetch posts', __FILE__, __LINE__, $this->db->error());

conv_message('Processing range', 'posts', $this->db->num_rows($result), $start_at, $start_at + PER_PAGE);
// conv_message('Processing range', 'posts', $this->db->num_rows($result), $start_at, $start_at + PER_PAGE, ceil($this->fetch_item_count('posts') / ));
conv_processing_message('posts', $start_at, $this->db->num_rows($result));

if (!$this->db->num_rows($result))
return false;
Expand Down Expand Up @@ -375,7 +376,7 @@ function convert_topics($start_at)
'LIMIT' => PER_PAGE,
)) or conv_error('Unable to fetch topics', __FILE__, __LINE__, $this->db->error());

conv_message('Processing range', 'topics', $this->db->num_rows($result), $start_at, $start_at + PER_PAGE);
conv_message('Processing range', 'topics', $this->db->num_rows($result), $start_at, $start_at + PER_PAGE, ceil());

if (!$this->db->num_rows($result))
return false;
Expand Down
36 changes: 20 additions & 16 deletions include/converter.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ class Converter

function __construct($fluxbb, $forum)
{
global $session;

$this->forum = $forum;
$this->fluxbb = $fluxbb;

if (!isset($session))
$session = array();
}

/**
Expand Down Expand Up @@ -56,19 +61,21 @@ function validate()
*/
function convert($step = null, $start_at = 0)
{
global $session;

$steps = array_keys($this->forum->steps);

// Start from beginning
if (!isset($step))
{
conv_log();
conv_message('Converting', 'start');
$_SESSION['fluxbb_converter']['start_time'] = get_microtime();
$session['start_time'] = get_microtime();

// Validate only first time we run converter (check whether database configuration is valid)
$this->validate();

$_SESSION['fluxbb_converter']['count'] = $this->forum->fetch_count();
$session['count'] = $this->forum->fetch_item_count();

// Drop the FluxBB database tables (when there is no NO_DB_CLEANUP constant defined for forum)
if (!defined(get_class($this->forum).'::NO_DB_CLEANUP'))
Expand Down Expand Up @@ -146,13 +153,15 @@ function cleanup_database()

function finish()
{
global $session;

// Handle users dupe
if (!empty($_SESSION['converter']['dupe_users']))
if (!empty($session['dupe_users']))
{
conv_log('Converting dupe users');
$start = get_microtime();

foreach ($_SESSION['converter']['dupe_users'] as $cur_user)
foreach ($session['dupe_users'] as $cur_user)
$this->fluxbb->convert_users_dupe($cur_user);

conv_log('Done in '.round(get_microtime() - $start, 6)."\n");
Expand All @@ -165,24 +174,18 @@ function finish()
$this->generate_cache();
conv_log('Done in '.round(get_microtime() - $start, 6)."\n");

$_SESSION['fluxbb_converter']['fluxbb_count'] = $this->fluxbb->fetch_count();
$_SESSION['fluxbb_converter']['time'] = get_microtime() - $_SESSION['fluxbb_converter']['start_time'];
$session['fluxbb_count'] = $this->fluxbb->fetch_count();
$session['time'] = get_microtime() - $session['start_time'];
}

function get_forum_item_count()
function get_forum_item_count($table = null)
{
if (isset($_SESSION['fluxbb_converter']['count']))
return $_SESSION['fluxbb_converter']['count'];
else
return $_SESSION['fluxbb_converter']['count'] = $this->forum->fetch_count();
return $this->forum->fetch_item_count($table);
}

function get_fluxbb_item_count()
function get_fluxbb_item_count($table = null)
{
if (isset($_SESSION['fluxbb_converter']['fluxbb_count']))
return $_SESSION['fluxbb_converter']['fluxbb_count'];
else
return $_SESSION['fluxbb_converter']['fluxbb_count'] = $this->fluxbb->fetch_count();
return $this->fluxbb->fetch_item_count($table);
}

/**
Expand All @@ -203,6 +206,7 @@ function generate_cache()
clear_feed_cache();
}

// TODO:
function get_time()
{
return get_microtime() - $this->start;
Expand Down
30 changes: 25 additions & 5 deletions include/fluxbb.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,12 @@ function add_row($table, $data, $error_callback = null)
*/
function error_users($cur_user)
{
if (!isset($_SESSION['converter']['dupe_users']))
$_SESSION['converter']['dupe_users'] = array();
global $session;

$_SESSION['converter']['dupe_users'][$cur_user['id']] = $cur_user;
if (!isset($session['dupe_users']))
$session['dupe_users'] = array();

$session['dupe_users'][$cur_user['id']] = $cur_user;
}

/**
Expand All @@ -141,6 +143,8 @@ function error_users($cur_user)
*/
function convert_users_dupe($cur_user)
{
global $session;

$old_username = $cur_user['username'];
$suffix = 1;

Expand All @@ -154,7 +158,7 @@ function convert_users_dupe($cur_user)
break;
}

$_SESSION['converter']['dupe_users'][$cur_user['id']]['username'] = $cur_user['username'] = $username;
$session['dupe_users'][$cur_user['id']]['username'] = $cur_user['username'] = $username;

$temp = array();
foreach ($cur_user as $idx => $value)
Expand Down Expand Up @@ -196,9 +200,20 @@ function convert_users_dupe($cur_user)
}
}

$_SESSION['converter']['dupe_users'][$cur_user['id']]['old_username'] = $old_username;
$session['dupe_users'][$cur_user['id']]['old_username'] = $old_username;
}

/**
* Save user avatar to the FluxBB avatar directory
*
* @param string $file
* A path to the file or image url
*
* @param integer $user_id
* Id of the user
*
* @return bool
*/
function save_avatar($file, $user_id)
{
// Download remote file
Expand Down Expand Up @@ -262,6 +277,11 @@ function save_avatar($file, $user_id)
return false;
}

/**
* Recount all posts, topics after conversion
*
* @return type
*/
function sync_db()
{
conv_log('Updating post count for each user');
Expand Down
53 changes: 34 additions & 19 deletions include/forum.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,31 +105,46 @@ function redirect($old_table, $old_field, $start_at)
return false;
}

function fetch_count()
function fetch_item_count($table = null)
{
$tables = array();
foreach ($this->steps as $cur_step => $table_info)
{
$count = 0;
global $session;

if (is_numeric($table_info))
$count = $table_info;
// First we look for whether we have this data stored in $session
if (isset($session['forum_item_count']))
$tables = $session['forum_item_count'];

else if (is_array($table_info))
// When not found, get item count and save in $session for futher use
else
{
$tables = array();
foreach ($this->steps as $cur_step => $table_info)
{
$query = array(
'SELECT' => 'COUNT('.$this->db->escape($table_info[1]).')',
'FROM' => $this->db->escape($table_info[0])
);
if (isset($table_info[2]))
$query['WHERE'] = $table_info[2];

$result = $this->db->query_build($query) or conv_error('Unable to fetch num rows for '.$cur_step, __FILE__, __LINE__, $this->db->error());
$count = $this->db->result($result);
}
$count = 0;

if (is_numeric($table_info))
$count = $table_info;

$tables[$cur_step] = $count;
else if (is_array($table_info))
{
$query = array(
'SELECT' => 'COUNT('.$this->db->escape($table_info[1]).')',
'FROM' => $this->db->escape($table_info[0])
);
if (isset($table_info[2]))
$query['WHERE'] = $table_info[2];

$result = $this->db->query_build($query) or conv_error('Unable to fetch num rows for '.$cur_step, __FILE__, __LINE__, $this->db->error());
$count = $this->db->result($result);
}

$tables[$cur_step] = $count;
}
$session['forum_item_count'] = $tables;
}

if (isset($table))
return isset($tables[$table]) ? $tables[$table] : -1;

return $tables;
}

Expand Down
41 changes: 38 additions & 3 deletions include/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@
* @package FluxBB
*/

function conv_processing_message($table, $start_at, $num_rows)
{
global $session;

if (!isset($session['num_done'][$table]))
$session['num_done'][$table] = 0;

conv_message('Processing rows', $num_rows, $start_at, $start_at + PER_PAGE);

$session['num_done'][$table] += $num_rows;
}

function validate_params($forum_config, $old_db_config)
{
global $pun_config;
Expand Down Expand Up @@ -155,11 +167,11 @@ function converter_list_langs()
*/
function alert_dupe_users()
{
global $pun_config;
global $pun_config, $session;

require PUN_ROOT.'include/email.php';

foreach ($_SESSION['converter']['dupe_users'] as $cur_user)
foreach ($$session['dupe_users'] as $cur_user)
{
// Email the user alerting them of the change
if (file_exists(PUN_ROOT.'lang/'.$cur_user['language'].'/mail_templates/rename.tpl'))
Expand All @@ -184,7 +196,20 @@ function alert_dupe_users()
}
}


/**
* Log message to the file
*
* @param string $message
* A message
*
* @param bool $first_time_only
* Should we log message for first step only
*
* @param bool $close
* When true, closes file handle
*
* @return type
*/
function conv_log($message = '', $first_time_only = false, $close = false)
{
static $prev_time, $fh, $pun_start;
Expand All @@ -207,6 +232,16 @@ function conv_log($message = '', $first_time_only = false, $close = false)
fclose($fh);
}

/**
* Converter error handler
* Writes messages to the log file
*
* @param integer $errno
* @param string $errstr
* @param string $errfile
* @param integer $errline
* @return type
*/
function conv_error_handler($errno, $errstr, $errfile, $errline)
{
ob_start();
Expand Down

0 comments on commit b255e8e

Please sign in to comment.