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 9if(!defined('DOKU_INC')) define('DOKU_INC',dirname(__FILE__).'/'); 10require_once(DOKU_INC.'inc/init.php'); 11require_once(DOKU_INC.'inc/common.php'); 12require_once(DOKU_INC.'inc/events.php'); 13require_once(DOKU_INC.'inc/parserutils.php'); 14require_once(DOKU_INC.'inc/feedcreator.class.php'); 15require_once(DOKU_INC.'inc/auth.php'); 16require_once(DOKU_INC.'inc/pageutils.php'); 17require_once(DOKU_INC.'inc/httputils.php'); 18 19//close session 20session_write_close(); 21 22// get params 23$opt = rss_parseOptions(); 24 25// the feed is dynamic - we need a cache for each combo 26// (but most people just use the default feed so it's still effective) 27$cache = getCacheName(join('',array_values($opt)).$_SERVER['REMOTE_USER'],'.feed'); 28$cmod = @filemtime($cache); // 0 if not exists 29if ($cmod && (@filemtime(DOKU_CONF.'local.php')>$cmod || @filemtime(DOKU_CONF.'dokuwiki.php')>$cmod)) { 30 // ignore cache if feed prefs may have changed 31 $cmod = 0; 32} 33 34// check cacheage and deliver if nothing has changed since last 35// time or the update interval has not passed, also handles conditional requests 36header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 37header('Pragma: public'); 38header('Content-Type: application/xml; charset=utf-8'); 39header('X-Robots-Tag: noindex'); 40if($cmod && (($cmod+$conf['rss_update']>time()) || ($cmod>@filemtime($conf['changelog'])))){ 41 http_conditionalRequest($cmod); 42 if($conf['allowdebug']) header("X-CacheUsed: $cache"); 43 print io_readFile($cache); 44 exit; 45} else { 46 http_conditionalRequest(time()); 47 } 48 49// create new feed 50$rss = new DokuWikiFeedCreator(); 51$rss->title = $conf['title'].(($opt['namespace']) ? ' '.$opt['namespace'] : ''); 52$rss->link = DOKU_URL; 53$rss->syndicationURL = DOKU_URL.'feed.php'; 54$rss->cssStyleSheet = DOKU_URL.'lib/exe/css.php?s=feed'; 55 56$image = new FeedImage(); 57$image->title = $conf['title']; 58$image->url = DOKU_URL."lib/images/favicon.ico"; 59$image->link = DOKU_URL; 60$rss->image = $image; 61 62$data = null; 63if($opt['feed_mode'] == 'list'){ 64 $data = rssListNamespace($opt); 65}elseif($opt['feed_mode'] == 'search'){ 66 $data = rssSearch($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 $data = rssRecentChanges($opt); 75 } 76 $event->advise_after(); 77} 78 79rss_buildItems($rss, $data, $opt); 80$feed = $rss->createFeed($opt['feed_type'],'utf-8'); 81 82// save cachefile 83io_saveFile($cache,$feed); 84 85// finally deliver 86print $feed; 87 88// ---------------------------------------------------------------- // 89 90/** 91 * Get URL parameters and config options and return a initialized option array 92 * 93 * @author Andreas Gohr <andi@splitbrain.org> 94 */ 95function rss_parseOptions(){ 96 global $conf; 97 98 $opt['items'] = (int) $_REQUEST['num']; 99 $opt['feed_type'] = $_REQUEST['type']; 100 $opt['feed_mode'] = $_REQUEST['mode']; 101 $opt['show_minor'] = $_REQUEST['minor']; 102 $opt['namespace'] = $_REQUEST['ns']; 103 $opt['link_to'] = $_REQUEST['linkto']; 104 $opt['item_content'] = $_REQUEST['content']; 105 $opt['search_query'] = $_REQUEST['q']; 106 107 if(!$opt['feed_type']) $opt['feed_type'] = $conf['rss_type']; 108 if(!$opt['item_content']) $opt['item_content'] = $conf['rss_content']; 109 if(!$opt['link_to']) $opt['link_to'] = $conf['rss_linkto']; 110 if(!$opt['items']) $opt['items'] = $conf['recent']; 111 $opt['guardmail'] = ($conf['mailguard'] != '' && $conf['mailguard'] != 'none'); 112 113 switch ($opt['feed_type']){ 114 case 'rss': 115 $opt['feed_type'] = 'RSS0.91'; 116 $opt['mime_type'] = 'text/xml'; 117 break; 118 case 'rss2': 119 $opt['feed_type'] = 'RSS2.0'; 120 $opt['mime_type'] = 'text/xml'; 121 break; 122 case 'atom': 123 $opt['feed_type'] = 'ATOM0.3'; 124 $opt['mime_type'] = 'application/xml'; 125 break; 126 case 'atom1': 127 $opt['feed_type'] = 'ATOM1.0'; 128 $opt['mime_type'] = 'application/atom+xml'; 129 break; 130 default: 131 $opt['feed_type'] = 'RSS1.0'; 132 $opt['mime_type'] = 'application/xml'; 133 } 134 135 $eventData = array( 136 'opt' => &$opt, 137 ); 138 trigger_event('FEED_OPTS_POSTPROCESS', $eventData); 139 return $opt; 140} 141 142/** 143 * Add recent changed pages to a feed object 144 * 145 * @author Andreas Gohr <andi@splitbrain.org> 146 * @param object $rss - the FeedCreator Object 147 * @param array $data - the items to add 148 * @param array $opt - the feed options 149 */ 150function rss_buildItems(&$rss,&$data,$opt){ 151 global $conf; 152 global $lang; 153 global $auth; 154 155 $eventData = array( 156 'rss' => &$rss, 157 'data' => &$data, 158 'opt' => &$opt, 159 ); 160 $event = new Doku_Event('FEED_DATA_PROCESS', $eventData); 161 if ($event->advise_before(false)){ 162 foreach($data as $ditem){ 163 if(!is_array($ditem)){ 164 // not an array? then only a list of IDs was given 165 $ditem = array( 'id' => $ditem ); 166 } 167 168 $item = new FeedItem(); 169 $id = $ditem['id']; 170 $meta = p_get_metadata($id); 171 172 // add date 173 if($ditem['date']){ 174 $date = $ditem['date']; 175 }elseif($meta['date']['modified']){ 176 $date = $meta['date']['modified']; 177 }else{ 178 $date = @filemtime(wikiFN($id)); 179 } 180 if($date) $item->date = date('r',$date); 181 182 // add title 183 if($conf['useheading'] && $meta['title']){ 184 $item->title = $meta['title']; 185 }else{ 186 $item->title = $ditem['id']; 187 } 188 if($conf['rss_show_summary'] && !empty($ditem['sum'])){ 189 $item->title .= ' - '.strip_tags($ditem['sum']); 190 } 191 192 // add item link 193 switch ($opt['link_to']){ 194 case 'page': 195 $item->link = wl($id,'rev='.$date,true,'&'); 196 break; 197 case 'rev': 198 $item->link = wl($id,'do=revisions&rev='.$date,true,'&'); 199 break; 200 case 'current': 201 $item->link = wl($id, '', true,'&'); 202 break; 203 case 'diff': 204 default: 205 $item->link = wl($id,'rev='.$date.'&do=diff',true,'&'); 206 } 207 208 // add item content 209 switch ($opt['item_content']){ 210 case 'diff': 211 case 'htmldiff': 212 require_once(DOKU_INC.'inc/DifferenceEngine.php'); 213 $revs = getRevisions($id, 0, 1); 214 $rev = $revs[0]; 215 216 if($rev){ 217 $df = new Diff(explode("\n",htmlspecialchars(rawWiki($id,$rev))), 218 explode("\n",htmlspecialchars(rawWiki($id,'')))); 219 }else{ 220 $df = new Diff(array(''), 221 explode("\n",htmlspecialchars(rawWiki($id,'')))); 222 } 223 224 if($opt['item_content'] == 'htmldiff'){ 225 $tdf = new TableDiffFormatter(); 226 $content = '<table>'; 227 $content .= '<tr><th colspan="2" width="50%">'.$rev.'</th>'; 228 $content .= '<th colspan="2" width="50%">'.$lang['current'].'</th></tr>'; 229 $content .= $tdf->format($df); 230 $content .= '</table>'; 231 }else{ 232 $udf = new UnifiedDiffFormatter(); 233 $content = "<pre>\n".$udf->format($df)."\n</pre>"; 234 } 235 break; 236 case 'html': 237 $content = p_wiki_xhtml($id,$date,false); 238 // no TOC in feeds 239 $content = preg_replace('/(<!-- TOC START -->).*(<!-- TOC END -->)/s','',$content); 240 241 // make URLs work when canonical is not set, regexp instead of rerendering! 242 if(!$conf['canonical']){ 243 $base = preg_quote(DOKU_REL,'/'); 244 $content = preg_replace('/(<a href|<img src)="('.$base.')/s','$1="'.DOKU_URL,$content); 245 } 246 247 break; 248 case 'abstract': 249 default: 250 $content = $meta['description']['abstract']; 251 } 252 $item->description = $content; //FIXME a plugin hook here could be senseful 253 254 // add user 255 # FIXME should the user be pulled from metadata as well? 256 $user = null; 257 $user = @$ditem['user']; // the @ spares time repeating lookup 258 $item->author = ''; 259 if($user && $conf['useacl'] && $auth){ 260 $userInfo = $auth->getUserData($user); 261 $item->author = $userInfo['name']; 262 if($userInfo && !$opt['guardmail']){ 263 $item->authorEmail = $userInfo['mail']; 264 }else{ 265 //cannot obfuscate because some RSS readers may check validity 266 $item->authorEmail = $user.'@'.$recent['ip']; 267 } 268 }elseif($user){ 269 // this happens when no ACL but some Apache auth is used 270 $item->author = $user; 271 $item->authorEmail = $user.'@'.$recent['ip']; 272 }else{ 273 $item->authorEmail = 'anonymous@'.$recent['ip']; 274 } 275 276 // add category 277 if($meta['subject']){ 278 $item->category = $meta['subject']; 279 }else{ 280 $cat = getNS($id); 281 if($cat) $item->category = $cat; 282 } 283 284 // finally add the item to the feed object, after handing it to registered plugins 285 $evdata = array('item' => &$item, 286 'opt' => &$opt, 287 'ditem' => &$ditem, 288 'rss' => &$rss); 289 $evt = new Doku_Event('FEED_ITEM_ADD', $evdata); 290 if ($evt->advise_before()){ 291 $rss->addItem($item); 292 } 293 $evt->advise_after(); // for completeness 294 } 295 } 296 $event->advise_after(); 297} 298 299 300/** 301 * Add recent changed pages to the feed object 302 * 303 * @author Andreas Gohr <andi@splitbrain.org> 304 */ 305function rssRecentChanges($opt){ 306 global $conf; 307 global $auth; 308 309 $flags = RECENTS_SKIP_DELETED; 310 if(!$opt['show_minor']) $flags += RECENTS_SKIP_MINORS; 311 312 $recents = getRecents(0,$opt['items'],$opt['namespace'],$flags); 313 return $recents; 314} 315 316/** 317 * Add all pages of a namespace to the feed object 318 * 319 * @author Andreas Gohr <andi@splitbrain.org> 320 */ 321function rssListNamespace($opt){ 322 require_once(DOKU_INC.'inc/search.php'); 323 global $conf; 324 325 $ns=':'.cleanID($opt['namespace']); 326 $ns=str_replace(':','/',$ns); 327 328 $data = array(); 329 sort($data); 330 search($data,$conf['datadir'],'search_list','',$ns); 331 332 return $data; 333} 334 335/** 336 * Add the result of a full text search to the feed object 337 * 338 * @author Andreas Gohr <andi@splitbrain.org> 339 */ 340function rssSearch($opt){ 341 if(!$opt['search_query']) return; 342 343 require_once(DOKU_INC.'inc/fulltext.php'); 344 $data = array(); 345 $data = ft_pageSearch($opt['search_query'],$poswords); 346 $data = array_keys($data); 347 348 return $data; 349} 350 351//Setup VIM: ex: et ts=4 enc=utf-8 : 352