Skip to content

Commit

Permalink
Added link shortener (#1372)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtrojan-ub authored and demiankatz committed Jun 12, 2019
1 parent edf6a0e commit f37a094
Show file tree
Hide file tree
Showing 24 changed files with 1,128 additions and 3 deletions.
4 changes: 4 additions & 0 deletions config/vufind/config.ini
Expand Up @@ -526,6 +526,10 @@ disable_from = false
; configuration options.
sms = enabled

; Set this value to "database" to shorten links sent via email/SMS and
; store its path in the database (default "none").
url_shortener = none

; This section needs to be changed to match your database connection information
[Database]
; Connection string format is [platform]://[username]:[password]@[host]:[port]/[db]
Expand Down
20 changes: 20 additions & 0 deletions module/VuFind/config/module.config.php
Expand Up @@ -45,6 +45,19 @@
]
],
],
'shortlink' => [
'type' => 'Zend\Router\Http\Segment',
'options' => [
'route' => '/short/[:id]',
'constraints' => [
'id' => '[a-zA-Z0-9]+',
],
'defaults' => [
'controller' => 'Shortlink',
'action' => 'redirect',
]
],
],
'legacy-alphabrowse-results' => [
'type' => 'Zend\Router\Http\Literal',
'options' => [
Expand Down Expand Up @@ -162,6 +175,7 @@
'VuFind\Controller\RelaisController' => 'VuFind\Controller\AbstractBaseFactory',
'VuFind\Controller\SearchController' => 'VuFind\Controller\AbstractBaseFactory',
'VuFind\Controller\ShibbolethLogoutNotificationController' => 'VuFind\Controller\AbstractBaseFactory',
'VuFind\Controller\ShortlinkController' => 'VuFind\Controller\AbstractBaseFactory',
'VuFind\Controller\SummonController' => 'VuFind\Controller\AbstractBaseFactory',
'VuFind\Controller\SummonrecordController' => 'VuFind\Controller\AbstractBaseFactory',
'VuFind\Controller\TagController' => 'VuFind\Controller\AbstractBaseFactory',
Expand Down Expand Up @@ -257,6 +271,8 @@
'search' => 'VuFind\Controller\SearchController',
'ShibbolethLogoutNotification' => 'VuFind\Controller\ShibbolethLogoutNotificationController',
'shibbolethlogoutnotification' => 'VuFind\Controller\ShibbolethLogoutNotificationController',
'Shortlink' => 'VuFind\Controller\ShortlinkController',
'shortlink' => 'VuFind\Controller\ShortlinkController',
'Summon' => 'VuFind\Controller\SummonController',
'summon' => 'VuFind\Controller\SummonController',
'SummonRecord' => 'VuFind\Controller\SummonrecordController',
Expand Down Expand Up @@ -396,6 +412,8 @@
'VuFind\SMS\SMSInterface' => 'VuFind\SMS\Factory',
'VuFind\Solr\Writer' => 'VuFind\Solr\WriterFactory',
'VuFind\Tags' => 'VuFind\TagsFactory',
'VuFind\UrlShortener\PluginManager' => 'VuFind\ServiceManager\AbstractPluginManagerFactory',
'VuFind\UrlShortener\UrlShortenerInterface' => 'VuFind\UrlShortener\ServiceFactory',
'VuFind\Validator\Csrf' => 'VuFind\Validator\CsrfFactory',
'VuFindHttp\HttpService' => 'VuFind\Service\HttpServiceFactory',
'VuFindSearch\Service' => 'VuFind\Service\SearchServiceFactory',
Expand Down Expand Up @@ -500,6 +518,7 @@
'resource_tags' => ['id', 'resource_tags_id_seq'],
'search' => ['id', 'search_id_seq'],
'session' => ['id', 'session_id_seq'],
'shortlinks' => ['id', 'shortlinks_id_seq'],
'tags' => ['id', 'tags_id_seq'],
'user' => ['id', 'user_id_seq'],
'user_card' => ['id', 'user_card_id_seq'],
Expand Down Expand Up @@ -542,6 +561,7 @@
'search_params' => [ /* See VuFind\Search\Params\PluginManager for defaults */ ],
'search_results' => [ /* See VuFind\Search\Results\PluginManager for defaults */ ],
'session' => [ /* see VuFind\Session\PluginManager for defaults */ ],
'urlshortener' => [ /* see VuFind\UrlShortener\PluginManager for defaults */ ],
],
],
// Authorization configuration:
Expand Down
@@ -0,0 +1,10 @@
--
-- Table structure for table shortlinks
--

CREATE TABLE shortlinks (
id SERIAL,
path text,
created timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id)
);
14 changes: 14 additions & 0 deletions module/VuFind/sql/mysql.sql
Expand Up @@ -163,6 +163,20 @@ CREATE TABLE `external_session` (
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE utf8_bin;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Table structure for table `shortlinks`
--

/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `shortlinks` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`path` MEDIUMTEXT NOT NULL,
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE utf8_bin;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Table structure for table `tags`
--
Expand Down
15 changes: 15 additions & 0 deletions module/VuFind/sql/pgsql.sql
Expand Up @@ -84,6 +84,21 @@ CREATE INDEX search_user_id_idx ON search (user_id);
CREATE INDEX search_folder_id_idx ON search (folder_id);
CREATE INDEX session_id_idx ON search (session_id);

-- --------------------------------------------------------

--
-- Table structure for table shortlinks
--

DROP TABLE IF EXISTS "shortlinks";

CREATE TABLE shortlinks (
id SERIAL,
path text,
created timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id)
);


