Merge branch 'master' of https://github.com/lupo49/dokuwiki into lupo49-master
[dokuwiki.git] / feed.php
blob615d1597dd03ca34e9727bda0f2a39cc222fc496
1 <?php
2 /**
3 * XML feed export
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author Andreas Gohr <andi@splitbrain.org>
7 */
9 if(!defined('DOKU_INC')) define('DOKU_INC',dirname(__FILE__).'/');
10 require_once(DOKU_INC.'inc/init.php');
12 //close session
13 session_write_close();
15 // get params
16 $opt = rss_parseOptions();
18 // the feed is dynamic - we need a cache for each combo
19 // (but most people just use the default feed so it's still effective)
20 $cache = getCacheName(join('',array_values($opt)).$_SERVER['REMOTE_USER'],'.feed');
21 $key = join('', array_values($opt)) . $_SERVER['REMOTE_USER'];
22 $cache = new cache($key, '.feed');
24 // prepare cache depends
25 $depends['files'] = getConfigFiles('main');
26 $depends['age'] = $conf['rss_update'];
27 $depends['purge'] = isset($_REQUEST['purge']);
29 // check cacheage and deliver if nothing has changed since last
30 // time or the update interval has not passed, also handles conditional requests
31 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
32 header('Pragma: public');
33 header('Content-Type: application/xml; charset=utf-8');
34 header('X-Robots-Tag: noindex');
35 if($cache->useCache($depends)) {
36 http_conditionalRequest($cache->_time);
37 if($conf['allowdebug']) header("X-CacheUsed: $cache->cache");
38 print $cache->retrieveCache();
39 exit;
40 } else {
41 http_conditionalRequest(time());
44 // create new feed
45 $rss = new DokuWikiFeedCreator();
46 $rss->title = $conf['title'].(($opt['namespace']) ? ' '.$opt['namespace'] : '');
47 $rss->link = DOKU_URL;
48 $rss->syndicationURL = DOKU_URL.'feed.php';
49 $rss->cssStyleSheet = DOKU_URL.'lib/exe/css.php?s=feed';
51 $image = new FeedImage();
52 $image->title = $conf['title'];
53 $image->url = DOKU_URL."lib/images/favicon.ico";
54 $image->link = DOKU_URL;
55 $rss->image = $image;
57 $data = null;
58 $modes = array('list' => 'rssListNamespace',
59 'search' => 'rssSearch',
60 'recent' => 'rssRecentChanges');
61 if (isset($modes[$opt['feed_mode']])) {
62 $data = $modes[$opt['feed_mode']]($opt);
63 } else {
64 $eventData = array(
65 'opt' => &$opt,
66 'data' => &$data,
68 $event = new Doku_Event('FEED_MODE_UNKNOWN', $eventData);
69 if ($event->advise_before(true)) {
70 echo sprintf('<error>Unknown feed mode %s</error>', hsc($opt['feed_mode']));
71 exit;
73 $event->advise_after();
76 rss_buildItems($rss, $data, $opt);
77 $feed = $rss->createFeed($opt['feed_type'],'utf-8');
79 // save cachefile
80 $cache->storeCache($feed);
82 // finally deliver
83 print $feed;
85 // ---------------------------------------------------------------- //
87 /**
88 * Get URL parameters and config options and return an initialized option array
90 * @author Andreas Gohr <andi@splitbrain.org>
92 function rss_parseOptions(){
93 global $conf;
95 $opt = array();
97 foreach(array(
98 // Basic feed properties
99 // Plugins may probably want to add new values to these
100 // properties for implementing own feeds
102 // One of: list, search, recent
103 'feed_mode' => array('mode', 'recent'),
104 // One of: diff, page, rev, current
105 'link_to' => array('linkto', $conf['rss_linkto']),
106 // One of: abstract, diff, htmldiff, html
107 'item_content' => array('content', $conf['rss_content']),
109 // Special feed properties
110 // These are only used by certain feed_modes
112 // String, used for feed title, in list and rc mode
113 'namespace' => array('ns', null),
114 // Positive integer, only used in rc mode
115 'items' => array('num', $conf['recent']),
116 // Boolean, only used in rc mode
117 'show_minor' => array('minor', false),
118 // String, only used in search mode
119 'search_query' => array('q', null),
121 ) as $name => $val) {
122 $opt[$name] = (isset($_REQUEST[$val[0]]) && !empty($_REQUEST[$val[0]]))
123 ? $_REQUEST[$val[0]] : $val[1];
126 $opt['items'] = max(0, (int) $opt['items']);
127 $opt['show_minor'] = (bool) $opt['show_minor'];
129 $opt['guardmail'] = ($conf['mailguard'] != '' && $conf['mailguard'] != 'none');
131 $type = valid_input_set('type', array('rss','rss2','atom','atom1','rss1',
132 'default' => $conf['rss_type']),
133 $_REQUEST);
134 switch ($type){
135 case 'rss':
136 $opt['feed_type'] = 'RSS0.91';
137 $opt['mime_type'] = 'text/xml';
138 break;
139 case 'rss2':
140 $opt['feed_type'] = 'RSS2.0';
141 $opt['mime_type'] = 'text/xml';
142 break;
143 case 'atom':
144 $opt['feed_type'] = 'ATOM0.3';
145 $opt['mime_type'] = 'application/xml';
146 break;
147 case 'atom1':
148 $opt['feed_type'] = 'ATOM1.0';
149 $opt['mime_type'] = 'application/atom+xml';
150 break;
151 default:
152 $opt['feed_type'] = 'RSS1.0';
153 $opt['mime_type'] = 'application/xml';
156 $eventData = array(
157 'opt' => &$opt,
159 trigger_event('FEED_OPTS_POSTPROCESS', $eventData);
160 return $opt;
164 * Add recent changed pages to a feed object
166 * @author Andreas Gohr <andi@splitbrain.org>
167 * @param object $rss - the FeedCreator Object
168 * @param array $data - the items to add
169 * @param array $opt - the feed options
171 function rss_buildItems(&$rss,&$data,$opt){
172 global $conf;
173 global $lang;
174 global $auth;
176 $eventData = array(
177 'rss' => &$rss,
178 'data' => &$data,
179 'opt' => &$opt,
181 $event = new Doku_Event('FEED_DATA_PROCESS', $eventData);
182 if ($event->advise_before(false)){
183 foreach($data as $ditem){
184 if(!is_array($ditem)){
185 // not an array? then only a list of IDs was given
186 $ditem = array( 'id' => $ditem );
189 $item = new FeedItem();
190 $id = $ditem['id'];
191 $meta = p_get_metadata($id);
193 // add date
194 if($ditem['date']){
195 $date = $ditem['date'];
196 }elseif($meta['date']['modified']){
197 $date = $meta['date']['modified'];
198 }else{
199 $date = @filemtime(wikiFN($id));
201 if($date) $item->date = date('r',$date);
203 // add title
204 if($conf['useheading'] && $meta['title']){
205 $item->title = $meta['title'];
206 }else{
207 $item->title = $ditem['id'];
209 if($conf['rss_show_summary'] && !empty($ditem['sum'])){
210 $item->title .= ' - '.strip_tags($ditem['sum']);
213 // add item link
214 switch ($opt['link_to']){
215 case 'page':
216 $item->link = wl($id,'rev='.$date,true,'&');
217 break;
218 case 'rev':
219 $item->link = wl($id,'do=revisions&rev='.$date,true,'&');
220 break;
221 case 'current':
222 $item->link = wl($id, '', true,'&');
223 break;
224 case 'diff':
225 default:
226 $item->link = wl($id,'rev='.$date.'&do=diff',true,'&');
229 // add item content
230 switch ($opt['item_content']){
231 case 'diff':
232 case 'htmldiff':
233 require_once(DOKU_INC.'inc/DifferenceEngine.php');
234 $revs = getRevisions($id, 0, 1);
235 $rev = $revs[0];
237 if($rev){
238 $df = new Diff(explode("\n",htmlspecialchars(rawWiki($id,$rev))),
239 explode("\n",htmlspecialchars(rawWiki($id,''))));
240 }else{
241 $df = new Diff(array(''),
242 explode("\n",htmlspecialchars(rawWiki($id,''))));
245 if($opt['item_content'] == 'htmldiff'){
246 $tdf = new TableDiffFormatter();
247 $content = '<table>';
248 $content .= '<tr><th colspan="2" width="50%">'.$rev.'</th>';
249 $content .= '<th colspan="2" width="50%">'.$lang['current'].'</th></tr>';
250 $content .= $tdf->format($df);
251 $content .= '</table>';
252 }else{
253 $udf = new UnifiedDiffFormatter();
254 $content = "<pre>\n".$udf->format($df)."\n</pre>";
256 break;
257 case 'html':
258 $content = p_wiki_xhtml($id,$date,false);
259 // no TOC in feeds
260 $content = preg_replace('/(<!-- TOC START -->).*(<!-- TOC END -->)/s','',$content);
262 // make URLs work when canonical is not set, regexp instead of rerendering!
263 if(!$conf['canonical']){
264 $base = preg_quote(DOKU_REL,'/');
265 $content = preg_replace('/(<a href|<img src)="('.$base.')/s','$1="'.DOKU_URL,$content);
268 break;
269 case 'abstract':
270 default:
271 $content = $meta['description']['abstract'];
273 $item->description = $content; //FIXME a plugin hook here could be senseful
275 // add user
276 # FIXME should the user be pulled from metadata as well?
277 $user = @$ditem['user']; // the @ spares time repeating lookup
278 $item->author = '';
279 if($user && $conf['useacl'] && $auth){
280 $userInfo = $auth->getUserData($user);
281 if ($userInfo){
282 switch ($conf['showuseras']){
283 case 'username':
284 $item->author = $userInfo['name'];
285 break;
286 default:
287 $item->author = $user;
288 break;
290 } else {
291 $item->author = $user;
293 if($userInfo && !$opt['guardmail']){
294 $item->authorEmail = $userInfo['mail'];
295 }else{
296 //cannot obfuscate because some RSS readers may check validity
297 $item->authorEmail = $user.'@'.$ditem['ip'];
299 }elseif($user){
300 // this happens when no ACL but some Apache auth is used
301 $item->author = $user;
302 $item->authorEmail = $user.'@'.$ditem['ip'];
303 }else{
304 $item->authorEmail = 'anonymous@'.$ditem['ip'];
307 // add category
308 if(isset($meta['subject'])) {
309 $item->category = $meta['subject'];
310 }else{
311 $cat = getNS($id);
312 if($cat) $item->category = $cat;
315 // finally add the item to the feed object, after handing it to registered plugins
316 $evdata = array('item' => &$item,
317 'opt' => &$opt,
318 'ditem' => &$ditem,
319 'rss' => &$rss);
320 $evt = new Doku_Event('FEED_ITEM_ADD', $evdata);
321 if ($evt->advise_before()){
322 $rss->addItem($item);
324 $evt->advise_after(); // for completeness
327 $event->advise_after();
332 * Add recent changed pages to the feed object
334 * @author Andreas Gohr <andi@splitbrain.org>
336 function rssRecentChanges($opt){
337 $flags = RECENTS_SKIP_DELETED;
338 if(!$opt['show_minor']) $flags += RECENTS_SKIP_MINORS;
340 $recents = getRecents(0,$opt['items'],$opt['namespace'],$flags);
341 return $recents;
345 * Add all pages of a namespace to the feed object
347 * @author Andreas Gohr <andi@splitbrain.org>
349 function rssListNamespace($opt){
350 require_once(DOKU_INC.'inc/search.php');
351 global $conf;
353 $ns=':'.cleanID($opt['namespace']);
354 $ns=str_replace(':','/',$ns);
356 $data = array();
357 sort($data);
358 search($data,$conf['datadir'],'search_list','',$ns);
360 return $data;
364 * Add the result of a full text search to the feed object
366 * @author Andreas Gohr <andi@splitbrain.org>
368 function rssSearch($opt){
369 if(!$opt['search_query']) return;
371 require_once(DOKU_INC.'inc/fulltext.php');
372 $data = ft_pageSearch($opt['search_query'],$poswords);
373 $data = array_keys($data);
375 return $data;
378 //Setup VIM: ex: et ts=4 :