Jeg har lavet et script som henter en masse data omkring WOW spiller.
Men når jeg køre det blive PHP ved med at crashe, og jeg kan ikke lige se hvorfor det gør det
Jeg køre programmet i Windows via en dos promt!
Måske ser mit program ikke så stort ud! men det skal håndtere kæmpe mængder af data.
Programmet henter nemlig alle bruger fra
http://wow.allakhazam.com/db/guildroster.htmlOg lægger dem ind i min database. Så hvis en af jer har en ide til hvad jeg kan forbædre for at kunne gøre det hurtiger, bare sig til
*-------- run.php ---------*
<?PHP
set_time_limit(0);
require "../include/function.php";
require "../include/db_interface.php";
global $update_time;
$update_time = time();
//Connecting to the database
db_connect("xxxxx","xxxxx","wow_chars");
//Cleaning up
If(file_exists("run.log"))unlink("run.log");
mysql_query("TRUNCATE TABLE `guilds`");
mysql_query("TRUNCATE TABLE `servers`");
//Setting script up
printlog("STARTING SCRIPT");
$retry['max']="2"; //How meny times it shale retry a connection
$retry['count'] = "0";
//Set to 1 for debug mode
$debug = "0";
//Gelts alle the servers
$link="http://wow.allakhazam.com/db/guildroster.html";
printlog("Connecting to ->".$link);
$sourcefile = implode('',file($link));
if(!$sourcefile)printlog("Connecting to ->".$link." faild");
//Splicing the data
$itall = splicedata($sourcefile, "Guilds</a></td></tr><tr>", "</div>");
unset($sourcefile);
//cleaning a bit up so it is easyer to explode
$itall = str_replace("</td>", "<br>", $itall);
$itall = strip_tags($itall, '<b><br>');
$itall = explode("<br>", $itall);
//Prosessing data
$serverid = "-1";
global $serverid,$servername,$guild;
//Looping the server and saving them to an array
for($i="0";$i<=count($itall)-2;$i++){
my_bar("Getting servers",$i+1,count($itall)-1); // For the prosses bar in the dos promt
if(!is_numeric($itall[$i]) && strpos(strtolower($itall[$i]), "test") === false && $itall[$i]){ //We wont have ppl's teste in oure stats!
$serverid++;
$servers[$serverid] = $itall[$i];
//Saving to the db
unset($db_name,$db_var);
$db_name[0]="server_name";
$db_var[0]=$itall[$i];
db_save("servers",$db_name,$db_var);
printlog("SERVER: ".$itall[$i]);
}
}
printlog("SERVER COUNT: ".count($servers));
// Loading debug mode
if($debug=="1"){ $count = "3"; }else{ $count = count($servers)-1; }
//Looping all the servers
$retry['count']="0";
$guildid = "-1";
for($o="0";$o<=$count;$o++){
if($retry['count']<=$retry['max']+1)$retry['count']="0";
// For the prosses bar in the dos promt
my_bar("Getting guilds",$o+1,$count);
//Gelts alle the guilds of each server
$link="http://wow.allakhazam.com/db/guildroster.html?server=".str_replace(" ", "%20",$servers[$o]);
printlog("Connecting to ->".$link);
$sourcefile = implode('',file($link));
if(!$sourcefile){
printlog("Connecting to ->".$link." faild");
if($retry['count']<=$retry['max']){
$o--; //Son in this house do we not accept falier
$retry['count']++;
}
}
//Splicing the data
$itall = splicedata($sourcefile, "Members</td></tr><tr>", "</div>");
unset($sourcefile);
//cleaning a bit up so it is easyer to explode
$itall = str_replace("</td>", "<br>", $itall);
$itall = strip_tags($itall, '<b><br>');
$itall = explode("<br>", $itall);
//Prosessing data
global $serverid,$servername,$guild;
//For the guild conter
$t_count = "0";
//Looping the guilds
for($i="0";$i<=count($itall)-2;$i++){
if(!is_numeric($itall[$i]) && strpos(strtolower($itall[$i]), "test") === false && $itall[$i] && $itall[$i] != "<b>".$servers[$o]."</b>:"){ //We wont have ppl's teste in oure stats!
$t_count++;
$guildid++;
$guild[$guildid][name] = $itall[$i];
$guild[$guildid][server] = $servers[$o];
//Saving to the db
unset($db_name,$db_var);
$db_name[0]="guild_name";
$db_var[0]=$itall[$i];
$db_name[1]="guild_server";
$db_var[1]=$servers[$o];
db_save("guilds",$db_name,$db_var);
//Saving to the database
printlog("Guild: ".$itall[$i]);
}
}
$_count = $i+1;
printlog("GUILD COUNT: ".$t_count);
}
printlog("TOTAL GUILD COUNT: ".count($guild));
//Getting all the guilds now
$retry['count']="0";
for($i="0";$i<=count($guild)-1;$i++){
// For the prosses bar in the dos promt
my_bar("Getting guild members",$i+1,count($guild));
//Gelts alle the guilds of each server
$link = "http://wow.allakhazam.com/cluster/guild-roster.pl?xml=1&g=".str_replace(" ", "%20",$guild[$i][name])."&s=".str_replace(" ", "%20",$guild[$i][server]);
printlog("Connecting to ->".$link);
$sourcefile = implode('',file($link));
if(!$sourcefile) {
printlog("Connecting to ->".$link." faild");
if($retry['count']<=$retry['max']){
$i--; //Son in this house do we not accept falier
$retry['count']++;
}
}
$itall = splicemultidata($sourcefile, "<member>", "</member>");
for($o="1";$o<=count($itall)-1;$o++){
$user[$user_n]['alla_id'] = splicedata($itall[$o], "<profile>http://wow.allakhazam.com/profile.html?", "</profile>");
$user[$user_n]['name'] = splicedata($itall[$o], "<name>", "</name>");
$user[$user_n]['lvl'] = splicedata($itall[$o], "<level>", "</level>");
$user[$user_n]['race'] = splicedata($itall[$o], "<race>", "</race>");
$user[$user_n]['class'] = splicedata($itall[$o], "<class>", "</class>");
$user[$user_n]['rank'] = splicedata($itall[$o], "<guildtitle>", "</guildtitle>");
$user[$user_n]['server'] = $guild[$i][server];
$user[$user_n]['guild'] = $guild[$i][name];
printlog("Putting user into array ->".$user[$user_n]['name']);
}
}
// adding users to the database
for($i="0";$i<=count($user)-1;$i++){
my_bar("Adding data to sql",$i+1,count($user));
$users_id = is_char_added($user[$i]['alla_id'],$sql_users);
printlog($user[$i]['name']."'s id is ".$users_id);
if($users_id){
printlog("UPDATING->".$user[$i]['name']);
unset($db_name,$db_var);
$db_name[0]="user_name";
$db_var[0]=$user[$i]['name'];
$db_name[1]="user_guild";
$db_var[1]=$user[$i]['guild'];
$db_name[2]="user_server";
$db_var[2]=$user[$i]['server'];
$db_name[3]="user_update";
$db_var[3]=$update_time;
$db_name[4]="user_alla_id";
$db_var[4]=$user[$i]['alla_id'];
db_update("users","user_id",$users_id,$db_name,$db_var);
}else{
printlog("ADDING->".$user[$i]['name']);
unset($db_name,$db_var);
$db_name[0]="user_name";
$db_var[0]=$user[$i]['name'];
$db_name[1]="user_guild";
$db_var[1]=$user[$i]['guild'];
$db_name[2]="user_server";
$db_var[2]=$user[$i]['server'];
$db_name[3]="user_update";
$db_var[3]=$update_time;
$db_name[4]="user_alla_id";
$db_var[4]=$user[$i]['alla_id'];
$db_name[5]="user_add";
$db_var[5]=$update_time;
db_save("users",$db_name,$db_var);
$sql = "SELECT user_id FROM `users` WHERE `user_alla_id` = '".$user[$i]['alla_id']."'";
$foresp = mysql_query($sql) or die (mysql_error());
$ja = mysql_num_rows($foresp);
IF ($ja == 0){} else {
while($data = mysql_fetch_array($foresp)){
$users_id = $data['user_id'];
printlog("NEW USER ID->".$users_id);
}
}
}
$sql = "SELECT * FROM `update` WHERE `up_user_id` = '".$users_id."' ORDER BY `up_date` DESC LIMIT 0,1";
$foresp = mysql_query($sql) or die (mysql_error());
$ja = mysql_num_rows($foresp);
IF ($ja == 0){
// This only happens if it are the first tile the script sees the user
printlog("Adding NEW to update->".$user[$i]['name']);
unset($db_name,$db_var);
$db_name[0]="up_user_id";
$db_var[0]=$users_id;
$db_name[1]="up_lvl";
$db_var[1]=$user[$i]['lvl'];
$db_name[2]="up_race";
$db_var[2]=$user[$i]['race'];
$db_name[3]="up_class";
$db_var[3]=$user[$i]['class'];
$db_name[6]="up_guild";
$db_var[6]=$user[$i]['guild'];
$db_name[4]="up_rank";
$db_var[4]=$user[$i]['rank'];
$db_name[5]="up_date";
$db_var[5]=$update_time;
db_save("update",$db_name,$db_var);
} else {
while($data = mysql_fetch_array($foresp)){
if($user[$i]['lvl']!=$data['up_lvl']||$user[$i]['race']!=$data['up_race']||$user[$i]['class']!=$data['up_class']||$user[$i]['rank']!=$data['up_rank']){
//If there have been changes
printlog("Adding to update->".$user[$i]['name']);
unset($db_name,$db_var);
$db_name[0]="up_user_id";
$db_var[0]=$users_id;
$db_name[1]="up_lvl";
$db_var[1]=$user[$i]['lvl'];
$db_name[2]="up_race";
$db_var[2]=$user[$i]['race'];
$db_name[3]="up_class";
$db_var[3]=$user[$i]['class'];
$db_name[6]="up_guild";
$db_var[6]=$user[$i]['guild'];
$db_name[4]="up_rank";
$db_var[4]=$user[$i]['rank'];
$db_name[5]="up_date";
$db_var[5]=$update_time;
db_save("update",$db_name,$db_var);
}else{
//If there have been no changens
printlog("No change->".$user[$i]['name']);
}
}
}
}
//Getting not updated users
printlog("Getting users who has not been updated");
$sql = "SELECT * FROM users WHERE user_update != '".$up_time."'";
$foresp = mysql_query($sql) or die (mysql_error());
$ja = mysql_num_rows($foresp);
IF ($ja == 0){} else {
while($data = mysql_fetch_array($foresp)){
$user_[] = $data['user_name']."||".$data['user_id']."||".$data['user_alla_id'];
}
}
printlog("Getting users done");
$retry['count']="0";
for($i="0";$i<=count($user_)-1;$i++){
my_bar("Downloading user information",$i+1,count($user_));
$_temp = explode("||", $user_[$i]);
$_user['name'] = $_temp[0];
$_user['id'] = $_temp[1];
$_user['alla_id'] = $_temp[2];
$link="http://wow.allakhazam.com/profile.xml?".$_user['alla_id'];
printlog("Connecting to -> ".$link);
$sourcefile = implode('',file($link));
if(!$sourcefile){
printlog("Connecting to ->".$link." faild");
if($retry['count']<=$retry['max']){
$i--; //Son in this house do we not accept falier
$retry['count']++;
}
}
$user_n++;
$not_user[$i]['alla_id']= $_user['alla_id'];
$not_user[$i]['name'] = $_user['name'];
$not_user[$i]['lvl'] = splicedata($sourcefile, "<level>", "</level>");
$not_user[$i]['race'] = splicedata($sourcefile, "<race>", "</race>");
$not_user[$i]['class'] = splicedata($sourcefile, "<class>", "</class>");
$not_user[$i]['rank'] = splicedata($sourcefile, "<gtitle>", "</gtitle>");
$not_user[$i]['server'] = splicedata($sourcefile, "<server>", "</server>");
$not_user[$i]['guild'] = splicedata($sourcefile, "<guild>", "</guild>");
}
for($i="0";$i<=count($not_user)-1;$i++){
my_bar("Adding not updated users to sql",$i+1,count($not_user));
$users_id = is_char_added($not_user[$i]['alla_id'],$sql_users);
printlog($not_user[$i]['name']."'s id is ".$users_id);
if($users_id){
printlog("UPDATING->".$not_user[$i]['name']);
unset($db_name,$db_var);
$db_name[0]="user_name";
$db_var[0]=$not_user[$i]['name'];
$db_name[1]="user_guild";
$db_var[1]=$not_user[$i]['guild'];
$db_name[2]="user_server";
$db_var[2]=$not_user[$i]['server'];
$db_name[3]="user_update";
$db_var[3]=$update_time;
$db_name[4]="user_alla_id";
$db_var[4]=$not_user[$i]['alla_id'];
db_update("users","user_id",$users_id,$db_name,$db_var);
}else{
printlog("ADDING->".$not_user[$i]['name']);
unset($db_name,$db_var);
$db_name[0]="user_name";
$db_var[0]=$not_user[$i]['name'];
$db_name[1]="user_guild";
$db_var[1]=$not_user[$i]['guild'];
$db_name[2]="user_server";
$db_var[2]=$not_user[$i]['server'];
$db_name[3]="user_update";
$db_var[3]=$update_time;
$db_name[4]="user_alla_id";
$db_var[4]=$not_user[$i]['alla_id'];
$db_name[5]="user_add";
$db_var[5]=$update_time;
db_save("users",$db_name,$db_var);
$sql = "SELECT user_id FROM `users` WHERE `user_alla_id` = '".$not_user[$i]['alla_id']."'";
$foresp = mysql_query($sql) or die (mysql_error());
$ja = mysql_num_rows($foresp);
IF ($ja == 0){} else {
while($data = mysql_fetch_array($foresp)){
$users_id = $data['user_id'];
printlog("NEW USER ID->".$users_id);
}
}
}
$sql = "SELECT * FROM `update` WHERE `up_user_id` = '".$users_id."' ORDER BY `up_date` DESC LIMIT 0 , 1";
$foresp = mysql_query($sql) or die (mysql_error());
$ja = mysql_num_rows($foresp);
IF ($ja == 0){
// This only happens if it are the first tile the script sees the user
printlog("Adding NEW to update->".$not_user[$i]['name']);
unset($db_name,$db_var);
$db_name[0]="up_user_id";
$db_var[0]=$users_id;
$db_name[1]="up_lvl";
$db_var[1]=$not_user[$i]['lvl'];
$db_name[2]="up_race";
$db_var[2]=$not_user[$i]['race'];
$db_name[3]="up_class";
$db_var[3]=$not_user[$i]['class'];
$db_name[6]="up_guild";
$db_var[6]=$not_user[$i]['guild'];
$db_name[4]="up_rank";
$db_var[4]=$not_user[$i]['rank'];
$db_name[5]="up_date";
$db_var[5]=$update_time;
db_save("update",$db_name,$db_var);
} else {
while($data = mysql_fetch_array($foresp)){
if($not_user[$i]['lvl']!=$data['up_lvl']||$not_user[$i]['race']!=$data['up_race']||$not_user[$i]['class']!=$data['up_class']||$not_user[$i]['rank']!=$data['up_rank']){
//If there have been changes
printlog("Adding to update->".$not_user[$i]['name']);
unset($db_name,$db_var);
$db_name[0]="up_user_id";
$db_var[0]=$users_id;
$db_name[1]="up_lvl";
$db_var[1]=$not_user[$i]['lvl'];
$db_name[2]="up_race";
$db_var[2]=$not_user[$i]['race'];
$db_name[3]="up_class";
$db_var[3]=$not_user[$i]['class'];
$db_name[6]="up_guild";
$db_var[6]=$not_user[$i]['guild'];
$db_name[4]="up_rank";
$db_var[4]=$not_user[$i]['rank'];
$db_name[5]="up_date";
$db_var[5]=$update_time;
db_save("update",$db_name,$db_var);
}else{
//If there have been no changens
printlog("No change->".$not_user[$i]['name']);
}
}
}
}
//saveing stats to database
printlog("Saving stats");
unset($db_name,$db_var);
$db_name[]="ex_time";
$db_var[]=time()-$update_time;
$db_name[]="users";
$db_var[]=count($user);
$db_name[]="servers";
$db_var[]=count($servers);
$db_name[]="guilds";
$db_var[]=count($guild);
$db_name[]="last_update";
$db_var[]=time();
db_update("stats","id","1",$db_name,$db_var);
// Closing connection to the database
db_close();
printlog("SCRIPT DONE");
my_bar("Done",1,1);
?>
*-------- functions.php -----------*
<?PHP
function write_file($file,$name,$new="0"){
IF(file_exists($name)){
if($new=="0"){
$fild_old = "";
$fil = fopen($name, "r");
while (!feof($fil)){
$streng = fgets($fil, "4096");
$fild_old .= $streng;
}
$file = $fild_old.$file;
}
}else{
touch($name);
}
$fil = fopen($name, "w");
fwrite($fil, $file);
fclose($fil);
}
function read_file($name){
IF(file_exists($name)){
$fild_old = "";
$fil = fopen($name, "r");
while (!feof($fil)){
$streng = fgets($fil, "4096");
$fild_old .= $streng;
}
fclose($fil);
}
return $fild_old;
}
function splicedata($sourcefile, $start, $end)
{
error_reporting(0);
$temp=split($start, $sourcefile);
$temp=split($end, $temp[1]);
error_reporting(1);
return $temp[0];
}
function splicemultidata($sourcefile, $start, $end)
{
error_reporting(0);
$targetdata = array();
$sourcedata = split($start, $sourcefile);
foreach ($sourcedata as $indsplit)
{
$target = split($end, $indsplit);
$targetdata[] = $target[0];
}
error_reporting(1);
return $targetdata;
}
//functions for the get chars stats
function printlog($text){
if($text=="SCRIPT DONE"){
write_file("-------- SCRIPT HAS BEEN ENDED AT ".date("d M Y H:i", time())." ------- \\n\\n\\n","run.log");
}elseif($text=="STARTING SCRIPT"){
write_file("-------- SCRIPT HAS BEEN STARTED AT ".date("d M Y H:i", time())." ------ \\n","run.log");
}else{
write_file("[".date("d M Y H:i", time())."] ".$text."\\n","run.log");
}
}
function my_bar($text,$nr="",$max=""){
echo "\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n";
$p = $max/100;
$now = $nr/$p;
echo "[".date("d M Y H:i", time())."] \\n";
echo round($now,1)."% ".$nr."/".$max."\\n Now running: ".$text."\\n|";
$times = round($now, -1)/10;
for($i=1;$i<=$times;$i++){
echo "*";
}
for($i=1;$i<=10-$times;$i++){
echo "-";
}
echo "|";
}
function is_char_added($user,$users){ // tjekking if the users already are added to oure database
for($i="0";$i<=count($users);$i++){
if($user==$users[$i]['alla_id']){
return $users[$i]['user_id'];
$i=count($users)+1; // time saver
}else{
}
}
}
function get_all_users(){
$sql = "SELECT user_alla_id, user_name, user_id FROM users";
$foresp = mysql_query($sql) or die (mysql_error());
$ja = mysql_num_rows($foresp);
IF ($ja == 0){} else {
while($data = mysql_fetch_array($foresp)){
$sql_users[$data[user_id]]['alla_id'] = $data['user_alla_id'];
$sql_users[$data[user_id]]['user_name'] = $data['user_name'];
$sql_users[$data[user_id]]['user_id'] = $data['user_id'];
}}
return $sql_users;
}
function get_all_servers(){
$sql = "SELECT server_name, server_id FROM servers";
$foresp = mysql_query($sql) or die (mysql_error());
$ja = mysql_num_rows($foresp);
IF ($ja == 0){} else {
while($data = mysql_fetch_array($foresp)){
$sql_users[$data[server_id]]['server_id'] = $data['server_id'];
$sql_users[$data[server_id]]['server_name'] = $data['server_name'];
}}
return $sql_users;
}
function is_server_added($server,$servers){ // tjekking if the users already are added to oure database
for($i="0";$i<=count($servers)-1;$i++){
if($server==$servers[$i]['server_name']){
return $servers[$i]['server_id'];
$i=count($servers)+1; // time saver
}else{
}
}
}
function get_all_guilds(){
$sql = "SELECT guild_name, guild_id FROM guilds";
$foresp = mysql_query($sql) or die (mysql_error());
$ja = mysql_num_rows($foresp);
IF ($ja == 0){} else {
while($data = mysql_fetch_array($foresp)){
$sql_users[$data[guild_id]]['guild_id'] = $data['guild_id'];
$sql_users[$data[guild_id]]['guild_name'] = $data['guild_name'];
}}
return $sql_users;
}
function is_guild_added($guild,$guilds){ // tjekking if the users already are added to oure database
for($i="0";$i<=count($guilds);$i++){
if($guild==$guilds[$i]['guild_name']){
return $guilds[$i]['guild_id'];
$i=count($guilds)+1; // time saver
}else{
}
}
}
?>
PÅ forhånd tak!
----------------------
DoomStone@DoomStone.dk