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