1
0
mirror of https://github.com/owncloudarchive/contacts.git synced 2024-11-28 10:24:11 +01:00

added standard vcard connector

cleaned code according to scrutinizer log
fixed import message on UI
This commit is contained in:
babelouest 2014-03-21 01:59:19 -04:00
parent c85b284208
commit d7f8b57025
7 changed files with 171 additions and 71 deletions

View File

@ -0,0 +1,77 @@
<?xml version='1.0' standalone='yes'?>
<!--
Parameters available
<import_core>
@brief: main parameters
<name></name>
@brief: Internal name
must match the xml file name with the pattern import_<name>_connector.xml
<display_name></display_name>
@brief: Display name
<type></type>
@brief: type (csv,vcard,ldif,...)
<active></active>
@brief: if 0, will not be used
<card_begin value=""/>
@brief: string delimiter to set the begining of a VCard
<card_end value=""/>
@brief: string delimiter to set the end of a VCard
<replace from="" to="" />
@brief: strings to replace in the source card before parsing
</import_core>
</vcard_entry>
</import_entry>
<import_entry name="" position="" enabled="" separator="" remove="">
@brief: vcard property to look for
property: property name
enabled: import_entry used or not
separator: string used to separate multiple values
remove: string to remove in the value
<group_entry property="" value="">
@brief: used to filter property search with group value
<vcard_favourites></vcard_favourites>
@brief: entry value to look for in order to add the current VCard to the Favourites section
<vcard_entry property="" type="" prefix=""/>
@brief: VCard entry to add
property: VCard property name
type: VCard property type
prefix: prefix to add to the value
<additional_property name="" value="" />
@brief: optional parameter to add
name: name of the parameter
value: value of the parameter
</import_entry>
-->
<!--
XML connector for Gmail VCard import
author: Nicolas Mora mail@babelouest.org
-->
<import_entries>
<import_core>
<name>vcard_standard</name>
<display_name>Standard VCard</display_name>
<type>vcard</type>
<active>1</active>
<card_begin>BEGIN:VCARD</card_begin>
<card_end>END:VCARD</card_end>
<replace from="http\://" to="http://" />
<replace from="https\://" to="https://" />
</import_core>
</import_entries>

View File