-- --------------------------------------------------------

Expand Down
59 changes: 59 additions & 0 deletions module/VuFind/src/VuFind/Controller/ShortlinkController.php
@@ -0,0 +1,59 @@
<?php
/**
* Short link controller
*
* PHP version 7
*
* Copyright (C) Villanova University 2019.
*
* 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 Controller
* @author Demian Katz <demian.katz@villanova.edu>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org Main Site
*/
namespace VuFind\Controller;

use VuFind\UrlShortener\UrlShortenerInterface;

/**
* Short link controller
*
* @category VuFind
* @package Controller
* @author Demian Katz <demian.katz@villanova.edu>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org Main Site
*/
class ShortlinkController extends AbstractBase
{
/**
* Resolve full version of shortlink & redirect to target.
*
* @return mixed
*/
public function redirectAction()
{
if ($id = $this->params('id')) {
$resolver = $this->serviceLocator->get(UrlShortenerInterface::class);
if ($url = $resolver->resolve($id)) {
return $this->redirect()->toUrl($url);
}
}

$this->getResponse()->setStatusCode(404);
}
}
2 changes: 2 additions & 0 deletions module/VuFind/src/VuFind/Db/Row/PluginManager.php
Expand Up @@ -53,6 +53,7 @@ class PluginManager extends \VuFind\ServiceManager\AbstractPluginManager
'resourcetags' => ResourceTags::class,
'search' => Search::class,
'session' => Session::class,
'shortlinks' => Shortlinks::class,
'tags' => Tags::class,
'user' => User::class,
'usercard' => UserCard::class,
Expand All @@ -75,6 +76,7 @@ class PluginManager extends \VuFind\ServiceManager\AbstractPluginManager
ResourceTags::class => RowGatewayFactory::class,
Search::class => RowGatewayFactory::class,
Session::class => RowGatewayFactory::class,
Shortlinks::class => RowGatewayFactory::class,
Tags::class => RowGatewayFactory::class,
User::class => UserFactory::class,
UserCard::class => RowGatewayFactory::class,
Expand Down
50 changes: 50 additions & 0 deletions module/VuFind/src/VuFind/Db/Row/Shortlinks.php
@@ -0,0 +1,50 @@
<?php
/**
* Row Definition for shortlinks
*
* PHP version 7
*
* Copyright (C) Villanova University 2019.
*
* 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 Db_Row
* @author Demian Katz <demian.katz@villanova.edu>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org Main Site
*/
namespace VuFind\Db\Row;

/**
* Row Definition for shortlinks
*
* @category VuFind
* @package Db_Row
* @author Demian Katz <demian.katz@villanova.edu>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org Main Site
*/
class Shortlinks extends RowGateway
{
/**
* Constructor
*
* @param \Zend\Db\Adapter\Adapter $adapter Database adapter
*/
public function __construct($adapter)
{
parent::__construct('id', 'shortlinks', $adapter);
}
}
2 changes: 2 additions & 0 deletions module/VuFind/src/VuFind/Db/Table/PluginManager.php
Expand Up @@ -53,6 +53,7 @@ class PluginManager extends \VuFind\ServiceManager\AbstractPluginManager
'resourcetags' => ResourceTags::class,
'search' => Search::class,
'session' => Session::class,
'shortlinks' => Shortlinks::class,
'tags' => Tags::class,
'user' => User::class,
'usercard' => UserCard::class,
Expand All @@ -75,6 +76,7 @@ class PluginManager extends \VuFind\ServiceManager\AbstractPluginManager
ResourceTags::class => CaseSensitiveTagsFactory::class,
Search::class => GatewayFactory::class,
Session::class => GatewayFactory::class,
Shortlinks::class => GatewayFactory::class,
Tags::class => CaseSensitiveTagsFactory::class,
User::class => UserFactory::class,
UserCard::class => GatewayFactory::class,
Expand Down
58 changes: 58 additions & 0 deletions module/VuFind/src/VuFind/Db/Table/Shortlinks.php
@@ -0,0 +1,58 @@
<?php
/**
* Table Definition for shortlinks
*
* PHP version 7
*
* Copyright (C) Villanova University 2019.
*
* 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 Db_Table
* @author Demian Katz <demian.katz@villanova.edu>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org Main Site
*/
namespace VuFind\Db\Table;

use VuFind\Db\Row\RowGateway;
use Zend\Db\Adapter\Adapter;

/**
* Table Definition for shortlinks
*
* @category VuFind
* @package Db_Table
* @author Demian Katz <demian.katz@villanova.edu>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org Main Site
*/
class Shortlinks extends Gateway
{
/**
* Constructor
*
* @param Adapter $adapter Database adapter
* @param PluginManager $tm Table manager
* @param array $cfg Zend Framework configuration
* @param RowGateway $rowObj Row prototype object (null for default)
* @param string $table Name of database table to interface with
*/
public function __construct(Adapter $adapter, PluginManager $tm, $cfg,
RowGateway $rowObj = null, $table = 'shortlinks'
) {
parent::__construct($adapter, $tm, $cfg, $rowObj, $table);
}
}

0 comments on commit f37a094

Please sign in to comment.