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 | | |||
32 | 33 | /** | |||
33 | 34 | * Extract feeds from database | |||
35 | */ | ||||
34 | class Feeds | 36 | class Feeds | ||
35 | { | 37 | { | ||
36 | static function news ($count) | 38 | /** | ||
39 | * @params int $count Number of news that need to be fetched | ||||
40 | * @return array an array of news | ||||
41 | */ | ||||
42 | static function news(int $count): array | ||||
37 | { | 43 | { | ||
38 | $output = []; | 44 | $output = []; | ||
39 | $request = DB::query('SELECT title, url, timestamp FROM news ORDER BY timestamp DESC LIMIT '.$count); | 45 | try { | ||
40 | $num = DB::numrows($request); | 46 | $db = new DB(); | ||
47 | $stmt = $db->prepare('SELECT title, url, timestamp FROM news ORDER BY timestamp DESC LIMIT :count'); | ||||
48 | $stmt->execute(['count' => $count]); | ||||
41 | 49 | | |||
42 | for($x = 0; $x < $num; $x++) { | 50 | while ($row = $stmt->fetch()) { | ||
43 | $data = DB::fetch_assoc($request); | | |||
44 | $output[] = [ | 51 | $output[] = [ | ||
45 | 'title' => $data['title'], | 52 | 'title' => $row['title'], | ||
46 | 'time' => $data['timestamp'], | 53 | 'time' => $row['timestamp'], | ||
47 | 'url' => $data['url'], | 54 | 'url' => $row['url'], | ||
48 | 'source' => 'news', | 55 | 'source' => 'news', | ||
49 | 'user' => 'KDE Community', | 56 | 'user' => 'KDE Community', | ||
50 | ]; | 57 | ]; | ||
51 | } | 58 | } | ||
52 | 59 | } catch (PDOException $e) { | |||
53 | DB::free_result($request); | 60 | print "Error!: " . $e->getMessage() . "<br/>"; | ||
61 | } | ||||
54 | return $output; | 62 | return $output; | ||
55 | } | 63 | } | ||
56 | 64 | | |||
57 | 65 | | |||
58 | static function blog($count) | 66 | /** | ||
67 | * @params int $count Number of blog posts that need to be fetched | ||||
68 | * @return array an array of blog posts | ||||
69 | * @deprecated | ||||
70 | */ | ||||
71 | static function blog(int $count): array | ||||
59 | { | 72 | { | ||
60 | $output = []; | 73 | $output = []; | ||
61 | $request = DB::query('SELECT user, title, url, timestamp FROM blog ORDER BY timestamp DESC LIMIT ' . $count); | 74 | try { | ||
62 | $num = DB::numrows($request); | 75 | $db = new DB(); | ||
76 | $stmt = $db->prepare('SELECT title, url, timestamp FROM blog ORDER BY timestamp DESC LIMIT :count'); | ||||
77 | $stmt->execute(['count' => $count]); | ||||
63 | 78 | | |||
64 | for($x = 0; $x < $num; $x++) { | 79 | while ($row = $stmt->fetch()) { | ||
65 | $data = DB::fetch_assoc($request); | | |||
66 | $output[] = [ | 80 | $output[] = [ | ||
67 | 'title' => $data['title'], | 81 | 'title' => $row['title'], | ||
68 | 'time' => $data['timestamp'], | 82 | 'time' => $row['timestamp'], | ||
69 | 'url' => $data['url'], | 83 | 'url' => $row['url'], | ||
70 | 'source' => 'blog', | 84 | 'source' => 'blog', | ||
71 | 'user' => $data['user'] | 85 | 'user' => $row['user'], | ||
72 | ]; | 86 | ]; | ||
73 | } | 87 | } | ||
88 | } catch (PDOException $e) { | ||||
89 | print "Error!: " . $e->getMessage() . "<br/>"; | ||||
90 | } | ||||
91 | return $output; | ||||
92 | } | ||||
74 | 93 | | |||
75 | DB::free_result($request); | 94 | static function planet(int $count): array | ||
95 | { | ||||
96 | $feed = simplexml_load_file('https://planet.kde.org/rss20.xml'); | ||||
97 | $output = []; | ||||
98 | if ($feed) { | ||||
99 | for($i = 0 ; $i <= $count ; $i++) { | ||||
100 | $item = $feed->channel->item[$i]; | ||||
101 | $output[] = [ | ||||
102 | 'title' => (string) $item->title, | ||||
103 | 'url' => (string) $item->link, | ||||
104 | ]; | ||||
105 | } | ||||
106 | } | ||||
76 | return $output; | 107 | return $output; | ||
77 | } | 108 | } | ||
78 | } | 109 | } |