From 3afd5b2fd1f217f12ecad465449524fe93cacf09 Mon Sep 17 00:00:00 2001 From: horus Date: Sat, 11 Apr 2020 19:44:37 +0200 Subject: Adds RSS- and Atom-Feeds. --- app/Http/Controllers/FeedController.php | 67 ++++++++++++++++++++++++++++++ app/Libraries/Helper.php | 73 +++++++++++++++++++++++++++++++++ composer.json | 3 +- composer.lock | 68 +++++++++++++++++++++++++++++- public/img/rss-square.svg | 1 + public/img/rss.svg | 1 + resources/views/layouts/app.blade.php | 5 ++- resources/views/list.blade.php | 14 +++++++ routes/web.php | 6 ++- 9 files changed, 234 insertions(+), 4 deletions(-) create mode 100644 app/Http/Controllers/FeedController.php create mode 100644 public/img/rss-square.svg create mode 100644 public/img/rss.svg diff --git a/app/Http/Controllers/FeedController.php b/app/Http/Controllers/FeedController.php new file mode 100644 index 0000000..9fd73a5 --- /dev/null +++ b/app/Http/Controllers/FeedController.php @@ -0,0 +1,67 @@ +take(20)->get(); + return Helper::makeFeed($articles, "new"); + } + + public function popular() + { + + $articles = new Article; + $articles = $articles->setTable('view_popular'); + $articles = $articles->take(20)->get(); + return Helper::makeFeed($articles, "popular"); + } + + public function search(Request $request) { + $search_unsafe = $request->input("q"); + + if ( "" == $search_unsafe ) { + $search_unsafe = ""; + } + + $search_unsafe = explode(",", $request->input("q")); + + $articles = new Article; + + if ( "on" == $request->input("onlypopular") ) { + $articles = $articles->setTable('view_popular'); + } + + foreach($search_unsafe as $q) { + $q = Helper::escapeLike($q); + $q = "%".$q."%"; + $articles = $articles->where(function ($query) use ($q) { + $query->whereHas('getCategories', function ($query) use ($q){ + $query->where('name', 'like', $q); + }) + ->orWhere('title', 'like', $q) + ->orWhere('url', 'like', $q) + ->orWhere('excerpt_html', 'like', $q); + }); + } + $count = $articles->count(); + + if ( "on" == $request->input("onlypopular") ) { + $articles = $articles->orderBy('impact', 'desc'); + } + $articles = $articles->orderBy('created_at', 'desc'); + + return Helper::makeFeed($articles->take(20)->get(), $request->input("q")); + } +} diff --git a/app/Libraries/Helper.php b/app/Libraries/Helper.php index d85d216..69e7de6 100644 --- a/app/Libraries/Helper.php +++ b/app/Libraries/Helper.php @@ -15,4 +15,77 @@ class Helper { public static function formatTimestamp($timestamp) { return Carbon::createFromTimestamp($timestamp)->format("Y-m-d"); } + + public static function makeFeed($model, $title, $cache = 60) { + + switch($title) { + case("new"): + $feed_title = 'Newest Articles'; + $feed_description = 'Newest interesting articles from Wikipedia. Keep exploring.'; + break; + case( "popular"): + $feed_title = 'Popular Articles'; + $feed_description = 'The most popular articles. Keep exploring.'; + break; + default: + $feed_title = 'Search for: ' . $title; + $feed_description = 'All articles for "' . $title . '".'; + break; + } + // create new feed + $feed = \App::make("feed"); + + // multiple feeds are supported + // if you are using caching you should set different cache keys for your feeds + + // cache the feed for $cache minutes (second parameter is optional) + $feed->setCache($cache, 'feed_' . $title); + + // check if there is cached feed and build new only if is not + if (!$feed->isCached()) + { + + // set your feed's title, description, link, pubdate and language + $feed->title = $feed_title; + $feed->description = $feed_description; + #$feed->logo = 'http://yoursite.tld/logo.jpg'; + $feed->link = url('feed/' . $title); + $feed->setDateFormat('datetime'); // 'datetime', 'timestamp' or 'carbon' + $feed->pubdate = $model[0]->created_at; + $feed->lang = 'en'; + $feed->setShortening(true); // true or false + $feed->setTextLimit(100); // maximum length of description text + + foreach ($model as $post) + { + + $desc = ($post->excerpt_html); + + $categories = "
"; + foreach( $post->getCategories()->get() as $cat ) { + $categories .= "name) ."'>". $cat->name . " | "; + } + $categories = rtrim($categories, " | "); + $desc .= "
Topics:"; + $desc .= $categories; + + $discussions = "
"; + foreach( $post->getDiscussions()->orderBy('comments', 'desc')->get() as $dis ) { + $discussions .= "" . $dis->title . " "; + $discussions .= $dis->upvotes . " Upvotes | " . $dis->comments . " Comments
"; + } + $desc .= "

