<?php
 // Crontab Curl Collection of Mismatched ID Old and New WordPress Blog Postings - RJM Programming - April, 2015

 set_time_limit(0);

 $reptxt = "";
 if (file_exists("old_new_wp_mismatch_id.txt")) {
   $reptxt = @file_get_contents("old_new_wp_mismatch_id.txt");
 }
 date_default_timezone_set('Australia/Perth');
 $sdate = date('l jS \of F Y h:i:s A');

 $hostname = 'localhost'; 

 /*** mysql username ***/
 $username = 'username';

 /*** mysql ***/
 $password = 'password';

 $dbaltname = 'itblog';
 $dbname = 'rjmprogr_wordpress';
 $link = mysql_connect($hostname, $username, $password);
 if (!$link) {
    die('Could not connect: ' . mysql_error());
 }
for ($goes=1; $goes<=2; $goes++) {

 if ($goes == 2) {
 mysql_select_db($dbaltname);
 $res = mysql_query("SELECT MAX(ID) FROM itblog.wps_posts");
 if ($res == 0) {
  echo("<b>Error " . mysql_errno() . ": " . mysql_error() . "</b>");
 } else if (mysql_num_rows($res) == 0) {
  echo("<b>Query executed successfully</b>");
 } else {
  if (($r_array = mysql_fetch_row($res))) {
    $bigval = $r_array[0];
    $bigval++;
    mysql_select_db($dbname);
    $res = mysql_query("SELECT MAX(ID) FROM rjmprogr_wordpress.wp_posts");
    if ($res == 0) {
     echo("<b>Error " . mysql_errno() . ": " . mysql_error() . "</b>");
    } else if (mysql_num_rows($res) == 0) {
     echo("<b>Query executed successfully</b>");
    } else {
     if (($r_array = mysql_fetch_row($res))) {
       if ($r_array[0] >= $bigval) {
        $bigval = $r_array[0];
        $bigval++;
       }
     }
    }

  }
 }
 }
 mysql_select_db($dbname);
 $res = mysql_query("SELECT t1.ID, t2.ID, t1.post_title, t2.post_title, substr(t1.guid, instr(t1.guid, '/ITblog/')) as t1g, substr(t2.guid, instr(t2.guid, '/wordpress/')) as t2g
FROM itblog.wps_posts t1
JOIN rjmprogr_wordpress.wp_posts t2
ON t2.post_title = t1.post_title and t2.post_status in ('publish','future') and t1.post_status in ('publish','future')
WHERE t2.ID != t1.ID");
 if ($res == 0) {
  echo("<b>Error " . mysql_errno() . ": " . mysql_error() . "</b>");
 } else if (mysql_num_rows($res) == 0) {
  echo("<b>Query executed successfully</b>");
 } else {
  while (($r_array = mysql_fetch_row($res))) {
    if ($goes == 1) {
     $reptxt .= "UPDATE rjmprogr_wordpress.wp_posts SET ID=" . $r_array[0] . ", guid=replace(guid,'" . $r_array[5] . "','" . str_replace("/ITblog/", "/wordpress/", $r_array[4])  . "') WHERE ID=" . $r_array[1] . " and post_title='" . $r_array[2] . "' and '" . $sdate . "'='" . $sdate . "'\n";
    mysql_query("UPDATE rjmprogr_wordpress.wp_posts SET ID=" . $r_array[0] . ", guid=replace(guid,'" . $r_array[5] . "','" . str_replace("/ITblog/", "/wordpress/", $r_array[4])  . "') WHERE ID=" . $r_array[1] . " and post_title='" . $r_array[2] . "'");
   } else {
    mysql_select_db($dbname);
     $reptxt .= "UPDATE rjmprogr_wordpress.wp_posts SET ID=" . $bigval . ", guid=replace(guid,'=" . $r_array[1] . "','=" . $bigval . "') WHERE ID=" . $r_array[1] . " and post_title='" . $r_array[2] . "' and '" . $sdate . "'='" . $sdate . "'\n";
    mysql_query("UPDATE rjmprogr_wordpress.wp_posts SET ID=" . $bigval . ", guid=replace(guid,'=" . $r_array[1] . "','=" . $bigval . "') WHERE ID=" . $r_array[1] . " and post_title='" . $r_array[2] . "'");
   mysql_select_db($dbaltname);
    $reptxt .= "UPDATE itblog.wps_posts SET ID=" . $bigval . ", guid=replace(guid,'=" . $r_array[0] . "','=" . $bigval . "') WHERE ID=" . $r_array[0] . " and post_title='" . $r_array[2] . "' and '" . $sdate . "'='" . $sdate . "'\n";
    mysql_query("UPDATE itblog.wps_posts SET ID=" . $bigval  . ", guid=replace(guid,'=" . $r_array[0] . "','=" . $bigval . "') WHERE ID=" . $r_array[0] . " and post_title='" . $r_array[2] . "'");
    $bigval++;
   }
  }
 }
 if ($reptxt != "") {
    file_put_contents("old_new_wp_mismatch_id.txt", $reptxt);
 }
}
 mysql_close($link);


?>

