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 require_once(DOKU_INC.'inc/httputils.php'); 18 19 //close session 20 session_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 29 if ($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 36 header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 37 header('Pragma: public'); 38 header('Content-Type: application/xml; charset=utf-8'); 39 header('X-Robots-Tag: noindex'); 40 if($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 if($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 73 io_saveFile($cache,$feed); 74 75 // finally deliver 76 print $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 233 // add user 234 # FIXME should the user be pulled from metadata as well? 235 $user = null; 236 $user = @$ditem['user']; // the @ spares time repeating lookup 237 $item->author = ''; 238 if($user && $conf['useacl'] && $auth){ 239 $userInfo = $auth->getUserData($user); 240 $item->author = $userInfo['name']; 241 if($userInfo && !$opt['guardmail']){ 242 $item->authorEmail = $userInfo['mail']; 243 }else{ 244 //cannot obfuscate because some RSS readers may check validity 245 $item->authorEmail = $user.'@'.$recent['ip']; 246 } 247 }elseif($user){ 248 // this happens when no ACL but some Apache auth is used 249 $item->author = $user; 250 $item->authorEmail = $user.'@'.$recent['ip']; 251 }else{ 252 $item->authorEmail = 'anonymous@'.$recent['ip']; 253 } 254 255 // add category 256 if($meta['subject']){ 257 $item->category = $meta['subject']; 258 }else{ 259 $cat = getNS($id); 260 if($cat) $item->category = $cat; 261 } 262 263 // finally add the item to the feed object, after handing it to registered plugins 264 $evdata = array('item' => &$item, 265 'opt' => &$opt, 266 'ditem' => &$ditem, 267 'rss' => &$rss); 268 $evt = new Doku_Event('FEED_ITEM_ADD', $evdata); 269 if ($evt->advise_before()){ 270 $rss->addItem($item); 271 } 272 $evt->advise_after(); // for completeness 273 } 274} 275 276 277/** 278 * Add recent changed pages to the feed object 279 * 280 * @author Andreas Gohr <andi@splitbrain.org> 281 */ 282function rssRecentChanges(&$rss,$opt){ 283 global $conf; 284 global $auth; 285 286 $flags = RECENTS_SKIP_DELETED; 287 if(!$opt['show_minor']) $flags += RECENTS_SKIP_MINORS; 288 289 $recents = getRecents(0,$opt['items'],$opt['namespace'],$flags); 290 291 rss_buildItems($rss,$recents,$opt); 292} 293 294/** 295 * Add all pages of a namespace to the feed object 296 * 297 * @author Andreas Gohr <andi@splitbrain.org> 298 */ 299function rssListNamespace(&$rss,$opt){ 300 require_once(DOKU_INC.'inc/search.php'); 301 global $conf; 302 303 $ns=':'.cleanID($opt['namespace']); 304 $ns=str_replace(':','/',$ns); 305 306 $data = array(); 307 sort($data); 308 search($data,$conf['datadir'],'search_list','',$ns); 309 310 rss_buildItems($rss,$data,$opt); 311} 312 313/** 314 * Add the result of a full text search to the feed object 315 * 316 * @author Andreas Gohr <andi@splitbrain.org> 317 */ 318function rssSearch(&$rss,$opt){ 319 if(!$opt['search_query']) return; 320 321 require_once(DOKU_INC.'inc/fulltext.php'); 322 $data = array(); 323 $data = ft_pageSearch($opt['search_query'],$poswords); 324 $data = array_keys($data); 325 rss_buildItems($rss,$data,$opt); 326} 327 328//Setup VIM: ex: et ts=4 enc=utf-8 : 329