From: poltawski Date: Mon, 3 Aug 2009 16:56:53 +0000 (+0000) Subject: block/rss_client: MDL-19990 Make feed discovery configurable X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=28dd2513a2e7cfd4be28c0d287c44a262214093b;p=moodle.git block/rss_client: MDL-19990 Make feed discovery configurable Simplepie will try quite aggresively to autodiscover rss feeds by default. Make this configurable when adding a feed and also store the autodiscovered location rather than the input location. I also removed the feed validator as this doesn't make much sense with feed autodiscovery, and I think we can improve simplepie integration to do this instead --- diff --git a/blocks/rss_client/editfeed.php b/blocks/rss_client/editfeed.php index 098cbb6a11..fe68002473 100644 --- a/blocks/rss_client/editfeed.php +++ b/blocks/rss_client/editfeed.php @@ -50,11 +50,10 @@ class feed_edit_form extends moodleform { $mform->setType('url', PARAM_URL); $mform->addRule('url', null, 'required'); - $validatejs = "window.open('http://feedvalidator.org/check.cgi?url='+" . - "getElementById('id_url').value, 'validate', " . - "'width=640,height=480,scrollbars=yes,status=yes,resizable=yes'); return true;"; - $validatelink = '' . get_string('validatefeed', 'block_rss_client') . ''; - $mform->addElement('static', 'validatelink', '', $validatelink); + $mform->addElement('checkbox', 'autodiscovery', get_string('enableautodiscovery', 'block_rss_client')); + $mform->setDefault('autodiscovery', 1); + $mform->setAdvanced('autodiscovery'); + $mform->setHelpButton('autodiscovery', array('feedautodiscovery', get_string('validatefeed', 'block_rss_client'), 'rss_client'), true); $mform->addElement('text', 'preferredtitle', get_string('customtitlelabel', 'block_rss_client'), array('size' => 60)); $mform->setType('preferredtitle', PARAM_NOTAGS); @@ -71,6 +70,14 @@ class feed_edit_form extends moodleform { $this->add_action_buttons(true, $submitlabal); } + function definition_after_data(){ + $mform =& $this->_form; + + if($mform->getElementValue('autodiscovery')){ + $mform->applyFilter('url', 'feed_edit_form::autodiscover_feed_url'); + } + } + function validation($data, $files) { $errors = parent::validation($data, $files); @@ -78,6 +85,8 @@ class feed_edit_form extends moodleform { // set timeout for longer than normal to try and grab the feed $rss->set_timeout(10); $rss->set_feed_url($data['url']); + $rss->set_autodiscovery_cache_duration(0); + $rss->set_autodiscovery_level(SIMPLEPIE_LOCATOR_NONE); $rss->init(); if ($rss->error()) { @@ -93,11 +102,46 @@ class feed_edit_form extends moodleform { function get_data() { $data = parent::get_data(); if ($data) { - $data->title = $this->title; - $data->description = $this->description; + $data->title = ''; + $data->description = ''; + + if($this->title){ + $data->title = $this->title; + } + + if($this->description){ + $data->description = $this->description; + } } return $data; } + + /** + * Autodiscovers a feed url from a given url, to be used by the formslibs + * filter function + * + * Uses simplepie with autodiscovery set to maximum level to try and find + * a feed to subscribe to. + * See: http://simplepie.org/wiki/reference/simplepie/set_autodiscovery_level + * + * @param string URL to autodiscover a url + * @return string URL of feed or original url if none found + */ + public static function autodiscover_feed_url($url){ + $rss = new moodle_simplepie(); + $rss->set_feed_url($url); + $rss->set_autodiscovery_level(SIMPLEPIE_LOCATOR_ALL); + // When autodiscovering an RSS feed, simplepie will try lots of + // rss links on a page, so set the timeout high + $rss->set_timeout(20); + $rss->init(); + + if($rss->error()){ + return $url; + } + + return $rss->subscribe_url(); + } } $returnurl = optional_param('returnurl', '', PARAM_LOCALURL); diff --git a/lang/en_utf8/block_rss_client.php b/lang/en_utf8/block_rss_client.php index 2836fc10ae..a8e6c1e72a 100644 --- a/lang/en_utf8/block_rss_client.php +++ b/lang/en_utf8/block_rss_client.php @@ -22,6 +22,7 @@ $string['editafeed'] = 'Edit a feed'; $string['editfeeds'] = 'Edit, subscribe or unsubsribe from RSS/Atom news feeds'; $string['editnewsfeeds'] = 'Edit news feeds'; $string['editrssblock'] = 'Edit RSS Headline Block'; +$string['enableautodiscovery'] = 'Enable auto-discovery of feeds?'; $string['errorloadingfeed'] = 'Error loading this RSS feed ($a)'; $string['feed'] = 'Feed'; $string['feedadded'] = 'News feed added'; diff --git a/lang/en_utf8/help/rss_client/feedautodiscovery.html b/lang/en_utf8/help/rss_client/feedautodiscovery.html new file mode 100644 index 0000000000..3a42d4e3e2 --- /dev/null +++ b/lang/en_utf8/help/rss_client/feedautodiscovery.html @@ -0,0 +1,10 @@ +

Feed Auto-discovery

+ +

+With this option enabled Moodle will try to automatically discover a feed +on a web page if is not given a direct link to a feed. +

+

For example, when entering the Moodle Docs URL:

http://docs.moodle.org
+Moodle would automatically discover the recent changes feed to subscribe to: +
http://docs.moodle.org/en/index.php?title=Special:RecentChanges&feed=rss
+