Contacts: Rewrote import script.

This commit is contained in:
Thomas Tanghus 2012-05-29 23:40:26 +02:00
parent fbe58755e5
commit dbcd26be68
1 changed files with 26 additions and 57 deletions

View File

@ -11,6 +11,7 @@ ob_start();
OCP\JSON::checkLoggedIn(); OCP\JSON::checkLoggedIn();
OCP\App::checkAppEnabled('contacts'); OCP\App::checkAppEnabled('contacts');
$nl = "\n"; $nl = "\n";
$progressfile = 'import_tmp/' . md5(session_id()) . '.txt'; $progressfile = 'import_tmp/' . md5(session_id()) . '.txt';
function writeProgress($pct) { function writeProgress($pct) {
@ -48,78 +49,46 @@ if(isset($_POST['method']) && $_POST['method'] == 'new'){
OC_Contacts_App::getAddressbook($id); // is owner access check OC_Contacts_App::getAddressbook($id); // is owner access check
} }
//analyse the contacts file //analyse the contacts file
writeProgress('20'); writeProgress('40');
$searchfor = array('VCARD'); $lines = explode($nl, $file);
$parts = $searchfor;
$filearr = explode($nl, $file);
$inelement = false; $inelement = false;
$parts = array(); $parts = array();
$i = 0; $card = array();
foreach($filearr as $line){ foreach($lines as $line){
foreach($searchfor as $search){ if(strtoupper(trim($line)) == 'BEGIN:VCARD'){
if(substr_count($line, $search) == 1){ $inelement = true;
list($attr, $val) = explode(':', $line); } elseif (strtoupper(trim($line)) == 'END:VCARD') {
if($attr == 'BEGIN'){ $card[] = $line;
$parts[]['begin'] = $i; $parts[] = implode($nl, $card);
$inelement = true; $card = array();
} $inelement = false;
if($attr == 'END'){ }
$parts[count($parts) - 1]['end'] = $i; if ($inelement === true && trim($line) != '') {
$inelement = false; $card[] = $line;
}
}
} }
$i++;
} }
//import the contacts //import the contacts
writeProgress('40');
$start = '';
for ($i = 0; $i < $parts[0]['begin']; $i++) {
if($i == 0){
$start = $filearr[0];
}else{
$start .= $nl . $filearr[$i];
}
}
$end = '';
for($i = $parts[count($parts) - 1]['end'] + 1;$i <= count($filearr) - 1; $i++){
if($i == $parts[count($parts) - 1]['end'] + 1){
$end = $filearr[$parts[count($parts) - 1]['end'] + 1];
}else{
$end .= $nl . $filearr[$i];
}
}
writeProgress('50');
$importready = array();
foreach($parts as $part){
for($i = $part['begin']; $i <= $part['end'];$i++){
if($i == $part['begin']){
$content = $filearr[$i];
}else{
$content .= $nl . $filearr[$i];
}
}
$importready[] = $start . $nl . $content . $nl . $end;
}
writeProgress('70'); writeProgress('70');
if(count($parts) == 1){
$importready = array($file);
}
$imported = 0; $imported = 0;
$failed = 0; $failed = 0;
if(!count($importready) > 0) { if(!count($parts) > 0) {
OCP\JSON::error(array('message' => 'No contacts to import in .'.$_POST['file'].' Please check if the file is corrupted.')); OCP\JSON::error(array('message' => 'No contacts to import in .'.$_POST['file'].' Please check if the file is corrupted.'));
exit(); exit();
} }
foreach($importready as $import){ foreach($parts as $part){
$card = OC_VObject::parse($import); $card = OC_VObject::parse($part);
if (!$card) { if (!$card) {
$failed += 1; $failed += 1;
OCP\Util::writeLog('contacts','Import: skipping card. Error parsing VCard: '.$import, OCP\Util::ERROR); OCP\Util::writeLog('contacts','Import: skipping card. Error parsing VCard: '.$part, OCP\Util::ERROR);
continue; // Ditch cards that can't be parsed by Sabre. continue; // Ditch cards that can't be parsed by Sabre.
} }
$imported += 1; try {
OC_Contacts_VCard::add($id, $card); OC_Contacts_VCard::add($id, $card);
$imported += 1;
} catch (Exception $e) {
OCP\Util::writeLog('contacts', 'Error importing vcard: '.$e->getMessage().$nl.$card, OCP\Util::ERROR);
$failed += 1;
}
} }
//done the import //done the import
writeProgress('100'); writeProgress('100');