xref: /dokuwiki/feed.php (revision e09875dbfe0a4db06ec7573d954b8403e916bba9)
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.'/feed.css';
56
57  $image = new FeedImage();
58  $image->title = $conf['title'];
59  $image->url = DOKU_URL."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  $recents = getRecents(0,$num);
81  foreach(array_keys($recents) as $id){
82    $desc = cleanDesc(p_wiki_xhtml($id,'',false));
83    $item = new FeedItem();
84    $item->title       = $id;
85    if(!empty($recents[$id]['sum'])){
86      $item->title .= ' - '.strip_tags($recents[$id]['sum']);
87    }
88
89		switch ($ltype){
90			case 'page':
91    		$item->link = wl($id,'rev='.$recents[$id]['date'],true);
92				break;
93			case 'rev':
94				$item->link = wl($id,'do=revisions&amp;rev='.$recents[$id]['date'],true);
95				break;
96			default:
97				$item->link = wl($id,'do=diff&amp;'.$recents[$id]['date'],true);
98		}
99
100    $item->description = $desc;
101    $item->date        = date('r',$recents[$id]['date']);
102    if(strpos($id,':')!==false){
103      $item->category    = substr($id,0,strrpos($id,':'));
104    }
105    if($recents[$id]['user']){
106      $item->author = $recents[$id]['user'].'@';
107    }else{
108      $item->author = 'anonymous@';
109    }
110    $item->author  .= $recents[$id]['ip'];
111    $rss->addItem($item);
112
113    //this can take some time if a lot of recaching has to be done
114    @set_time_limit(30); //reset execution time
115  }
116}
117
118/**
119 * Add all pages of a namespace to a feedobject
120 *
121 * @author Andreas Gohr <andi@splitbrain.org>
122 */
123function rssListNamespace(&$rss,$ns){
124  require_once("inc/search.php");
125  global $conf;
126
127  $ns=':'.cleanID($ns);
128  $ns=str_replace(':','/',$ns);
129
130  $data = array();
131  sort($data);
132  search($data,$conf['datadir'],'search_list','',$ns);
133  foreach($data as $row){
134    $id = $row['id'];
135    $date = filemtime(wikiFN($id));
136    $desc = cleanDesc(p_wiki_xhtml($id,'',false));
137    $item = new FeedItem();
138    $item->title       = $id;
139    $item->link        = wl($id,'rev='.$date,true);
140    $item->description = $desc;
141    $item->date        = date('r',$date);
142    $rss->addItem($item);
143  }
144}
145
146/**
147 * Clean description for feed inclusion
148 *
149 * Removes HTML tags and line breaks and trims the text to
150 * 250 chars
151 *
152 * @author Andreas Gohr <andi@splitbrain.org>
153 */
154function cleanDesc($desc){
155  //remove TOC
156  $desc = preg_replace('!<div class="toc">.*?(</div>\n</div>)!s','',$desc);
157  $desc = strip_tags($desc);
158  $desc = preg_replace('/[\n\r\t]/',' ',$desc);
159  $desc = preg_replace('/  /',' ',$desc);
160  $desc = utf8_substr($desc,0,250);
161  $desc = $desc.'...';
162  return $desc;
163}
164
165?>
166