xref: /dokuwiki/feed.php (revision 241f3a361bd5977e2271eb2b94fd6c8300639d74)
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  //set auth header for login FIXME: is this used anymore???
17  if($_REQUEST['login'] && !isset($_SERVER['PHP_AUTH_USER'])){
18    header('WWW-Authenticate: Basic realm="'.$conf['title'].'"');
19    header('HTTP/1.0 401 Unauthorized');
20    auth_logoff();
21  }
22
23  //close sesseion
24  session_write_close();
25
26
27  $num   = $_REQUEST['num'];
28  $type  = $_REQUEST['type'];
29  $mode  = $_REQUEST['mode'];
30  $ns    = $_REQUEST['ns'];
31  $ltype = $_REQUEST['linkto'];
32
33  switch ($type){
34    case 'rss':
35       $type = 'RSS0.9';
36       break;
37    case 'rss2':
38       $type = 'RSS2.0';
39       break;
40    case 'atom':
41       $type = 'ATOM0.3';
42       break;
43    default:
44       $type = 'RSS1.0';
45  }
46
47  //some defaults for the feed
48  $CACHEGROUP = 'feed';
49  $conf['typography'] = false;
50  $conf['canonical']  = true;
51  $parser['toc']      = false;
52
53#  $rss = new UniversalFeedCreator();
54  $rss = new DokuWikiFeedCreator();
55  $rss->title = $conf['title'];
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);
70  }
71
72  header('Content-Type: application/xml; charset=utf-8');
73  print $rss->createFeed($type,'utf-8');
74
75// ---------------------------------------------------------------- //
76
77/**
78 * Add recent changed to a feed object
79 *
80 * @author Andreas Gohr <andi@splitbrain.org>
81 */
82function rssRecentChanges(&$rss,$num,$ltype){
83  global $conf;
84	if(!$num) $num = $conf['recent'];
85
86  $recents = getRecents(0,$num);
87
88  //this can take some time if a lot of recaching has to be done
89  @set_time_limit(90); // set max execution time
90
91  foreach(array_keys($recents) as $id){
92    $desc = cleanDesc(p_wiki_xhtml($id,'',false));
93    $item = new FeedItem();
94    $item->title       = $id;
95    if(!empty($recents[$id]['sum'])){
96      $item->title .= ' - '.strip_tags($recents[$id]['sum']);
97    }
98
99		switch ($ltype){
100			case 'page':
101    		$item->link = wl($id,'rev='.$recents[$id]['date'],true);
102				break;
103			case 'rev':
104				$item->link = wl($id,'do=revisions&amp;rev='.$recents[$id]['date'],true);
105				break;
106			default:
107				$item->link = wl($id,'do=diff&amp;'.$recents[$id]['date'],true);
108		}
109
110    $item->description = $desc;
111    $item->date        = date('r',$recents[$id]['date']);
112    if(strpos($id,':')!==false){
113      $item->category    = substr($id,0,strrpos($id,':'));
114    }
115    if($recents[$id]['user']){
116      $item->author = $recents[$id]['user'].'@';
117    }else{
118      $item->author = 'anonymous@';
119    }
120    $item->author  .= $recents[$id]['ip'];
121    $rss->addItem($item);
122  }
123}
124
125/**
126 * Add all pages of a namespace to a feedobject
127 *
128 * @author Andreas Gohr <andi@splitbrain.org>
129 */
130function rssListNamespace(&$rss,$ns){
131  require_once(DOKU_INC.'inc/search.php');
132  global $conf;
133
134  $ns=':'.cleanID($ns);
135  $ns=str_replace(':','/',$ns);
136
137  $data = array();
138  sort($data);
139  search($data,$conf['datadir'],'search_list','',$ns);
140  foreach($data as $row){
141    $id = $row['id'];
142    $date = filemtime(wikiFN($id));
143    $desc = cleanDesc(p_wiki_xhtml($id,'',false));
144    $item = new FeedItem();
145    $item->title       = $id;
146    $item->link        = wl($id,'rev='.$date,true);
147    $item->description = $desc;
148    $item->date        = date('r',$date);
149    $rss->addItem($item);
150  }
151}
152
153/**
154 * Clean description for feed inclusion
155 *
156 * Removes HTML tags and line breaks and trims the text to
157 * 250 chars
158 *
159 * @author Andreas Gohr <andi@splitbrain.org>
160 */
161function cleanDesc($desc){
162  //remove TOC
163  $desc = preg_replace('!<div class="toc">.*?(</div>\n</div>)!s','',$desc);
164  $desc = strip_tags($desc);
165  $desc = preg_replace('/[\n\r\t]/',' ',$desc);
166  $desc = preg_replace('/  /',' ',$desc);
167  $desc = utf8_substr($desc,0,250);
168  $desc = $desc.'...';
169  return $desc;
170}
171
172?>
173