Changeset View
Changeset View
Standalone View
Standalone View
aetherlibs/feeds.php
1 | <?php | 1 | <?php | ||
---|---|---|---|---|---|
2 | 2 | | |||
3 | /** | 3 | /** | ||
4 | * News Lib | 4 | * News Lib | ||
5 | * | 5 | * | ||
6 | * @author Frank Karlitschek | 6 | * @author Frank Karlitschek | ||
7 | * @copyright 2010 Frank Karlitschek karlitschek@kde.org | 7 | * @copyright 2010 Frank Karlitschek karlitschek@kde.org | ||
8 | * | 8 | * | ||
9 | * @modifications Added default image fallback | 9 | * @modifications Added default image fallback | ||
10 | * Improve code readability | 10 | * Improve code readability | ||
11 | * @author Sayak Banerjee | 11 | * @author Sayak Banerjee | ||
12 | * @copyright 2010 Sayak Banerjee <sayakb@kde.org> | 12 | * @copyright 2010 Sayak Banerjee <sayakb@kde.org> | ||
13 | * | 13 | * | ||
14 | * @author Ken Vermette | 14 | * @author Ken Vermette | ||
15 | * @copyright 2017 Ken Vermette <vermette@gmail.com> | 15 | * @copyright 2017 Ken Vermette <vermette@gmail.com> | ||
16 | * @copyright 2019 Carl Schwan <carl@carlschwan.eu> | ||||
16 | * | 17 | * | ||
17 | * This library is free software; you can redistribute it and/or | 18 | * This library is free software; you can redistribute it and/or | ||
18 | * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE | 19 | * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE | ||
19 | * License as published by the Free Software Foundation; either | 20 | * License as published by the Free Software Foundation; either | ||
20 | * version 3 of the License, or any later version. | 21 | * version 3 of the License, or any later version. | ||
21 | * | 22 | * | ||
22 | * This library is distributed in the hope that it will be useful, | 23 | * This library is distributed in the hope that it will be useful, | ||
23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 24 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 25 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
25 | * GNU AFFERO GENERAL PUBLIC LICENSE for more details. | 26 | * GNU AFFERO GENERAL PUBLIC LICENSE for more details. | ||
26 | * | 27 | * | ||
27 | * You should have received a copy of the GNU Lesser General Public | 28 | * You should have received a copy of the GNU Lesser General Public | ||
28 | * License along with this library. If not, see <http://www.gnu.org/licenses/>. | 29 | * License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||
29 | * | 30 | * | ||
30 | */ | 31 | */ | ||
31 | 32 | | |||
33 | require_once __DIR__.'/../vendor/autoload.php'; | ||||
32 | 34 | | |||
35 | use Symfony\Contracts\Cache\ItemInterface; | ||||
36 | use Symfony\Component\Cache\Adapter\FilesystemAdapter; | ||||
33 | 37 | | |||
38 | /** | ||||
39 | * Extract feeds from database | ||||
40 | */ | ||||
34 | class Feeds | 41 | class Feeds | ||
35 | { | 42 | { | ||
36 | static function news ($count) | 43 | private $db; | ||
44 | | ||||
45 | /** | ||||
46 | * @params PDO $db Database connection | ||||
47 | */ | ||||
48 | public function __construct(PDO $db) | ||||
49 | { | ||||
50 | $this->db = $db; | ||||
51 | } | ||||
52 | | ||||
53 | /** | ||||
54 | * @params int $count Number of news that need to be fetched | ||||
55 | * @return array an array of news | ||||
56 | */ | ||||
57 | function news(int $count): array | ||||
37 | { | 58 | { | ||
38 | $output = []; | 59 | $output = []; | ||
39 | $request = DB::query('SELECT title, url, timestamp FROM news ORDER BY timestamp DESC LIMIT '.$count); | 60 | try { | ||
40 | $num = DB::numrows($request); | 61 | $stmt = $this->db->prepare('SELECT title, url, timestamp FROM news ORDER BY timestamp DESC LIMIT :count'); | ||
62 | $stmt->execute(['count' => $count]); | ||||
41 | 63 | | |||
42 | for($x = 0; $x < $num; $x++) { | 64 | while ($row = $stmt->fetch()) { | ||
43 | $data = DB::fetch_assoc($request); | | |||
44 | $output[] = [ | 65 | $output[] = [ | ||
45 | 'title' => $data['title'], | 66 | 'title' => $row['title'], | ||
46 | 'time' => $data['timestamp'], | 67 | 'time' => $row['timestamp'], | ||
47 | 'url' => $data['url'], | 68 | 'url' => $row['url'], | ||
48 | 'source' => 'news', | 69 | 'source' => 'news', | ||
49 | 'user' => 'KDE Community', | 70 | 'user' => 'KDE Community', | ||
50 | ]; | 71 | ]; | ||
51 | } | 72 | } | ||
52 | 73 | } catch (PDOException $e) { | |||
53 | DB::free_result($request); | 74 | print "Error!: " . $e->getMessage() . "<br/>"; | ||
75 | } | ||||
54 | return $output; | 76 | return $output; | ||
55 | } | 77 | } | ||
56 | 78 | | |||
57 | 79 | | |||
58 | static function blog($count) | 80 | /** | ||
81 | * @params int $count Number of blog posts that need to be fetched | ||||
82 | * @return array an array of blog posts | ||||
83 | * @deprecated | ||||
84 | */ | ||||
85 | function blog(int $count): array | ||||
59 | { | 86 | { | ||
60 | $output = []; | 87 | $output = []; | ||
61 | $request = DB::query('SELECT user, title, url, timestamp FROM blog ORDER BY timestamp DESC LIMIT ' . $count); | 88 | try { | ||
62 | $num = DB::numrows($request); | 89 | $db = new DB(); | ||
90 | $stmt = $this->db->prepare('SELECT title, url, timestamp FROM blog ORDER BY timestamp DESC LIMIT :count'); | ||||
91 | $stmt->execute(['count' => $count]); | ||||
63 | 92 | | |||
64 | for($x = 0; $x < $num; $x++) { | 93 | while ($row = $stmt->fetch()) { | ||
65 | $data = DB::fetch_assoc($request); | | |||
66 | $output[] = [ | 94 | $output[] = [ | ||
67 | 'title' => $data['title'], | 95 | 'title' => $row['title'], | ||
68 | 'time' => $data['timestamp'], | 96 | 'time' => $row['timestamp'], | ||
69 | 'url' => $data['url'], | 97 | 'url' => $row['url'], | ||
70 | 'source' => 'blog', | 98 | 'source' => 'blog', | ||
71 | 'user' => $data['user'] | 99 | 'user' => $row['user'], | ||
72 | ]; | 100 | ]; | ||
73 | } | 101 | } | ||
102 | } catch (PDOException $e) { | ||||
103 | print "Error!: " . $e->getMessage() . "<br/>"; | ||||
104 | } | ||||
105 | return $output; | ||||
106 | } | ||||
74 | 107 | | |||
75 | DB::free_result($request); | 108 | static function planet(int $count): array | ||
109 | { | ||||
110 | $cache = new FilesystemAdapter(); | ||||
111 | $output = $cache->get('feedrss', function (ItemInterface $item) use ($count) { | ||||
112 | $item->expiresAfter(3600); | ||||
113 | | ||||
114 | $feed = simplexml_load_file('https://planet.kde.org/rss20.xml'); | ||||
115 | $output = []; | ||||
116 | if ($feed) { | ||||
117 | for($i = 0 ; $i <= $count ; $i++) { | ||||
118 | $item = $feed->channel->item[$i]; | ||||
119 | $output[] = [ | ||||
120 | 'title' => (string) $item->title, | ||||
121 | 'url' => (string) $item->link, | ||||
122 | ]; | ||||
123 | } | ||||
124 | } | ||||
125 | | ||||
126 | return $output; | ||||
127 | }); | ||||
76 | return $output; | 128 | return $output; | ||
77 | } | 129 | } | ||
78 | } | 130 | } |