addressbookadapter.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace OCA\Dav\Migration;
  3. use OCP\IDBConnection;
  4. use Symfony\Component\Console\Command\Command;
  5. use Symfony\Component\Console\Input\InputArgument;
  6. use Symfony\Component\Console\Input\InputInterface;
  7. use Symfony\Component\Console\Output\OutputInterface;
  8. class AddressBookAdapter {
  9. /** @var \OCP\IDBConnection */
  10. protected $dbConnection;
  11. /** @var string */
  12. private $sourceBookTable;
  13. /** @var string */
  14. private $sourceCardsTable;
  15. /**
  16. * @param IDBConnection $dbConnection
  17. * @param string $sourceBookTable
  18. * @param string $sourceCardsTable
  19. */
  20. function __construct(IDBConnection $dbConnection,
  21. $sourceBookTable = 'contacts_addressbooks',
  22. $sourceCardsTable = 'contacts_cards') {
  23. $this->dbConnection = $dbConnection;
  24. $this->sourceBookTable = $sourceBookTable;
  25. $this->sourceCardsTable = $sourceCardsTable;
  26. }
  27. /**
  28. * @param string $user
  29. * @param \Closure $callBack
  30. */
  31. public function foreachBook($user, \Closure $callBack) {
  32. // get all addressbooks of that user
  33. $query = $this->dbConnection->getQueryBuilder();
  34. $stmt = $query->select('*')->from($this->sourceBookTable)
  35. ->where($query->expr()->eq('userid', $query->createNamedParameter($user)))
  36. ->execute();
  37. while($row = $stmt->fetch()) {
  38. $callBack($row);
  39. }
  40. }
  41. public function setup() {
  42. if (!$this->dbConnection->tableExists($this->sourceBookTable)) {
  43. throw new \DomainException('Contacts tables are missing. Nothing to do.');
  44. }
  45. }
  46. /**
  47. * @param int $addressBookId
  48. * @param \Closure $callBack
  49. */
  50. public function foreachCard($addressBookId, \Closure $callBack) {
  51. $query = $this->dbConnection->getQueryBuilder();
  52. $stmt = $query->select('*')->from($this->sourceCardsTable)
  53. ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId)))
  54. ->execute();
  55. while($row = $stmt->fetch()) {
  56. $callBack($row);
  57. }
  58. }
  59. /**
  60. * @param int $addressBookId
  61. * @return array
  62. */
  63. public function getShares($addressBookId) {
  64. $query = $this->dbConnection->getQueryBuilder();
  65. $shares = $query->select('*')->from('share')
  66. ->where($query->expr()->eq('item_source', $query->createNamedParameter($addressBookId)))
  67. ->andWhere($query->expr()->eq('item_type', $query->expr()->literal('addressbook')))
  68. ->andWhere($query->expr()->in('share_type', [ $query->expr()->literal(0), $query->expr()->literal(1)]))
  69. ->execute()
  70. ->fetchAll();
  71. return $shares;
  72. }
  73. }