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 62if($opt['feed_mode'] == 'list'){ 63 rssListNamespace($rss,$opt); 64}elseif($opt['feed_mode'] == 'search'){ 65 rssSearch($rss,$opt); 66}else{ 67 rssRecentChanges($rss,$opt); 68} 69 70$feed = $rss->createFeed($opt['feed_type'],'utf-8'); 71 72// save cachefile 73io_saveFile($cache,$feed); 74 75// finally deliver 76print $feed; 77 78// ---------------------------------------------------------------- // 79 80/** 81 * Get URL parameters and config options and return a initialized option array 82 * 83 * @author Andreas Gohr <andi@splitbrain.org> 84 */ 85function rss_parseOptions(){ 86 global $conf; 87 88 $opt['items'] = (int) $_REQUEST['num']; 89 $opt['feed_type'] = $_REQUEST['type']; 90 $opt['feed_mode'] = $_REQUEST['mode']; 91 $opt['show_minor'] = $_REQUEST['minor']; 92 $opt['namespace'] = $_REQUEST['ns']; 93 $opt['link_to'] = $_REQUEST['linkto']; 94 $opt['item_content'] = $_REQUEST['content']; 95 $opt['search_query'] = $_REQUEST['q']; 96 97 if(!$opt['feed_type']) $opt['feed_type'] = $conf['rss_type']; 98 if(!$opt['item_content']) $opt['item_content'] = $conf['rss_content']; 99 if(!$opt['link_to']) $opt['link_to'] = $conf['rss_linkto']; 100 if(!$opt['items']) $opt['items'] = $conf['recent']; 101 $opt['guardmail'] = ($conf['mailguard'] != '' && $conf['mailguard'] != 'none'); 102 103 switch ($opt['feed_type']){ 104 case 'rss': 105 $opt['feed_type'] = 'RSS0.91'; 106 $opt['mime_type'] = 'text/xml'; 107 break; 108 case 'rss2': 109 $opt['feed_type'] = 'RSS2.0'; 110 $opt['mime_type'] = 'text/xml'; 111 break; 112 case 'atom': 113 $opt['feed_type'] = 'ATOM0.3'; 114 $opt['mime_type'] = 'application/xml'; 115 break; 116 case 'atom1': 117 $opt['feed_type'] = 'ATOM1.0'; 118 $opt['mime_type'] = 'application/atom+xml'; 119 break; 120 default: 121 $opt['feed_type'] = 'RSS1.0'; 122 $opt['mime_type'] = 'application/xml'; 123 } 124 return $opt; 125} 126 127/** 128 * Add recent changed pages to a feed object 129 * 130 * @author Andreas Gohr <andi@splitbrain.org> 131 * @param object $rss - the FeedCreator Object 132 * @param array $data - the items to add 133 * @param array $opt - the feed options 134 */ 135function rss_buildItems(&$rss,&$data,$opt){ 136 global $conf; 137 global $lang; 138 global $auth; 139 140 foreach($data as $ditem){ 141 if(!is_array($ditem)){ 142 // not an array? then only a list of IDs was given 143 $ditem = array( 'id' => $ditem ); 144 } 145 146 $item = new FeedItem(); 147 $id = $ditem['id']; 148 $meta = p_get_metadata($id); 149 150 // add date 151 if($ditem['date']){ 152 $date = $ditem['date']; 153 }elseif($meta['date']['modified']){ 154 $date = $meta['date']['modified']; 155 }else{ 156 $date = @filemtime(wikiFN($id)); 157 } 158 if($date) $item->date = date('r',$date); 159 160 // add title 161 if($conf['useheading'] && $meta['title']){ 162 $item->title = $meta['title']; 163 }else{ 164 $item->title = $ditem['id']; 165 } 166 if($conf['rss_show_summary'] && !empty($ditem['sum'])){ 167 $item->title .= ' - '.strip_tags($ditem['sum']); 168 } 169 170 // add item link 171 switch ($opt['link_to']){ 172 case 'page': 173 $item->link = wl($id,'rev='.$date,true,'&'); 174 break; 175 case 'rev': 176 $item->link = wl($id,'do=revisions&rev='.$date,true,'&'); 177 break; 178 case 'current': 179 $item->link = wl($id, '', true,'&'); 180 break; 181 case 'diff': 182 default: 183 $item->link = wl($id,'rev='.$date.'&do=diff',true,'&'); 184 } 185 186 // add item content 187 switch ($opt['item_content']){ 188 case 'diff': 189 case 'htmldiff': 190 require_once(DOKU_INC.'inc/DifferenceEngine.php'); 191 $revs = getRevisions($id, 0, 1); 192 $rev = $revs[0]; 193 194 if($rev){ 195 $df = new Diff(explode("\n",htmlspecialchars(rawWiki($id,$rev))), 196 explode("\n",htmlspecialchars(rawWiki($id,'')))); 197 }else{ 198 $df = new Diff(array(''), 199 explode("\n",htmlspecialchars(rawWiki($id,'')))); 200 } 201 202 if($opt['item_content'] == 'htmldiff'){ 203 $tdf = new TableDiffFormatter(); 204 $content = '<table>'; 205 $content .= '<tr><th colspan="2" width="50%">'.$rev.'</th>'; 206 $content .= '<th colspan="2" width="50%">'.$lang['current'].'</th></tr>'; 207 $content .= $tdf->format($df); 208 $content .= '</table>'; 209 }else{ 210 $udf = new UnifiedDiffFormatter(); 211 $content = "<pre>\n".$udf->format($df)."\n</pre>"; 212 } 213 break; 214 case 'html': 215 $content = p_wiki_xhtml($id,$date,false); 216 // no TOC in feeds 217 $content = preg_replace('/(<!-- TOC START -->).*(<!-- TOC END -->)/s','',$content); 218 219 // make URLs work when canonical is not set, regexp instead of rerendering! 220 if(!$conf['canonical']){ 221 $base = preg_quote(DOKU_REL,'/'); 222 $content = preg_replace('/(<a href|<img src)="('.$base.')/s','$1="'.DOKU_URL,$content); 223 } 224 225 break; 226 case 'abstract': 227 default: 228 $content = $meta['description']['abstract']; 229 } 230 $item->description = $content; //FIXME a plugin hook here could be senseful 231 232 // add user 233 # FIXME should the user be pulled from metadata as well? 234 $user = null; 235 $user = @$ditem['user']; // the @ spares time repeating lookup 236 $item->author = ''; 237 if($user && $conf['useacl'] && $auth){ 238 $userInfo = $auth->getUserData($user); 239 $item->author = $userInfo['name']; 240 if($userInfo && !$opt['guardmail']){ 241 $item->authorEmail = $userInfo['mail']; 242 }else{ 243 //cannot obfuscate because some RSS readers may check validity 244 $item->authorEmail = $user.'@'.$recent['ip']; 245 } 246 }elseif($user){ 247 // this happens when no ACL but some Apache auth is used 248 $item->author = $user; 249 $item->authorEmail = $user.'@'.$recent['ip']; 250 }else{ 251 $item->authorEmail = 'anonymous@'.$recent['ip']; 252 } 253 254 // add category 255 if($meta['subject']){ 256 $item->category = $meta['subject']; 257 }else{ 258 $cat = getNS($id); 259 if($cat) $item->category = $cat; 260 } 261 262 // finally add the item to the feed object, after handing it to registered plugins 263 $evdata = array('item' => &$item, 264 'opt' => &$opt, 265 'ditem' => &$ditem, 266 'rss' => &$rss); 267 $evt = new Doku_Event('FEED_ITEM_ADD', $evdata); 268 if ($evt->advise_before()){ 269 $rss->addItem($item); 270 } 271 $evt->advise_after(); // for completeness 272 } 273} 274 275 276/** 277 * Add recent changed pages to the feed object 278 * 279 * @author Andreas Gohr <andi@splitbrain.org> 280 */ 281function rssRecentChanges(&$rss,$opt){ 282 global $conf; 283 global $auth; 284 285 $flags = RECENTS_SKIP_DELETED; 286 if(!$opt['show_minor']) $flags += RECENTS_SKIP_MINORS; 287 288 $recents = getRecents(0,$opt['items'],$opt['namespace'],$flags); 289 290 rss_buildItems($rss,$recents,$opt); 291} 292 293/** 294 * Add all pages of a namespace to the feed object 295 * 296 * @author Andreas Gohr <andi@splitbrain.org> 297 */ 298function rssListNamespace(&$rss,$opt){ 299 require_once(DOKU_INC.'inc/search.php'); 300 global $conf; 301 302 $ns=':'.cleanID($opt['namespace']); 303 $ns=str_replace(':','/',$ns); 304 305 $data = array(); 306 sort($data); 307 search($data,$conf['datadir'],'search_list','',$ns); 308 309 rss_buildItems($rss,$data,$opt); 310} 311 312/** 313 * Add the result of a full text search to the feed object 314 * 315 * @author Andreas Gohr <andi@splitbrain.org> 316 */ 317function rssSearch(&$rss,$opt){ 318 if(!$opt['search_query']) return; 319 320 require_once(DOKU_INC.'inc/fulltext.php'); 321 $data = array(); 322 $data = ft_pageSearch($opt['search_query'],$poswords); 323 $data = array_keys($data); 324 rss_buildItems($rss,$data,$opt); 325} 326 327//Setup VIM: ex: et ts=4 enc=utf-8 : 328