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