rss MDL-23391 made lib/rsslib.php more generic

This commit is contained in:
Andrew Davis 2010-07-21 02:11:53 +00:00
parent e269084401
commit 43b92251be
9 changed files with 40 additions and 37 deletions

View File

@ -816,11 +816,14 @@ class blog_listing {
echo $OUTPUT->render($pagingbar);
/* TODO RSS link
if ($CFG->enablerssfeeds) {
$this->blog_rss_print_link($filtertype, $filterselect, $tag);
//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

View File

@ -23,9 +23,9 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
function rss_add_http_header($context, $modname, $modinstance, $title) {
function rss_add_http_header($context, $componentname, $componentinstance, $title) {
global $PAGE, $USER;
$rsspath = rss_get_url($context->id, $USER->id, $modname, $modinstance->id);
$rsspath = rss_get_url($context->id, $USER->id, $componentname, $componentinstance->id);
$PAGE->add_alternate_version($title, $rsspath, 'application/rss+xml');
}
@ -35,12 +35,12 @@
* @global object
* @global object
*/
function rss_get_link($contextid, $userid, $modulename, $id, $tooltiptext='') {
function rss_get_link($contextid, $userid, $componentname, $id, $tooltiptext='') {
global $OUTPUT;
static $rsspath = '';
$rsspath = rss_get_url($contextid, $userid, $modulename, $id);
$rsspath = rss_get_url($contextid, $userid, $componentname, $id);
$rsspix = $OUTPUT->pix_url('i/rss');
return '<a href="'. $rsspath .'"><img src="'. $rsspix .'" title="'. strip_tags($tooltiptext) .'" alt="'.get_string('rss').'" /></a>';
@ -55,18 +55,18 @@ function rss_get_link($contextid, $userid, $modulename, $id, $tooltiptext='') {
* @param string modulename the name of the current module. For example "forum"
* @param int id For modules, module instance id
*/
function rss_get_url($contextid, $userid, $modulename, $id) {
function rss_get_url($contextid, $userid, $componentname, $id) {
global $CFG;
require_once($CFG->libdir.'/filelib.php');
$usertoken = rss_get_token($userid);
return get_file_url($contextid.'/'.$usertoken.'/'.$modulename.'/'.$id.'/rss.xml', null, 'rssfile');
return get_file_url($contextid.'/'.$usertoken.'/'.$componentname.'/'.$id.'/rss.xml', null, 'rssfile');
}
/**
* This function prints the icon (from theme) with the link to rss/file.php
*/
function rss_print_link($contextid, $userid, $modulename, $id, $tooltiptext='') {
print rss_get_link($contextid, $userid, $modulename, $id, $tooltiptext);
function rss_print_link($contextid, $userid, $componentname, $id, $tooltiptext='') {
print rss_get_link($contextid, $userid, $componentname, $id, $tooltiptext);
}
@ -74,20 +74,20 @@ function rss_print_link($contextid, $userid, $modulename, $id, $tooltiptext='')
* Given an object, deletes all RSS files associated with it.
* Relies on a naming convention. See rss_get_filename()
*
* @param string $modname the name of the module ie 'forum'. Used to construct the cache path.
* @param string $componentname the name of the module ie 'forum'. Used to construct the cache path.
* @param object $instance An object with an id member variable ie $forum, $glossary.
* @return void
*/
function rss_delete_file($modname, $instance) {
function rss_delete_file($componentname, $instance) {
global $CFG;
$dirpath = "$CFG->dataroot/cache/rss/$modname";
$dirpath = "$CFG->dataroot/cache/rss/$componentname";
if (is_dir($dirpath)) {
$dh = opendir($dirpath);
while (false !== ($filename = readdir($dh))) {
if ($filename!='.' && $filename!='..') {
if (preg_match("/{$instance->id}_/", $filename)) {
unlink("$CFG->dataroot/cache/rss/$modname/$filename");
unlink("$dirpath/$filename");
}
}
}
@ -100,7 +100,7 @@ function rss_delete_file($modname, $instance) {
* @param boolean $hasrsstype Should there be a rsstype member variable?
* @param boolean $hasrssarticles Should there be a rssarticles member variable?
*/
function rss_enabled($modname, $instance, $hasrsstype=true, $hasrssarticles=true) {
function rss_enabled_for_mod($modname, $instance=null, $hasrsstype=true, $hasrssarticles=true) {
if ($hasrsstype) {
if (empty($instance->rsstype) || $instance->rsstype==0) {
return false;
@ -114,7 +114,7 @@ function rss_enabled($modname, $instance, $hasrsstype=true, $hasrssarticles=true
}
}
if (!instance_is_visible($modname,$instance)) {
if (!empty($instance) && !instance_is_visible($modname,$instance)) {
return false;
}
@ -125,22 +125,22 @@ function rss_enabled($modname, $instance, $hasrsstype=true, $hasrssarticles=true
* This function saves to file the rss feed specified in the parameters
*
* @global object
* @param string $modname the module name ie forum. Used to create a cache directory.
* @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
*/
function rss_save_file($modname, $filename, $result) {
function rss_save_file($componentname, $filename, $result) {
global $CFG;
$status = true;
if (! $basedir = make_upload_directory ('cache/rss/'. $modname)) {
if (! $basedir = make_upload_directory ('cache/rss/'. $componentname)) {
//Cannot be created, so error
$status = false;
}
if ($status) {
$fullfilename = rss_get_file_full_name($modname, $filename);
$fullfilename = rss_get_file_full_name($componentname, $filename);
$rss_file = fopen($fullfilename, "w");
if ($rss_file) {
$status = fwrite ($rss_file, $result);
@ -153,9 +153,9 @@ function rss_save_file($modname, $filename, $result) {
}
function rss_get_file_full_name($modname, $filename) {
function rss_get_file_full_name($componentname, $filename) {
global $CFG;
return "$CFG->dataroot/cache/rss/$modname/$filename.xml";
return "$CFG->dataroot/cache/rss/$componentname/$filename.xml";
}
function rss_get_file_name($instance, $sql) {

View File

@ -19,7 +19,7 @@
$dataid = $args[3];
$data = $DB->get_record('data', array('id' => $dataid), '*', MUST_EXIST);
if (!rss_enabled('data', $data, false, true)) {
if (!rss_enabled_for_mod('data', $data, false, true)) {
return null;
}
@ -27,7 +27,7 @@
//get the cache file info
$filename = rss_get_file_name($data, $sql);
$cachedfilepath = rss_get_file_full_name('data', $filename);
$cachedfilepath = rss_get_file_full_name('mod_data', $filename);
//Is the cache out of date?
$cachedfilelastmodified = 0;
@ -91,7 +91,7 @@
$rss = $header.$articles.$footer;
//Save the XML contents to file.
$status = rss_save_file('data', $filename, $rss);
$status = rss_save_file('mod_data', $filename, $rss);
}
}

View File

@ -267,7 +267,7 @@
$meta = '';
if (!empty($CFG->enablerssfeeds) && !empty($CFG->data_enablerssfeeds) && $data->rssarticles > 0) {
$rsstitle = format_string($course->shortname) . ': %fullname%';
rss_add_http_header($context, 'data', $data, $rsstitle);
rss_add_http_header($context, 'mod_data', $data, $rsstitle);
}
if ($data->csstemplate) {
$PAGE->requires->css('/mod/data/css.php?d='.$data->id);

View File

@ -83,7 +83,7 @@
require_once("$CFG->libdir/rsslib.php");
$rsstitle = format_string($course->shortname) . ': %fullname%';
rss_add_http_header($modcontext, 'forum', $forum, $rsstitle);
rss_add_http_header($modcontext, 'mod_forum', $forum, $rsstitle);
}
if ($forum->type == 'news') {

View File

@ -63,7 +63,7 @@ function forum_rss_get_feed($context, $args) {
$forum = $DB->get_record('forum', array('id' => $forumid), '*', MUST_EXIST);
if (!rss_enabled('forum', $forum)) {
if (!rss_enabled_for_mod('forum', $forum)) {
return null;
}
@ -72,7 +72,7 @@ function forum_rss_get_feed($context, $args) {
//hash the sql to get the cache file name
$filename = rss_get_file_name($forum, $sql);
$cachedfilepath = rss_get_file_full_name('forum', $filename);
$cachedfilepath = rss_get_file_full_name('mod_forum', $filename);
//Is the cache out of date?
$cachedfilelastmodified = 0;
@ -83,7 +83,7 @@ function forum_rss_get_feed($context, $args) {
//need to regenerate the cached version
$result = forum_rss_feed_contents($forum, $sql);
if (!empty($result)) {
$status = rss_save_file('forum',$filename,$result);
$status = rss_save_file('mod_forum',$filename,$result);
}
}
@ -98,7 +98,7 @@ function forum_rss_get_feed($context, $args) {
* @return void
*/
function forum_rss_delete_file($forum) {
rss_delete_file('forum', $forum);
rss_delete_file('mod_forum', $forum);
}
///////////////////////////////////////////////////////

View File

@ -94,7 +94,7 @@
require_once("$CFG->libdir/rsslib.php");
$rsstitle = format_string($course->shortname) . ': %fullname%';
rss_add_http_header($context, 'forum', $forum, $rsstitle);
rss_add_http_header($context, 'mod_forum', $forum, $rsstitle);
}
/// Print header.

View File

@ -22,7 +22,7 @@
$glossaryid = $args[3];
$glossary = $DB->get_record('glossary', array('id' => $glossaryid), '*', MUST_EXIST);
if (!rss_enabled('glossary', $glossary)) {
if (!rss_enabled_for_mod('glossary', $glossary)) {
return null;
}
@ -30,7 +30,7 @@
//get the cache file info
$filename = rss_get_file_name($glossary, $sql);
$cachedfilepath = rss_get_file_full_name('glossary', $filename);
$cachedfilepath = rss_get_file_full_name('mod_glossary', $filename);
//Is the cache out of date?
$cachedfilelastmodified = 0;
@ -83,7 +83,7 @@
$rss = $header.$articles.$footer;
//Save the XML contents to file.
$status = rss_save_file('glossary', $filename, $rss);
$status = rss_save_file('mod_glossary', $filename, $rss);
}
}

View File

@ -241,7 +241,7 @@ if (!empty($CFG->enablerssfeeds) && !empty($CFG->glossary_enablerssfeeds)
&& $glossary->rsstype && $glossary->rssarticles) {
$rsstitle = format_string($course->shortname) . ': %fullname%';
rss_add_http_header($context, 'glossary', $glossary, $rsstitle);
rss_add_http_header($context, 'mod_glossary', $glossary, $rsstitle);
}
if ($tab == GLOSSARY_APPROVAL_VIEW) {