]> git.mjollnir.org Git - moodle.git/commitdiff
block/rss_client: MDL-19990 Make feed discovery configurable
authorpoltawski <poltawski>
Mon, 3 Aug 2009 16:56:53 +0000 (16:56 +0000)
committerpoltawski <poltawski>
Mon, 3 Aug 2009 16:56:53 +0000 (16:56 +0000)
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

blocks/rss_client/editfeed.php
lang/en_utf8/block_rss_client.php
lang/en_utf8/help/rss_client/feedautodiscovery.html [new file with mode: 0644]

index 098cbb6a1119fa29395314f1e450b98b65d0f9f5..fe68002473765a87215fbcf1db4375565b6ff141 100644 (file)
@@ -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 = '<a href="#" onclick="' . $validatejs . '">' . get_string('validatefeed', 'block_rss_client') . '</a>';
-        $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);
index 2836fc10aef732a6783c18c6c82bd62d1feeef8e..a8e6c1e72abef89cebc8103d39f2d59bd1641348 100644 (file)
@@ -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 (file)
index 0000000..3a42d4e
--- /dev/null
@@ -0,0 +1,10 @@
+<h1>Feed Auto-discovery</h1>
+
+<p>
+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.
+</p>
+<p>For example, when entering the Moodle Docs URL: <pre>http://docs.moodle.org</pre>
+Moodle would automatically discover the recent changes feed to subscribe to:
+<pre>http://docs.moodle.org/en/index.php?title=Special:RecentChanges&amp;feed=rss</pre>
+</p>