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