2018-11-29 07:36:39 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2020-02-09 14:45:36 +00:00
|
|
|
* @copyright Copyright (C) 2020, Friendica
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
2018-11-29 07:36:39 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Friendica\Model\Storage;
|
|
|
|
|
2020-01-18 19:59:39 +00:00
|
|
|
use Friendica\Core\L10n;
|
2020-01-05 00:58:49 +00:00
|
|
|
use Psr\Log\LoggerInterface;
|
2020-01-06 16:42:28 +00:00
|
|
|
use Friendica\Database\Database as DBA;
|
2018-11-29 07:36:39 +00:00
|
|
|
|
|
|
|
/**
|
2020-01-19 06:05:23 +00:00
|
|
|
* Database based storage system
|
2018-11-29 07:36:39 +00:00
|
|
|
*
|
|
|
|
* This class manage data stored in database table.
|
|
|
|
*/
|
2020-01-06 16:42:28 +00:00
|
|
|
class Database extends AbstractStorage
|
2018-11-29 07:36:39 +00:00
|
|
|
{
|
2020-01-05 00:58:49 +00:00
|
|
|
const NAME = 'Database';
|
|
|
|
|
2020-01-06 16:42:28 +00:00
|
|
|
/** @var DBA */
|
2020-01-05 00:58:49 +00:00
|
|
|
private $dba;
|
|
|
|
|
|
|
|
/**
|
2020-01-06 16:42:28 +00:00
|
|
|
* @param DBA $dba
|
|
|
|
* @param LoggerInterface $logger
|
|
|
|
* @param L10n $l10n
|
2020-01-05 00:58:49 +00:00
|
|
|
*/
|
2020-01-06 16:42:28 +00:00
|
|
|
public function __construct(DBA $dba, LoggerInterface $logger, L10n $l10n)
|
2020-01-05 00:58:49 +00:00
|
|
|
{
|
2020-01-06 16:42:28 +00:00
|
|
|
parent::__construct($l10n, $logger);
|
|
|
|
|
|
|
|
$this->dba = $dba;
|
2020-01-05 00:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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) {
|
2020-01-06 16:42:28 +00:00
|
|
|
$this->logger->warning('Failed to update data.', ['id' => $reference, 'errorCode' => $this->dba->errorNo(), 'errorMessage' => $this->dba->errorMessage()]);
|
2020-01-05 00:58:49 +00:00
|
|
|
throw new StorageException($this->l10n->t('Database storage failed to update %s', $reference));
|
2020-01-06 16:42:28 +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) {
|
2020-01-06 16:42:28 +00:00
|
|
|
$this->logger->warning('Failed to insert data.', ['errorCode' => $this->dba->errorNo(), 'errorMessage' => $this->dba->errorMessage()]);
|
2020-01-05 00:58:49 +00:00
|
|
|
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
|
|
|
|
*/
|
2020-01-06 16:42:28 +00:00
|
|
|
public static function getName()
|
2020-01-05 00:58:49 +00:00
|
|
|
{
|
|
|
|
return self::NAME;
|
|
|
|
}
|
2018-12-08 17:49:16 +00:00
|
|
|
}
|