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