Repository navigation
Expand file tree
/
Copy pathinteroperability.php
More file actions
432 lines (387 loc) · 12.3 KB
/
Copy pathinteroperability.php
File metadata and controls
432 lines (387 loc) · 12.3 KB
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
<?php
// +--------------------------------------------------------------------------+
// | Maps Plugin 1.6.0 |
// +--------------------------------------------------------------------------+
// | Maintainer: ::Ben |
// | interoperability.php |
// | |
// | Structured content interoperability for Geeklog consumers. |
// +--------------------------------------------------------------------------+
if (!defined('VERSION')) {
die('This file can not be used on its own.');
}
/**
* Return the canonical public URL for a map.
*
* @param int $mid
* @return string
*/
function MAPS_contentUrl($mid)
{
global $_MAPS_CONF;
$mid = (int) $mid;
if ($mid <= 0) {
return '';
}
return rtrim($_MAPS_CONF['site_url'], '/') . '/index.php?mode=map&mid=' . $mid;
}
/**
* Return the canonical public URL for a marker.
*
* @param string|int $mkid
* @return string
*/
function MAPS_markerContentUrl($mkid)
{
global $_MAPS_CONF;
$mkid = trim((string) $mkid);
if ($mkid === '') {
return '';
}
return rtrim($_MAPS_CONF['site_url'], '/') . '/index.php?mode=marker&mkid='
. rawurlencode($mkid);
}
/**
* Normalize a marker row to the same Item Info contract used for maps.
*
* @param array $row
* @return array
*/
function MAPS_markerContentNormalizeRow($row)
{
if (!is_array($row) || !isset($row['mkid']) || trim((string) $row['mkid']) === '') {
return array();
}
$mkid = trim((string) $row['mkid']);
$title = trim(MAPS_decodeStoredText(isset($row['name']) ? $row['name'] : ''));
$description = trim(MAPS_decodeStoredText(isset($row['description']) ? $row['description'] : ''));
if ($title === '') {
$title = '#' . $mkid;
}
$excerpt = trim(strip_tags($description));
if (function_exists('COM_truncate')) {
$excerpt = COM_truncate($excerpt, 255, '...');
} elseif (strlen($excerpt) > 255) {
$excerpt = substr($excerpt, 0, 252) . '...';
}
$created = isset($row['created']) ? strtotime($row['created']) : false;
$modified = isset($row['modified']) ? strtotime($row['modified']) : false;
$uid = isset($row['owner_id']) ? (int) $row['owner_id'] : 0;
return array(
'id' => 'marker:' . $mkid,
'title' => $title,
'url' => MAPS_markerContentUrl($mkid),
'description' => $description,
'excerpt' => $excerpt,
'date-created' => ($created === false ? 0 : $created),
'date-modified' => ($modified === false ? 0 : $modified),
'uid' => $uid,
'author' => ($uid > 0 && function_exists('COM_getDisplayName')) ? COM_getDisplayName($uid) : '',
'type' => 'maps',
'subtype' => 'marker'
);
}
/**
* Return one public/accessible marker for Item Info consumers.
*
* @param string $mkid
* @param int $uid
* @return array
*/
function MAPS_markerContentQuery($mkid, $uid = 0)
{
global $_TABLES;
$mkid = trim((string) $mkid);
if ($mkid === '') {
return array();
}
$sql = "SELECT mk.mkid,mk.mid,mk.name,mk.description,mk.created,mk.modified,mk.owner_id "
. "FROM {$_TABLES['maps_markers']} mk "
. "INNER JOIN {$_TABLES['maps_maps']} m ON m.mid=mk.mid "
. "WHERE mk.mkid='" . MAPS_dbEscape($mkid) . "' "
. "AND mk.active=1 AND mk.hidden=0 AND m.active=1 AND m.hidden=0"
. COM_getPermSQL('AND', (int) $uid, 2, 'mk')
. COM_getPermSQL('AND', (int) $uid, 2, 'm')
. " LIMIT 1";
$result = DB_query($sql);
if (!$result || DB_numRows($result) === 0) {
return array();
}
return MAPS_markerContentNormalizeRow(DB_fetchArray($result));
}
/**
* Emit a marker-save lifecycle event and refresh every affected parent map.
* The marker reference is namespaced so it can never collide with a map ID.
*
* @param string $mkid
* @param int $mid
* @param int $previousMid
* @param bool $notifyParents
* @return void
*/
function MAPS_notifyMarkerSaved($mkid, $mid = 0, $previousMid = 0, $notifyParents = true)
{
$mkid = trim((string) $mkid);
if ($mkid === '') {
return;
}
PLG_itemSaved('marker:' . $mkid, 'maps');
if (!$notifyParents) {
return;
}
$mid = (int) $mid;
$previousMid = (int) $previousMid;
if ($mid > 0) {
updateMap($mid);
}
if ($previousMid > 0 && $previousMid !== $mid) {
updateMap($previousMid);
}
}
/**
* Emit a marker-delete lifecycle event and refresh its former parent map.
* URL consumers can still resolve marker:<id> after deletion because the
* canonical URL is deterministic and does not require a database lookup.
*
* @param string $mkid
* @param int $mid
* @return void
*/
function MAPS_notifyMarkerDeleted($mkid, $mid = 0)
{
$mkid = trim((string) $mkid);
if ($mkid === '') {
return;
}
PLG_itemDeleted('marker:' . $mkid, 'maps');
$mid = (int) $mid;
if ($mid > 0) {
updateMap($mid);
}
}
/**
* Normalize a requested Item Info field list.
*
* @param string|array $what
* @return array
*/
function MAPS_contentRequestedFields($what)
{
$allowed = array(
'id', 'title', 'url', 'description', 'excerpt',
'date-created', 'date-modified', 'uid', 'author', 'type', 'subtype'
);
if (is_array($what)) {
$requested = $what;
} else {
$what = trim((string) $what);
if ($what === '' || $what === '*' || strtolower($what) === 'all') {
return $allowed;
}
$requested = explode(',', $what);
}
$fields = array();
foreach ($requested as $field) {
$field = strtolower(trim((string) $field));
if ($field !== '' && in_array($field, $allowed, true) && !in_array($field, $fields, true)) {
$fields[] = $field;
}
}
return $fields;
}
/**
* Convert a stored map row to normalized interoperability metadata.
*
* Dates are exposed as Unix timestamps so consumers can compare them directly
* with the timestamp accepted by the collection `since` option.
*
* @param array $row
* @return array
*/
function MAPS_contentNormalizeRow($row)
{
if (!is_array($row) || empty($row['mid'])) {
return array();
}
$mid = (int) $row['mid'];
$title = MAPS_decodeStoredText(isset($row['name']) ? $row['name'] : '');
$description = MAPS_decodeStoredText(isset($row['description']) ? $row['description'] : '');
$title = trim($title);
$description = trim($description);
if ($title === '') {
$title = '#' . $mid;
}
$excerpt = trim(strip_tags($description));
if (function_exists('COM_truncate')) {
$excerpt = COM_truncate($excerpt, 255, '...');
} elseif (strlen($excerpt) > 255) {
$excerpt = substr($excerpt, 0, 252) . '...';
}
$created = isset($row['created']) ? strtotime($row['created']) : false;
$modified = isset($row['modified']) ? strtotime($row['modified']) : false;
$uid = isset($row['owner_id']) ? (int) $row['owner_id'] : 0;
return array(
'id' => $mid,
'title' => $title,
'url' => MAPS_contentUrl($mid),
'description' => $description,
'excerpt' => $excerpt,
'date-created' => ($created === false ? 0 : $created),
'date-modified' => ($modified === false ? 0 : $modified),
'uid' => $uid,
'author' => ($uid > 0 && function_exists('COM_getDisplayName')) ? COM_getDisplayName($uid) : '',
'type' => 'maps',
'subtype' => 'map'
);
}
/**
* Return public/accessible Maps content rows through one permission-aware query.
*
* Supported options:
* - since: Unix timestamp (or parseable date string)
* - limit: 1..100, default 20 for collections
* - order: modified-desc or created-desc
*
* @param int|string $id
* @param int $uid
* @param array $options
* @return array
*/
function MAPS_contentQuery($id = '*', $uid = 0, $options = array())
{
global $_TABLES;
if (!is_array($options)) {
$options = array();
}
$collection = ((string) $id === '*');
$limit = isset($options['limit']) ? (int) $options['limit'] : ($collection ? 20 : 1);
$limit = max(1, min(100, $limit));
$order = isset($options['order']) ? strtolower(trim((string) $options['order'])) : 'modified-desc';
$orderSql = ($order === 'created-desc') ? 'created DESC, mid DESC' : 'modified DESC, mid DESC';
$where = array('active=1', 'hidden=0');
if (!$collection) {
$mid = (int) $id;
if ($mid <= 0) {
return array();
}
$where[] = 'mid=' . $mid;
}
if (isset($options['since']) && $options['since'] !== '' && $options['since'] !== null) {
$since = $options['since'];
if (is_numeric($since)) {
$since = (int) $since;
} else {
$since = strtotime((string) $since);
}
if ($since !== false && $since > 0) {
$sinceSql = MAPS_dbEscape(date('Y-m-d H:i:s', $since));
$where[] = "(created>='{$sinceSql}' OR modified>='{$sinceSql}')";
}
}
$sql = "SELECT mid,name,description,created,modified,owner_id "
. "FROM {$_TABLES['maps_maps']} WHERE " . implode(' AND ', $where)
. COM_getPermSQL('AND', (int) $uid, 2)
. ' ORDER BY ' . $orderSql
. ' LIMIT ' . $limit;
$result = DB_query($sql);
if (!$result) {
return array();
}
$items = array();
while ($row = DB_fetchArray($result)) {
$item = MAPS_contentNormalizeRow($row);
if (!empty($item)) {
$items[] = $item;
}
}
return $items;
}
/**
* Filter normalized metadata to the fields requested through Item Info.
*
* @param array $item
* @param array $fields
* @return array
*/
function MAPS_contentSelectFields($item, $fields, $numeric = false)
{
$selected = array();
foreach ($fields as $field) {
if (array_key_exists($field, $item)) {
if ($numeric) {
$selected[] = $item[$field];
} else {
$selected[$field] = $item[$field];
}
}
}
return $selected;
}
/**
* Geeklog structured Item Info callback.
*
* `$id = '*'` returns a collection of normalized records. A concrete map ID
* returns one normalized record. Permissions and visibility remain owned by
* Maps rather than by consumers such as Hub, Hello, IndexNow or Sitemap.
*
* @param int|string $id
* @param string|array $what
* @param int $uid
* @param array $options
* @return array
*/
function plugin_getiteminfo_maps($id, $what, $uid = 0, $options = array())
{
$fields = MAPS_contentRequestedFields($what);
if (empty($fields)) {
return array();
}
$idText = trim((string) $id);
if (strncmp($idText, 'marker:', 7) === 0) {
$marker = MAPS_markerContentQuery(substr($idText, 7), $uid);
if (empty($marker)) {
return array();
}
return MAPS_contentSelectFields($marker, $fields, true);
}
$items = MAPS_contentQuery($id, $uid, $options);
if ((string) $id !== '*') {
if (empty($items)) {
return array();
}
return MAPS_contentSelectFields($items[0], $fields, true);
}
$result = array();
foreach ($items as $item) {
$result[] = MAPS_contentSelectFields($item, $fields);
}
return $result;
}
/**
* Resolve a Maps item ID to its canonical URL on subtype-aware Geeklog cores.
*
* @param string|null $sub_type
* @param int|string $item_id
* @return string
*/
function plugin_idtourl_maps($sub_type = '', $item_id = null)
{
// Some Geeklog/plugin consumers call the callback with one identifier,
// while subtype-aware cores pass ($sub_type, $item_id). Support both.
if ($item_id === null) {
$item_id = $sub_type;
$sub_type = '';
}
$subType = strtolower(trim((string) $sub_type));
$itemText = trim((string) $item_id);
if (strncmp($itemText, 'marker:', 7) === 0) {
return MAPS_markerContentUrl(substr($itemText, 7));
}
if ($subType === 'marker') {
return MAPS_markerContentUrl($itemText);
}
if ($subType !== '' && $subType !== 'map' && $subType !== 'maps') {
return '';
}
return MAPS_contentUrl((int) $itemText);
}