<?php

//ini_set("memory_limit","1024M");
//set_time_limit(60000);

// get data
// connect import database

$connection=mysql_connect('localhost', 'rf_aquaristik_ne', 'K+jQ?VC,Bp') or die ("Verbindungsversuch fehlgeschlagen");
mysql_select_db('rf_aquaristik_ne', $connection) or die("Konnte die Datenbank nicht waehlen.");
echo "get items";
$query = mysql_query('SELECT * FROM items') or die("Anfrage nicht erfolgreich");
$data = array();
echo "set uris";
while($obj = mysql_fetch_object($query)) {
	$short_uri = $obj->name;
	$short_uri = str_replace(array('ä', 'Ä', 'ö', 'Ö', 'ü', 'Ü', 'ß'), array('ae', 'Ae', 'oe', 'Oe', 'ue', 'Ue', 'ss'), $short_uri);
	$short_uri = preg_replace("@[^0-9aA-zZ\-]@si", "-", $short_uri);
	$short_uri = preg_replace("@(-{2,})@si", "-", $short_uri);
	$new_uri = '';
	$old_uri = $short_uri;
	$i = 0;
	while ($new_uri == '') {
		$sql = "SELECT id FROM items WHERE short_uri = '$short_uri' AND id != ".$obj->id;
		$result = mysql_query($sql);
		if (mysql_num_rows($result) == 0) {
			$new_uri = $short_uri;
		} else {
			$i++;
			$short_uri = $old_uri . '_' . $i;
		}
	}
	
	$sql = "
		UPDATE items
		SET
			short_uri='".$short_uri."'
		WHERE id=".$obj->id."
	";
	echo "<br>".$sql;
	mysql_query($sql);
	
}
echo "ende";
?>