summaryrefslogtreecommitdiff
path: root/feed.php
blob: 738bd595fd8561adb38491d4cc0df043456722f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?php
/**
 * XML feed export
 *
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Andreas Gohr <andi@splitbrain.org>
 */

  if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__)).'/');
  require_once(DOKU_INC.'inc/init.php');
  require_once(DOKU_INC.'inc/common.php');
  require_once(DOKU_INC.'inc/events.php');
  require_once(DOKU_INC.'inc/parserutils.php');
  require_once(DOKU_INC.'inc/feedcreator.class.php');
  require_once(DOKU_INC.'inc/auth.php');
  require_once(DOKU_INC.'inc/pageutils.php');

  //close session
  session_write_close();


  $num   = $_REQUEST['num'];
  $type  = $_REQUEST['type'];
  $mode  = $_REQUEST['mode'];
  $minor = $_REQUEST['minor'];
  $ns    = $_REQUEST['ns'];
  $ltype = $_REQUEST['linkto'];

  if($type == '')
    $type = $conf['rss_type'];

  switch ($type){
    case 'rss':
       $type = 'RSS0.91';
       $mime = 'text/xml';
       break;
    case 'rss2':
       $type = 'RSS2.0';
       $mime = 'text/xml';
       break;
    case 'atom':
       $type = 'ATOM0.3';
       $mime = 'application/xml';
       break;
    case 'atom1':
       $type = 'ATOM1.0';
       $mime = 'application/atom+xml';
       break;
    default:
       $type = 'RSS1.0';
       $mime = 'application/xml';
  }

  // the feed is dynamic - we need a cache for each combo
  // (but most people just use the default feed so it's still effective)
  $cache = getCacheName($num.$type.$mode.$ns.$ltype.$_SERVER['REMOTE_USER'],'.feed');

  // check cacheage and deliver if nothing has changed since last
  // time or the update interval has not passed, also handles conditional requests
  header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  header('Pragma: public');
  header('Content-Type: application/xml; charset=utf-8');
  $cmod = @filemtime($cache); // 0 if not exists
  if($cmod && (($cmod+$conf['rss_update']>time()) || ($cmod>@filemtime($conf['changelog'])))){
    http_conditionalRequest($cmod);
    print io_readFile($cache);
    exit;
  } else {
    http_conditionalRequest(time());
  }

  // create new feed
  $rss = new DokuWikiFeedCreator();
  $rss->title = $conf['title'].(($ns) ? ' '.$ns : '');
  $rss->link  = DOKU_URL;
  $rss->syndicationURL = DOKU_URL.'feed.php';
  $rss->cssStyleSheet  = DOKU_URL.'lib/styles/feed.css';

  $image = new FeedImage();
  $image->title = $conf['title'];
  $image->url = DOKU_URL."lib/images/favicon.ico";
  $image->link = DOKU_URL;
  $rss->image = $image;

  if($mode == 'list'){
    rssListNamespace($rss,$ns);
  }else{
    rssRecentChanges($rss,$num,$ltype,$ns,$minor);
  }

  $feed = $rss->createFeed($type,'utf-8');

  // save cachefile
  io_saveFile($cache,$feed);

  // finally deliver
  print $feed;

// ---------------------------------------------------------------- //

/**
 * Add recent changed to a feed object
 *
 * @author Andreas Gohr <andi@splitbrain.org>
 */
function rssRecentChanges(&$rss,$num,$ltype,$ns,$minor){
  global $conf;
  global $auth;

  if(!$num) $num = $conf['recent'];
  $guardmail = ($conf['mailguard'] != '' && $conf['mailguard'] != 'none');


  $flags = RECENTS_SKIP_DELETED;
  if(!$minor) $flags += RECENTS_SKIP_MINORS;

  $recents = getRecents(0,$num,$ns,$flags);

  //this can take some time if a lot of recaching has to be done
  @set_time_limit(90); // set max execution time

  foreach($recents as $recent){

    $item = new FeedItem();
    $item->title = $recent['id'];
    $xhtml = p_wiki_xhtml($recent['id'],'',false);

    if($conf['useheading']) {
        $matches = array();
        if(preg_match('|<h([1-9])>(.*?)</h\1>|', $xhtml, $matches))
            $item->title = trim($matches[2]);
    }
    if(!empty($recent['sum'])){
      $item->title .= ' - '.strip_tags($recent['sum']);
    }

    $desc = cleanDesc($xhtml);

    if(empty($ltype))
      $ltype = $conf['rss_linkto'];

    switch ($ltype){
      case 'page':
        $item->link = wl($recent['id'],'rev='.$recent['date'],true);
        break;
      case 'rev':
        $item->link = wl($recent['id'],'do=revisions&rev='.$recent['date'],true);
        break;
      case 'current':
        $item->link = wl($recent['id'], '', true);
        break;
      case 'diff':
      default:
        $item->link = wl($recent['id'],'rev='.$recent['date'].'&do=diff'.$recent['date'],true);
    }

    $item->description = $desc;
    $item->date        = date('r',$recent['date']);
    $cat = getNS($recent['id']);
    if($cat) $item->category = $cat;

    $user = null;
    $user = @$recent['user']; // the @ spares time repeating lookup
    $item->author = '';

    if($user){
      $userInfo = $auth->getUserData($user);
      $item->author = $userInfo['name'];
      if($guardmail) {
        //cannot obfuscate because some RSS readers may check validity
        $item->authorEmail = $user.'@'.$recent['ip'];
      }else{
        $item->authorEmail = $userInfo['mail'];
      }
    }else{
      $item->authorEmail = 'anonymous@'.$recent['ip'];
    }
    $rss->addItem($item);
  }
}

/**
 * Add all pages of a namespace to a feedobject
 *
 * @author Andreas Gohr <andi@splitbrain.org>
 */
function rssListNamespace(&$rss,$ns){
  require_once(DOKU_INC.'inc/search.php');
  global $conf;

  $ns=':'.cleanID($ns);
  $ns=str_replace(':','/',$ns);

  $data = array();
  sort($data);
  search($data,$conf['datadir'],'search_list','',$ns);
  foreach($data as $row){
    $item = new FeedItem();

    $id    = $row['id'];
    $date  = filemtime(wikiFN($id));
    $xhtml = p_wiki_xhtml($id,'',false);
    $desc  = cleanDesc($xhtml);
    $item->title       = $id;

    if($conf['useheading']) {
        $matches = array();
        if(preg_match('|<h([1-9])>(.*?)</h\1>|', $xhtml, $matches))
            $item->title = trim($matches[2]);
    }

    $item->link        = wl($id,'rev='.$date,true);
    $item->description = $desc;
    $item->date        = date('r',$date);
    $rss->addItem($item);
  }
}

/**
 * Clean description for feed inclusion
 *
 * Removes HTML tags and line breaks and trims the text to
 * 250 chars
 *
 * @author Andreas Gohr <andi@splitbrain.org>
 */
function cleanDesc($desc){
  //start description at text of first paragraph
  $matches = array();
  if(preg_match('/<p>|<p\s.*?>/', $desc, $matches, PREG_OFFSET_CAPTURE))
      $desc = substr($desc, $matches[0][1]);

  //remove TOC
  $desc = preg_replace('!<div class="toc">.*?(</div>\n</div>)!s','',$desc);
  $desc = strip_tags($desc);
  $desc = preg_replace('/[\n\r\t]/',' ',$desc);
  $desc = preg_replace('/  /',' ',$desc);
  $desc = utf8_substr($desc,0,250);
  $desc = $desc.'...';
  return $desc;
}

?>