xref: /dokuwiki/feed.php (revision f8925855f0c0939a3e9c02f7bf41d465443d6cc6)
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		switch ($ltype){
115			case 'page':
116    		$item->link = wl($id,'rev='.$recents[$id]['date'],true);
117				break;
118			case 'rev':
119				$item->link = wl($id,'do=revisions&amp;rev='.$recents[$id]['date'],true);
120				break;
121			default:
122				$item->link = wl($id,'do=diff&amp;'.$recents[$id]['date'],true);
123		}
124
125    $item->description = $desc;
126    $item->date        = date('r',$recents[$id]['date']);
127    if(strpos($id,':')!==false){
128      $item->category    = substr($id,0,strrpos($id,':'));
129    }
130    if($recents[$id]['user']){
131      $item->author = $recents[$id]['user'].'@';
132    }else{
133      $item->author = 'anonymous@';
134    }
135    $item->author  .= $recents[$id]['ip'];
136    $rss->addItem($item);
137  }
138}
139
140/**
141 * Add all pages of a namespace to a feedobject
142 *
143 * @author Andreas Gohr <andi@splitbrain.org>
144 */
145function rssListNamespace(&$rss,$ns){
146  require_once(DOKU_INC.'inc/search.php');
147  global $conf;
148
149  $ns=':'.cleanID($ns);
150  $ns=str_replace(':','/',$ns);
151
152  $data = array();
153  sort($data);
154  search($data,$conf['datadir'],'search_list','',$ns);
155  foreach($data as $row){
156    $id = $row['id'];
157    $date = filemtime(wikiFN($id));
158    $desc = cleanDesc(p_wiki_xhtml($id,'',false));
159    $item = new FeedItem();
160    $item->title       = $id;
161    $item->link        = wl($id,'rev='.$date,true);
162    $item->description = $desc;
163    $item->date        = date('r',$date);
164    $rss->addItem($item);
165  }
166}
167
168/**
169 * Clean description for feed inclusion
170 *
171 * Removes HTML tags and line breaks and trims the text to
172 * 250 chars
173 *
174 * @author Andreas Gohr <andi@splitbrain.org>
175 */
176function cleanDesc($desc){
177  //start description at text of first paragraph
178  $matches = array();
179  if(preg_match('/<p>|<p\s.*?>/', $desc, $matches, PREG_OFFSET_CAPTURE))
180      $desc = substr($desc, $matches[0][1]);
181
182  //remove TOC
183  $desc = preg_replace('!<div class="toc">.*?(</div>\n</div>)!s','',$desc);
184  $desc = strip_tags($desc);
185  $desc = preg_replace('/[\n\r\t]/',' ',$desc);
186  $desc = preg_replace('/  /',' ',$desc);
187  $desc = utf8_substr($desc,0,250);
188  $desc = $desc.'...';
189  return $desc;
190}
191
192?>
193