2018-11-29 07:36:39 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @file src/Model/Storage/Filesystem.php
|
|
|
|
* @brief Storage backend system
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Friendica\Model\Storage;
|
|
|
|
|
|
|
|
use Friendica\Core\L10n;
|
2020-01-05 00:58:49 +00:00
|
|
|
use Psr\Log\LoggerInterface;
|
2018-11-29 07:36:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Database based storage system
|
|
|
|
*
|
|
|
|
* This class manage data stored in database table.
|
|
|
|
*/
|
|
|
|
class Database implements IStorage
|
|
|
|
{
|
2020-01-05 00:58:49 +00:00
|
|
|
const NAME = 'Database';
|
|
|
|
|
|
|
|
/** @var \Friendica\Database\Database */
|
|
|
|
private $dba;
|
|
|
|
/** @var LoggerInterface */
|
|
|
|
private $logger;
|
|
|
|
/** @var L10n\L10n */
|
|
|
|
private $l10n;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \Friendica\Database\Database $dba
|
|
|
|
* @param LoggerInterface $logger
|
|
|
|
* @param L10n\L10n $l10n
|
|
|
|
*/
|
|
|
|
public function __construct(\Friendica\Database\Database $dba, LoggerInterface $logger, L10n\L10n $l10n)
|
|
|
|
{
|
|
|
|
$this->dba = $dba;
|
|
|
|
$this->logger = $logger;
|
|
|
|
$this->l10n = $l10n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function get(string $reference)
|
2018-11-29 07:36:39 +00:00
|
|
|
{
|
2020-01-05 00:58:49 +00:00
|
|
|
$result = $this->dba->selectFirst('storage', ['data'], ['id' => $reference]);
|
|
|
|
if (!$this->dba->isResult($result)) {
|
2018-11-29 07:36:39 +00:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2020-01-05 00:58:49 +00:00
|
|
|
return $result['data'];
|
2018-11-29 07:36:39 +00:00
|
|
|
}
|
|
|
|
|
2020-01-05 00:58:49 +00:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function put(string $data, string $reference = '')
|
2018-11-29 07:36:39 +00:00
|
|
|
{
|
2020-01-05 00:58:49 +00:00
|
|
|
if ($reference !== '') {
|
|
|
|
$result = $this->dba->update('storage', ['data' => $data], ['id' => $reference]);
|
|
|
|
if ($result === false) {
|
|
|
|
$this->logger->warning('Failed to update data.', ['id' => $reference, 'errorCode' => $this->dba->errorNo(), 'errorMessage' => $this->dba->errorMessage()]);
|
|
|
|
throw new StorageException($this->l10n->t('Database storage failed to update %s', $reference));
|
2018-11-29 07:36:39 +00:00
|
|
|
}
|
2020-01-05 00:58:49 +00:00
|
|
|
|
|
|
|
return $reference;
|
2018-11-29 07:36:39 +00:00
|
|
|
} else {
|
2020-01-05 00:58:49 +00:00
|
|
|
$result = $this->dba->insert('storage', ['data' => $data]);
|
|
|
|
if ($result === false) {
|
|
|
|
$this->logger->warning('Failed to insert data.', ['errorCode' => $this->dba->errorNo(), 'errorMessage' => $this->dba->errorMessage()]);
|
|
|
|
throw new StorageException($this->l10n->t('Database storage failed to insert data'));
|
2018-11-29 07:36:39 +00:00
|
|
|
}
|
2020-01-05 00:58:49 +00:00
|
|
|
|
|
|
|
return $this->dba->lastInsertId();
|
2018-11-29 07:36:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-05 00:58:49 +00:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function delete(string $reference)
|
2018-11-29 07:36:39 +00:00
|
|
|
{
|
2020-01-05 00:58:49 +00:00
|
|
|
return $this->dba->delete('storage', ['id' => $reference]);
|
2018-11-29 07:36:39 +00:00
|
|
|
}
|
2019-02-19 00:56:41 +00:00
|
|
|
|
2020-01-05 00:58:49 +00:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function getOptions()
|
2019-02-19 00:56:41 +00:00
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2020-01-05 00:58:49 +00:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function saveOptions(array $data)
|
2019-02-19 00:56:41 +00:00
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
2020-01-05 00:58:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function __toString()
|
|
|
|
{
|
|
|
|
return self::NAME;
|
|
|
|
}
|
2018-12-08 17:49:16 +00:00
|
|
|
}
|