Skip to content

Instantly share code, notes, and snippets.

@ckujau
Last active June 11, 2016 02:31
Show Gist options
  • Save ckujau/779f9d4f77353b9cd367f95e2f46440c to your computer and use it in GitHub Desktop.
Save ckujau/779f9d4f77353b9cd367f95e2f46440c to your computer and use it in GitHub Desktop.
Trying to convert Owncloud addressbook data from oc_contacts_cards to oc_cards
#!/bin/sh
#
# oc_contacts_cards
# FROM: id, addressbookid, carddata, uri, lastmodified, fullname
# id|fullname|carddata|uri|addressbookid|lastmodified
#
# oc_cards
# TO: id, addressbookid, carddata, uri, lastmodified, etag, size
# id|addressbookid|carddata|uri|lastmodified|etag|size
#
if [ ! -f "$1" ]; then
echo "Usage: $(basename $0) [db] [num]"
exit 1
else
DB="$1"
[ -n "$2" ] && HEAD="head -${2}" || HEAD="cat"
fi
echo "SELECT fullname FROM oc_contacts_cards;" | sqlite3 "$DB" | $HEAD | while read fn; do
echo "FN: $fn"
# This could be wrapped in one big eval() function, but for the sake of simplicity we do one SELECT
# for every field. Performance will suck for big address books.
id=$(echo "SELECT id FROM oc_contacts_cards WHERE fullname = '$fn';" | sqlite3 "$DB")
addressbookid=$(echo "SELECT addressbookid FROM oc_contacts_cards WHERE fullname = '$fn';" | sqlite3 "$DB")
carddata=$(echo "SELECT carddata FROM oc_contacts_cards WHERE fullname = '$fn';" | sqlite3 "$DB" | awk '{printf "%s\r\n", $0}')
# carddata=$(echo "$carddata" | sed 's/.$/&&/')
# carddata=$(echo "SELECT carddata FROM oc_contacts_cards WHERE fullname = '$fn';" | sqlite3 "$DB" | sed '/END:VCARD.$/G')
uri=$(echo "SELECT uri FROM oc_contacts_cards WHERE fullname = '$fn';" | sqlite3 "$DB")
lastmodified=$(echo "SELECT lastmodified FROM oc_contacts_cards WHERE fullname = '$fn';" | sqlite3 "$DB")
# From apps/dav/lib/CardDAV/CardDavBackend.php:
# etag - A unique etag. This must change every time the card changes.
# size - The size of the card in bytes.
# sed '/^$/d'
etag=$(echo "SELECT carddata FROM oc_contacts_cards WHERE fullname = '$fn';" | sqlite3 "$DB" | openssl md5 | awk '{print $NF}')
size=$(echo "SELECT carddata FROM oc_contacts_cards WHERE fullname = '$fn';" | sqlite3 "$DB" | wc -c | awk '{print $1}')
# DEBUG
# echo "CARDDATA: $carddata"
# echo "DEBUG: id: $id addressbookid: $addressbookid, uri: $uri, lastmodified: $lastmodified, fullname: $fn, size: $size, etag: $etag"
# Now that we have all fields, let's INSERT them
# ||char(13)||char(10)
echo "INSERT INTO oc_cards (id,addressbookid,carddata,uri,lastmodified,etag,size) VALUES \
('$id', '$addressbookid', '$carddata', '$uri', '$lastmodified', '$etag', '$size');" | sqlite3 "$DB"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment