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