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',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 header('X-Robots-Tag: noindex'); 39 if($cmod && (($cmod+$conf['rss_update']>time()) || ($cmod>@filemtime($conf['changelog'])))){ 40 http_conditionalRequest($cmod); 41 if($conf['allowdebug']) header("X-CacheUsed: $cache"); 42 print io_readFile($cache); 43 exit; 44 } else { 45 http_conditionalRequest(time()); 46 } 47 48 // create new feed 49 $rss = new DokuWikiFeedCreator(); 50 $rss->title = $conf['title'].(($opt['namespace']) ? ' '.$opt['namespace'] : ''); 51 $rss->link = DOKU_URL; 52 $rss->syndicationURL = DOKU_URL.'feed.php'; 53 $rss->cssStyleSheet = DOKU_URL.'lib/exe/css.php?s=feed'; 54 55 $image = new FeedImage(); 56 $image->title = $conf['title']; 57 $image->url = DOKU_URL."lib/images/favicon.ico"; 58 $image->link = DOKU_URL; 59 $rss->image = $image; 60 61 if($opt['feed_mode'] == 'list'){ 62 rssListNamespace($rss,$opt); 63 }elseif($opt['feed_mode'] == 'search'){ 64 rssSearch($rss,$opt); 65 }else{ 66 rssRecentChanges($rss,$opt); 67 } 68 69 $feed = $rss->createFeed($opt['feed_type'],'utf-8'); 70 71 // save cachefile 72 io_saveFile($cache,$feed); 73 74 // finally deliver 75 print $feed; 76 77// ---------------------------------------------------------------- // 78 79/** 80 * Get URL parameters and config options and return a initialized option array 81 * 82 * @author Andreas Gohr <andi@splitbrain.org> 83 */ 84function rss_parseOptions(){ 85 global $conf; 86 87 $opt['items'] = (int) $_REQUEST['num']; 88 $opt['feed_type'] = $_REQUEST['type']; 89 $opt['feed_mode'] = $_REQUEST['mode']; 90 $opt['show_minor'] = $_REQUEST['minor']; 91 $opt['namespace'] = $_REQUEST['ns']; 92 $opt['link_to'] = $_REQUEST['linkto']; 93 $opt['item_content'] = $_REQUEST['content']; 94 $opt['search_query'] = $_REQUEST['q']; 95 96 if(!$opt['feed_type']) $opt['feed_type'] = $conf['rss_type']; 97 if(!$opt['item_content']) $opt['item_content'] = $conf['rss_content']; 98 if(!$opt['link_to']) $opt['link_to'] = $conf['rss_linkto']; 99 if(!$opt['items']) $opt['items'] = $conf['recent']; 100 $opt['guardmail'] = ($conf['mailguard'] != '' && $conf['mailguard'] != 'none'); 101 102 switch ($opt['feed_type']){ 103 case 'rss': 104 $opt['feed_type'] = 'RSS0.91'; 105 $opt['mime_type'] = 'text/xml'; 106 break; 107 case 'rss2': 108 $opt['feed_type'] = 'RSS2.0'; 109 $opt['mime_type'] = 'text/xml'; 110 break; 111 case 'atom': 112 $opt['feed_type'] = 'ATOM0.3'; 113 $opt['mime_type'] = 'application/xml'; 114 break; 115 case 'atom1': 116 $opt['feed_type'] = 'ATOM1.0'; 117 $opt['mime_type'] = 'application/atom+xml'; 118 break; 119 default: 120 $opt['feed_type'] = 'RSS1.0'; 121 $opt['mime_type'] = 'application/xml'; 122 } 123 return $opt; 124} 125 126/** 127 * Add recent changed pages to a feed object 128 * 129 * @author Andreas Gohr <andi@splitbrain.org> 130 * @param object $rss - the FeedCreator Object 131 * @param array $data - the items to add 132 * @param array $opt - the feed options 133 */ 134function rss_buildItems(&$rss,&$data,$opt){ 135 global $conf; 136 global $lang; 137 global $auth; 138 139 foreach($data as $ditem){ 140 if(!is_array($ditem)){ 141 // not an array? then only a list of IDs was given 142 $ditem = array( 'id' => $ditem ); 143 } 144 145 $item = new FeedItem(); 146 $id = $ditem['id']; 147 $meta = p_get_metadata($id); 148 149 // add date 150 if($ditem['date']){ 151 $date = $ditem['date']; 152 }elseif($meta['date']['modified']){ 153 $date = $meta['date']['modified']; 154 }else{ 155 $date = @filemtime(wikiFN($id)); 156 } 157 if($date) $item->date = date('r',$date); 158 159 // add title 160 if($conf['useheading'] && $meta['title']){ 161 $item->title = $meta['title']; 162 }else{ 163 $item->title = $ditem['id']; 164 } 165 if($conf['rss_show_summary'] && !empty($ditem['sum'])){ 166 $item->title .= ' - '.strip_tags($ditem['sum']); 167 } 168 169 // add item link 170 switch ($opt['link_to']){ 171 case 'page': 172 $item->link = wl($id,'rev='.$date,true,'&'); 173 break; 174 case 'rev': 175 $item->link = wl($id,'do=revisions&rev='.$date,true,'&'); 176 break; 177 case 'current': 178 $item->link = wl($id, '', true,'&'); 179 break; 180 case 'diff': 181 default: 182 $item->link = wl($id,'rev='.$date.'&do=diff',true,'&'); 183 } 184 185 // add item content 186 switch ($opt['item_content']){ 187 case 'diff': 188 case 'htmldiff': 189 require_once(DOKU_INC.'inc/DifferenceEngine.php'); 190 $revs = getRevisions($id, 0, 1); 191 $rev = $revs[0]; 192 193 if($rev){ 194 $df = new Diff(explode("\n",htmlspecialchars(rawWiki($id,$rev))), 195 explode("\n",htmlspecialchars(rawWiki($id,'')))); 196 }else{ 197 $df = new Diff(array(''), 198 explode("\n",htmlspecialchars(rawWiki($id,'')))); 199 } 200 201 if($opt['item_content'] == 'htmldiff'){ 202 $tdf = new TableDiffFormatter(); 203 $content = '<table>'; 204 $content .= '<tr><th colspan="2" width="50%">'.$rev.'</th>'; 205 $content .= '<th colspan="2" width="50%">'.$lang['current'].'</th></tr>'; 206 $content .= $tdf->format($df); 207 $content .= '</table>'; 208 }else{ 209 $udf = new UnifiedDiffFormatter(); 210 $content = "<pre>\n".$udf->format($df)."\n</pre>"; 211 } 212 break; 213 case 'html': 214 $content = p_wiki_xhtml($id,$date,false); 215 // no TOC in feeds 216 $content = preg_replace('/(<!-- TOC START -->).*(<!-- TOC END -->)/s','',$content); 217 218 // make URLs work when canonical is not set, regexp instead of rerendering! 219 if(!$conf['canonical']){ 220 $base = preg_quote(DOKU_REL,'/'); 221 $content = preg_replace('/(<a href|<img src)="('.$base.')/s','$1="'.DOKU_URL,$content); 222 } 223 224 break; 225 case 'abstract': 226 default: 227 $content = $meta['description']['abstract']; 228 } 229 $item->description = $content; //FIXME a plugin hook here could be senseful 230 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?> 329