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