-
Notifications
You must be signed in to change notification settings - Fork 15
/
_api.php
258 lines (237 loc) · 6.92 KB
/
_api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
/**
* This file pulls in member/affiliate data from the json files
* and defines helper functions that may be used throughout the website
*/
class NiwaDataHelper
{
/**
* JSON object of member wikis
*
* @var object
*/
protected $memberWikis;
/**
* JSON object of affiliate wikis
*
* @var object
*/
protected $affiliates;
/**
* JSON object of wiki Cross-Wiki Weekend guides
*
* @var object
*/
protected $cww;
const URL_REPLACE_STRING = "$1";
function __construct()
{
$this->memberWikis = json_decode(
file_get_contents("data/members.json")
);
$this->affiliates = json_decode(
file_get_contents("data/affiliates.json")
);
$this->cww = json_decode(
file_get_contents("data/cww.json")
);
}
/**
* Return all member wikis or if language code given,
* all wikis for that language code.
* $includeFormer may be set to true to retrieve former members.
*
* @param string $languageCode
* @param string $former
* @return object
*/
public function getMemberWikis($languageCode = null, $includeFormer = null)
{
$filteredMembers = [];
if ($languageCode) {
// If specific language is requested...
if($includeFormer) {
$filteredMembers = $this->memberWikis->{$languageCode};
} else {
$filteredMembers = array_filter(
$this->memberWikis->{$languageCode},
function($val){
return !($val->former ?? FALSE);
});
}
} else {
// If all languages are requested...
// Merge language arrays:
$allMemberWikis = [];
foreach ($this->memberWikis as $memberWikis) {
$allMemberWikis = array_merge($allMemberWikis, $memberWikis);
}
if ($includeFormer) {
$filteredMembers = $allMemberWikis;
} else {
$filteredMembers = array_filter(
$allMemberWikis,
function($val){
return !($val->former ?? FALSE);
});
}
}
return $filteredMembers;
}
/**
* Return the affiliates
*
* @return object
*/
public function getAffiliates()
{
return $this->affiliates;
}
/**
* Return count of all member wikis or if language code given,
* count of all wikis for that language code.
*
* @param string $languageCode
* @return int
*/
public function getMemberWikiCount($languageCode = null)
{
if ($languageCode) {
return count(array_filter($this->memberWikis->{$languageCode}, function($val) {
return !($val->former ?? FALSE);
}));
}
$wikiCount = 0;
foreach ($this->memberWikis as $languageMemberWikis) {
$wikiCount += count(array_filter($languageMemberWikis, function($val){
return !($val->former ?? FALSE);
}));
}
return $wikiCount;
}
/**
* Return count of all affiliates
*
* @return int
*/
public function getAffiliateCount()
{
return count($this->affiliates);
}
/**
* Returns a link for the given wiki's interwiki url and page
*
* @param string $url Interwiki URL
* @param string $page Wiki page
* @return string
*/
public function getWikiLink($url, $page = "")
{
return str_replace(self::URL_REPLACE_STRING, urlencode($page), $url);
}
/**
* Returns a given wiki's mainpage
*
* @param object $wiki
* @return string
*/
public function getWikiMainpage($wiki)
{
return $this->getWikiLink($wiki->url, $wiki->mainpage);
}
/**
* Generates a link for the member or affiliate
*
* @param string $url The anchor tag href
* @param string $text The anchor tag display text
* @return string
*/
protected function generateMemberLink($url, $text)
{
return "<a class='member-wiki-link' href='{$url}'>{$text}</a>";
}
/**
* Return all member wikis or if language code given,
* all wikis for that language code.
*
* @param string $languageCode
* @return object
*/
public function getCWW($year, $languageCode = null)
{
if ($languageCode) {
return $this->cww->{$year}->{$languageCode};
}
return $this->cww->{$year};
}
/**
* Generates the HTML string for links with error checking for wikis that do not have
* one of the options.
*
* Requires a individual wiki array from the api.
*
* @param object $member
* @return string $links
*/
public function generateMemberLinks($member)
{
// Check if wiki mainpage is specified or if we should just use the URL as-is
if (isset($member->mainpage)) {
$links = $this->generateMemberLink(
$this->getWikiMainpage($member),
$member->title
);
} else {
$links = $this->generateMemberLink(
$member->url,
$member->title
);
}
if (isset($member->site)) {
$links .= $this->generateMemberLink($member->site, $member->siteName);
};
if (isset($member->forums)) {
$links .= $this->generateMemberLink($member->forums, "Forums");
};
if (isset($member->chat)) {
$links .= $this->generateMemberLink($member->chat, "Chat");
};
if (isset($member->discord)) {
$links .= $this->generateMemberLink($member->discord, '<img src="/images/social/discord.png" alt="Discord" width="20" />');
};
if (isset($member->instagram)) {
$links .= $this->generateMemberLink($member->instagram, '<img src="/images/social/instagram.png" alt="Instagram" width="20" />');
};
if (isset($member->twitter)) {
$links .= $this->generateMemberLink($member->twitter, '<img src="/images/social/twitter.png" alt="Twitter" width="20" />');
};
if (isset($member->mastodon)) {
$links .= $this->generateMemberLink($member->mastodon, '<img src="/images/social/mastodon.png" alt="Mastodon" width="20" />');
};
if (isset($member->bluesky)) {
$links .= $this->generateMemberLink($member->bluesky, '<img src="/images/social/bluesky.png" alt="Bluesky" width="20" />');
};
if (isset($member->twitch)) {
$links .= $this->generateMemberLink($member->twitch, '<img src="/images/social/twitch.png" alt="Twitch" width="20" />');
};
if (isset($member->facebook)) {
$links .= $this->generateMemberLink($member->facebook, '<img src="/images/social/facebook.png" alt="Facebook" width="20" />');
};
if (isset($member->tumblr)) {
$links .= $this->generateMemberLink($member->tumblr, '<img src="/images/social/tumblr.png" alt="Tumblr" width="20" />');
};
if (isset($member->reddit)) {
$links .= $this->generateMemberLink($member->reddit, '<img src="/images/social/reddit.png" alt="Reddit" width="20" />');
};
if (isset($member->telegram)) {
$links .= $this->generateMemberLink($member->telegram, '<img src="/images/social/telegram.png" alt="Telegram" width="20" />');
};
if (isset($member->tiktok)) {
$links .= $this->generateMemberLink($member->tiktok, '<img src="/images/social/tiktok.png" alt="TikTok" width="20" />');
};
if (isset($member->youtube)) {
$links .= $this->generateMemberLink($member->youtube, '<img src="/images/social/youtube.png" alt="YouTube" width="20" />');
};
return $links;
}
}