-
Notifications
You must be signed in to change notification settings - Fork 352
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce and use ShortlinksService (#3749)
- Loading branch information
1 parent
692b8bd
commit 17888e6
Showing
9 changed files
with
462 additions
and
183 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
module/VuFind/src/VuFind/Db/Service/Feature/TransactionInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
module/VuFind/src/VuFind/Db/Service/ShortlinksService.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
78
module/VuFind/src/VuFind/Db/Service/ShortlinksServiceInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.