Skip to content

Commit

Permalink
Introduce and use ShortlinksService (#3749)
Browse files Browse the repository at this point in the history
  • Loading branch information
demiankatz committed Jun 13, 2024
1 parent 692b8bd commit 17888e6
Show file tree
Hide file tree
Showing 9 changed files with 462 additions and 183 deletions.
12 changes: 5 additions & 7 deletions module/VuFind/src/VuFind/Controller/UpgradeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
use VuFind\Db\Service\ResourceServiceInterface;
use VuFind\Db\Service\ResourceTagsServiceInterface;
use VuFind\Db\Service\SearchServiceInterface;
use VuFind\Db\Service\ShortlinksServiceInterface;
use VuFind\Exception\RecordMissing as RecordMissingException;
use VuFind\Record\ResourcePopulator;
use VuFind\Search\Results\PluginManager as ResultsManager;
Expand Down Expand Up @@ -1042,18 +1043,15 @@ public function resetAction()
*/
protected function fixshortlinks()
{
$shortlinksTable = $this->getTable('shortlinks');
$shortlinks = $this->getDbService(ShortlinksServiceInterface::class);
$base62 = new Base62();

try {
$results = $shortlinksTable->select(['hash' => null]);
$results = $shortlinks->getShortLinksWithMissingHashes();

foreach ($results as $result) {
$id = $result['id'];
$shortlinksTable->update(
['hash' => $base62->encode($id)],
['id' => $id]
);
$result->setHash($base62->encode($result->getId()));
$shortlinks->persistEntity($result);
}

if (count($results) > 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

/**
* Interface for exposing the database transaction functionality.
*
* PHP version 8
*
* Copyright (C) Villanova University 2024.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category VuFind
* @package Database
* @author Demian Katz <demian.katz@villanova.edu>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/development:plugins:database_gateways Wiki
*/

namespace VuFind\Db\Service\Feature;

/**
* Interface for exposing the database transaction functionality.
*
* @category VuFind
* @package Database
* @author Demian Katz <demian.katz@villanova.edu>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/development:plugins:database_gateways Wiki
*/
interface TransactionInterface
{
/**
* Begin a database transaction.
*
* @return void
* @throws Exception
*/
public function beginTransaction(): void;

/**
* Commit a database transaction.
*
* @return void
* @throws Exception
*/
public function commitTransaction(): void;

/**
* Roll back a database transaction.
*
* @return void
* @throws Exception
*/
public function rollBackTransaction(): void;
}
2 changes: 2 additions & 0 deletions module/VuFind/src/VuFind/Db/Service/PluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class PluginManager extends \VuFind\ServiceManager\AbstractPluginManager
ResourceTagsServiceInterface::class => ResourceTagsService::class,
SearchServiceInterface::class => SearchService::class,
SessionServiceInterface::class => SessionService::class,
ShortlinksServiceInterface::class => ShortlinksService::class,
TagServiceInterface::class => TagService::class,
UserCardServiceInterface::class => UserCardService::class,
UserListServiceInterface::class => UserListService::class,
Expand Down Expand Up @@ -90,6 +91,7 @@ class PluginManager extends \VuFind\ServiceManager\AbstractPluginManager
ResourceTagsService::class => AbstractDbServiceFactory::class,
SearchService::class => AbstractDbServiceFactory::class,
SessionService::class => AbstractDbServiceFactory::class,
ShortlinksService::class => AbstractDbServiceFactory::class,
TagService::class => AbstractDbServiceFactory::class,
UserCardService::class => UserCardServiceFactory::class,
UserListService::class => AbstractDbServiceFactory::class,
Expand Down
134 changes: 134 additions & 0 deletions module/VuFind/src/VuFind/Db/Service/ShortlinksService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php

/**
* Database service for shortlinks.
*
* PHP version 8
*
* Copyright (C) Villanova University 2023.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category VuFind
* @package Database
* @author Sudharma Kellampalli <skellamp@villanova.edu>
* @author Demian Katz <demian.katz@villanova.edu>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/development:plugins:database_gateways Wiki
*/

namespace VuFind\Db\Service;

use Exception;
use VuFind\Db\Entity\ShortlinksEntityInterface;
use VuFind\Db\Table\DbTableAwareInterface;
use VuFind\Db\Table\DbTableAwareTrait;

/**
* Database service for shortlinks.
*
* @category VuFind
* @package Database
* @author Sudharma Kellampalli <skellamp@villanova.edu>
* @author Demian Katz <demian.katz@villanova.edu>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/development:plugins:database_gateways Wiki
*/
class ShortlinksService extends AbstractDbService implements
DbTableAwareInterface,
ShortlinksServiceInterface,
Feature\TransactionInterface
{
use DbTableAwareTrait;

/**
* Begin a database transaction.
*
* @return void
* @throws Exception
*/
public function beginTransaction(): void
{
$this->getDbTable('shortlinks')->getAdapter()->getDriver()->getConnection()->beginTransaction();
}

/**
* Commit a database transaction.
*
* @return void
* @throws Exception
*/
public function commitTransaction(): void
{
$this->getDbTable('shortlinks')->getAdapter()->getDriver()->getConnection()->commit();
}

/**
* Roll back a database transaction.
*
* @return void
* @throws Exception
*/
public function rollBackTransaction(): void
{
$this->getDbTable('shortlinks')->getAdapter()->getDriver()->getConnection()->rollback();
}

/**
* Create a short link entity.
*
* @return ShortlinksEntityInterface
*/
public function createEntity(): ShortlinksEntityInterface
{
return $this->getDbTable('shortlinks')->createRow();
}

/**
* Create and persist an entity for the provided path.
*
* @param string $path Path part of URL being shortened.
*
* @return ShortlinksEntityInterface
*/
public function createAndPersistEntityForPath(string $path): ShortlinksEntityInterface
{
$table = $this->getDbTable('shortlinks');
$table->insert(['path' => $path]);
$id = $table->getLastInsertValue();
return $table->select(['id' => $id])->current();
}

/**
* Look up a short link by hash value.
*
* @param string $hash Hash value.
*
* @return ?ShortlinksEntityInterface
*/
public function getShortLinkByHash(string $hash): ?ShortlinksEntityInterface
{
return $this->getDbTable('shortlinks')->select(['hash' => $hash])->current();
}

/**
* Get rows with missing hashes (for legacy upgrading).
*
* @return ShortlinksEntityInterface[]
*/
public function getShortLinksWithMissingHashes(): array
{
return iterator_to_array($this->getDbTable('shortlinks')->select(['hash' => null]));
}
}
78 changes: 78 additions & 0 deletions module/VuFind/src/VuFind/Db/Service/ShortlinksServiceInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

/**
* Database service interface for shortlinks.
*
* PHP version 8
*
* Copyright (C) Villanova University 2024.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category VuFind
* @package Database
* @author Sudharma Kellampalli <skellamp@villanova.edu>
* @author Demian Katz <demian.katz@villanova.edu>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/development:plugins:database_gateways Wiki
*/

namespace VuFind\Db\Service;

use VuFind\Db\Entity\ShortlinksEntityInterface;

/**
* Database service interface for shortlinks.
*
* @category VuFind
* @package Database
* @author Sudharma Kellampalli <skellamp@villanova.edu>
* @author Demian Katz <demian.katz@villanova.edu>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/development:plugins:database_gateways interface
*/
interface ShortlinksServiceInterface extends DbServiceInterface
{
/**
* Create a short link entity.
*
* @return ShortlinksEntityInterface
*/
public function createEntity(): ShortlinksEntityInterface;

/**
* Create and persist an entity for the provided path.
*
* @param string $path Path part of URL being shortened.
*
* @return ShortlinksEntityInterface
*/
public function createAndPersistEntityForPath(string $path): ShortlinksEntityInterface;

/**
* Look up a short link by hash value.
*
* @param string $hash Hash value.
*
* @return ?ShortlinksEntityInterface
*/
public function getShortLinkByHash(string $hash): ?ShortlinksEntityInterface;

/**
* Get rows with missing hashes (for legacy upgrading).
*
* @return ShortlinksEntityInterface[]
*/
public function getShortLinksWithMissingHashes(): array;
}
Loading

0 comments on commit 17888e6

Please sign in to comment.