xref: /dokuwiki/feed.php (revision 3a8a9050bc97ca54a26f0163fb9b8d50c82f8b3d)
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  if($mode == 'list'){
57    rssListNamespace($rss,$ns);
58  }else{
59    rssRecentChanges($rss,$num);
60  }
61
62  header('Content-Type: application/xml; charset='.$lang['encoding']);
63  print $rss->createFeed($type,$lang['encoding']);
64
65// ---------------------------------------------------------------- //
66
67/**
68 * Add recent changed to a feed object
69 *
70 * @author Andreas Gohr <andi@splitbrain.org>
71 */
72function rssRecentChanges(&$rss,$num){
73  $recents = getRecents($num);
74  foreach(array_keys($recents) as $id){
75    $desc = cleanDesc(parsedWiki($id));
76    if(!empty($recents[$id]['sum'])){
77      $desc = '['.strip_tags($recents[$id]['sum']).'] '.$desc;
78    }
79    $item = new FeedItem();
80    $item->title       = $id;
81    $item->link        = wl($id,'rev='.$recents[$id]['date'],true);
82    $item->description = $desc;
83    $item->date        = date('r',$recents[$id]['date']);
84    if(strpos($id,':')!==false){
85      $item->category    = substr($id,0,strrpos($id,':'));
86    }
87    if($recents[$id]['user']){
88      $item->author = $recents[$id]['user'].'@';
89    }else{
90      $item->author = 'anonymous@';
91    }
92    $item->author  .= $recents[$id]['ip'];
93
94    $rss->addItem($item);
95  }
96}
97
98/**
99 * Add all pages of a namespace to a feedobject
100 *
101 * @author Andreas Gohr <andi@splitbrain.org>
102 */
103function rssListNamespace(&$rss,$ns){
104  require_once("inc/search.php");
105  global $conf;
106
107  $ns=':'.cleanID($ns);
108  $ns=str_replace(':','/',$ns);
109
110  $data = array();
111  sort($data);
112  search($data,$conf['datadir'],'search_list','',$ns);
113  foreach($data as $row){
114    $id = $row['id'];
115    $date = filemtime(wikiFN($id));
116    $desc = cleanDesc(parsedWiki($id));
117    $item = new FeedItem();
118    $item->title       = $id;
119    $item->link        = wl($id,'rev='.$date,true);
120    $item->description = $desc;
121    $item->date        = date('r',$date);
122    $rss->addItem($item);
123  }
124}
125
126/**
127 * Clean description for feed inclusion
128 *
129 * Removes HTML tags and line breaks and trims the text to
130 * 250 chars
131 *
132 * @author Andreas Gohr <andi@splitbrain.org>
133 */
134function cleanDesc($desc){
135  //remove TOC
136  $desc = strip_tags($desc);
137  $desc = preg_replace('/[\n\r\t]/',' ',$desc);
138  $desc = preg_replace('/  /',' ',$desc);
139  $desc = substr($desc,0,250);
140  $desc = $desc.'...';
141  return $desc;
142}
143
144?>
145