Skip to content
Snippets Groups Projects
Commit 2c3932da authored by Dorian Merz's avatar Dorian Merz Committed by Robert Lange
Browse files

refs #19118 [fid_bbi] blog integration via rss feed

* add real blog-entries
** includes latest blog entries via Rss feed reader

* make Rss feed consumer more configurable, add caching
* improve excerpt shortening
parent 636b7910
Branches
Tags
No related merge requests found
......@@ -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
......
......@@ -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' => [
......
<?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)
);
}
}
<?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;
}
}
......@@ -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">
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment