forked from dokuwiki/dokuwiki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
feed.php
492 lines (441 loc) · 17.9 KB
/
feed.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
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
<?php
/**
* XML feed export
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Andreas Gohr <[email protected]>
*
* @global array $conf
* @global Input $INPUT
*/
if(!defined('DOKU_INC')) define('DOKU_INC', dirname(__FILE__).'/');
require_once(DOKU_INC.'inc/init.php');
//close session
session_write_close();
// get params
$opt = rss_parseOptions();
// the feed is dynamic - we need a cache for each combo
// (but most people just use the default feed so it's still effective)
$cache = getCacheName(join('', array_values($opt)).$_SERVER['REMOTE_USER'], '.feed');
$key = join('', array_values($opt)).$_SERVER['REMOTE_USER'];
$cache = new cache($key, '.feed');
// prepare cache depends
$depends['files'] = getConfigFiles('main');
$depends['age'] = $conf['rss_update'];
$depends['purge'] = $INPUT->bool('purge');
// check cacheage and deliver if nothing has changed since last
// time or the update interval has not passed, also handles conditional requests
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Type: application/xml; charset=utf-8');
header('X-Robots-Tag: noindex');
if($cache->useCache($depends)) {
http_conditionalRequest($cache->_time);
if($conf['allowdebug']) header("X-CacheUsed: $cache->cache");
print $cache->retrieveCache();
exit;
} else {
http_conditionalRequest(time());
}
// create new feed
$rss = new DokuWikiFeedCreator();
$rss->title = $conf['title'].(($opt['namespace']) ? ' '.$opt['namespace'] : '');
$rss->link = DOKU_URL;
$rss->syndicationURL = DOKU_URL.'feed.php';
$rss->cssStyleSheet = DOKU_URL.'lib/exe/css.php?s=feed';
$image = new FeedImage();
$image->title = $conf['title'];
$image->url = tpl_getMediaFile(array(':wiki:favicon.ico', ':favicon.ico', 'images/favicon.ico'), true);
$image->link = DOKU_URL;
$rss->image = $image;
$data = null;
$modes = array(
'list' => 'rssListNamespace',
'search' => 'rssSearch',
'recent' => 'rssRecentChanges'
);
if(isset($modes[$opt['feed_mode']])) {
$data = $modes[$opt['feed_mode']]($opt);
} else {
$eventData = array(
'opt' => &$opt,
'data' => &$data,
);
$event = new Doku_Event('FEED_MODE_UNKNOWN', $eventData);
if($event->advise_before(true)) {
echo sprintf('<error>Unknown feed mode %s</error>', hsc($opt['feed_mode']));
exit;
}
$event->advise_after();
}
rss_buildItems($rss, $data, $opt);
$feed = $rss->createFeed($opt['feed_type'], 'utf-8');
// save cachefile
$cache->storeCache($feed);
// finally deliver
print $feed;
// ---------------------------------------------------------------- //
/**
* Get URL parameters and config options and return an initialized option array
*
* @author Andreas Gohr <[email protected]>
*/
function rss_parseOptions() {
global $conf;
global $INPUT;
$opt = array();
foreach(array(
// Basic feed properties
// Plugins may probably want to add new values to these
// properties for implementing own feeds
// One of: list, search, recent
'feed_mode' => array('str', 'mode', 'recent'),
// One of: diff, page, rev, current
'link_to' => array('str', 'linkto', $conf['rss_linkto']),
// One of: abstract, diff, htmldiff, html
'item_content' => array('str', 'content', $conf['rss_content']),
// Special feed properties
// These are only used by certain feed_modes
// String, used for feed title, in list and rc mode
'namespace' => array('str', 'ns', null),
// Positive integer, only used in rc mode
'items' => array('int', 'num', $conf['recent']),
// Boolean, only used in rc mode
'show_minor' => array('bool', 'minor', false),
// String, only used in search mode
'search_query' => array('str', 'q', null),
// One of: pages, media, both
'content_type' => array('str', 'view', $conf['rss_media'])
) as $name => $val) {
$opt[$name] = $INPUT->$val[0]($val[1], $val[2], true);
}
$opt['items'] = max(0, (int) $opt['items']);
$opt['show_minor'] = (bool) $opt['show_minor'];
$opt['guardmail'] = ($conf['mailguard'] != '' && $conf['mailguard'] != 'none');
$type = valid_input_set(
'type', array(
'rss', 'rss2', 'atom', 'atom1', 'rss1',
'default' => $conf['rss_type']
),
$_REQUEST
);
switch($type) {
case 'rss':
$opt['feed_type'] = 'RSS0.91';
$opt['mime_type'] = 'text/xml';
break;
case 'rss2':
$opt['feed_type'] = 'RSS2.0';
$opt['mime_type'] = 'text/xml';
break;
case 'atom':
$opt['feed_type'] = 'ATOM0.3';
$opt['mime_type'] = 'application/xml';
break;
case 'atom1':
$opt['feed_type'] = 'ATOM1.0';
$opt['mime_type'] = 'application/atom+xml';
break;
default:
$opt['feed_type'] = 'RSS1.0';
$opt['mime_type'] = 'application/xml';
}
$eventData = array(
'opt' => &$opt,
);
trigger_event('FEED_OPTS_POSTPROCESS', $eventData);
return $opt;
}
/**
* Add recent changed pages to a feed object
*
* @author Andreas Gohr <[email protected]>
* @param FeedCreator $rss the FeedCreator Object
* @param array $data the items to add
* @param array $opt the feed options
*/
function rss_buildItems(&$rss, &$data, $opt) {
global $conf;
global $lang;
/* @var auth_basic $auth */
global $auth;
$eventData = array(
'rss' => &$rss,
'data' => &$data,
'opt' => &$opt,
);
$event = new Doku_Event('FEED_DATA_PROCESS', $eventData);
if($event->advise_before(false)) {
foreach($data as $ditem) {
if(!is_array($ditem)) {
// not an array? then only a list of IDs was given
$ditem = array('id' => $ditem);
}
$item = new FeedItem();
$id = $ditem['id'];
if(!$ditem['media']) {
$meta = p_get_metadata($id);
} else {
$meta = array();
}
// add date
if($ditem['date']) {
$date = $ditem['date'];
} elseif($meta['date']['modified']) {
$date = $meta['date']['modified'];
} else if ($ditem['media']) {
$date = @filemtime(mediaFN($id));
} else {
$date = @filemtime(wikiFN($id));
}
if($date) $item->date = date('r', $date);
// add title
if($conf['useheading'] && $meta['title']) {
$item->title = $meta['title'];
} else {
$item->title = $ditem['id'];
}
if($conf['rss_show_summary'] && !empty($ditem['sum'])) {
$item->title .= ' - '.strip_tags($ditem['sum']);
}
// add item link
switch($opt['link_to']) {
case 'page':
if($ditem['media']) {
$item->link = media_managerURL(
array(
'image' => $id,
'ns' => getNS($id),
'rev' => $date
), '&', true
);
} else {
$item->link = wl($id, 'rev='.$date, true, '&', true);
}
break;
case 'rev':
if($ditem['media']) {
$item->link = media_managerURL(
array(
'image' => $id,
'ns' => getNS($id),
'rev' => $date,
'tab_details' => 'history'
), '&', true
);
} else {
$item->link = wl($id, 'do=revisions&rev='.$date, true, '&');
}
break;
case 'current':
if($ditem['media']) {
$item->link = media_managerURL(
array(
'image' => $id,
'ns' => getNS($id)
), '&', true
);
} else {
$item->link = wl($id, '', true, '&');
}
break;
case 'diff':
default:
if($ditem['media']) {
$item->link = media_managerURL(
array(
'image' => $id,
'ns' => getNS($id),
'rev' => $date,
'tab_details' => 'history',
'mediado' => 'diff'
), '&', true
);
} else {
$item->link = wl($id, 'rev='.$date.'&do=diff', true, '&');
}
}
// add item content
switch($opt['item_content']) {
case 'diff':
case 'htmldiff':
if($ditem['media']) {
$revs = getRevisions($id, 0, 1, 8192, true);
$rev = $revs[0];
$src_r = '';
$src_l = '';
if($size = media_image_preview_size($id, false, new JpegMeta(mediaFN($id)), 300)) {
$more = 'w='.$size[0].'&h='.$size[1].'t='.@filemtime(mediaFN($id));
$src_r = ml($id, $more);
}
if($rev && $size = media_image_preview_size($id, $rev, new JpegMeta(mediaFN($id, $rev)), 300)) {
$more = 'rev='.$rev.'&w='.$size[0].'&h='.$size[1];
$src_l = ml($id, $more);
}
$content = '';
if($src_r) {
$content = '<table>';
$content .= '<tr><th width="50%">'.$rev.'</th>';
$content .= '<th width="50%">'.$lang['current'].'</th></tr>';
$content .= '<tr align="center"><td><img src="'.$src_l.'" alt="" /></td><td>';
$content .= '<img src="'.$src_r.'" alt="'.$id.'" /></td></tr>';
$content .= '</table>';
}
} else {
require_once(DOKU_INC.'inc/DifferenceEngine.php');
$revs = getRevisions($id, 0, 1);
$rev = $revs[0];
if($rev) {
$df = new Diff(explode("\n", htmlspecialchars(rawWiki($id, $rev))),
explode("\n", htmlspecialchars(rawWiki($id, ''))));
} else {
$df = new Diff(array(''),
explode("\n", htmlspecialchars(rawWiki($id, ''))));
}
if($opt['item_content'] == 'htmldiff') {
$tdf = new TableDiffFormatter();
$content = '<table>';
$content .= '<tr><th colspan="2" width="50%">'.$rev.'</th>';
$content .= '<th colspan="2" width="50%">'.$lang['current'].'</th></tr>';
$content .= $tdf->format($df);
$content .= '</table>';
} else {
$udf = new UnifiedDiffFormatter();
$content = "<pre>\n".$udf->format($df)."\n</pre>";
}
}
break;
case 'html':
if($ditem['media']) {
if($size = media_image_preview_size($id, false, new JpegMeta(mediaFN($id)))) {
$more = 'w='.$size[0].'&h='.$size[1].'t='.@filemtime(mediaFN($id));
$src = ml($id, $more);
$content = '<img src="'.$src.'" alt="'.$id.'" />';
} else {
$content = '';
}
} else {
$content = p_wiki_xhtml($id, $date, false);
// no TOC in feeds
$content = preg_replace('/(<!-- TOC START -->).*(<!-- TOC END -->)/s', '', $content);
// add alignment for images
$content = preg_replace('/(<img .*?class="medialeft")/s', '\\1 align="left"', $content);
$content = preg_replace('/(<img .*?class="mediaright")/s', '\\1 align="right"', $content);
// make URLs work when canonical is not set, regexp instead of rerendering!
if(!$conf['canonical']) {
$base = preg_quote(DOKU_REL, '/');
$content = preg_replace('/(<a href|<img src)="('.$base.')/s', '$1="'.DOKU_URL, $content);
}
}
break;
case 'abstract':
default:
if($ditem['media']) {
if($size = media_image_preview_size($id, false, new JpegMeta(mediaFN($id)))) {
$more = 'w='.$size[0].'&h='.$size[1].'t='.@filemtime(mediaFN($id));
$src = ml($id, $more);
$content = '<img src="'.$src.'" alt="'.$id.'" />';
} else {
$content = '';
}
} else {
$content = $meta['description']['abstract'];
}
}
$item->description = $content; //FIXME a plugin hook here could be senseful
// add user
# FIXME should the user be pulled from metadata as well?
$user = @$ditem['user']; // the @ spares time repeating lookup
$item->author = '';
if($user && $conf['useacl'] && $auth) {
$userInfo = $auth->getUserData($user);
if($userInfo) {
switch($conf['showuseras']) {
case 'username':
$item->author = $userInfo['name'];
break;
default:
$item->author = $user;
break;
}
} else {
$item->author = $user;
}
if($userInfo && !$opt['guardmail']) {
$item->authorEmail = $userInfo['mail'];
} else {
//cannot obfuscate because some RSS readers may check validity
$item->authorEmail = $user.'@'.$ditem['ip'];
}
} elseif($user) {
// this happens when no ACL but some Apache auth is used
$item->author = $user;
$item->authorEmail = $user.'@'.$ditem['ip'];
} else {
$item->authorEmail = 'anonymous@'.$ditem['ip'];
}
// add category
if(isset($meta['subject'])) {
$item->category = $meta['subject'];
} else {
$cat = getNS($id);
if($cat) $item->category = $cat;
}
// finally add the item to the feed object, after handing it to registered plugins
$evdata = array(
'item' => &$item,
'opt' => &$opt,
'ditem' => &$ditem,
'rss' => &$rss
);
$evt = new Doku_Event('FEED_ITEM_ADD', $evdata);
if($evt->advise_before()) {
$rss->addItem($item);
}
$evt->advise_after(); // for completeness
}
}
$event->advise_after();
}
/**
* Add recent changed pages to the feed object
*
* @author Andreas Gohr <[email protected]>
*/
function rssRecentChanges($opt) {
global $conf;
$flags = RECENTS_SKIP_DELETED;
if(!$opt['show_minor']) $flags += RECENTS_SKIP_MINORS;
if($opt['content_type'] == 'media' && $conf['mediarevisions']) $flags += RECENTS_MEDIA_CHANGES;
if($opt['content_type'] == 'both' && $conf['mediarevisions']) $flags += RECENTS_MEDIA_PAGES_MIXED;
$recents = getRecents(0, $opt['items'], $opt['namespace'], $flags);
return $recents;
}
/**
* Add all pages of a namespace to the feed object
*
* @author Andreas Gohr <[email protected]>
*/
function rssListNamespace($opt) {
require_once(DOKU_INC.'inc/search.php');
global $conf;
$ns = ':'.cleanID($opt['namespace']);
$ns = str_replace(':', '/', $ns);
$data = array();
sort($data);
search($data, $conf['datadir'], 'search_list', '', $ns);
return $data;
}
/**
* Add the result of a full text search to the feed object
*
* @author Andreas Gohr <[email protected]>
*/
function rssSearch($opt) {
if(!$opt['search_query']) return array();
require_once(DOKU_INC.'inc/fulltext.php');
$data = ft_pageSearch($opt['search_query'], $poswords);
$data = array_keys($data);
return $data;
}
//Setup VIM: ex: et ts=4 :