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