<?php
//================================================================
// genarete 100 xoopscomments data for weblinks
// for debug
// 2005-03-28 K.OHWADA
//================================================================

// --- xoopscomments table ---
// com_id   mediumint(8)
// com_pid  mediumint(8)
// com_rootid  mediumint(8)
// com_modid   smallint(5)
// com_itemid  mediumint(8)
// com_icon    varchar(25)
// com_created  int(10)
// com_modified int(10)
// com_uid    mediumint(8)
// com_ip     varchar(15)
// com_title  varchar(255)
// com_text   text
// com_sig    tinyint(1)
// com_status tinyint(1)
// com_exparams  varchar(255)
// dohtml   tinyint(1)
// dosmiley tinyint(1)
// doxcode  tinyint(1)
// doimage  tinyint(1)
// dobr     tinyint(1)

include '../../../../mainfile.php';
include '../../../../include/cp_header.php';

global $xoopsDB, $xoopsUser, $xoopsModule;
xoops_cp_header();

// check permission
$flag = false;
if ($xoopsUser && $xoopsUser->isAdmin($xoopsModule->mid())) 
{	$flag = true;	}

if (!$flag)
{
	echo "<font color=red>you are not admin</font><br>";
	xoops_cp_footer();
	exit();
}

$MAX_COM  = 100;

$max_lid = intval( $MAX_COM / 4 );

$mid = $xoopsModule->getVar('mid');

// table name
$table_com  = $xoopsDB->prefix("xoopscomments");
$table_link = $xoopsDB->prefix("weblinks_link");

echo "<h3>start</h3>";

$com_id_arr     = array();
$com_itemid_arr = array();

srand( (double)microtime()*1000000 );

// constant
	$com_icon = '';
	$com_sig  = 0;
	$dohtml   = 0;
	$dosmiley = 1;
	$doxcode  = 1;
	$doimage  = 1;
	$dobr     = 1;
	$com_status   = 2;
	$com_exparams = '';
	$com_modid    = $mid;

for ($i=0; $i<$MAX_COM; $i++)
{

// randum data
	$com_itemid = rand(1, $max_lid);
	$com_uid    = rand(1, 10);
	$com_ip     = get_randum_ip();
	$com_title  = get_randum_char() . sprintf( "%06d", rand(0,999999) );
	$com_created  = get_randum_time();
	$com_modified = $com_created;

// other
	$com_text  = "$com_title\n $com_created\n";

// xoopscomments table
	$sql  = "INSERT INTO $table_com ";
	$sql .= "(com_modid, com_itemid, com_icon, com_created, com_modified, com_uid, com_ip, com_title, com_text, com_sig, com_status, com_exparams, dohtml, dosmiley, doxcode, doimage, dobr)";
	$sql .= "VALUES ";
	$sql .= "($com_modid, $com_itemid, '$com_icon', $com_created, $com_modified, $com_uid, '$com_ip', '$com_title', '$com_text', $com_sig, $com_status, '$com_exparams', $dohtml, $dosmiley, $doxcode, $doimage, $dobr)";

	echo "$sql <br>";
	sql_exec($sql);

	$newid = $xoopsDB->getInsertId();

// pid: 1 per 2
	$com_id     = $newid;
	$com_rootid = $newid;
	$com_pid    = 0;

	if ($i % 2 == 0)
	{
		if ( isset( $com_itemid_arr[$com_itemid] ) )
		{
			$pid_arr = $com_itemid_arr[$com_itemid];
			$count   = count($pid_arr) - 1;

			if ( $count < 0 )
			{
				$count = 0;
			}

			$id = rand(0, $count);

			if ( isset( $pid_arr[$id] ))
			{
				$com_pid    = $pid_arr[$id];
				$com_rootid = $com_id_arr[$com_pid]['com_rootid'];
			}
		}
	}

	$sql  = "UPDATE $table_com SET ";
	$sql .= "com_rootid=$com_rootid, ";
	$sql .= "com_pid=$com_pid ";
	$sql .= "WHERE com_id=$com_id";

	echo "$sql <br>";
	sql_exec($sql);

	$com_id_arr[$com_id]['com_rootid'] = $com_rootid;
	$com_itemid_arr[$com_itemid][]     = $com_id;

}

echo "<br>";

$sql2 = "SELECT com_itemid, count(com_itemid) FROM $table_com WHERE com_modid=$mid GROUP BY com_itemid ";
$res2 = sql_exec($sql2);

while ( $row2 = $xoopsDB->fetchRow($res2) )
{
	$itemid = $row2[0];
	$count  = $row2[1];

	$sql = "UPDATE $table_link SET comments=$count WHERE lid=$itemid";

	echo "$sql <br>";
	sql_exec($sql);
}

echo "<h3>end</h3>";
xoops_cp_footer();
exit();
// ---


function get_randum_char()
{
	return chr( rand(97,122) );
}

function get_randum_time()
{
	$time = time() - rand(0, 365*24*60*60);	// 1 year
	return $time;
}

function get_randum_ip()
{
	$ip = "192.168.1." . rand(1,255);
	return $ip;
}

function sql_exec($sql)
{ 
	global $xoopsDB;

	$ret = $xoopsDB->queryF($sql);
	if ($ret != false ) { return $ret; }

	$error = $xoopsDB->error();
	echo "<font color=red>$sql<br>$error</font><br>";

	return false;
}


?>