Discussions:"; + $desc .= $discussions; + + // set item's title, author, url, pubdate, description, content, enclosure (optional)* + $feed->add($post->title, env('APP_NAME'), \URL::to($post->url), $post->created_at, $desc, $desc); + } + + } + + // first param is the feed format + // optional: second param is cache duration (value of 0 turns off caching) + // optional: you can set custom cache key with 3rd param as string + return $feed->render('atom', $cache, 'feed_' . $title); + } } diff --git a/composer.json b/composer.json index afb27a9..6952835 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,8 @@ "fruitcake/laravel-cors": "^1.0", "guzzlehttp/guzzle": "^6.3", "laravel/framework": "^7.0", - "laravel/tinker": "^2.0" + "laravel/tinker": "^2.0", + "laravelium/feed": "^7.0" }, "require-dev": { "facade/ignition": "^2.0", diff --git a/composer.lock b/composer.lock index df443cd..837d032 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "5cb6e369ed424d05439b41685b0d3441", + "content-hash": "d5d6501f3e93b2aa424fb9448c79439d", "packages": [ { "name": "asm89/stack-cors", @@ -683,6 +683,7 @@ "email": "jakub.onderka@gmail.com" } ], + "abandoned": "php-parallel-lint/php-console-color", "time": "2018-09-29T17:23:10+00:00" }, { @@ -729,6 +730,7 @@ } ], "description": "Highlight PHP code in terminal", + "abandoned": "php-parallel-lint/php-console-highlighter", "time": "2018-09-29T18:48:56+00:00" }, { @@ -946,6 +948,70 @@ ], "time": "2020-03-17T15:34:59+00:00" }, + { + "name": "laravelium/feed", + "version": "v7.0.1", + "source": { + "type": "git", + "url": "https://gitlab.com/Laravelium/Feed.git", + "reference": "e7f108f49abec3a3c836636a5c8fb1d3b8d3d1d7" + }, + "dist": { + "type": "zip", + "url": "https://gitlab.com/api/v4/projects/Laravelium%2FFeed/repository/archive.zip?sha=e7f108f49abec3a3c836636a5c8fb1d3b8d3d1d7", + "reference": "e7f108f49abec3a3c836636a5c8fb1d3b8d3d1d7", + "shasum": "" + }, + "require": { + "illuminate/filesystem": "^7.0", + "illuminate/support": "^7.0", + "php": ">=7.2.5" + }, + "require-dev": { + "laravel/framework": "^7.0", + "orchestra/testbench-core": "^5.0", + "phpunit/phpunit": "^8.0" + }, + "type": "library", + "extra": { + "laravel": { + "providers": [ + "Laravelium\\Feed\\FeedServiceProvider" + ], + "aliases": { + "Feed": "Laravelium\\Feed\\Feed" + } + } + }, + "autoload": { + "psr-0": { + "Laravelium\\Feed": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Rumen Damyanov", + "email": "r@alfamatter.com", + "homepage": "https://darumen.com", + "role": "Developer" + } + ], + "description": "Laravelium Feed package for Laravel.", + "homepage": "https://gitlab.com/Laravelium", + "keywords": [ + "atom", + "feed", + "generator", + "laravel", + "laravelium", + "rss" + ], + "time": "2020-03-28T12:53:42+00:00" + }, { "name": "league/commonmark", "version": "1.3.1", diff --git a/public/img/rss-square.svg b/public/img/rss-square.svg new file mode 100644 index 0000000..ff9a15a --- /dev/null +++ b/public/img/rss-square.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/public/img/rss.svg b/public/img/rss.svg new file mode 100644 index 0000000..e6fa54c --- /dev/null +++ b/public/img/rss.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/resources/views/layouts/app.blade.php b/resources/views/layouts/app.blade.php index a9cbf37..5671edb 100644 --- a/resources/views/layouts/app.blade.php +++ b/resources/views/layouts/app.blade.php @@ -22,6 +22,9 @@ + @if ( "new" == Request::route()->getName() || "popular" == Request::route()->getName() ) + {!! \Feed::link( url('feed/' . Request::route()->getName()), 'atom', 'RSS-Feed for ' . Request::route()->getName() . ' Articles'); !!} + @endif
@@ -55,7 +58,7 @@ @endif