1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* * Common controller |
5
|
|
|
* * |
6
|
|
|
* The file contains every function needed in many other controllers. |
7
|
|
|
* |
8
|
|
|
* * @category Controllers |
9
|
|
|
* * @package SuperHive |
10
|
|
|
* * @author Florent Kosmala <[email protected]> |
11
|
|
|
* * @license https://www.gnu.org/licenses/gpl-3.0.txt GPL-3.0 |
12
|
|
|
* */ |
13
|
|
|
|
14
|
|
|
declare(strict_types=1); |
15
|
|
|
|
16
|
|
|
namespace App\Controllers; |
17
|
|
|
|
18
|
|
|
use Hive\PhpLib\Hive\Condenser as HiveCondenser; |
19
|
|
|
use Psr\Container\ContainerInterface; |
20
|
|
|
|
21
|
|
|
final class CommonController |
22
|
|
|
{ |
23
|
|
|
private ContainerInterface $app; |
24
|
|
|
|
25
|
|
|
public function __construct(ContainerInterface $app) |
26
|
|
|
{ |
27
|
|
|
$this->app = $app; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* * genPostsFile function |
32
|
|
|
* * |
33
|
|
|
* This function will query the blockchain to have posts |
34
|
|
|
* It will generate a JSON file stored in /resources/blog/ folder |
35
|
|
|
* */ |
36
|
|
|
public function genPostsFile(): void |
37
|
|
|
{ |
38
|
|
|
$settings = $this->app->get('settings'); |
39
|
|
|
$result = ''; |
40
|
|
|
|
41
|
|
|
// Hive API communication init |
42
|
|
|
$apiConfig = [ |
43
|
|
|
'hiveNode' => $settings['api'], |
44
|
|
|
'debug' => false, |
45
|
|
|
]; |
46
|
|
|
$api = new HiveCondenser($apiConfig); |
47
|
|
|
|
48
|
|
|
// The file with the latest posts. |
49
|
|
|
$file = $this->app->get('blogfile'); |
50
|
|
|
|
51
|
|
|
// if the JSON file doesn't exist or if it's old, take it from API |
52
|
|
|
if (!file_exists($file) || (time() - filemtime($file) > $settings['delay'])) { |
53
|
|
|
// Prepare API call according to displayed posts type |
54
|
|
|
$displayType = $settings['displayType']['type']; |
55
|
|
|
if ($displayType === 'author') { |
56
|
|
|
if (preg_match('/(hive-\d{6})/i', $settings['author']) == 1) { |
57
|
|
|
$result = json_encode( |
58
|
|
|
$api->getDiscussionsByCreated( |
59
|
|
|
$settings['author'] |
60
|
|
|
), |
61
|
|
|
JSON_PRETTY_PRINT |
62
|
|
|
); |
63
|
|
|
} else { |
64
|
|
|
$dateNow = (new \DateTime())->format('Y-m-d\TH:i:s'); |
65
|
|
|
$result = json_encode( |
66
|
|
|
$api->getDiscussionsByAuthorBeforeDate( |
67
|
|
|
$settings['author'], |
68
|
|
|
'', |
69
|
|
|
$dateNow, |
70
|
|
|
100 |
71
|
|
|
), |
72
|
|
|
JSON_PRETTY_PRINT |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
} elseif (($displayType === 'tag')) { |
76
|
|
|
$displayTag = $settings['displayType']['tag']; |
77
|
|
|
$taggedPosts = []; |
78
|
|
|
$allPosts = json_encode( |
79
|
|
|
$api->getDiscussionsByAuthorBeforeDate( |
80
|
|
|
$settings['author'], |
81
|
|
|
'', |
82
|
|
|
'', |
83
|
|
|
100 |
84
|
|
|
) |
85
|
|
|
); |
86
|
|
|
$allPosts = json_decode($allPosts, true); |
87
|
|
|
foreach ($allPosts as &$post) { |
88
|
|
|
$postMeta = json_decode($post['json_metadata'], true); |
89
|
|
|
$postTags = $postMeta['tags']; |
90
|
|
|
if (in_array($displayTag, $postTags)) { |
91
|
|
|
$taggedPosts[] = $post; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$result = json_encode($taggedPosts, JSON_PRETTY_PRINT); |
96
|
|
|
unset($taggedPosts); |
97
|
|
|
} elseif ($displayType === 'reblog') { |
98
|
|
|
$result = json_encode($api->getDiscussionsByBlog($settings['author']), JSON_PRETTY_PRINT); |
99
|
|
|
} |
100
|
|
|
file_put_contents($file, $result); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* * getMostUsedTags function |
106
|
|
|
* * |
107
|
|
|
* This function will get tags from each post and sort them |
108
|
|
|
* by occurence. |
109
|
|
|
* |
110
|
|
|
* @return array<string, int> $mostUsedTags |
111
|
|
|
* */ |
112
|
|
|
public function getMostUsedTags(): array |
113
|
|
|
{ |
114
|
|
|
$file = $this->app->get('blogfile'); |
115
|
|
|
$tags = ''; |
116
|
|
|
|
117
|
|
|
$data = json_decode(file_get_contents($file), true); |
118
|
|
|
|
119
|
|
|
/* For each post, get metadata, to extract tags only */ |
120
|
|
|
foreach ($data as &$post) { |
121
|
|
|
$meta = json_decode($post['json_metadata'], true); |
122
|
|
|
$tags .= implode(',', $meta['tags']) . ','; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$tagsArray = explode(',', $tags); |
126
|
|
|
$mostUsedTags = array_count_values($tagsArray); //get all occurrences of each values |
127
|
|
|
arsort($mostUsedTags); |
128
|
|
|
$mostUsedTags = array_slice($mostUsedTags, 0, 10, true); |
129
|
|
|
|
130
|
|
|
return $mostUsedTags; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* * getPopularPosts function |
135
|
|
|
* * |
136
|
|
|
* This function will get 5 posts with most upvotes |
137
|
|
|
* |
138
|
|
|
* @return array<string, int> $popularPosts |
139
|
|
|
* */ |
140
|
|
|
public function getPopularPosts(): array |
141
|
|
|
{ |
142
|
|
|
$file = $this->app->get('blogfile'); |
143
|
|
|
$data = json_decode(file_get_contents($file), true); |
144
|
|
|
|
145
|
|
|
uasort($data, function ($a, $b) { |
146
|
|
|
if (strpos($a['body'], 'cross post') === false) { |
147
|
|
|
$a = count($a['active_votes']); |
148
|
|
|
$b = count($b['active_votes']); |
149
|
|
|
return ($a == $b) ? 0 : (($a > $b) ? -1 : 1); |
150
|
|
|
} |
151
|
|
|
}); |
152
|
|
|
|
153
|
|
|
$popularPosts = array_slice($data, 0, 5, true); |
154
|
|
|
|
155
|
|
|
return $popularPosts; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* * getLastPosts function |
160
|
|
|
* * |
161
|
|
|
* This function will get 5 posts with most upvotes |
162
|
|
|
* |
163
|
|
|
* @return array<string, int> $lastPosts |
164
|
|
|
* */ |
165
|
|
|
public function getLastPosts(): array |
166
|
|
|
{ |
167
|
|
|
$file = $this->app->get('blogfile'); |
168
|
|
|
$data = json_decode(file_get_contents($file), true); |
169
|
|
|
|
170
|
|
|
$lastPosts = array_slice($data, 0, 5, true); |
171
|
|
|
|
172
|
|
|
return $lastPosts; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|