1<?php 2/** 3 * XML feed export 4 * 5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6 * @author Andreas Gohr <andi@splitbrain.org> 7 * 8 * @global array $conf 9 * @global Input $INPUT 10 */ 11 12use dokuwiki\Cache\Cache; 13use dokuwiki\ChangeLog\MediaChangeLog; 14use dokuwiki\ChangeLog\PageChangeLog; 15use dokuwiki\Extension\AuthPlugin; 16use dokuwiki\Extension\Event; 17 18if(!defined('DOKU_INC')) define('DOKU_INC', dirname(__FILE__) . '/'); 19require_once(DOKU_INC . 'inc/init.php'); 20 21//close session 22session_write_close(); 23 24//feed disabled? 25if(!actionOK('rss')) { 26 http_status(404); 27 echo '<error>RSS feed is disabled.</error>'; 28 exit; 29} 30 31// get params 32$opt = rss_parseOptions(); 33 34// the feed is dynamic - we need a cache for each combo 35// (but most people just use the default feed so it's still effective) 36$key = join('', array_values($opt)) . '$' . $_SERVER['REMOTE_USER'] . '$' . $_SERVER['HTTP_HOST'] . $_SERVER['SERVER_PORT']; 37$cache = new Cache($key, '.feed'); 38 39// prepare cache depends 40$depends['files'] = getConfigFiles('main'); 41$depends['age'] = $conf['rss_update']; 42$depends['purge'] = $INPUT->bool('purge'); 43 44// check cacheage and deliver if nothing has changed since last 45// time or the update interval has not passed, also handles conditional requests 46header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 47header('Pragma: public'); 48header('Content-Type: application/xml; charset=utf-8'); 49header('X-Robots-Tag: noindex'); 50if($cache->useCache($depends)) { 51 http_conditionalRequest($cache->getTime()); 52 if($conf['allowdebug']) header("X-CacheUsed: $cache->cache"); 53 print $cache->retrieveCache(); 54 exit; 55} else { 56 http_conditionalRequest(time()); 57} 58 59// create new feed 60$rss = new UniversalFeedCreator(); 61$rss->title = $conf['title'] . (($opt['namespace']) ? ' ' . $opt['namespace'] : ''); 62$rss->link = DOKU_URL; 63$rss->syndicationURL = DOKU_URL . 'feed.php'; 64$rss->cssStyleSheet = DOKU_URL . 'lib/exe/css.php?s=feed'; 65 66$image = new FeedImage(); 67$image->title = $conf['title']; 68$image->url = tpl_getMediaFile([':wiki:favicon.ico', ':favicon.ico', 'images/favicon.ico'], true); 69$image->link = DOKU_URL; 70$rss->image = $image; 71 72$data = null; 73$modes = [ 74 'list' => 'rssListNamespace', 75 'search' => 'rssSearch', 76 'recent' => 'rssRecentChanges' 77]; 78 79if(isset($modes[$opt['feed_mode']])) { 80 $data = $modes[$opt['feed_mode']]($opt); 81} else { 82 $eventData = [ 83 'opt' => &$opt, 84 'data' => &$data, 85 ]; 86 $event = new Event('FEED_MODE_UNKNOWN', $eventData); 87 if($event->advise_before(true)) { 88 echo sprintf('<error>Unknown feed mode %s</error>', hsc($opt['feed_mode'])); 89 exit; 90 } 91 $event->advise_after(); 92} 93 94rss_buildItems($rss, $data, $opt); 95$feed = $rss->createFeed($opt['feed_type']); 96 97// save cachefile 98$cache->storeCache($feed); 99 100// finally deliver 101print $feed; 102 103// ---------------------------------------------------------------- // 104 105/** 106 * Get URL parameters and config options and return an initialized option array 107 * 108 * @author Andreas Gohr <andi@splitbrain.org> 109 */ 110function rss_parseOptions() { 111 global $conf; 112 global $INPUT; 113 114 $opt = []; 115 116 foreach([ 117 // Basic feed properties 118 // Plugins may probably want to add new values to these 119 // properties for implementing own feeds 120 121 // One of: list, search, recent 122 'feed_mode' => ['str', 'mode', 'recent'], 123 // One of: diff, page, rev, current 124 'link_to' => ['str', 'linkto', $conf['rss_linkto']], 125 // One of: abstract, diff, htmldiff, html 126 'item_content' => ['str', 'content', $conf['rss_content']], 127 128 // Special feed properties 129 // These are only used by certain feed_modes 130 131 // String, used for feed title, in list and rc mode 132 'namespace' => ['str', 'ns', null], 133 // Positive integer, only used in rc mode 134 'items' => ['int', 'num', $conf['recent']], 135 // Boolean, only used in rc mode 136 'show_minor' => ['bool', 'minor', false], 137 // Boolean, only used in rc mode 138 'only_new' => ['bool', 'onlynewpages', false], 139 // String, only used in list mode 140 'sort' => ['str', 'sort', 'natural'], 141 // String, only used in search mode 142 'search_query' => ['str', 'q', null], 143 // One of: pages, media, both 144 'content_type' => ['str', 'view', $conf['rss_media']] 145 146 ] as $name => $val) { 147 $opt[$name] = $INPUT->{$val[0]}($val[1], $val[2], true); 148 } 149 150 $opt['items'] = max(0, (int) $opt['items']); 151 $opt['show_minor'] = (bool) $opt['show_minor']; 152 $opt['only_new'] = (bool) $opt['only_new']; 153 $opt['sort'] = valid_input_set('sort', ['default' => 'natural', 'date'], $opt); 154 155 $opt['guardmail'] = ($conf['mailguard'] != '' && $conf['mailguard'] != 'none'); 156 157 $type = $INPUT->valid( 158 'type', 159 ['rss', 'rss2', 'atom', 'atom1', 'rss1'], 160 $conf['rss_type'] 161 ); 162 switch($type) { 163 case 'rss': 164 $opt['feed_type'] = 'RSS0.91'; 165 $opt['mime_type'] = 'text/xml'; 166 break; 167 case 'rss2': 168 $opt['feed_type'] = 'RSS2.0'; 169 $opt['mime_type'] = 'text/xml'; 170 break; 171 case 'atom': 172 $opt['feed_type'] = 'ATOM0.3'; 173 $opt['mime_type'] = 'application/xml'; 174 break; 175 case 'atom1': 176 $opt['feed_type'] = 'ATOM1.0'; 177 $opt['mime_type'] = 'application/atom+xml'; 178 break; 179 default: 180 $opt['feed_type'] = 'RSS1.0'; 181 $opt['mime_type'] = 'application/xml'; 182 } 183 184 $eventData = [ 185 'opt' => &$opt, 186 ]; 187 Event::createAndTrigger('FEED_OPTS_POSTPROCESS', $eventData); 188 return $opt; 189} 190 191/** 192 * Add recent changed pages to a feed object 193 * 194 * @param FeedCreator $rss the FeedCreator Object 195 * @param array $data the items to add 196 * @param array $opt the feed options 197 * @author Andreas Gohr <andi@splitbrain.org> 198 */ 199function rss_buildItems(&$rss, &$data, $opt) { 200 global $conf; 201 global $lang; 202 /* @var AuthPlugin $auth */ 203 global $auth; 204 205 $eventData = [ 206 'rss' => &$rss, 207 'data' => &$data, 208 'opt' => &$opt, 209 ]; 210 $event = new Event('FEED_DATA_PROCESS', $eventData); 211 if($event->advise_before(false)) { 212 foreach($data as $ditem) { 213 if(!is_array($ditem)) { 214 // not an array? then only a list of IDs was given 215 $ditem = ['id' => $ditem]; 216 } 217 218 $item = new FeedItem(); 219 $id = $ditem['id']; 220 if(!$ditem['media']) { 221 $meta = p_get_metadata($id); 222 } else { 223 $meta = []; 224 } 225 226 // add date 227 if($ditem['date']) { 228 $date = $ditem['date']; 229 } elseif($ditem['media']) { 230 $date = @filemtime(mediaFN($id)); 231 } elseif(file_exists(wikiFN($id))) { 232 $date = @filemtime(wikiFN($id)); 233 } elseif($meta['date']['modified']) { 234 $date = $meta['date']['modified']; 235 } else { 236 $date = 0; 237 } 238 if($date) $item->date = date('r', $date); 239 240 // add title 241 if($conf['useheading'] && $meta['title']) { 242 $item->title = $meta['title']; 243 } else { 244 $item->title = $ditem['id']; 245 } 246 if($conf['rss_show_summary'] && !empty($ditem['sum'])) { 247 $item->title .= ' - ' . strip_tags($ditem['sum']); 248 } 249 250 // add item link 251 switch($opt['link_to']) { 252 case 'page': 253 if($ditem['media']) { 254 $item->link = media_managerURL( 255 [ 256 'image' => $id, 257 'ns' => getNS($id), 258 'rev' => $date 259 ], '&', true 260 ); 261 } else { 262 $item->link = wl($id, 'rev=' . $date, true, '&'); 263 } 264 break; 265 case 'rev': 266 if($ditem['media']) { 267 $item->link = media_managerURL( 268 [ 269 'image' => $id, 270 'ns' => getNS($id), 271 'rev' => $date, 272 'tab_details' => 'history' 273 ], '&', true 274 ); 275 } else { 276 $item->link = wl($id, 'do=revisions&rev=' . $date, true, '&'); 277 } 278 break; 279 case 'current': 280 if($ditem['media']) { 281 $item->link = media_managerURL( 282 [ 283 'image' => $id, 284 'ns' => getNS($id) 285 ], '&', true 286 ); 287 } else { 288 $item->link = wl($id, '', true, '&'); 289 } 290 break; 291 case 'diff': 292 default: 293 if($ditem['media']) { 294 $item->link = media_managerURL( 295 [ 296 'image' => $id, 297 'ns' => getNS($id), 298 'rev' => $date, 299 'tab_details' => 'history', 300 'mediado' => 'diff' 301 ], '&', true 302 ); 303 } else { 304 $item->link = wl($id, 'rev=' . $date . '&do=diff', true, '&'); 305 } 306 } 307 308 // add item content 309 switch($opt['item_content']) { 310 case 'diff': 311 case 'htmldiff': 312 if($ditem['media']) { 313 $medialog = new MediaChangeLog($id); 314 $revs = $medialog->getRevisions(0, 1); 315 $rev = $revs[0]; 316 $src_r = ''; 317 $src_l = ''; 318 319 if($size = media_image_preview_size($id, '', new JpegMeta(mediaFN($id)), 300)) { 320 $more = 'w=' . $size[0] . '&h=' . $size[1] . '&t=' . @filemtime(mediaFN($id)); 321 $src_r = ml($id, $more, true, '&', true); 322 } 323 if($rev && $size = media_image_preview_size($id, $rev, new JpegMeta(mediaFN($id, $rev)), 300)) { 324 $more = 'rev=' . $rev . '&w=' . $size[0] . '&h=' . $size[1]; 325 $src_l = ml($id, $more, true, '&', true); 326 } 327 $content = ''; 328 if($src_r) { 329 $content = '<table>'; 330 $content .= '<tr><th width="50%">' . $rev . '</th>'; 331 $content .= '<th width="50%">' . $lang['current'] . '</th></tr>'; 332 $content .= '<tr align="center"><td><img src="' . $src_l . '" alt="" /></td><td>'; 333 $content .= '<img src="' . $src_r . '" alt="' . $id . '" /></td></tr>'; 334 $content .= '</table>'; 335 } 336 337 } else { 338 require_once(DOKU_INC . 'inc/DifferenceEngine.php'); 339 $pagelog = new PageChangeLog($id); 340 $revs = $pagelog->getRevisions(0, 1); 341 $rev = $revs[0]; 342 343 if($rev) { 344 $df = new Diff( 345 explode("\n", rawWiki($id, $rev)), 346 explode("\n", rawWiki($id, '')) 347 ); 348 } else { 349 $df = new Diff( 350 [''], 351 explode("\n", rawWiki($id, '')) 352 ); 353 } 354 355 if($opt['item_content'] == 'htmldiff') { 356 // note: no need to escape diff output, TableDiffFormatter provides 'safe' html 357 $tdf = new TableDiffFormatter(); 358 $content = '<table>'; 359 $content .= '<tr><th colspan="2" width="50%">' . $rev . '</th>'; 360 $content .= '<th colspan="2" width="50%">' . $lang['current'] . '</th></tr>'; 361 $content .= $tdf->format($df); 362 $content .= '</table>'; 363 } else { 364 // note: diff output must be escaped, UnifiedDiffFormatter provides plain text 365 $udf = new UnifiedDiffFormatter(); 366 $content = "<pre>\n" . hsc($udf->format($df)) . "\n</pre>"; 367 } 368 } 369 break; 370 case 'html': 371 if($ditem['media']) { 372 if($size = media_image_preview_size($id, '', new JpegMeta(mediaFN($id)))) { 373 $more = 'w=' . $size[0] . '&h=' . $size[1] . '&t=' . @filemtime(mediaFN($id)); 374 $src = ml($id, $more, true, '&', true); 375 $content = '<img src="' . $src . '" alt="' . $id . '" />'; 376 } else { 377 $content = ''; 378 } 379 } else { 380 if(@filemtime(wikiFN($id)) === $date) { 381 $content = p_wiki_xhtml($id, '', false); 382 } else { 383 $content = p_wiki_xhtml($id, $date, false); 384 } 385 // no TOC in feeds 386 $content = preg_replace('/(<!-- TOC START -->).*(<!-- TOC END -->)/s', '', $content); 387 388 // add alignment for images 389 $content = preg_replace('/(<img .*?class="medialeft")/s', '\\1 align="left"', $content); 390 $content = preg_replace('/(<img .*?class="mediaright")/s', '\\1 align="right"', $content); 391 392 // make URLs work when canonical is not set, regexp instead of rerendering! 393 if(!$conf['canonical']) { 394 $base = preg_quote(DOKU_REL, '/'); 395 $content = preg_replace('/(<a href|<img src)="(' . $base . ')/s', '$1="' . DOKU_URL, $content); 396 } 397 } 398 399 break; 400 case 'abstract': 401 default: 402 if($ditem['media']) { 403 if($size = media_image_preview_size($id, '', new JpegMeta(mediaFN($id)))) { 404 $more = 'w=' . $size[0] . '&h=' . $size[1] . '&t=' . @filemtime(mediaFN($id)); 405 $src = ml($id, $more, true, '&', true); 406 $content = '<img src="' . $src . '" alt="' . $id . '" />'; 407 } else { 408 $content = ''; 409 } 410 } else { 411 $content = $meta['description']['abstract']; 412 } 413 } 414 $item->description = $content; //FIXME a plugin hook here could be senseful 415 416 // add user 417 # FIXME should the user be pulled from metadata as well? 418 $user = @$ditem['user']; // the @ spares time repeating lookup 419 if(blank($user)) { 420 $item->author = 'Anonymous'; 421 $item->authorEmail = 'anonymous@undisclosed.example.com'; 422 } else { 423 $item->author = $user; 424 $item->authorEmail = $user . '@undisclosed.example.com'; 425 426 // get real user name if configured 427 if($conf['useacl'] && $auth) { 428 $userInfo = $auth->getUserData($user); 429 if($userInfo) { 430 switch($conf['showuseras']) { 431 case 'username': 432 case 'username_link': 433 $item->author = $userInfo['name']; 434 break; 435 default: 436 $item->author = $user; 437 break; 438 } 439 } else { 440 $item->author = $user; 441 } 442 } 443 } 444 445 // add category 446 if(isset($meta['subject'])) { 447 $item->category = $meta['subject']; 448 } else { 449 $cat = getNS($id); 450 if($cat) $item->category = $cat; 451 } 452 453 // finally add the item to the feed object, after handing it to registered plugins 454 $evdata = [ 455 'item' => &$item, 456 'opt' => &$opt, 457 'ditem' => &$ditem, 458 'rss' => &$rss 459 ]; 460 $evt = new Event('FEED_ITEM_ADD', $evdata); 461 if($evt->advise_before()) { 462 $rss->addItem($item); 463 } 464 $evt->advise_after(); // for completeness 465 } 466 } 467 $event->advise_after(); 468} 469 470/** 471 * Add recent changed pages to the feed object 472 * 473 * @author Andreas Gohr <andi@splitbrain.org> 474 */ 475function rssRecentChanges($opt) { 476 global $conf; 477 $flags = 0; 478 if(!$conf['rss_show_deleted']) $flags += RECENTS_SKIP_DELETED; 479 if(!$opt['show_minor']) $flags += RECENTS_SKIP_MINORS; 480 if($opt['only_new']) $flags += RECENTS_ONLY_CREATION; 481 if($opt['content_type'] == 'media' && $conf['mediarevisions']) $flags += RECENTS_MEDIA_CHANGES; 482 if($opt['content_type'] == 'both' && $conf['mediarevisions']) $flags += RECENTS_MEDIA_PAGES_MIXED; 483 484 $recents = getRecents(0, $opt['items'], $opt['namespace'], $flags); 485 return $recents; 486} 487 488/** 489 * Add all pages of a namespace to the feed object 490 * 491 * @author Andreas Gohr <andi@splitbrain.org> 492 */ 493function rssListNamespace($opt) { 494 require_once(DOKU_INC . 'inc/search.php'); 495 global $conf; 496 497 $ns = ':' . cleanID($opt['namespace']); 498 $ns = utf8_encodeFN(str_replace(':', '/', $ns)); 499 500 $data = []; 501 $search_opts = [ 502 'depth' => 1, 503 'pagesonly' => true, 504 'listfiles' => true 505 ]; 506 search($data, $conf['datadir'], 'search_universal', $search_opts, $ns, $lvl = 1, $opt['sort']); 507 508 return $data; 509} 510 511/** 512 * Add the result of a full text search to the feed object 513 * 514 * @author Andreas Gohr <andi@splitbrain.org> 515 */ 516function rssSearch($opt) { 517 if(!$opt['search_query'] || !actionOK('search')) return []; 518 519 require_once(DOKU_INC . 'inc/fulltext.php'); 520 $data = ft_pageSearch($opt['search_query'], $poswords); 521 $data = array_keys($data); 522 523 return $data; 524} 525 526//Setup VIM: ex: et ts=4 : 527