-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfunctions.php
257 lines (207 loc) · 6.18 KB
/
functions.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
<?php
function mw_request($action, $params) {
$ch = curl_init();
$cwd = dirname(__FILE__);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cwd . '/cookies.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, $cwd . '/cookies.txt');
if(is_array($params)) {
curl_setopt($ch, CURLOPT_URL, Config::$wikiAPI);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array_merge(array(
'action' => $action,
'format' => 'json'
), $params)));
} else {
$params = array(
'action' => $action,
'format' => 'json'
);
curl_setopt($ch, CURLOPT_URL, Config::$wikiAPI . '?' . http_build_query($params));
}
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$json = curl_exec($ch);
return json_decode($json);
}
function mw_get_raw_page($rev_id) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, Config::$wikiAPI . '?oldid=' . $rev_id . '&action=raw');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
return curl_exec($ch);
}
function mw_entry($title) {
$titleURI = pageTitleToURL($title);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, Config::$wikiBaseURL . $titleURI);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$response = curl_exec($ch);
$parser = new \mf2\Parser($response, Config::$wikiBaseURL . $titleURI);
$output = $parser->parse();
$item = false;
if(array_key_exists('items', $output)) {
foreach($output['items'] as $it) {
if(array_key_exists('type', $it) && (in_array('h-entry', $it['type']) || in_array('h-event', $it['type']))) {
$item = $it;
}
}
}
return $item;
}
function pageTitleToURL($s) {
// Copied mostly from Mediawiki's includes/GlobalFunctions.php file
static $needle;
if ( is_null( $needle ) ) {
$needle = array( '%3B', '%40', '%24', '%21', '%2A', '%28', '%29', '%2C', '%2F' );
}
$s = urlencode( str_replace(' ','_',$s) );
$s = str_ireplace(
$needle,
array( ';', '@', '$', '!', '*', '(', ')', ',', '/', ':' ),
$s
);
return $s;
}
function parse_page($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$response = curl_exec($ch);
$parser = new \mf2\Parser($response, $url);
return $parser->parse();
}
function download_photo($url, $date) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$response = curl_exec($ch);
$hash = sha1($response);
$archiveFolder = Config::$publicPath . 'images/' . date('Y-m-d', $date);
if(!is_dir($archiveFolder))
mkdir($archiveFolder);
$tmp = tempnam(sys_get_temp_dir(), 'iwc-'.$hash);
file_put_contents($tmp, $response);
$type = exif_imagetype($tmp);
if($type == IMAGETYPE_JPEG)
$ext = 'jpg';
elseif($type == IMAGETYPE_PNG)
$ext = 'png';
elseif($type == IMAGETYPE_GIF)
$ext = 'gif';
else
$ext = 'jpg';
if(filesize($tmp) <= 5242880) { # don't download the file if it's more than 5mb (twitter's limit)
$filename = $hash . '.' . $ext;
rename($tmp, $archiveFolder . '/' . $filename);
chmod($archiveFolder . '/' . $filename, 0644);
return date('Y-m-d', $date) . '/' . $filename;
} else {
return false;
}
}
function join_with_and($array) {
if(count($array) == 0)
return '';
if(count($array) == 1)
return array_pop($array);
if(count($array) == 2)
return implode(' and ', $array);
$last = array_pop($array);
return implode(', ', $array) . ', and ' . $last;
}
function e($text) {
return htmlspecialchars($text);
}
function tweet_text($text) {
$body = http_build_query([
'h' => 'entry',
'content' => $text,
]);
$headers = [
'Authorization: Bearer '.Config::$twitterSyndicateToken
];
$ch = curl_init('https://silopub.p3k.io/micropub');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
return $response;
}
## Events
/**
* For text summaries in the form "event_name was..." or "event_name is...",
* return the event_name. If " was " or " is " are not found,
* return the $fallback value
*/
function eventNameFromSummary(string $summary, string $fallback) {
if (($pos = strpos($summary, ' was ')) !== false) {
return substr($summary, 0, $pos);
}
if (($pos = strpos($summary, ' is ')) !== false) {
return substr($summary, 0, $pos);
}
return $fallback;
}
/**
* Extract YYYY-MM-DD from page names
* On the wiki, event page names *should* be in
* the format events/YYYY-MM-DD-event-slug
*/
function eventDateFromTitle(string $title) {
preg_match('/\d{4}-\d{2}-\d{2}/', $title, $matches);
if (count($matches) > 0) {
return $matches[0];
}
return null;
}
/**
* Add an event to the the list, grouping them
* by event name.
*/
function addEventToList(array &$events, string $name, string $summary) {
$key = eventNameFromSummary($summary, $name);
if (!array_key_exists($key, $events)) {
$events[$key] = [];
}
$date = eventDateFromTitle($name);
$events[$key][$name] = $date;
}
/**
* Convert an array of wiki event slugs and YYYY-MM-DD
* dates into an array of links.
*/
function buildEventDateLinks(array $event_dates) {
$results = [];
foreach ($event_dates as $title => $date) {
$label = $date;
if (!$date) {
$label = str_replace('events/', '', $title);
}
$results[] = sprintf('<a href="%s">%s</a>',
Config::$wikiBaseURL . pageTitleToURL($title),
$label
);
}
return $results;
}
function buildEventNotes(array $events) {
$output = '';
foreach ($events as $event_name => &$options) {
$without_dates = array_filter($options, 'is_null');
$with_dates = array_filter($options);
# sort events with dates, newest first
arsort($with_dates);
# move events without dates to the end
$options = $with_dates + $without_dates;
$links = buildEventDateLinks($options);
$output .= PHP_EOL . sprintf('<p><b>%s:</b> %s</p>',
$event_name,
implode(', ', $links)
);
}
return $output;
}