/** * @dataProvider firefoxtests */ public function test_from_file($data) { $locator = new SimplePie_Locator($data, 0, null, 'MockSimplePie_File', false); $expected = SimplePie_Misc::get_element('link', $data->body); $feed = $locator->find(SIMPLEPIE_LOCATOR_ALL, $all); $this->assertFalse($locator->is_feed($data), 'HTML document not be a feed itself'); $this->assertInstanceOf('MockSimplePie_File', $feed); $expected = array_map(array(get_class(), 'map_url_attrib'), $expected); $success = array_filter($expected, array(get_class(), 'filter_success')); $found = array_map(array(get_class(), 'map_url_file'), $all); $this->assertEquals($success, $found); }
/** * Additional validation includes checking URL and tags */ public function validation($data, $files) { global $CFG; $errors = parent::validation($data, $files); require_once $CFG->libdir . '/simplepie/moodle_simplepie.php'; $rssfile = new moodle_simplepie_file($data['url']); $filetest = new SimplePie_Locator($rssfile); if (!$filetest->is_feed($rssfile)) { $errors['url'] = get_string('feedisinvalid', 'blog'); } else { $rss = new moodle_simplepie($data['url']); if (!$rss->init()) { $errors['url'] = get_string('emptyrssfeed', 'blog'); } } return $errors; }
static function getFeedlinks($url) { if (!is_string($url)) { return false; } $parsed = @parse_url($url); if (empty($parsed['scheme'])) { return false; } if (!in_array($parsed['scheme'], array('http', 'https'))) { return false; } $timeout = 10; $redirects = 5; $headers = null; $useragent = null; $file = new SimplePie_File($url, $timeout, $redirects, $headers, $useragent); $locator = new SimplePie_Locator($file, $timeout, $useragent); $r = $locator->find(); return !empty($r->url) ? array($r->url) : false; }
/** * Given a record in the {blog_external} table, checks the blog's URL * for new entries not yet copied into Moodle. * Also attempts to identify and remove deleted blog entries * * @param object $externalblog * @return boolean False if the Feed is invalid */ function blog_sync_external_entries($externalblog) { global $CFG, $DB; require_once $CFG->libdir . '/simplepie/moodle_simplepie.php'; $rssfile = new moodle_simplepie_file($externalblog->url); $filetest = new SimplePie_Locator($rssfile); if (!$filetest->is_feed($rssfile)) { $externalblog->failedlastsync = 1; $DB->update_record('blog_external', $externalblog); return false; } else { if (!empty($externalblog->failedlastsync)) { $externalblog->failedlastsync = 0; $DB->update_record('blog_external', $externalblog); } } $rss = new moodle_simplepie($externalblog->url); if (empty($rss->data)) { return null; } //used to identify blog posts that have been deleted from the source feed $oldesttimestamp = null; $uniquehashes = array(); foreach ($rss->get_items() as $entry) { // If filtertags are defined, use them to filter the entries by RSS category if (!empty($externalblog->filtertags)) { $containsfiltertag = false; $categories = $entry->get_categories(); $filtertags = explode(',', $externalblog->filtertags); $filtertags = array_map('trim', $filtertags); $filtertags = array_map('strtolower', $filtertags); foreach ($categories as $category) { if (in_array(trim(strtolower($category->term)), $filtertags)) { $containsfiltertag = true; } } if (!$containsfiltertag) { continue; } } $uniquehashes[] = $entry->get_permalink(); $newentry = new stdClass(); $newentry->userid = $externalblog->userid; $newentry->module = 'blog_external'; $newentry->content = $externalblog->id; $newentry->uniquehash = $entry->get_permalink(); $newentry->publishstate = 'site'; $newentry->format = FORMAT_HTML; // Clean subject of html, just in case $newentry->subject = clean_param($entry->get_title(), PARAM_TEXT); // Observe 128 max chars in DB // TODO: +1 to raise this to 255 if (textlib::strlen($newentry->subject) > 128) { $newentry->subject = textlib::substr($newentry->subject, 0, 125) . '...'; } $newentry->summary = $entry->get_description(); //used to decide whether to insert or update //uses enty permalink plus creation date if available $existingpostconditions = array('uniquehash' => $entry->get_permalink()); //our DB doesnt allow null creation or modified timestamps so check the external blog supplied one $entrydate = $entry->get_date('U'); if (!empty($entrydate)) { $existingpostconditions['created'] = $entrydate; } //the post ID or false if post not found in DB $postid = $DB->get_field('post', 'id', $existingpostconditions); $timestamp = null; if (empty($entrydate)) { $timestamp = time(); } else { $timestamp = $entrydate; } //only set created if its a new post so we retain the original creation timestamp if the post is edited if ($postid === false) { $newentry->created = $timestamp; } $newentry->lastmodified = $timestamp; if (empty($oldesttimestamp) || $timestamp < $oldesttimestamp) { //found an older post $oldesttimestamp = $timestamp; } if (textlib::strlen($newentry->uniquehash) > 255) { // The URL for this item is too long for the field. Rather than add // the entry without the link we will skip straight over it. // RSS spec says recommended length 500, we use 255. debugging('External blog entry skipped because of oversized URL', DEBUG_DEVELOPER); continue; } if ($postid === false) { $id = $DB->insert_record('post', $newentry); // Set tags if ($tags = tag_get_tags_array('blog_external', $externalblog->id)) { tag_set('post', $id, $tags); } } else { $newentry->id = $postid; $DB->update_record('post', $newentry); } } // Look at the posts we have in the database to check if any of them have been deleted from the feed. // Only checking posts within the time frame returned by the rss feed. Older items may have been deleted or // may just not be returned anymore. We can't tell the difference so we leave older posts alone. $sql = "SELECT id, uniquehash\n FROM {post}\n WHERE module = 'blog_external'\n AND " . $DB->sql_compare_text('content') . " = " . $DB->sql_compare_text(':blogid') . "\n AND created > :ts"; $dbposts = $DB->get_records_sql($sql, array('blogid' => $externalblog->id, 'ts' => $oldesttimestamp)); $todelete = array(); foreach ($dbposts as $dbpost) { if (!in_array($dbpost->uniquehash, $uniquehashes)) { $todelete[] = $dbpost->id; } } $DB->delete_records_list('post', 'id', $todelete); $DB->update_record('blog_external', array('id' => $externalblog->id, 'timefetched' => time())); }
/** * @dataProvider firefoxtests */ public function test_from_file($data) { $locator = new SimplePie_Locator($data, 0, null, false); $registry = new SimplePie_Registry(); $registry->register('File', 'MockSimplePie_File'); $locator->set_registry($registry); $expected = array(); $document = new DOMDocument(); $document->loadHTML($data->body); $xpath = new DOMXPath($document); foreach ($xpath->query('//link') as $element) { $expected[] = 'http://example.com' . $element->getAttribute('href'); } //$expected = SimplePie_Misc::get_element('link', $data->body); $feed = $locator->find(SIMPLEPIE_LOCATOR_ALL, $all); $this->assertFalse($locator->is_feed($data), 'HTML document not be a feed itself'); $this->assertInstanceOf('MockSimplePie_File', $feed); $success = array_filter($expected, array(get_class(), 'filter_success')); $found = array_map(array(get_class(), 'map_url_file'), $all); $this->assertEquals($success, $found); }
public static function autoDiscovery($url) { $parts = parse_url($url); if (empty($parts['path'])) { $url .= '/'; } $result = ''; $proxy = null; if (sfConfig::get('op_http_proxy')) { $proxy = sfConfig::get('op_http_proxy'); } $file = @new SimplePie_File($url, 10, 5, null, null, false, $proxy); $locator = new SimplePie_Locator($file, 10, null, 'SimplePie_File', 10, $proxy); $feedUrl = $locator->find(); if (SimplePie_Misc::is_a($feedUrl, 'SimplePie_File')) { $result = $feedUrl->url; } return $result; }
function check_feed($url) { require_once dirname(__FILE__) . '/lib/simplepie_1.3.compiled.php'; $file = new SimplePie_File($url); $test = new SimplePie_Locator($file); $test->set_registry(new SimplePie_Registry()); if ($test->is_feed($file)) { return true; } else { return false; } }
/** * RSS/Atom Auto-Discovery に対応したlinkタグからURLを抽出する(static) */ function auto_discovery($url) { // path 未指定の場合は「/」に設定する $parts = parse_url($url); if (empty($parts['path'])) { $url .= '/'; } $result = ''; if (OPENPNE_USE_HTTP_PROXY) { $proxy = OPENPNE_HTTP_PROXY_HOST . ":" . OPENPNE_HTTP_PROXY_PORT; } $file = @new SimplePie_File($url, 10, 5, null, null, false, $proxy); $locator = new SimplePie_Locator($file, 10, null, 'SimplePie_File', 10, $proxy); $feed_url = $locator->find(); if (SimplePie_Misc::is_a($feed_url, 'SimplePie_File')) { $result = $feed_url->url; } return $result; }
/** * Given a record in the {blog_external} table, checks the blog's URL * for new entries not yet copied into Moodle. * * @param object $externalblog * @return boolean False if the Feed is invalid */ function blog_sync_external_entries($externalblog) { global $CFG, $DB; require_once $CFG->libdir . '/simplepie/moodle_simplepie.php'; $rssfile = new moodle_simplepie_file($externalblog->url); $filetest = new SimplePie_Locator($rssfile); if (!$filetest->is_feed($rssfile)) { $externalblog->failedlastsync = 1; $DB->update_record('blog_external', $externalblog); return false; } else { if (!empty($externalblog->failedlastsync)) { $externalblog->failedlastsync = 0; $DB->update_record('blog_external', $externalblog); } } // Delete all blog entries associated with this external blog blog_delete_external_entries($externalblog); $rss = new moodle_simplepie($externalblog->url); if (empty($rss->data)) { return null; } foreach ($rss->get_items() as $entry) { // If filtertags are defined, use them to filter the entries by RSS category if (!empty($externalblog->filtertags)) { $containsfiltertag = false; $categories = $entry->get_categories(); $filtertags = explode(',', $externalblog->filtertags); $filtertags = array_map('trim', $filtertags); $filtertags = array_map('strtolower', $filtertags); foreach ($categories as $category) { if (in_array(trim(strtolower($category->term)), $filtertags)) { $containsfiltertag = true; } } if (!$containsfiltertag) { continue; } } $newentry = new stdClass(); $newentry->userid = $externalblog->userid; $newentry->module = 'blog_external'; $newentry->content = $externalblog->id; $newentry->uniquehash = $entry->get_permalink(); $newentry->publishstate = 'site'; $newentry->format = FORMAT_HTML; $newentry->subject = $entry->get_title(); $newentry->summary = $entry->get_description(); //our DB doesnt allow null creation or modified timestamps so check the external blog didnt supply one $entrydate = $entry->get_date('U'); if (empty($entrydate)) { $newentry->created = time(); $newentry->lastmodified = time(); } else { $newentry->created = $entrydate; $newentry->lastmodified = $entrydate; } $textlib = textlib_get_instance(); if ($textlib->strlen($newentry->uniquehash) > 255) { // The URL for this item is too long for the field. Rather than add // the entry without the link we will skip straight over it. // RSS spec says recommended length 500, we use 255. debugging('External blog entry skipped because of oversized URL', DEBUG_DEVELOPER); continue; } $id = $DB->insert_record('post', $newentry); // Set tags if ($tags = tag_get_tags_array('blog_external', $externalblog->id)) { tag_set('post', $id, $tags); } } $DB->update_record('blog_external', array('id' => $externalblog->id, 'timefetched' => mktime())); }