From e858c368ff83f2ada069608cde22f4f1dec22eb5 Mon Sep 17 00:00:00 2001 From: Andrew Davis Date: Wed, 28 Jul 2010 02:32:11 +0000 Subject: [PATCH] rss MDL-23473 made blog rss feeds work --- blog/index.php | 15 +++++ blog/locallib.php | 9 --- blog/rsslib.php | 145 ++++++++++++++++++++++++++++++++-------------- lib/rsslib.php | 28 ++++++--- message/lib.php | 4 -- 5 files changed, 137 insertions(+), 64 deletions(-) diff --git a/blog/index.php b/blog/index.php index ae1c2534295..c78e666e826 100755 --- a/blog/index.php +++ b/blog/index.php @@ -190,6 +190,21 @@ if (empty($entryid) && empty($modid) && empty($groupid)) { $PAGE->set_context(get_context_instance(CONTEXT_USER, $USER->id)); } +if ($CFG->enablerssfeeds) { + $rsscontext = $filtertype = $thingid = null; + list($thingid, $rsscontext, $filtertype) = blog_rss_get_params($blogheaders['filters']); + + $rsstitle = $blogheaders['heading']; + + //check we haven't started output by outputting an error message + if ($PAGE->state == moodle_page::STATE_BEFORE_HEADER) { + blog_rss_add_http_header($rsscontext, $rsstitle, $filtertype, $thingid, $tagid); + } + + //this works but there isn't a great place to put the link + //blog_rss_print_link($rsscontext, $filtertype, $thingid, $tagid); +} + echo $OUTPUT->header(); echo $OUTPUT->heading($blogheaders['heading'], 2); diff --git a/blog/locallib.php b/blog/locallib.php index d0673477994..99aa225f31f 100644 --- a/blog/locallib.php +++ b/blog/locallib.php @@ -816,15 +816,6 @@ class blog_listing { echo $OUTPUT->render($pagingbar); - if ($CFG->enablerssfeeds) { - //todo reimplement blog rss feeds - - //blog_rss_print_link($filtertype, $filterselect, $tag); - - //require_once("$CFG->libdir/rsslib.php"); - //rss_add_http_header($sitecontext, 'blog', $forum, $rsstitle); - } - if (has_capability('moodle/blog:create', $sitecontext)) { //the user's blog is enabled and they are viewing their own blog $userid = optional_param('userid', null, PARAM_INT); diff --git a/blog/rsslib.php b/blog/rsslib.php index efd8652aec4..6e4f4f00ac2 100755 --- a/blog/rsslib.php +++ b/blog/rsslib.php @@ -3,10 +3,35 @@ require_once($CFG->dirroot.'/lib/rsslib.php'); require_once($CFG->dirroot .'/blog/lib.php'); +function blog_rss_get_url($contextid, $userid, $filtertype, $filterselect=0, $tagid=0) { + $componentname = 'blog'; + + $additionalargs = null; + switch ($filtertype) { + case 'site': + $additionalargs = 'site/'.SITEID; + break; + case 'course': + $additionalargs = 'course/'.$filterselect; + break; + case 'group': + $additionalargs = 'group/'.$filterselect; + break; + case 'user': + $additionalargs = 'user/'.$filterselect; + break; + } + + if ($tagid) { + $additionalargs .= '/'.$tagid; + } + + return rss_get_url($contextid, $userid, $componentname, $additionalargs); +} + // This function returns the icon (from theme) with the link to rss/file.php // needs some hacking to rss/file.php -function blog_rss_print_link($filtertype, $filterselect, $tagid=0, $tooltiptext='') { - +function blog_rss_print_link($context, $filtertype, $filterselect=0, $tagid=0, $tooltiptext='') { global $CFG, $USER, $OUTPUT; if (!isloggedin()) { @@ -15,32 +40,61 @@ function blog_rss_print_link($filtertype, $filterselect, $tagid=0, $tooltiptext= $userid = $USER->id; } - switch ($filtertype) { - case 'site': - $path = SITEID.'/'.$userid.'/blog/site/'.SITEID; - break; - case 'course': - $path = $filterselect.'/'.$userid.'/blog/course/'.$filterselect; - break; - case 'group': - $path = SITEID.'/'.$userid.'/blog/group/'.$filterselect; - break; - case 'user': - $path = SITEID.'/'.$userid.'/blog/user/'.$filterselect; - break; - } - - if ($tagid) { - $path .= '/'.$tagid; - } - - $path .= '/rss.xml'; + $url = blog_rss_get_url($context->id, $userid, $filtertype, $filterselect, $tagid); $rsspix = $OUTPUT->pix_url('i/rss'); + print '
'.get_string('rss').'
'; +} - require_once($CFG->libdir.'/filelib.php'); - $path = get_file_url($path, null, 'rssfile'); - print '
'.get_string('rss').'
'; +function blog_rss_add_http_header($context, $title, $filtertype, $filterselect=0, $tagid=0) { + global $PAGE, $USER, $CFG; + //$componentname = 'blog'; + //rss_add_http_header($context, $componentname, $filterselect, $title); + + if (!isloggedin()) { + $userid = $CFG->siteguest; + } else { + $userid = $USER->id; + } + + $rsspath = blog_rss_get_url($context->id, $userid, $filtertype, $filterselect, $tagid); + $PAGE->add_alternate_version($title, $rsspath, 'application/rss+xml'); +} + +/** + * Utility function to extract parameters needed to generate RSS URLs from the blog filters + * @param $filters + * @return array array containing the id of the user/course/group, the relevant context and the filter type (site/user/course/group) + */ +function blog_rss_get_params($filters) { + $thingid = $rsscontext = $filtertype = null; + + if (!$filters) { + $thingid = SITEID; + $rsscontext = $sitecontext; + $filtertype = 'site'; + } else if (array_key_exists('course', $filters)) { + $thingid = $filters['course']; + + $coursecontext = get_context_instance(CONTEXT_COURSE, $thingid); + $rsscontext = $coursecontext; + + $filtertype = 'course'; + } else if (array_key_exists('user', $filters)) { + $thingid = $filters['user']; + + $usercontext = get_context_instance(CONTEXT_USER, $thingid); + $rsscontext = $usercontext; + + $filtertype = 'user'; + } else if (array_key_exists('group', $filters)) { + $thingid = $filters['group']; + + $rsscontext = $sitecontext; //is this the context we should be using for group blogs? + $filtertype = 'group'; + } + + return array($thingid, $rsscontext, $filtertype); } @@ -72,15 +126,18 @@ function blog_rss_get_feed($context, $args) { if (file_exists($filename)) { if (filemtime($filename) + 3600 > time()) { - return $filename; /// It's already done so we return cached version + return $filename; // It's already done so we return cached version } } -/// Get all the entries from the database + // Get all the entries from the database + //$blogentries = blog_fetch_entries('', 20, '', $type, $id, $tagid); + require_once($CFG->dirroot .'/blog/locallib.php'); + $blogheaders = blog_get_headers(); + $bloglisting = new blog_listing($blogheaders['filters']); + $blogentries = $bloglisting->get_entries(); - $blogentries = blog_fetch_entries('', 20, '', $type, $id, $tagid); - -/// Now generate an array of RSS items + // Now generate an array of RSS items if ($blogentries) { $items = array(); foreach ($blogentries as $blog_entry) { @@ -134,11 +191,8 @@ function blog_rss_get_feed($context, $args) { $footer = rss_standard_footer(); - -/// Save the XML contents to file. - + // Save the XML contents to file. $rssdata = $header.$articles.$footer; - if (blog_rss_save_file($type,$id,$tagid,$rssdata)) { return $filename; } else { @@ -160,18 +214,23 @@ function blog_rss_file_name($type, $id, $tagid=0) { //This function saves to file the rss feed specified in the parameters function blog_rss_save_file($type, $id, $tagid=0, $contents='') { global $CFG; + + $status = true; - if (! $basedir = make_upload_directory("rss/blog/$type/$id")) { - return false; + //blog creates some additional dirs within the rss cache so make sure they all exist + if (! $basedir = make_upload_directory ('cache/rss/blog')) { + //Cannot be created, so error + $status = false; + } + if (! $basedir = make_upload_directory ('cache/rss/blog/'.$type)) { + //Cannot be created, so error + $status = false; } - $file = blog_rss_file_name($type, $id, $tagid); - $rss_file = fopen($file, 'w'); - if ($rss_file) { - $status = fwrite ($rss_file, $contents); - fclose($rss_file); - } else { - $status = false; + if ($status) { + $filename = blog_rss_file_name($type, $id, $tagid); + $expandfilename = false; //we're supplying a full file path + $status = rss_save_file('blog', $filename, $contents, $expandfilename); } return $status; diff --git a/lib/rsslib.php b/lib/rsslib.php index ce18a88bc6a..02c64d33fda 100644 --- a/lib/rsslib.php +++ b/lib/rsslib.php @@ -28,7 +28,15 @@ defined('MOODLE_INTERNAL') || die(); function rss_add_http_header($context, $componentname, $componentinstance, $title) { global $PAGE, $USER; - $rsspath = rss_get_url($context->id, $USER->id, $componentname, $componentinstance->id); + + $componentid = null; + if (is_object($componentinstance)) { + $componentid = $componentinstance->id; + } else { + $componentid = $componentinstance; + } + + $rsspath = rss_get_url($context->id, $USER->id, $componentname, $componentid); $PAGE->add_alternate_version($title, $rsspath, 'application/rss+xml'); } @@ -56,13 +64,13 @@ function rss_get_link($contextid, $userid, $componentname, $id, $tooltiptext='') * @param int contextid the course id * @param int userid the current user id * @param string modulename the name of the current module. For example "forum" - * @param int id For modules, module instance id + * @param string $additionalargs For modules, module instance id */ -function rss_get_url($contextid, $userid, $componentname, $id) { +function rss_get_url($contextid, $userid, $componentname, $additionalargs) { global $CFG; require_once($CFG->libdir.'/filelib.php'); $usertoken = rss_get_token($userid); - return get_file_url($contextid.'/'.$usertoken.'/'.$componentname.'/'.$id.'/rss.xml', null, 'rssfile'); + return get_file_url($contextid.'/'.$usertoken.'/'.$componentname.'/'.$additionalargs.'/rss.xml', null, 'rssfile'); } /** @@ -130,9 +138,9 @@ function rss_enabled_for_mod($modname, $instance=null, $hasrsstype=true, $hasrss * @global object * @param string $componentname the module name ie forum. Used to create a cache directory. * @param string $filename the name of the file to be created ie "1234" - * @param string $result the data to be written to the file + * @param string $contents the data to be written to the file */ -function rss_save_file($componentname, $filename, $result) { +function rss_save_file($componentname, $filename, $contents, $expandfilename=true) { global $CFG; $status = true; @@ -143,10 +151,14 @@ function rss_save_file($componentname, $filename, $result) { } if ($status) { - $fullfilename = rss_get_file_full_name($componentname, $filename); + $fullfilename = $filename; + if ($expandfilename) { + $fullfilename = rss_get_file_full_name($componentname, $filename); + } + $rss_file = fopen($fullfilename, "w"); if ($rss_file) { - $status = fwrite ($rss_file, $result); + $status = fwrite ($rss_file, $contents); fclose($rss_file); } else { $status = false; diff --git a/message/lib.php b/message/lib.php index 5bdb5f25e4f..f850ebb9a0e 100644 --- a/message/lib.php +++ b/message/lib.php @@ -1029,10 +1029,6 @@ function message_history_link($userid1, $userid2, $returnstr=false, $keywords='' $strmessagehistory = get_string('messagehistory', 'message'); } -//todo andrew user1 is generally the current user - //if (!$userid2) { -// $userid2 = $USER->id; - //} if ($position) { $position = "#$position"; }