xref: /dokuwiki/feed.php (revision 9773c5edfdb8d172ce0d2bb7644b774728be894a)
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(array_keys($recents) as $id){
102
103    $item = new FeedItem();
104    $item->title = $id;
105    $xhtml = p_wiki_xhtml($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($recents[$id]['sum'])){
113      $item->title .= ' - '.strip_tags($recents[$id]['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($id,'rev='.$recents[$id]['date'],true);
124        break;
125      case 'rev':
126        $item->link = wl($id,'do=revisions&amp;rev='.$recents[$id]['date'],true);
127        break;
128      case 'current':
129        $item->link = wl($id, '', true);
130        break;
131      case 'diff':
132      default:
133        $item->link = wl($id,'do=diff&amp;'.$recents[$id]['date'],true);
134    }
135
136    $item->description = $desc;
137    $item->date        = date('r',$recents[$id]['date']);
138    if(strpos($id,':')!==false){
139      $item->category    = substr($id,0,strrpos($id,':'));
140    }
141
142    $user = null;
143    $user = @$recents[$id]['user']; // the @ spares time repeating lookup
144    $item->author = '';
145
146    if($user){
147      $userInfo = auth_getUserData($user);
148      $item->author = $userInfo['name'];
149      if($guardmail) {
150        //cannot obfuscate because some RSS readers may check validity
151        $item->authorEmail = $user.'@'.$recents[$id]['ip'];
152      }else{
153        $item->authorEmail = $userInfo['mail'];
154      }
155    }else{
156      $item->authorEmail = 'anonymous@'.$recents[$id]['ip'];
157    }
158    $rss->addItem($item);
159  }
160}
161
162/**
163 * Add all pages of a namespace to a feedobject
164 *
165 * @author Andreas Gohr <andi@splitbrain.org>
166 */
167function rssListNamespace(&$rss,$ns){
168  require_once(DOKU_INC.'inc/search.php');
169  global $conf;
170
171  $ns=':'.cleanID($ns);
172  $ns=str_replace(':','/',$ns);
173
174  $data = array();
175  sort($data);
176  search($data,$conf['datadir'],'search_list','',$ns);
177  foreach($data as $row){
178    $id = $row['id'];
179    $date = filemtime(wikiFN($id));
180    $desc = cleanDesc(p_wiki_xhtml($id,'',false));
181    $item = new FeedItem();
182    $item->title       = $id;
183    $item->link        = wl($id,'rev='.$date,true);
184    $item->description = $desc;
185    $item->date        = date('r',$date);
186    $rss->addItem($item);
187  }
188}
189
190/**
191 * Clean description for feed inclusion
192 *
193 * Removes HTML tags and line breaks and trims the text to
194 * 250 chars
195 *
196 * @author Andreas Gohr <andi@splitbrain.org>
197 */
198function cleanDesc($desc){
199  //start description at text of first paragraph
200  $matches = array();
201  if(preg_match('/<p>|<p\s.*?>/', $desc, $matches, PREG_OFFSET_CAPTURE))
202      $desc = substr($desc, $matches[0][1]);
203
204  //remove TOC
205  $desc = preg_replace('!<div class="toc">.*?(</div>\n</div>)!s','',$desc);
206  $desc = strip_tags($desc);
207  $desc = preg_replace('/[\n\r\t]/',' ',$desc);
208  $desc = preg_replace('/  /',' ',$desc);
209  $desc = utf8_substr($desc,0,250);
210  $desc = $desc.'...';
211  return $desc;
212}
213
214?>
215