Current File : /home/lifechur/carp_evolution_4.1.8/fix-mysql-urls.php
<html>
<head>
	<title>Fix CaRP's MySQL Plugin's URL Values</title>
	<?php
	
	require_once '/REPLACE/THIS/WITH/YOUR/PATH/TO/carp/carp.php';
	
	// Enter your MySQL database information here:
	
	$username='';
	$password='';
	$dbname='';
	$host='localhost'; // Most of the time, you won't need to change this

	CarpLoadPlugin('mysql.php');
	
	// If you're not using the default table and/or field names for the
	// MySQL plugin's tables, enter the code used to override the default
	// values here:
	
	
	?>
</head>
<body>
<h1>Fix CaRP's MySQL Plugin's URL Values</h1>

<?php
$auxfields=&$mysqlcarpconf['auxfields'];

MysqlCarpConf('connect',0);
mysql_connect($host,$username,$password);
mysql_select_db($dbname);

if (isset($_POST['url'])) {
	$old_id=$new_id=-1;
	if ($r=mysql_query('select '.$auxfields['feed_id'].' from '.$mysqlcarpconf['feedtable'].
		' where '.$auxfields['real_url'].'="'.$_POST['url'].'"')
	) {
		if ($l=mysql_fetch_array($r)) $new_id=$l[$auxfields['feed_id']];
		mysql_free_result($r);
	} else echo 'Database error checking for updated record for this feed.<br />';
	if ($r=mysql_query('select '.$auxfields['feed_id'].' from '.$mysqlcarpconf['feedtable'].
		' where '.$auxfields['real_url'].' like "%autocache/'.md5(stripslashes($_POST['url'])).'"')
	) {
		if ($l=mysql_fetch_array($r)) $old_id=$l[$auxfields['feed_id']];
		mysql_free_result($r);
	} else echo 'Database error checking for old record for this feed.<br />';

	if ($old_id>-1) {
		if (mysql_query('update '.$mysqlcarpconf['feedtable'].' set '.
			$auxfields['real_url'].'="'.$_POST['url'].'",'.$auxfields['url'].'="'.md5(stripslashes($_POST['url'])).'" '.
			'where '.$auxfields['feed_id'].'='.$old_id)
		) {
			if ($new_id==-1) { // check again just in case some other process just created this record!
				sleep(1);
				if ($r=mysql_query('select '.$auxfields['feed_id'].' from '.$mysqlcarpconf['feedtable'].
					' where ('.$auxfields['real_url'].'="'.$_POST['url'].'")&&('.$auxfields['feed_id'].'!='.$old_id.')')
				) {
					if ($l=mysql_fetch_array($r)) $new_id=$l[$auxfields['feed_id']];
					mysql_free_result($r);
				} else echo 'Database error double-checking for updated record for this feed.<br />';
			}
			if ($new_id>-1) {
				if (mysql_query('delete from '.$mysqlcarpconf['feedtable'].' where '.$auxfields['feed_id'].'='.$new_id)) {
					if ($r=mysql_query('select newf.'.$auxfields['item_id'].' as newid,oldf.'.$auxfields['item_id'].' as oldid '.
						'from '.$mysqlcarpconf['itemtable'].' as newf left join '.$mysqlcarpconf['itemtable'].' as oldf '.
						'on (oldf.'.$auxfields['feed_id'].'='.$old_id.')&&(newf.'.$auxfields['checksum'].'=oldf.'.$auxfields['checksum'].') '.
						'where newf.'.$auxfields['feed_id'].'='.$new_id)
					) {
						while ($l=mysql_fetch_array($r)) {
							if (isset($l['oldid'])&&isset($l['oldid']{0})) {
								if (!mysql_query('delete from '.$mysqlcarpconf['itemtable'].' where '.$auxfields['item_id'].'='.$l['newid']))
									echo 'Database error deleting duplicate item.<br />';
							} else if (!mysql_query('update '.$mysqlcarpconf['itemtable'].' set '.$auxfields['feed_id'].'='.$old_id.
								' where '.$auxfields['item_id'].'='.$l['newid']))
									echo 'Database error merging item.<br />';
						}
						mysql_free_result($r);
					} else echo 'Database error checking for items needing to be merged.<br />';				
				} else echo 'Database error deleting duplicate record for this feed.<br />';
			}
		} else echo 'Database error attempting to update record for that feed.<br />';
		
	} else echo 'Old record for that URL not found.<br />';
}

if ($r=mysql_query('select '.$auxfields['feed_id'].' from '.$mysqlcarpconf['feedtable'].
	' where !('.$auxfields['real_url'].' like "http%")')
) {
	if (mysql_num_rows($r)) {
		echo 'The feeds with the following IDs have not been fixed:';
		while ($l=mysql_fetch_array($r)) echo ' '.$l[$auxfields['feed_id']];
		?>
		<form action="fix-mysql-urls.php" method="post">
		Enter the URL of any one of the feeds:<br />
		<input name="url" size="40" />
			<input type="submit" value="Fix It" />
		</form>
		<?php
	} else echo 'All feeds have been fixed.<br />';
	mysql_free_result($r);
} else echo 'Database error attempting to find un-fixed feeds.<br />';
?>

</body>
</html>