2019-11-29 11:06:17 +00:00
|
|
|
<?php
|
2020-09-21 12:25:50 +00:00
|
|
|
/*
|
2024-02-02 18:27:50 +00:00
|
|
|
* Copyright (c) 2020-2024. The Nextcloud Bookmarks contributors.
|
2020-09-21 12:25:50 +00:00
|
|
|
*
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or later. See the COPYING file.
|
|
|
|
*/
|
2019-12-28 16:41:31 +00:00
|
|
|
|
2019-11-29 11:06:17 +00:00
|
|
|
namespace OCA\Bookmarks\Db;
|
|
|
|
|
2019-12-28 16:41:31 +00:00
|
|
|
use Exception;
|
2019-11-29 11:06:17 +00:00
|
|
|
use OCP\AppFramework\Db\DoesNotExistException;
|
|
|
|
use OCP\AppFramework\Db\Entity;
|
2020-09-21 12:17:46 +00:00
|
|
|
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
|
2019-11-29 11:06:17 +00:00
|
|
|
use OCP\AppFramework\Db\QBMapper;
|
2019-12-28 16:41:31 +00:00
|
|
|
use OCP\IDBConnection;
|
2020-09-21 12:17:46 +00:00
|
|
|
use RangeException;
|
2019-11-29 11:06:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class PublicFolderMapper
|
|
|
|
*
|
|
|
|
* @package OCA\Bookmarks\Db
|
2023-07-16 14:26:20 +00:00
|
|
|
* @template-extends QBMapper<PublicFolder>
|
2019-11-29 11:06:17 +00:00
|
|
|
*/
|
|
|
|
class PublicFolderMapper extends QBMapper {
|
|
|
|
/**
|
|
|
|
* @var IDBConnection
|
|
|
|
*/
|
|
|
|
protected $db;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TagMapper constructor.
|
|
|
|
*
|
|
|
|
* @param IDBConnection $db
|
|
|
|
*/
|
|
|
|
public function __construct(IDBConnection $db) {
|
|
|
|
parent::__construct($db, 'bookmarks_folders_public', PublicFolder::class);
|
|
|
|
$this->db = $db;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $id
|
2023-07-16 14:26:20 +00:00
|
|
|
* @return PublicFolder
|
2019-12-28 16:41:31 +00:00
|
|
|
* @throws DoesNotExistException
|
2020-09-21 12:17:46 +00:00
|
|
|
* @throws MultipleObjectsReturnedException
|
2019-11-29 11:06:17 +00:00
|
|
|
*/
|
2023-07-16 14:26:20 +00:00
|
|
|
public function find(string $id): PublicFolder {
|
2019-11-29 11:06:17 +00:00
|
|
|
$qb = $this->db->getQueryBuilder();
|
|
|
|
$qb
|
|
|
|
->select('*')
|
|
|
|
->from('bookmarks_folders_public')
|
|
|
|
->where($qb->expr()->eq('id', $qb->createNamedParameter($id)));
|
|
|
|
|
|
|
|
return $this->findEntity($qb);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $folderId
|
2023-07-16 14:26:20 +00:00
|
|
|
* @return PublicFolder
|
2019-12-28 16:41:31 +00:00
|
|
|
* @throws DoesNotExistException
|
2020-09-21 12:17:46 +00:00
|
|
|
* @throws MultipleObjectsReturnedException
|
2019-11-29 11:06:17 +00:00
|
|
|
*/
|
2023-07-16 14:26:20 +00:00
|
|
|
public function findByFolder(int $folderId): PublicFolder {
|
2019-11-29 11:06:17 +00:00
|
|
|
$qb = $this->db->getQueryBuilder();
|
|
|
|
$qb
|
|
|
|
->select('*')
|
|
|
|
->from('bookmarks_folders_public')
|
|
|
|
->where($qb->expr()->eq('folder_id', $qb->createNamedParameter($folderId)));
|
|
|
|
|
|
|
|
return $this->findEntity($qb);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $createdAt
|
2021-01-20 14:42:41 +00:00
|
|
|
*
|
2023-07-16 14:26:20 +00:00
|
|
|
* @return PublicFolder[]
|
2021-01-20 14:42:41 +00:00
|
|
|
*
|
2023-07-16 14:26:20 +00:00
|
|
|
* @psalm-return list<PublicFolder>
|
2019-11-29 11:06:17 +00:00
|
|
|
*/
|
2020-03-21 12:38:33 +00:00
|
|
|
public function findAllCreatedBefore(int $createdAt): array {
|
2019-11-29 11:06:17 +00:00
|
|
|
$qb = $this->db->getQueryBuilder();
|
|
|
|
$qb
|
|
|
|
->select('*')
|
|
|
|
->from('bookmarks_folders_public')
|
|
|
|
->where($qb->expr()->lt('created_at', $qb->createNamedParameter($createdAt)));
|
|
|
|
|
|
|
|
return $this->findEntities($qb);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-06-24 10:26:38 +00:00
|
|
|
* @param Entity $entity
|
2023-07-16 14:26:20 +00:00
|
|
|
* @psalm-param PublicFolder $entity
|
|
|
|
* @return PublicFolder
|
2020-09-21 12:17:46 +00:00
|
|
|
* @throws MultipleObjectsReturnedException
|
2019-12-28 16:41:31 +00:00
|
|
|
* @throws Exception
|
2019-11-29 11:06:17 +00:00
|
|
|
*/
|
2023-07-16 14:26:20 +00:00
|
|
|
public function insert(Entity $entity): PublicFolder {
|
2019-12-28 16:41:31 +00:00
|
|
|
try {
|
|
|
|
while (true) {
|
2020-04-08 13:13:34 +00:00
|
|
|
// 63^7 = 3 939 000 000 000 links -- I guess that's enough.
|
2021-06-24 10:26:38 +00:00
|
|
|
$entity->setId(self::randomString(7));
|
|
|
|
$this->find($entity->getId());
|
2019-11-29 11:06:17 +00:00
|
|
|
}
|
2019-12-28 16:41:31 +00:00
|
|
|
} catch (DoesNotExistException $e) {
|
2021-06-24 10:26:38 +00:00
|
|
|
return parent::insert($entity);
|
2019-11-29 11:06:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-06-24 10:26:38 +00:00
|
|
|
* @param Entity $entity
|
2023-07-16 14:26:20 +00:00
|
|
|
* @psalm-param PublicFolder $entity
|
|
|
|
* @return PublicFolder
|
2020-09-21 12:17:46 +00:00
|
|
|
* @throws MultipleObjectsReturnedException
|
2019-11-29 11:06:17 +00:00
|
|
|
*/
|
2023-07-16 14:26:20 +00:00
|
|
|
public function insertOrUpdate(Entity $entity): PublicFolder {
|
2019-12-28 16:41:31 +00:00
|
|
|
try {
|
2021-06-24 10:26:38 +00:00
|
|
|
$this->find($entity->getId());
|
2019-12-28 16:41:31 +00:00
|
|
|
} catch (DoesNotExistException $e) {
|
2021-06-24 10:26:38 +00:00
|
|
|
return $this->insert($entity);
|
2019-11-29 11:06:17 +00:00
|
|
|
}
|
2021-06-24 10:26:38 +00:00
|
|
|
return $this->update($entity);
|
2019-11-29 11:06:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a random string, using a cryptographically secure
|
|
|
|
* pseudorandom number generator (random_int)
|
|
|
|
*
|
|
|
|
* This function uses type hints now (PHP 7+ only), but it was originally
|
|
|
|
* written for PHP 5 as well.
|
|
|
|
*
|
|
|
|
* For PHP 7, random_int is a PHP core function
|
|
|
|
* For PHP 5.x, depends on https://github.com/paragonie/random_compat
|
|
|
|
*
|
|
|
|
* @param int $length How many characters do we want?
|
|
|
|
* @param string $keyspace A string of all possible characters
|
|
|
|
* to select from
|
|
|
|
* @return string
|
2020-09-21 12:17:46 +00:00
|
|
|
* @throws RangeException
|
2019-12-28 16:41:31 +00:00
|
|
|
* @throws Exception
|
2019-11-29 11:06:17 +00:00
|
|
|
*/
|
|
|
|
public static function randomString(
|
|
|
|
int $length = 64,
|
|
|
|
string $keyspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
2019-12-28 16:41:31 +00:00
|
|
|
): string {
|
2019-11-29 11:06:17 +00:00
|
|
|
if ($length < 1) {
|
2020-09-21 12:17:46 +00:00
|
|
|
throw new RangeException('Length must be a positive integer');
|
2019-11-29 11:06:17 +00:00
|
|
|
}
|
|
|
|
$pieces = [];
|
|
|
|
$max = mb_strlen($keyspace, '8bit') - 1;
|
|
|
|
for ($i = 0; $i < $length; ++$i) {
|
2019-12-28 16:41:31 +00:00
|
|
|
$pieces [] = $keyspace[random_int(0, $max)];
|
2019-11-29 11:06:17 +00:00
|
|
|
}
|
|
|
|
return implode('', $pieces);
|
|
|
|
}
|
|
|
|
}
|