From 8716966d3ce0882e5a02fae07bcdeb2c3f698eeb Mon Sep 17 00:00:00 2001
From: Andreas Gohr <andi@splitbrain.org>
Date: Sun, 28 May 2006 15:35:52 +0200
Subject: [PATCH] Use meta data in RSS feed

darcs-hash:20060528133552-7ad00-8281720582dbc448534f35cc978e9c8175b54f0d.gz
---
 feed.php            | 219 +++++++++++++++++++-------------------------
 inc/parserutils.php |   3 +-
 2 files changed, 96 insertions(+), 126 deletions(-)

diff --git a/feed.php b/feed.php
index 738bd595f..e38235eb9 100644
--- a/feed.php
+++ b/feed.php
@@ -63,6 +63,7 @@
   $cmod = @filemtime($cache); // 0 if not exists
   if($cmod && (($cmod+$conf['rss_update']>time()) || ($cmod>@filemtime($conf['changelog'])))){
     http_conditionalRequest($cmod);
+    if($conf['allowdebug']) header("X-CacheUsed: $cache");
     print io_readFile($cache);
     exit;
   } else {
@@ -99,84 +100,77 @@
 // ---------------------------------------------------------------- //
 
 /**
- * Add recent changed to a feed object
+ * Add recent changed pages 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']);
+    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);
+
+    foreach($recents as $recent){
+        $item = new FeedItem();
+        $meta = p_get_metadata($recent['id']);
+
+        if($conf['useheading'] && $meta['title']){
+            $item->title = $meta['title'];
+        }else{
+            $item->title = $recent['id'];
+        }
+        if(!empty($recent['sum'])){
+            $item->title .= ' - '.strip_tags($recent['sum']);
+        }
+
+        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 = $meta['description']['abstract'];
+        $item->date        = date('r',$recent['date']);
+        $cat = getNS($recent['id']);
+        if($cat) $item->category = $cat;
+
+        // FIXME should the user be pulled from metadata as well?
+        $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);
     }
-
-    $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);
-  }
 }
 
 /**
@@ -185,59 +179,34 @@ function rssRecentChanges(&$rss,$num,$ltype,$ns,$minor){
  * @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);
+    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));
+        $meta = p_get_metadata($id);
+
+        if($conf['useheading'] && $meta['title']){
+            $item->title = $meta['title'];
+        }else{
+            $item->title = $id;
+        }
+
+        $item->link        = wl($id,'rev='.$date,true);
+        $item->description = $meta['description']['abstract']];
+        $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;
-}
-
+//Setup VIM: ex: et ts=4 enc=utf-8 :
 ?>
diff --git a/inc/parserutils.php b/inc/parserutils.php
index 93c2b3604..f2fe0ea48 100644
--- a/inc/parserutils.php
+++ b/inc/parserutils.php
@@ -59,7 +59,8 @@ function p_wiki_xhtml($id, $rev='', $excuse=true){
  *
  * @param string wiki page id
  * @param reference populated with page title from heading or page id
- * @author Andreas Gohr <hfuecks@gmail.com>
+ * @deprecated
+ * @author Harry Fuecks <hfuecks@gmail.com>
  */
 function p_wiki_xhtml_summary($id, &$title, $rev='', $excuse=true){
   $file = wikiFN($id,$rev);
-- 
GitLab