cid = mysqli_connect($host, $user, $password, $name);
if(!$this->cid)
self::error();
$this->query("SET NAMES UTF8");
}
public function query($query){
return mysqli_query($this->cid, $query);
}
public function queryArr($query){
return $this->getArrFromQuery($this->query($query));
}
public function queryFetch($query){
return $this->fetch_array($this->query($query));
}
public function fetch_array($fetch_array){
return mysqli_fetch_array($fetch_array);
}
public function escape($escape_string){
if(!is_null($escape_string)){
return mysqli_real_escape_string($this->cid, $escape_string);
}else{
return null;
}
}
private function getArrFromQuery($dbreq){
$i=0;
$result=array();
while ($dbres = $this->fetch_array($dbreq)) {
foreach (array_keys($dbres) as $col) {
$result[$i][$col] = $dbres[$col];
}
$i++;
}
return $result;
}
private function error(){
if (mysqli_errno($this->cid) != 0){
die("
\nMySQL error: ".mysqli_errno($this->cid).": ".mysqli_error($this->cid)."
\n");
}
}
public function disconnect(){
return mysqli_close($this->cid);
}
}
define("MYSQL_HOST", "localhost");
define("MYSQL_USER", "");
define("MYSQL_PASS", "");
define("MYSQL_NAME", "owncloud");
$db = new MySQL_Connector(MYSQL_HOST,MYSQL_USER,MYSQL_PASS,MYSQL_NAME);
$fullDB = array();
$shouldDeletedIDs = array();
// Get all double contacts, separated in address-books
$q = $db->query("
SELECT
b.id,
b.addressbookid,
b.fullname
FROM
(
SELECT
x.addressbookid,
x.fullname
FROM
(
SELECT
COUNT(*) AS cnt,
fullname,
addressbookid
FROM
oc_contacts_cards
GROUP BY
addressbookid, fullname
) x
WHERE
x.cnt > 1
) a
RIGHT JOIN oc_contacts_cards b ON a.addressbookid=b.addressbookid
WHERE
a.fullname = b.fullname
ORDER BY
b.addressbookid ASC,
b.fullname ASC,
b.id ASC
");
/* Build up internal database with all possible contacts
*
* $fullDB[][][
* idList -> List of IDs for this full-name
* card -> List of all contact-cards
* ]
*/
while($elem = $db->fetch_array($q)){
if(empty($elem['fullname']))
continue;
if(!isset($fullDB[$elem['addressbookid']])){
$fullDB[$elem['addressbookid']] = array(
$elem['fullname'] => array(
"idList" => array(),
"card" => array()
)
);
}
if(!isset($fullDB[$elem['addressbookid']][$elem['fullname']])){
$fullDB[$elem['addressbookid']][$elem['fullname']] = array(
"idList" => array(),
"card" => array()
);
}
if(!in_array($elem['id'],$fullDB[$elem['addressbookid']][$elem['fullname']]['idList'])){
$fullDB[$elem['addressbookid']][$elem['fullname']]['idList'][] = $elem['id'];
}
}
// Build up list of contact-cards and selecting ids, who can be deleted
foreach($fullDB as $addressbook=>$names){
foreach($names as $name=>$nameData){
foreach($nameData['idList'] as $id){
$q = $db->query("
SELECT
*
FROM
oc_contacts_cards_properties
WHERE
contactid = ".$id."
");
// Get the actual contact-card, except the UID-Field
$card = array();
while($elem = $db->fetch_array($q)){
if($elem['name']=="UID")
continue;
$card[$elem['name']] = $elem['value'];
}
// If no contact-card exist, add them to the local DB and continue
if($nameData['card'] == array()){
foreach($card as $cardKey=>$cardValue){
$nameData['card'][0][$cardKey] = $cardValue;
}
continue;
}
// Check, if the actual contact-card exist for this contact
$cardAlreadyExist = false;
$tmpNameData = array();
for($i=0;$i$cardValue){
if(!isset($card[$cardKey]) || $card[$cardKey]!=$cardValue){
$curCardAlreadyExist = false;
}
}
}else{
$curCardAlreadyExist = false;
}
if($curCardAlreadyExist){
$cardAlreadyExist = true;
break;
}
}
// If exist, delete. If not, add it to the local DB
if($cardAlreadyExist){
$shouldDeletedIDs[] = $id;
}else{
$arr = array();
foreach($card as $cardKey=>$cardValue){
$arr[$cardKey] = $cardValue;
}
$nameData['card'][] = $arr;
}
}
}
}
$cnt = count($shouldDeletedIDs);
$lst = implode(',',$shouldDeletedIDs);
$db->query("DELETE FROM oc_contacts_cards WHERE id IN (".$lst.")");
$db->query("DELETE FROM oc_contacts_cards_properties WHERE contactid IN (".$lst.")");
echo "Es wurden ".$cnt." doppelte Kontakte gelöscht.";