diff --git a/fid_bbi/config/vufind/config.ini b/fid_bbi/config/vufind/config.ini index 77220ec6b1dcbff5fb51a996b7eecbd3c4775f2e..e6dc0416fa7d6659b1d3d037fbdcb6c0e9f54acf 100644 --- a/fid_bbi/config/vufind/config.ini +++ b/fid_bbi/config/vufind/config.ini @@ -93,6 +93,20 @@ host = https://fid-bbi.idm.oclc.org ; authentication requests in VuFind: ;disable_ticket_auth_logging = true +[RssConsumer] +base_url = "https://www.fid-bbi.de/wordpress/" +xml_doc = "?feed=rss2" +input_date_format = "D, j M Y G:i:s O" +excerpt_length = 150 +; excerpt_suffix is the string added to excerpts to denote they were shortened +excerpt_suffix = " ..." +limit = 4 +; set cache_ttl to +; * number of seconds for cache lifetime +; * or 0 for infinite caching (default) +; * or -1 for no caching +cache_ttl = 3600 ;one hour + [Content] coversize = false diff --git a/module/fid_bbi/config/module.config.php b/module/fid_bbi/config/module.config.php index 889ae6fb7be25a2b06848a648658e7504ad05af9..88410003d0499eded3965176d3d053adc747066d 100644 --- a/module/fid_bbi/config/module.config.php +++ b/module/fid_bbi/config/module.config.php @@ -23,6 +23,7 @@ use VuFind\Db\Table\Tags as BaseTagsTable; use fid_bbi\Db\Table\Tags as BBITagsTable; +use fid_bbi\Helper\Rss; use VuFind\Db\Row\Tags as BaseTagsRow; use VuFind\Db\Table\CaseSensitiveTagsFactory; use fid_bbi\Controller\RecordController; @@ -84,6 +85,14 @@ $config = [ ], ], ], + 'view_helpers' => [ + 'aliases' => [ + 'rss' => Rss::class, + ], + 'factories' => [ + Rss::class => 'fid_bbi\Helper\Factory::getRss', + ], + ], 'vufind' => [ 'plugin_managers' => [ 'recorddriver' => [ diff --git a/module/fid_bbi/src/fid_bbi/Helper/Factory.php b/module/fid_bbi/src/fid_bbi/Helper/Factory.php new file mode 100644 index 0000000000000000000000000000000000000000..4c0fa1165aebb4a5871ec4e5175e6e356757c94c --- /dev/null +++ b/module/fid_bbi/src/fid_bbi/Helper/Factory.php @@ -0,0 +1,58 @@ +<?php +/** + * Factory for view helpers. + * + * PHP version 7 + * + * Copyright (C) Leipzig University Library 2021. + * + * 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 View_Helpers + * @author Dorain Merz <merz@ub.uni-leipzig.de> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link https://vufind.org/wiki/development Wiki + */ +namespace fid_bbi\Helper; + +use Interop\Container\ContainerInterface; + +/** + * Factory for Root view helpers. + * + * @category VuFind + * @package View_Helpers + * @author Dorain Merz <merz@ub.uni-leipzig.de> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link https://vufind.org/wiki/development Wiki + */ +class Factory +{ + /** + * Construct the Rss helper. + * + * @param ContainerInterface $container Service manager. + * + * @return Rss + */ + public static function getRss(ContainerInterface $container) + { + return new Rss( + $container->get('VuFind\Config')->get('config')->RssConsumer, + $container->get('VuFind\Date\Converter'), + $container->get('VuFind\Cache\Manager')->getCache('object',Rss::class) + ); + } +} diff --git a/module/fid_bbi/src/fid_bbi/Helper/Rss.php b/module/fid_bbi/src/fid_bbi/Helper/Rss.php new file mode 100644 index 0000000000000000000000000000000000000000..4f0bbd58989c027e0570aebe106b41e6e094ce29 --- /dev/null +++ b/module/fid_bbi/src/fid_bbi/Helper/Rss.php @@ -0,0 +1,196 @@ +<?php +/** + * Copyright (C) 2019 Leipzig University Library + * + * 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. + * + * @author Robert Lange <lange@ub.uni-leipzig.de> + * @author Dorian Merz <merz@ub.uni-leipzig.de> + * @license http://opensource.org/licenses/gpl-2.0.php GNU GPLv2 + */ + +namespace fid_bbi\Helper; + +use DOMDocument; +use VuFind\Date\Converter; +use Zend\Cache\Storage\StorageInterface; +use Zend\Config\Config; +use Zend\View\Helper\AbstractHelper; + +/** + * Class Rss Provides feed reader functionality + * Mostly a copy from instance/fid_adlr, + * * adds excerpt and date, + * * returns objects instead of arrays + * * provides __invoke + * * alters constructor to include config and dateConverter + * + * @deprecated pull up to FID-module + * @package fid_bbi\Helper + */ +class Rss extends AbstractHelper +{ + /** + * @var Config + */ + protected $rssConfig; + + /** + * @var Converter + */ + protected $dateConverter; + + /** + * @var StorageInterface + */ + protected $cache; + + /** + * @var bool + */ + protected $caching; + + /** + * @var string + */ + protected $cacheKey; + + /** + * Rss constructor. + * @param Config $rssConfig + * @param Converter $dateConverter + * @param StorageInterface $cacheManager + */ + public function __construct(Config $rssConfig, Converter $dateConverter, StorageInterface $cache) + { + $this->rssConfig = $rssConfig; + $this->dateConverter = $dateConverter; + $this->feedDateFormat = $this->rssConfig->input_date_format ?? 'c'; + $this->prepareCache($cache); + } + + protected function prepareCache(StorageInterface $cache) + { + $ttl = $this->rssConfig['cache_ttl'] ?? 0; + if ($ttl == -1) { + $this->caching = false; + } else { + $this->caching = true; + $this->cache = $cache; + $this->cache->getOptions()->setTtl($ttl); + $this->cacheKey = md5(implode($this->rssConfig->toArray())); + } + } + + /** + * Read cached feed + * + * @return bool|mixed cached feed or false if caching is inactive + */ + protected function readCacheData() + { + if ($this->caching) { + return $this->cache->getItem($this->cacheKey); + } + return false; + } + + protected function writeCacheData($data) + { + if ($this->caching) { + $this->cache->setItem($this->cacheKey,$data); + } + } + + /** + * Read the feed + * + * @return array|bool|mixed + */ + public function __invoke() + { + return $this->getFeed(); + } + + /** + * get Rss feed data + * + * @return array|bool|mixed + */ + protected function getFeed() + { + if ($cacheData = $this->readCacheData()) { + return $cacheData; + } else { + $feed = $this->load($this->rssConfig); + $this->writeCacheData($feed); + return $feed; + } + } + + /** + * @param array of rss origin data + * @return array of rss feeds + */ + public function load($rssOrigin) + { + $baseUrl = $rssOrigin["base_url"] ?? ""; + $fallback = $rssOrigin["fallback_url"] ?? ""; + $docPath = $rssOrigin["xml_doc"] ?? ""; + + $rssDoc = new DOMDocument(); + $feeds = array(); + try { + if ($rssDoc->load($baseUrl . '/' . $docPath)) { + $limit = $rssOrigin['limit'] ?? 3; + $n = 0; + + foreach ($rssDoc->getElementsByTagName('item') as $node) { + $excerpt = $node->getElementsByTagName('description')->item(0)->nodeValue; + if (strlen($excerpt) > $this->rssConfig->excerpt_length) { + $excerpt = substr($excerpt, 0, $this->rssConfig->excerpt_length); + $lastSpace = strrpos($excerpt, ' '); + $excerpt = substr($excerpt, 0, $lastSpace); + if (isset($this->rssConfig['excerpt_suffix'])) { + $excerpt .= $this->rssConfig['excerpt_suffix']; + } + } + $date = $node->getElementsByTagName('pubDate')->item(0)->nodeValue; + $date = $this->dateConverter->convertToDisplayDate($this->feedDateFormat,$date); + $item = array( + 'title' => $node->getElementsByTagName('title')->item(0)->nodeValue, + 'link' => $node->getElementsByTagName('link')->item(0)->nodeValue, + 'excerpt' => $excerpt, + 'date' => $date, + ); + array_push($feeds, (object)$item); + + if (++$n >= $limit) { + break; + } + } + } + } catch (\Exception $ex) { + if ($fallback) { + $item = array( + 'title' => $fallback, + 'link' => $fallback + ); + array_push($feeds, $item); + } + } + + return $feeds; + } +} diff --git a/themes/fid_bbi/templates/search/home.phtml b/themes/fid_bbi/templates/search/home.phtml index e9938c8016196ec628b6f07dbe06df1d2815edf9..07126cca94c0c4b8849f9f30d676c09042e330b1 100644 --- a/themes/fid_bbi/templates/search/home.phtml +++ b/themes/fid_bbi/templates/search/home.phtml @@ -11,13 +11,9 @@ $this->layout()->breadcrumbs = false; <h2 class="sr-only"><?=$this->translate('Recent Blog Posts')?></h2> <div class="posts"> - <?php - // TODO: Replace this once blog API is available - $postsFile = APPLICATION_PATH . '/themes/fid_bbi/dummy-posts.json'; - $posts = @json_decode(@file_get_contents($postsFile)); - ?> - - <?php if ($posts): ?> + <?php + $posts = $this->rss(); + if ($posts): ?> <?php foreach ($posts as $post): ?> <article class="post"> <header class="post_header">