xref: /dokuwiki/feed.php (revision 0cdf47d1bceb43ef108a4751dbd3463acc991aae)
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/parserutils.php');
13  require_once(DOKU_INC.'inc/feedcreator.class.php');
14  require_once(DOKU_INC.'inc/auth.php');
15
16  //close session
17  session_write_close();
18
19
20  $num   = $_REQUEST['num'];
21  $type  = $_REQUEST['type'];
22  $mode  = $_REQUEST['mode'];
23  $ns    = $_REQUEST['ns'];
24  $ltype = $_REQUEST['linkto'];
25
26  if($type == '')
27    $type = $conf['rss_type'];
28
29  switch ($type){
30    case 'rss':
31       $type = 'RSS0.9';
32       break;
33    case 'rss2':
34       $type = 'RSS2.0';
35       break;
36    case 'atom':
37       $type = 'ATOM0.3';
38       break;
39    default:
40       $type = 'RSS1.0';
41  }
42
43	// the feed is dynamic - we need a cache for each combo
44  // (but most people just use the default feed so it's still effective)
45	$cache = getCacheName($num.$type.$mode.$ns.$ltype.$_SERVER['REMOTE_USER'],'.feed');
46
47	// check cacheage and deliver if nothing has changed since last
48  // time (with 5 minutes settletime)
49	$cmod = @filemtime($cache); // 0 if not exists
50	if($cmod && ($cmod+(5*60) >= @filemtime($conf['changelog']))){
51  	header('Content-Type: application/xml; charset=utf-8');
52		print io_readFile($cache);
53		exit;
54  }
55
56	// create new feed
57  $rss = new DokuWikiFeedCreator();
58  $rss->title = $conf['title'].(($ns) ? ' '.$ns : '');
59  $rss->link  = DOKU_URL;
60  $rss->syndicationURL = DOKU_URL.'feed.php';
61  $rss->cssStyleSheet  = DOKU_URL.'lib/styles/feed.css';
62
63  $image = new FeedImage();
64  $image->title = $conf['title'];
65  $image->url = DOKU_URL."lib/images/favicon.ico";
66  $image->link = DOKU_URL;
67  $rss->image = $image;
68
69  if($mode == 'list'){
70    rssListNamespace($rss,$ns);
71  }else{
72    rssRecentChanges($rss,$num,$ltype,$ns);
73  }
74
75  $feed = $rss->createFeed($type,'utf-8');
76
77  // save cachefile
78	io_saveFile($cache,$feed);
79
80	// finally deliver
81  header('Content-Type: application/xml; charset=utf-8');
82  print $feed;
83
84// ---------------------------------------------------------------- //
85
86/**
87 * Add recent changed to a feed object
88 *
89 * @author Andreas Gohr <andi@splitbrain.org>
90 */
91function rssRecentChanges(&$rss,$num,$ltype,$ns){
92  global $conf;
93  if(!$num) $num = $conf['recent'];
94  $guardmail = ($conf['mailguard'] != '' && $conf['mailguard'] != 'none');
95
96  $recents = getRecents(0,$num,false,$ns);
97
98  //this can take some time if a lot of recaching has to be done
99  @set_time_limit(90); // set max execution time
100
101	foreach($recents as $recent){
102
103    $item = new FeedItem();
104    $item->title = $recent['id'];
105    $xhtml = p_wiki_xhtml($recent['id'],'',false);
106
107    if($conf['useheading']) {
108        $matches = array();
109        if(preg_match('|<h([1-9])>(.*?)</h\1>|', $xhtml, $matches))
110            $item->title = trim($matches[2]);
111    }
112    if(!empty($recent['sum'])){
113      $item->title .= ' - '.strip_tags($recent['sum']);
114    }
115
116    $desc = cleanDesc($xhtml);
117
118    if(empty($ltype))
119      $ltype = $conf['rss_linkto'];
120
121    switch ($ltype){
122      case 'page':
123        $item->link = wl($recent['id'],'rev='.$recent['date'],true);
124        break;
125      case 'rev':
126        $item->link = wl($recent['id'],'do=revisions&amp;rev='.$recent['date'],true);
127        break;
128      case 'current':
129        $item->link = wl($recent['id'], '', true);
130        break;
131      case 'diff':
132      default:
133        $item->link = wl($recent['id'],'do=diff&amp;'.$recent['date'],true);
134    }
135
136    $item->description = $desc;
137    $item->date        = date('r',$recent['date']);
138		$cat = getNS($recent['id']);
139		if($cat) $item->category = $cat;
140
141    $user = null;
142    $user = @$recent['user']; // the @ spares time repeating lookup
143    $item->author = '';
144
145    if($user){
146      $userInfo = auth_getUserData($user);
147      $item->author = $userInfo['name'];
148      if($guardmail) {
149        //cannot obfuscate because some RSS readers may check validity
150        $item->authorEmail = $user.'@'.$recent['ip'];
151      }else{
152        $item->authorEmail = $userInfo['mail'];
153      }
154    }else{
155      $item->authorEmail = 'anonymous@'.$recent['ip'];
156    }
157    $rss->addItem($item);
158  }
159}
160
161/**
162 * Add all pages of a namespace to a feedobject
163 *
164 * @author Andreas Gohr <andi@splitbrain.org>
165 */
166function rssListNamespace(&$rss,$ns){
167  require_once(DOKU_INC.'inc/search.php');
168  global $conf;
169
170  $ns=':'.cleanID($ns);
171  $ns=str_replace(':','/',$ns);
172
173  $data = array();
174  sort($data);
175  search($data,$conf['datadir'],'search_list','',$ns);
176  foreach($data as $row){
177    $id = $row['id'];
178    $date = filemtime(wikiFN($id));
179    $desc = cleanDesc(p_wiki_xhtml($id,'',false));
180    $item = new FeedItem();
181    $item->title       = $id;
182    $item->link        = wl($id,'rev='.$date,true);
183    $item->description = $desc;
184    $item->date        = date('r',$date);
185    $rss->addItem($item);
186  }
187}
188
189/**
190 * Clean description for feed inclusion
191 *
192 * Removes HTML tags and line breaks and trims the text to
193 * 250 chars
194 *
195 * @author Andreas Gohr <andi@splitbrain.org>
196 */
197function cleanDesc($desc){
198  //start description at text of first paragraph
199  $matches = array();
200  if(preg_match('/<p>|<p\s.*?>/', $desc, $matches, PREG_OFFSET_CAPTURE))
201      $desc = substr($desc, $matches[0][1]);
202
203  //remove TOC
204  $desc = preg_replace('!<div class="toc">.*?(</div>\n</div>)!s','',$desc);
205  $desc = strip_tags($desc);
206  $desc = preg_replace('/[\n\r\t]/',' ',$desc);
207  $desc = preg_replace('/  /',' ',$desc);
208  $desc = utf8_substr($desc,0,250);
209  $desc = $desc.'...';
210  return $desc;
211}
212
213?>
214