xref: /dokuwiki/feed.php (revision 31f1284d8344c4ed31c2fbb0f337ecb1c29f9042)
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
95  $recents = getRecents(0,$num,false,$ns);
96
97  //this can take some time if a lot of recaching has to be done
98  @set_time_limit(90); // set max execution time
99
100  foreach(array_keys($recents) as $id){
101
102    $item = new FeedItem();
103    $item->title = $id;
104    $xhtml = p_wiki_xhtml($id,'',false);
105
106    if($conf['useheading']) {
107        $matches = array();
108        if(preg_match('|<h([1-9])>(.*?)</h\1>|', $xhtml, $matches))
109            $item->title = trim($matches[2]);
110    }
111    if(!empty($recents[$id]['sum'])){
112      $item->title .= ' - '.strip_tags($recents[$id]['sum']);
113    }
114
115    $desc = cleanDesc($xhtml);
116
117    if(empty($ltype))
118      $ltype = $conf['rss_linkto'];
119
120    switch ($ltype){
121      case 'page':
122        $item->link = wl($id,'rev='.$recents[$id]['date'],true);
123        break;
124      case 'rev':
125        $item->link = wl($id,'do=revisions&amp;rev='.$recents[$id]['date'],true);
126        break;
127      case 'current':
128        $item->link = wl($id, '', true);
129        break;
130      case 'diff':
131      default:
132        $item->link = wl($id,'do=diff&amp;'.$recents[$id]['date'],true);
133    }
134
135    $item->description = $desc;
136    $item->date        = date('r',$recents[$id]['date']);
137    if(strpos($id,':')!==false){
138      $item->category    = substr($id,0,strrpos($id,':'));
139    }
140    if($recents[$id]['user']){
141      $item->author = $recents[$id]['user'].'@';
142    }else{
143      $item->author = 'anonymous@';
144    }
145    $item->author  .= $recents[$id]['ip'];
146    $rss->addItem($item);
147  }
148}
149
150/**
151 * Add all pages of a namespace to a feedobject
152 *
153 * @author Andreas Gohr <andi@splitbrain.org>
154 */
155function rssListNamespace(&$rss,$ns){
156  require_once(DOKU_INC.'inc/search.php');
157  global $conf;
158
159  $ns=':'.cleanID($ns);
160  $ns=str_replace(':','/',$ns);
161
162  $data = array();
163  sort($data);
164  search($data,$conf['datadir'],'search_list','',$ns);
165  foreach($data as $row){
166    $id = $row['id'];
167    $date = filemtime(wikiFN($id));
168    $desc = cleanDesc(p_wiki_xhtml($id,'',false));
169    $item = new FeedItem();
170    $item->title       = $id;
171    $item->link        = wl($id,'rev='.$date,true);
172    $item->description = $desc;
173    $item->date        = date('r',$date);
174    $rss->addItem($item);
175  }
176}
177
178/**
179 * Clean description for feed inclusion
180 *
181 * Removes HTML tags and line breaks and trims the text to
182 * 250 chars
183 *
184 * @author Andreas Gohr <andi@splitbrain.org>
185 */
186function cleanDesc($desc){
187  //start description at text of first paragraph
188  $matches = array();
189  if(preg_match('/<p>|<p\s.*?>/', $desc, $matches, PREG_OFFSET_CAPTURE))
190      $desc = substr($desc, $matches[0][1]);
191
192  //remove TOC
193  $desc = preg_replace('!<div class="toc">.*?(</div>\n</div>)!s','',$desc);
194  $desc = strip_tags($desc);
195  $desc = preg_replace('/[\n\r\t]/',' ',$desc);
196  $desc = preg_replace('/  /',' ',$desc);
197  $desc = utf8_substr($desc,0,250);
198  $desc = $desc.'...';
199  return $desc;
200}
201
202?>
203