@ -293,7 +293,6 @@ OC.Contacts = OC.Contacts || {};
}
);
self.$importFileInput.fileupload('option', 'url', url);
//self.$importFileInput.attr('data-url', url);
}
});
this.$importFileInput.fileupload({
@ -304,10 +303,15 @@ OC.Contacts = OC.Contacts || {};
$('.import-upload').hide();
$('.import-status').show();
self.$importProgress.fadeIn();
self.$importStatusText.text(t('contacts', 'Uploading...'));
self.$importStatusText.text(t('contacts', 'Starting file import'));
},
done: function (e, data) {
self.$importStatusText.text(t('contacts', 'Importing...'));
if ($('#import_format').find('option:selected').val() != 'automatic') {
$('#import-status-text').text(t('contacts', 'Format selected: {format}',
{format: $('#import_format').find('option:selected').text() }));
} else {
$('#import-status-text').text(t('contacts', 'Automatic format detection'));
}
console.log('Upload done:', data);
self.doImport(self.storage.formatResponse(data.result, data.jqXHR));
},
@ -330,7 +334,12 @@ OC.Contacts = OC.Contacts || {};
AddressBookList.prototype.prepareImport = function(backend, addressBookId, importType, path, fileName) {
console.log('prepareImport', backend, addressBookId, importType, path, fileName);
this.$importProgress.progressbar({value:false});
this.$importStatusText.text(t('contacts', 'Preparing...'));
if (importType != 'automatic') {
this.$importStatusText.text(t('contacts', 'Format selected: {format}',
{format: self.$importFormatSelect.find('option:selected').val() }));
} else {
this.$importStatusText.text(t('contacts', 'Automatic format detection'));
}
return this.storage.prepareImport(
backend, addressBookId, importType,
{filename:fileName, path:path}
@ -338,7 +347,7 @@ OC.Contacts = OC.Contacts || {};
};
AddressBookList.prototype.doImport = function(response) {
console.log('doImport');
console.log('doImport', response);
var defer = $.Deferred();
var done = false;
var interval = null, isChecking = false;
@ -353,12 +362,10 @@ OC.Contacts = OC.Contacts || {};
if(self.$importProgress.hasClass('ui-progressbar')) {
self.$importProgress.progressbar('destroy');
}
}, 5000);
}, 3000);
};
if(!response.error) {
this.importCount = response.data.count;
this.$importProgress.progressbar('value', 0);
this.$importProgress.progressbar('option', 'max', this.importCount);
var data = response.data;
var getStatus = function(backend, addressbookid, importType, progresskey, interval, done) {
if(done) {
@ -377,9 +384,13 @@ OC.Contacts = OC.Contacts || {};
))
.then(function(response) {
if(!response.error) {
self.$importProgress.progressbar('value', Number(response.data.progress));
self.$importStatusText.text(t('contacts', 'Imported {count} of {total} contacts',
{count:response.data.progress, total: self.importCount}));
console.log('status, response: ', response);
if (response.data.total != null && response.data.progress != null) {
self.$importProgress.progressbar('option', 'max', Number(response.data.total));
self.$importProgress.progressbar('value', Number(response.data.progress));
self.$importStatusText.text(t('contacts', 'Processing {count}/{total} cards',
{count: response.data.progress, total: response.data.total}));
}
} else {
console.warn('Error', response.message);
self.$importStatusText.text(response.message);
@ -401,8 +412,8 @@ OC.Contacts = OC.Contacts || {};
console.log('response', response);
if(!response.error) {
console.log('Import done');
self.$importStatusText.text(t('contacts', 'Imported {imported} contacts. {failed} failed.',
{imported:response.data.imported, failed: response.data.failed}));
self.$importStatusText.text(t('contacts', 'Total:{total}, Success:{imported}, Errors:{failed}',
{total: response.data.total, imported:response.data.imported, failed: response.data.failed}));
var addressBook = self.find({id:response.data.addressBookId, backend: response.data.backend});
$(document).trigger('status.addressbook.imported', {
addressbook: addressBook

View File

@ -81,9 +81,9 @@ abstract class ImportConnector {
$separator=$importEntry->vcard_entry['separator'];
}
$position = $importEntry->vcard_entry['position'];
$v_array = explode($separator, $property);
$v_array[intval($position)] = StringUtil::convertToUTF8($value);
$property->setValue(implode($separator, $v_array));
$vArray = explode($separator, $property);
$vArray[intval($position)] = StringUtil::convertToUTF8($value);
$property->setValue(implode($separator, $vArray));
} else {
if (isset($importEntry->vcard_entry['value'])) {
$property->parameters[] = new \Sabre\VObject\Parameter('TYPE', ''.StringUtil::convertToUTF8($value));

View File

@ -88,7 +88,7 @@ class ImportCsvConnector extends ImportConnector {
}
$csv->setCsvControl($delimiter, "\"", "\\");
$ignore_first_line = (isset($this->configContent->import_core->ignore_first_line)
$ignoreFirstLine = (isset($this->configContent->import_core->ignore_first_line)
&& (((string)$this->configContent->import_core->ignore_first_line) == 'true')
|| ((string)$this->configContent->import_core->ignore_first_line) == '1');
@ -98,14 +98,14 @@ class ImportCsvConnector extends ImportConnector {
$index = 0;
foreach($csv as $line) {
if (!($ignore_first_line && $index == 0) && count($line) > 1) { // Ignore first line
if (!($ignoreFirstLine && $index == 0) && count($line) > 1) { // Ignore first line
$lines[] = $line;
if (count($lines) == $limit) {
break;
}
} else if ($ignore_first_line && $index == 0) {
} else if ($ignoreFirstLine && $index == 0) {
$titles = $line;
}
$index++;
@ -126,9 +126,10 @@ class ImportCsvConnector extends ImportConnector {
public function convertElementToVCard($element, $title = null) {
$vcard = \Sabre\VObject\Component::create('VCARD');
for ($i=0; $i < count($element); $i++) {
$nbElt = count($element);
for ($i=0; $i < $nbElt; $i++) {
if ($element[$i] != '') {
//error_log("element at $i, value:'".$element[$i]."'");
//$importEntry = false;
// Look for the right import_entry
if (isset($this->configContent->import_core->base_parsing)) {
if (strcasecmp((string)$this->configContent->import_core->base_parsing, 'position') == 0) {
@ -181,7 +182,8 @@ class ImportCsvConnector extends ImportConnector {
* @return int|false
*/
private function getImportEntryFromPosition($position) {
for ($i=0; $i < $this->configContent->import_entry->count(); $i++) {
$nbElt = $this->configContent->import_entry->count();
for ($i=0; $i < $nbElt; $i++) {
if ($this->configContent->import_entry[$i]['position'] == $position && $this->configContent->import_entry[$i]['enabled'] == 'true') {
return $this->configContent->import_entry[$i];
}
@ -195,7 +197,8 @@ class ImportCsvConnector extends ImportConnector {
* @return string|false
*/
private function getImportEntryFromName($name) {
for ($i=0; $i < $this->configContent->import_entry->count(); $i++) {
$nbElt = $this->configContent->import_entry->count();
for ($i=0; $i < $nbElt; $i++) {
if ($this->configContent->import_entry[$i]['name'] == StringUtil::convertToUTF8($name) && $this->configContent->import_entry[$i]['enabled'] == 'true') {
return $this->configContent->import_entry[$i];
}

View File

@ -61,16 +61,16 @@ class ImportLdifConnector extends ImportConnector{
$file = StringUtil::convertToUTF8(file_get_contents($file));
$nl = "\n";
$replace_from = array("\r","\n ");
$replace_to = array("\n","");
$replaceFrom = array("\r","\n ");
$replaceTo = array("\n","");
foreach ($this->configContent->import_core->replace as $replace) {
if (isset($replace['from']) && isset($replace['to'])) {
$replace_from[] = $replace['from'];
$replace_to[] = $replace['to'];
$replaceFrom[] = $replace['from'];
$replaceTo[] = $replace['to'];
}
}
$file = str_replace($replace_from, $replace_to, $file);
$file = str_replace($replaceFrom, $replaceTo, $file);
$lines = explode($nl, $file);
$parts = array();
@ -81,7 +81,7 @@ class ImportLdifConnector extends ImportConnector{
if(preg_match("/^\w+:: /",$line)) {
$kv = explode(':: ', $line, 2);
$key = $kv[0];
$value = base64_decode($kv[1]);
$value = base64_decode($kv[1], true);
} else {
$kv = explode(': ', $line, 2);
$key = $kv[0];
@ -132,28 +132,7 @@ class ImportLdifConnector extends ImportConnector{
}
foreach ($values as $oneValue) {
if (isset($importEntry->vcard_favourites)) {
foreach ($importEntry->vcard_favourites as $vcardFavourite) {
if (strcasecmp((string)$vcardFavourite, trim($oneValue)) == 0) {
$property = \Sabre\VObject\Property::create("X-FAVOURITES", 'yes');
$dest->add($property);
} else {
$property = $this->getOrCreateVCardProperty($dest, $importEntry->vcard_entry);
if (isset($importEntry['image']) && $importEntry['image'] == "true") {
$this->updateImageProperty($property, $oneValue);
} else {
$this->updateProperty($property, $importEntry, $oneValue);
}
}
}
} else {
$property = $this->getOrCreateVCardProperty($dest, $importEntry->vcard_entry);
if (isset($importEntry['image']) && $importEntry['image'] == "true") {
$this->updateImageProperty($property, $oneValue);
} else {
$this->updateProperty($property, $importEntry, $oneValue);
}
}
$this->convertElementToProperty($oneValue, $importEntry, $dest);
}
} else {
$property = \Sabre\VObject\Property::create("X-Unknown-Element", ''.StringUtil::convertToUTF8($ldifProperty[1]));
@ -166,6 +145,38 @@ class ImportLdifConnector extends ImportConnector{
return $dest;
}
/**
* @brief converts an LDIF element into a VCard property
* and updates the VCard
* @param $value the LDIF value
* @param $importEntry the VCard entry to modify
* @param $dest the VCard to modify (for adding a X-FAVOURITE property)
*/
private function convertElementToProperty($value, $importEntry, &$dest) {
if (isset($importEntry->vcard_favourites)) {
foreach ($importEntry->vcard_favourites as $vcardFavourite) {
if (strcasecmp((string)$vcardFavourite, trim($value)) == 0) {
$property = \Sabre\VObject\Property::create("X-FAVOURITES", 'yes');
$dest->add($property);
} else {
$property = $this->getOrCreateVCardProperty($dest, $importEntry->vcard_entry);
if (isset($importEntry['image']) && $importEntry['image'] == "true") {
$this->updateImageProperty($property, $value);
} else {
$this->updateProperty($property, $importEntry, $value);
}
}
}
} else {
$property = $this->getOrCreateVCardProperty($dest, $importEntry->vcard_entry);
if (isset($importEntry['image']) && $importEntry['image'] == "true") {
$this->updateImageProperty($property, $value);
} else {
$this->updateProperty($property, $importEntry, $value);
}
}
}
/**
* @brief tests if the property has to be translated by looking for its signature in the xml configuration
* @param $property Sabre VObject Property too look

View File

@ -61,16 +61,16 @@ class ImportVCardConnector extends ImportConnector{
$file = StringUtil::convertToUTF8(file_get_contents($file));
$nl = "\n";
$replace_from = array("\r","\n\n");
$replace_to = array("\n","\n");
$replaceFrom = array("\r","\n\n");
$replaceTo = array("\n","\n");
foreach ($this->configContent->import_core->replace as $replace) {
if (isset($replace['from']) && isset($replace['to'])) {
$replace_from[] = $replace['from'];
$replace_to[] = $replace['to'];
$replaceFrom[] = $replace['from'];
$replaceTo[] = $replace['to'];
}
}
$file = str_replace($replace_from, $replace_to, $file);
$file = str_replace($replaceFrom, $replaceTo, $file);
$lines = explode($nl, $file);
$inelement = false;
@ -150,10 +150,10 @@ class ImportVCardConnector extends ImportConnector{
* @param $vcard the parent Sabre VCard object to look for a
*/
private function getImportEntry($property, $vcard) {
for ($i=0; $i < $this->configContent->import_entry->count(); $i++) {
$nbElt = $this->configContent->import_entry->count();
for ($i=0; $i < $nbElt; $i++) {
if ($this->configContent->import_entry[$i]['property'] == $property->name && $this->configContent->import_entry[$i]['enabled'] == 'true') {
if (isset($this->configContent->import_entry[$i]->group_entry)) {
$toUnset = array();
$numElt = 0;
foreach($this->configContent->import_entry[$i]->group_entry as $groupEntry) {
$sourceGroupList = $vcard->select($groupEntry['property']);

View File

@ -28,8 +28,8 @@ class ImportController extends Controller {
public function upload() {
$request = $this->request;
$params = $this->request->urlParams;
$addressBookId = $params['addressBookId'];
$format = $params['importType'];
$addressBookId = $params['addressBookId'];
$format = $params['importType'];
$response = new JSONResponse();
$view = \OCP\Files::getStorage('contacts');
@ -81,18 +81,16 @@ class ImportController extends Controller {
\OC_FileProxy::$enabled = false;
if($view->file_put_contents('/imports/'.$filename, $content)) {
\OC_FileProxy::$enabled = $proxyStatus;
$count = substr_count($content, 'BEGIN:');
$progresskey = 'contacts-import-' . rand();
$response->setParams(
array(
'filename'=>$filename,
'count' => $count,
'progresskey' => $progresskey,
'backend' => $params['backend'],
'addressBookId' => $params['addressBookId']
'addressBookId' => $params['addressBookId'],
'importType' => $format
)
);
\OC_Cache::set($progresskey, '0', 300);
} else {
\OC_FileProxy::$enabled = $proxyStatus;
$response->bailOut(App::$l10n->t('Error uploading contacts to storage.'));
@ -128,19 +126,16 @@ class ImportController extends Controller {
//$content = file_get_contents('oc://' . $path . '/' . $filename);
if($view->file_put_contents('/imports/' . $filename, $content)) {
\OC_FileProxy::$enabled = $proxyStatus;
$count = substr_count($content, 'BEGIN:');
$progresskey = 'contacts-import-' . rand();
$response->setParams(
array(
'filename'=>$filename,
'count' => $count,
'progresskey' => $progresskey,
'backend' => $params['backend'],
'addressBookId' => $params['addressBookId'],
'importType' => $params['importType']
)
);
\OC_Cache::set($progresskey, '0', 300);
} else {
\OC_FileProxy::$enabled = $proxyStatus;
$response->bailOut(App::$l10n->t('Error moving file to imports folder.'));
@ -190,8 +185,9 @@ class ImportController extends Controller {
$file = $view->file_get_contents('/imports/' . $filename);
\OC_FileProxy::$enabled = $proxyStatus;
$writeProgress = function($pct) use ($progresskey) {
$writeProgress = function($pct, $total) use ($progresskey) {
\OC_Cache::set($progresskey, $pct, 300);
\OC_Cache::set($progresskey.'_total', $total, 300);
};
$cleanup = function() use ($view, $filename, $progresskey) {
@ -199,10 +195,11 @@ class ImportController extends Controller {
$response->debug('Unable to unlink /imports/' . $filename);
}
\OC_Cache::remove($progresskey);
\OC_Cache::remove($progresskey.'_total');
};
$importManager = new ImportManager();
$file = str_replace(array("\r","\n\n"), array("\n","\n"), $file);
$formatList = $importManager->getTypes();
$found = false;
@ -266,7 +263,7 @@ class ImportController extends Controller {
$failed++;
}
$processed++;
$writeProgress($processed);
$writeProgress($processed, $total);
}
} else {
$imported = 0;
@ -304,7 +301,8 @@ class ImportController extends Controller {
return $response;
}
$response->setParams(array('progress' => \OC_Cache::get($progresskey)));
error_log("progresskey: ".\OC_Cache::get($progresskey)." total: ".\OC_Cache::get($progresskey.'_total') );
$response->setParams(array('progress' => \OC_Cache::get($progresskey), 'total' => \OC_Cache::get($progresskey.'_total') ));
return $response;
}
}