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

Contacts: Misc backend updates.

This commit is contained in:
Thomas Tanghus 2013-03-16 15:59:23 +01:00
parent 6735114c7c
commit 5bfe561937
7 changed files with 106 additions and 33 deletions

View File

@ -27,6 +27,8 @@ namespace OCA\Contacts;
*/
class Addressbook extends PIMCollectionAbstract {
protected $backend;
/**
* An array containing the mandatory:
* 'displayname'
@ -45,9 +47,10 @@ class Addressbook extends PIMCollectionAbstract {
* @param AbstractBackend $backend The storage backend
* @param array $addressBookInfo
*/
public function __construct(AbstractBackend $backend, array $addressBookInfo) {
public function __construct(Backend\AbstractBackend $backend, array $addressBookInfo) {
$this->backend = $backend;
$this->addressBookInfo = $addressBookInfo;
//\OCP\Util::writeLog('contacts', __METHOD__.' backend: ' . print_r($this->backend, true), \OCP\Util::DEBUG);
}
/**
@ -57,6 +60,15 @@ class Addressbook extends PIMCollectionAbstract {
return $this->addressBookInfo['id'];
}
/**
* @return array
*/
public function getMetaData() {
$metadata = $this->addressBookInfo;
$metadata['lastmodified'] = $this->lastModified();
return $metadata;
}
/**
* @return string
*/
@ -114,12 +126,14 @@ class Addressbook extends PIMCollectionAbstract {
/**
* Returns an array with all the child nodes
*
* @return IPIMObject[]
* @return Contact[]
*/
function getChildren($limit = null, $offset = null, $omitdata = false) {
//\OCP\Util::writeLog('contacts', __METHOD__.' backend: ' . print_r($this->backend, true), \OCP\Util::DEBUG);
$contacts = array();
foreach($this->backend->getContacts($this->getId(), $limit, $offset, $omitdata) as $contact) {
//\OCP\Util::writeLog('contacts', __METHOD__.' id: '.$contact['id'], \OCP\Util::DEBUG);
if(!isset($this->objects[$contact['id']])) {
$this->objects[$contact['id']] = new Contact($this, $this->backend, $contact);
}

View File

@ -1,6 +1,6 @@
<?php
/**
* Copyright (c) 2011 Bart Visscher bartv@thisnet.nl
* Copyright (c) 2013 Thomas Tanghus (thomas@tanghus.net)
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
@ -62,7 +62,7 @@ class App {
static public function createBackend($name) {
$name = $name ? $name : 'database';
if (isset(self::$backendClasses[$name])) {
return new self::$classMap[$name]();
return new self::$backendClasses[$name]();
} else {
throw new \Exception('No backend for: ' . $name);
}
@ -96,6 +96,7 @@ class App {
*
* @param string $backendName
* @param string $addressbookid
* @return AddressBook|null
*/
public function getAddressBook($backendName, $addressbookid) {
foreach(self::$addressBooks as $addressBook) {
@ -121,6 +122,8 @@ class App {
* @param string $backendName
* @param string $addressbookid
* @param string $id - Contact id
* @return Contact|null
*
*/
public function getContact($backendName, $addressbookid, $id) {
$addressBook = $this->getAddressBook($backendName, $addressbookid);

View File

@ -105,10 +105,12 @@ abstract class AbstractBackend {
* Must return a UNIX time stamp or null if the backend
* doesn't support it.
*
* TODO: Implement default methods get/set for backends that
* don't support.
* @param string $addressbookid
* @returns int | null
*/
public static function lastModifiedAddressBook($addressbookid) {
public function lastModifiedAddressBook($addressbookid) {
}
/**

View File

@ -31,7 +31,7 @@ use Sabre\VObject\Reader;
class Database extends AbstractBackend {
public $backendname = 'database';
public $name = 'database';
static private $preparedQueries = array();
/**
@ -190,7 +190,7 @@ class Database extends AbstractBackend {
*/
public function createAddressBook(array $properties, $userid = null) {
$userid = $userid ? $userid : $this->userid;
if(count($changes) === 0) {
if(count($properties) === 0) {
return false;
}
@ -202,6 +202,7 @@ class Database extends AbstractBackend {
? $properties['uri']
: $this->createAddressBookURI($properties['displayname']);
$updates[] = isset($properties['description']) ? $properties['description'] : '';
$updates[] = time();
try {
if(!isset(self::$preparedQueries['createaddressbook'])) {
@ -268,7 +269,7 @@ class Database extends AbstractBackend {
* @param integer $id
* @return boolean
*/
public static function touchAddressBook($id) {
public function touchAddressBook($id) {
$query = 'UPDATE `' . $this->addressBooksTableName
. '` SET `ctag` = `ctag` + 1 WHERE `id` = ?';
if(!isset(self::$preparedQueries['touchaddressbook'])) {
@ -279,7 +280,7 @@ class Database extends AbstractBackend {
return true;
}
public static function lastModifiedAddressBook($addressbookid) {
public function lastModifiedAddressBook($addressbookid) {
$sql = 'SELECT MAX(`lastmodified`) FROM `' . $this->cardsTableName . '`, `' . $this->addressBooksTableName . '` ' .
'WHERE `' . $this->cardsTableName . '`.`addressbookid` = `*PREFIX*contacts_addressbooks`.`id` AND ' .
'`' . $this->addressBooksTableName . '`.`userid` = ? AND `' . $this->addressBooksTableName . '`.`id` = ?';
@ -304,7 +305,7 @@ class Database extends AbstractBackend {
public function getContacts($addressbookid, $limit = null, $offset = null, $omitdata = false) {
$cards = array();
try {
$qfields = $omitdata ? '`id`, `fullname`' : '*';
$qfields = $omitdata ? '`id`, `fullname` AS `displayname`' : '*';
$query = 'SELECT ' . $qfields . ' FROM `' . $this->cardsTableName
. '` WHERE `addressbookid` = ? ORDER BY `fullname`';
$stmt = \OCP\DB::prepare($query, $limit, $offset);
@ -556,7 +557,7 @@ class Database extends AbstractBackend {
*/
public function lastModifiedContact($addressbookid, $id) {
$contact = $this->getContact($addressbookid, $id);
return $contact ? $contact['lastmodified'] : null;
return ($contact ? $contact['lastmodified'] : null);
}
private function createAddressBookURI($displayname, $userid = null) {
@ -580,7 +581,7 @@ class Database extends AbstractBackend {
$newname = $name;
$i = 1;
while(in_array($newname, $existing)) {
while(in_array($newname, $uris)) {
$newname = $name.$i;
$i = $i + 1;
}

View File

@ -40,6 +40,7 @@ class Contact extends VObject\VCard implements IPIMObject {
protected $props = array();
public function __construct($parent, $backend, $properties = null) {
//\OCP\Util::writeLog('contacts', __METHOD__ . ' ' . print_r($properties, true), \OCP\Util::DEBUG);
//\OCP\Util::writeLog('contacts', __METHOD__, \OCP\Util::DEBUG);
$this->props['parent'] = $parent;
$this->props['backend'] = $backend;
@ -61,7 +62,7 @@ class Contact extends VObject\VCard implements IPIMObject {
break;
case 'vcard':
$this->props['vcard'] = $value;
$this->read();
$this->retrieve();
break;
case 'displayname':
case 'fullname':
@ -72,6 +73,26 @@ class Contact extends VObject\VCard implements IPIMObject {
}
}
/**
* @return array|null
*/
public function getMetaData() {
if(!isset($this->props['displayname'])) {
if(!$this->retrieve()) {
\OCP\Util::writeLog('contacts', __METHOD__.' error reading: '.print_r($this->props, true), \OCP\Util::ERROR);
return null;
}
}
return array(
'id' => $this->getId(),
'displayname' => $this->getDisplayName(),
'permissions' => $this->getPermissions(),
'lastmodified' => $this->lastModified(),
'owner' => $this->getOwner(),
'parent' => $this->getParent()->getId(),
);
}
/**
* @return string|null
*/
@ -114,7 +135,7 @@ class Contact extends VObject\VCard implements IPIMObject {
* @return integer
*/
function getPermissions() {
return $this->props['permissions'];
return $this->props['parent']->getPermissions();
}
/**
@ -186,19 +207,23 @@ class Contact extends VObject\VCard implements IPIMObject {
}
}
public function read($data = null) {
// NOTE: Maybe this will mess with
// the magic accessors.
/**
* Get the data from the backend
*/
public function retrieve() {
//\OCP\Util::writeLog('contacts', __METHOD__.' ' . print_r($this->props, true), \OCP\Util::DEBUG);
if($this->children && is_null($data)) {
return true;
} else {
if(isset($this->props['vcard'])
&& $this->props['vcard'] instanceof VObject\VCard) {
$this->children = $this->props['vcard']->children();
foreach($this->props['vcard']->children() as $child) {
$this->add($child);
}
//$this->children = $this->props['vcard']->children();
unset($this->props['vcard']);
return true;
}
if(!isset($this->props['carddata']) && is_null($data)) {
} elseif(!isset($this->props['carddata']) && is_null($data)) {
$result = $this->props['backend']->getContact(
$this->parent->getId(),
$this->id
@ -206,33 +231,39 @@ class Contact extends VObject\VCard implements IPIMObject {
if($result) {
if(isset($result['vcard'])
&& $result['vcard'] instanceof VObject\VCard) {
// NOTE: Maybe iterate over $result['vcard']->children
// and add() them
$this->children = $result['vcard']->children;
foreach($result['vcard']->children() as $child) {
$this->add($child);
}
return true;
} elseif(isset($result['carddata'])) {
// Save internal values
$data = $result['carddata'];
$this->props['carddata'] = $result['carddata'];
$this->props['lastmodified'] = $result['lastmodified'];
$this->props['displayname'] = $result['displayname'];
$this->props['displayname'] = $result['fullname'];
$this->props['permissions'] = $result['permissions'];
} else {
return false;
}
}
} elseif(isset($this->props['carddata'])) {
$data = $this->props['carddata'];
}
try {
$obj = \Sabre\VObject\Reader::read(
$data,
\Sabre\VObject\Reader::OPTION_IGNORE_INVALID_LINES
);
// NOTE: Maybe iterate over $result['vcard']->children
// and add() them
$this->children = $obj->children;
if($obj) {
foreach($obj->children as $child) {
$this->add($child);
}
} else {
return false;
}
} catch (\Exception $e) {
\OCP\Util::writeLog('contacts', __METHOD__ .
'Error parsing carddata: ' . $e->getMessage(),
' Error parsing carddata: ' . $e->getMessage(),
\OCP\Util::ERROR);
return false;
}
@ -247,7 +278,7 @@ class Contact extends VObject\VCard implements IPIMObject {
* @return \Sabre\VObject\Propert|null
*/
public function getPropertyByChecksum($checksum) {
$this->read();
$this->retrieve();
$line = null;
foreach($this->children as $i => $property) {
if(substr(md5($property->serialize()), 0, 8) == $checksum ) {
@ -260,10 +291,10 @@ class Contact extends VObject\VCard implements IPIMObject {
public function lastModified() {
if(!isset($this->props['lastmodified'])) {
$this->read();
$this->retrieve();
}
return isset($this->props['lastmodified'])
? isset($this->props['lastmodified'])
? $this->props['lastmodified']
: null;
}
}

View File

@ -36,11 +36,31 @@ interface IPIMObject {
//function getParent();
/**
* Get the identifier for the object.
* @return string
*/
public function getId();
/**
* A convenience method for getting all the info about the object.
*
* The returned array MUST contain:
* 'id' @see getId().
* 'displayname' @see getDisplayName()
* 'owner' @see getOwner()
* 'permissions' @see getPermissions
* 'lastmodified' @see lastModified()
*
* If the object is part of a collection it MUST contain
* 'parent' The identifier for the parent object. @see getParent()
*
* @return array|null
*/
public function getMetaData();
/**
* FIXME: This should probably not be in the interface
* as it's *DAV specific.
* @return string
*/
public function getURI();
@ -51,6 +71,7 @@ interface IPIMObject {
function getDisplayName();
/**
* Get the owner of the object.
* @return string|null
*/
function getOwner();
@ -85,6 +106,7 @@ interface IPIMObject {
/**
* Save the contact data to backend
* FIXME: This isn't consistent.
*
* @param array $data
* @return bool

View File

@ -79,8 +79,8 @@ class Share_Backend_Addressbook implements \OCP\Share_Backend_Collection {
* It is only called through calls to the public getItem(s)Shared(With) functions.
*/
public function formatItems($items, $format, $parameters = null, $include = false) {
\OCP\Util::writeLog('contacts', __METHOD__
. ' ' . $include . ' ' . print_r($items, true), \OCP\Util::DEBUG);
//\OCP\Util::writeLog('contacts', __METHOD__
// . ' ' . $include . ' ' . print_r($items, true), \OCP\Util::DEBUG);
$addressbooks = array();
if ($format == self::FORMAT_ADDRESSBOOKS) {
foreach ($items as $item) {