xref: /dokuwiki/feed.php (revision 0cecf9d507451346a32ddf45a85b425784fbb0f8)
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("inc/common.php");
12  require_once("inc/parser.php");
13  require_once("inc/feedcreator.class.php");
14  require_once("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
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  //some defaults for the feed
44  $CACHEGROUP = 'feed';
45  $conf['typography'] = false;
46  $conf['canonical']  = true;
47  $parser['toc']      = false;
48
49#  $rss = new UniversalFeedCreator();
50  $rss = new DokuWikiFeedCreator();
51  $rss->title = $conf['title'];
52  $rss->link  = DOKU_URL;
53  $rss->syndicationURL = DOKU_URL.'/feed.php';
54  $rss->cssStyleSheet  = DOKU_URL.'/feed.css';
55
56  $image = new FeedImage();
57  $image->title = $conf['title'];
58  $image->url = DOKU_URL."images/favicon.ico";
59  $image->link = DOKU_URL;
60  $rss->image = $image;
61
62  if($mode == 'list'){
63    rssListNamespace($rss,$ns);
64  }else{
65    rssRecentChanges($rss,$num);
66  }
67
68  header('Content-Type: application/xml; charset='.$lang['encoding']);
69  print $rss->createFeed($type,$lang['encoding']);
70
71// ---------------------------------------------------------------- //
72
73/**
74 * Add recent changed to a feed object
75 *
76 * @author Andreas Gohr <andi@splitbrain.org>
77 */
78function rssRecentChanges(&$rss,$num){
79  $recents = getRecents($num);
80  foreach(array_keys($recents) as $id){
81    $desc = cleanDesc(parsedWiki($id));
82    $item = new FeedItem();
83    $item->title       = $id;
84    if(!empty($recents[$id]['sum'])){
85      $item->title .= ': '.strip_tags($recents[$id]['sum']);
86    }
87    $item->link        = wl($id,'rev='.$recents[$id]['date'],true);
88    $item->description = $desc;
89    $item->date        = date('r',$recents[$id]['date']);
90    if(strpos($id,':')!==false){
91      $item->category    = substr($id,0,strrpos($id,':'));
92    }
93    if($recents[$id]['user']){
94      $item->author = $recents[$id]['user'].'@';
95    }else{
96      $item->author = 'anonymous@';
97    }
98    $item->author  .= $recents[$id]['ip'];
99    $rss->addItem($item);
100
101    //this can take some time if a lot of recaching has to be done
102    @set_time_limit(30); //reset execution time
103  }
104}
105
106/**
107 * Add all pages of a namespace to a feedobject
108 *
109 * @author Andreas Gohr <andi@splitbrain.org>
110 */
111function rssListNamespace(&$rss,$ns){
112  require_once("inc/search.php");
113  global $conf;
114
115  $ns=':'.cleanID($ns);
116  $ns=str_replace(':','/',$ns);
117
118  $data = array();
119  sort($data);
120  search($data,$conf['datadir'],'search_list','',$ns);
121  foreach($data as $row){
122    $id = $row['id'];
123    $date = filemtime(wikiFN($id));
124    $desc = cleanDesc(parsedWiki($id));
125    $item = new FeedItem();
126    $item->title       = $id;
127    $item->link        = wl($id,'rev='.$date,true);
128    $item->description = $desc;
129    $item->date        = date('r',$date);
130    $rss->addItem($item);
131  }
132}
133
134/**
135 * Clean description for feed inclusion
136 *
137 * Removes HTML tags and line breaks and trims the text to
138 * 250 chars
139 *
140 * @author Andreas Gohr <andi@splitbrain.org>
141 */
142function cleanDesc($desc){
143  //remove TOC
144  $desc = strip_tags($desc);
145  $desc = preg_replace('/[\n\r\t]/',' ',$desc);
146  $desc = preg_replace('/  /',' ',$desc);
147  $desc = substr($desc,0,250);
148  $desc = $desc.'...';
149  return $desc;
150}
151
152?>
153