xref: /dokuwiki/feed.php (revision a8a703a4d824a2a611926012072647b4a84cdfec)
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
22  $num   = $_REQUEST['num'];
23  $type  = $_REQUEST['type'];
24  $mode  = $_REQUEST['mode'];
25  $minor = $_REQUEST['minor'];
26  $ns    = $_REQUEST['ns'];
27  $ltype = $_REQUEST['linkto'];
28
29  if($type == '')
30    $type = $conf['rss_type'];
31
32  switch ($type){
33    case 'rss':
34       $type = 'RSS0.91';
35       $mime = 'text/xml';
36       break;
37    case 'rss2':
38       $type = 'RSS2.0';
39       $mime = 'text/xml';
40       break;
41    case 'atom':
42       $type = 'ATOM0.3';
43       $mime = 'application/xml';
44       break;
45    case 'atom1':
46       $type = 'ATOM1.0';
47       $mime = 'application/atom+xml';
48       break;
49    default:
50       $type = 'RSS1.0';
51       $mime = 'application/xml';
52  }
53
54  // the feed is dynamic - we need a cache for each combo
55  // (but most people just use the default feed so it's still effective)
56  $cache = getCacheName($num.$type.$mode.$ns.$ltype.$_SERVER['REMOTE_USER'],'.feed');
57
58  // check cacheage and deliver if nothing has changed since last
59  // time or the update interval has not passed, also handles conditional requests
60  header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
61  header('Pragma: public');
62  header('Content-Type: application/xml; charset=utf-8');
63  $cmod = @filemtime($cache); // 0 if not exists
64  if($cmod && (($cmod+$conf['rss_update']>time()) || ($cmod>@filemtime($conf['changelog'])))){
65    http_conditionalRequest($cmod);
66    if($conf['allowdebug']) header("X-CacheUsed: $cache");
67    print io_readFile($cache);
68    exit;
69  } else {
70    http_conditionalRequest(time());
71  }
72
73  // create new feed
74  $rss = new DokuWikiFeedCreator();
75  $rss->title = $conf['title'].(($ns) ? ' '.$ns : '');
76  $rss->link  = DOKU_URL;
77  $rss->syndicationURL = DOKU_URL.'feed.php';
78  $rss->cssStyleSheet  = DOKU_URL.'lib/styles/feed.css';
79
80  $image = new FeedImage();
81  $image->title = $conf['title'];
82  $image->url = DOKU_URL."lib/images/favicon.ico";
83  $image->link = DOKU_URL;
84  $rss->image = $image;
85
86  if($mode == 'list'){
87    rssListNamespace($rss,$ns);
88  }else{
89    rssRecentChanges($rss,$num,$ltype,$ns,$minor);
90  }
91
92  $feed = $rss->createFeed($type,'utf-8');
93
94  // save cachefile
95  io_saveFile($cache,$feed);
96
97  // finally deliver
98  print $feed;
99
100// ---------------------------------------------------------------- //
101
102/**
103 * Add recent changed pages to a feed object
104 *
105 * @author Andreas Gohr <andi@splitbrain.org>
106 */
107function rssRecentChanges(&$rss,$num,$ltype,$ns,$minor){
108    global $conf;
109    global $auth;
110
111    if(!$num) $num = $conf['recent'];
112    $guardmail = ($conf['mailguard'] != '' && $conf['mailguard'] != 'none');
113
114
115    $flags = RECENTS_SKIP_DELETED;
116    if(!$minor) $flags += RECENTS_SKIP_MINORS;
117
118    $recents = getRecents(0,$num,$ns,$flags);
119
120    foreach($recents as $recent){
121        $item = new FeedItem();
122        $meta = p_get_metadata($recent['id']);
123
124        if($conf['useheading'] && $meta['title']){
125            $item->title = $meta['title'];
126        }else{
127            $item->title = $recent['id'];
128        }
129        if(!empty($recent['sum'])){
130            $item->title .= ' - '.strip_tags($recent['sum']);
131        }
132
133        if(empty($ltype)) $ltype = $conf['rss_linkto'];
134
135        switch ($ltype){
136            case 'page':
137                $item->link = wl($recent['id'],'rev='.$recent['date'],true);
138                break;
139            case 'rev':
140                $item->link = wl($recent['id'],'do=revisions&rev='.$recent['date'],true);
141                break;
142            case 'current':
143                $item->link = wl($recent['id'], '', true);
144                break;
145            case 'diff':
146            default:
147                $item->link = wl($recent['id'],'rev='.$recent['date'].'&do=diff'.$recent['date'],true);
148        }
149
150        $item->description = $meta['description']['abstract'];
151        $item->date        = date('r',$recent['date']);
152        $cat = getNS($recent['id']);
153        if($cat) $item->category = $cat;
154
155        // FIXME should the user be pulled from metadata as well?
156        $user = null;
157        $user = @$recent['user']; // the @ spares time repeating lookup
158        $item->author = '';
159
160        if($user){
161            $userInfo = $auth->getUserData($user);
162            $item->author = $userInfo['name'];
163            if($guardmail) {
164            //cannot obfuscate because some RSS readers may check validity
165                $item->authorEmail = $user.'@'.$recent['ip'];
166            }else{
167                $item->authorEmail = $userInfo['mail'];
168            }
169        }else{
170            $item->authorEmail = 'anonymous@'.$recent['ip'];
171        }
172        $rss->addItem($item);
173    }
174}
175
176/**
177 * Add all pages of a namespace to a feedobject
178 *
179 * @author Andreas Gohr <andi@splitbrain.org>
180 */
181function rssListNamespace(&$rss,$ns){
182    require_once(DOKU_INC.'inc/search.php');
183    global $conf;
184
185    $ns=':'.cleanID($ns);
186    $ns=str_replace(':','/',$ns);
187
188    $data = array();
189    sort($data);
190    search($data,$conf['datadir'],'search_list','',$ns);
191    foreach($data as $row){
192        $item = new FeedItem();
193
194        $id   = $row['id'];
195        $date = filemtime(wikiFN($id));
196        $meta = p_get_metadata($id);
197
198        if($conf['useheading'] && $meta['title']){
199            $item->title = $meta['title'];
200        }else{
201            $item->title = $id;
202        }
203
204        $item->link        = wl($id,'rev='.$date,true);
205        $item->description = $meta['description']['abstract'];
206        $item->date        = date('r',$date);
207        $rss->addItem($item);
208  }
209}
210
211//Setup VIM: ex: et ts=4 enc=utf-8 :
212?>
213