diff options
author | Dries Buytaert <dries@buytaert.net> | 2008-05-15 21:27:32 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2008-05-15 21:27:32 +0000 |
commit | 6b88e61700f87c98442534e4c9a2604b1cd8a057 (patch) | |
tree | c0b81015fd3a743639eab0f366b0bec188055fb6 /modules/aggregator/aggregator.module | |
parent | 7cd8427f65b870300e1aa8ea1229dd61beff18d3 (diff) | |
download | brdo-6b88e61700f87c98442534e4c9a2604b1cd8a057.tar.gz brdo-6b88e61700f87c98442534e4c9a2604b1cd8a057.tar.bz2 |
- Patch #256793 by Senpai and flobruit: code clean up.
Diffstat (limited to 'modules/aggregator/aggregator.module')
-rw-r--r-- | modules/aggregator/aggregator.module | 66 |
1 files changed, 36 insertions, 30 deletions
diff --git a/modules/aggregator/aggregator.module b/modules/aggregator/aggregator.module index 565a3617c..33796a5a9 100644 --- a/modules/aggregator/aggregator.module +++ b/modules/aggregator/aggregator.module @@ -28,7 +28,7 @@ function aggregator_help($path, $arg) { } /** - * Implementation of hook_theme() + * Implementation of hook_theme(). */ function aggregator_theme() { return array( @@ -294,7 +294,7 @@ function aggregator_block($op = 'list', $delta = '', $edit = array()) { $block['feed-' . $feed->fid]['info'] = t('!title feed latest items', array('!title' => $feed->title)); } } - else if ($op == 'configure') { + elseif ($op == 'configure') { list($type, $id) = explode('-', $delta); if ($type == 'category') { $value = db_result(db_query('SELECT block FROM {aggregator_category} WHERE cid = %d', $id)); @@ -305,7 +305,7 @@ function aggregator_block($op = 'list', $delta = '', $edit = array()) { $form['block'] = array('#type' => 'select', '#title' => t('Number of news items in block'), '#default_value' => $value, '#options' => drupal_map_assoc(array(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20))); return $form; } - else if ($op == 'save') { + elseif ($op == 'save') { list($type, $id) = explode('-', $delta); if ($type == 'category') { $value = db_query('UPDATE {aggregator_category} SET block = %d WHERE cid = %d', $edit['block'], $id); @@ -314,7 +314,7 @@ function aggregator_block($op = 'list', $delta = '', $edit = array()) { $value = db_query('UPDATE {aggregator_feed} SET block = %d WHERE fid = %d', $edit['block'], $id); } } - else if ($op == 'view') { + elseif ($op == 'view') { list($type, $id) = explode('-', $delta); switch ($type) { case 'feed': @@ -369,8 +369,8 @@ function aggregator_save_category($edit) { $op = 'delete'; } } - else if (!empty($edit['title'])) { - // A single unique id for bundles and feeds, to use in blocks + elseif (!empty($edit['title'])) { + // A single unique id for bundles and feeds, to use in blocks. db_query("INSERT INTO {aggregator_category} (title, description, block) VALUES ('%s', '%s', 5)", $edit['title'], $edit['description']); $link_path .= db_last_insert_id('aggregator', 'cid'); $op = 'insert'; @@ -394,7 +394,7 @@ function aggregator_save_feed($edit) { if (!empty($edit['fid']) && !empty($edit['title'])) { db_query("UPDATE {aggregator_feed} SET title = '%s', url = '%s', refresh = %d WHERE fid = %d", $edit['title'], $edit['url'], $edit['refresh'], $edit['fid']); } - else if (!empty($edit['fid'])) { + elseif (!empty($edit['fid'])) { $items = array(); $result = db_query('SELECT iid FROM {aggregator_item} WHERE fid = %d', $edit['fid']); while ($item = db_fetch_object($result)) { @@ -406,9 +406,9 @@ function aggregator_save_feed($edit) { db_query('DELETE FROM {aggregator_feed} WHERE fid = %d', $edit['fid']); db_query('DELETE FROM {aggregator_item} WHERE fid = %d', $edit['fid']); } - else if (!empty($edit['title'])) { + elseif (!empty($edit['title'])) { db_query("INSERT INTO {aggregator_feed} (title, url, refresh, block, description, image) VALUES ('%s', '%s', %d, 5, '', '')", $edit['title'], $edit['url'], $edit['refresh']); - // A single unique id for bundles and feeds, to use in blocks. + // A single unique ID for bundles and feeds, to use in blocks. $edit['fid'] = db_last_insert_id('aggregator_feed', 'fid'); } if (!empty($edit['title'])) { @@ -443,7 +443,7 @@ function aggregator_remove($feed) { } /** - * Call-back function used by the XML parser. + * Callback function used by the XML parser. */ function aggregator_element_start($parser, $name, $attributes) { global $item, $element, $tag, $items, $channel; @@ -509,7 +509,7 @@ function aggregator_element_end($parser, $name) { } /** - * Call-back function used by the XML parser. + * Callback function used by the XML parser. */ function aggregator_element_data($parser, $data) { global $channel, $element, $items, $item, $image, $tag; @@ -585,11 +585,11 @@ function aggregator_refresh($feed) { case 301: $feed['url'] = $result->redirect_url; watchdog('aggregator', 'Updated URL for feed %title to %url.', array('%title' => $feed['title'], '%url' => $feed['url'])); - // Deliberate no break. + // Do not break here. case 200: case 302: case 307: - // Filter the input data: + // Filter the input data. if (aggregator_parse_feed($result->data, $feed)) { $modified = empty($result->headers['Last-Modified']) ? 0 : strtotime($result->headers['Last-Modified']); @@ -604,7 +604,8 @@ function aggregator_refresh($feed) { } if (!empty($image['LINK']) && !empty($image['URL']) && !empty($image['TITLE'])) { - // Note, we should really use theme_image() here but that only works with local images it won't work with images fetched with a URL unless PHP version > 5 + // TODO: we should really use theme_image() here, but that only works with + // local images. It won't work with images fetched with a URL unless PHP version > 5. $image = '<a href="' . check_url($image['LINK']) . '" class="feed-image"><img src="' . check_url($image['URL']) . '" alt="' . check_plain($image['TITLE']) . '" /></a>'; } else { @@ -623,7 +624,7 @@ function aggregator_refresh($feed) { break; } $result->error = t('feed not parseable'); - // Deliberate no break. + // Do not break here.. default: watchdog('aggregator', 'The feed from %site seems to be broken, due to "%error".', array('%site' => $feed['title'], '%error' => $result->code . ' ' . $result->error), WATCHDOG_WARNING); drupal_set_message(t('The feed from %site seems to be broken, because of error "%error".', array('%site' => $feed['title'], '%error' => $result->code . ' ' . $result->error))); @@ -632,8 +633,9 @@ function aggregator_refresh($feed) { } /** - * Parse the W3C date/time format, a subset of ISO 8601. PHP date parsing - * functions do not handle this format. + * Parse the W3C date/time format, a subset of ISO 8601. + * + * PHP date parsing functions do not handle this format. * See http://www.w3.org/TR/NOTE-datetime for more information. * Originally from MagpieRSS (http://magpierss.sourceforge.net/). * @@ -645,11 +647,11 @@ function aggregator_refresh($feed) { function aggregator_parse_w3cdtf($date_str) { if (preg_match('/(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})(:(\d{2}))?(?:([-+])(\d{2}):?(\d{2})|(Z))?/', $date_str, $match)) { list($year, $month, $day, $hours, $minutes, $seconds) = array($match[1], $match[2], $match[3], $match[4], $match[5], $match[6]); - // calc epoch for current date assuming GMT + // Calculate the epoch for current date assuming GMT. $epoch = gmmktime($hours, $minutes, $seconds, $month, $day, $year); if ($match[10] != 'Z') { // Z is zulu time, aka GMT list($tz_mod, $tz_hour, $tz_min) = array($match[8], $match[9], $match[10]); - // zero out the variables + // Zero out the variables. if (!$tz_hour) { $tz_hour = 0; } @@ -657,7 +659,7 @@ function aggregator_parse_w3cdtf($date_str) { $tz_min = 0; } $offset_secs = (($tz_hour * 60) + $tz_min) * 60; - // is timezone ahead of GMT? then subtract offset + // Is timezone ahead of GMT? If yes, subtract offset. if ($tz_mod == '+') { $offset_secs *= -1; } @@ -683,13 +685,13 @@ function aggregator_parse_w3cdtf($date_str) { function aggregator_parse_feed(&$data, $feed) { global $items, $image, $channel; - // Unset the global variables before we use them: + // Unset the global variables before we use them. unset($GLOBALS['element'], $GLOBALS['item'], $GLOBALS['tag']); $items = array(); $image = array(); $channel = array(); - // parse the data: + // Parse the data. $xml_parser = drupal_xml_parser_create($data); xml_set_element_handler($xml_parser, 'aggregator_element_start', 'aggregator_element_end'); xml_set_character_data_handler($xml_parser, 'aggregator_element_data'); @@ -716,7 +718,7 @@ function aggregator_parse_feed(&$data, $feed) { } // Resolve the item's title. If no title is found, we use up to 40 - // characters of the description ending at a word boundary but not + // characters of the description ending at a word boundary, but not // splitting potential entities. if (!empty($item['TITLE'])) { $title = $item['TITLE']; @@ -741,15 +743,15 @@ function aggregator_parse_feed(&$data, $feed) { if (!empty($item['CONTENT:ENCODED'])) { $item['DESCRIPTION'] = $item['CONTENT:ENCODED']; } - else if (!empty($item['SUMMARY'])) { + elseif (!empty($item['SUMMARY'])) { $item['DESCRIPTION'] = $item['SUMMARY']; } - else if (!empty($item['CONTENT'])) { + elseif (!empty($item['CONTENT'])) { $item['DESCRIPTION'] = $item['CONTENT']; } // Try to resolve and parse the item's publication date. If no date is - // found, we use the current date instead. + // found, use the current date instead. $date = 'now'; foreach (array('PUBDATE', 'DC:DATE', 'DCTERMS:ISSUED', 'DCTERMS:CREATED', 'DCTERMS:MODIFIED', 'ISSUED', 'CREATED', 'MODIFIED', 'PUBLISHED', 'UPDATED') as $key) { if (!empty($item[$key])) { @@ -759,10 +761,12 @@ function aggregator_parse_feed(&$data, $feed) { } $timestamp = strtotime($date); // As of PHP 5.1.0, strtotime returns FALSE on failure instead of -1. + if ($timestamp <= 0) { - $timestamp = aggregator_parse_w3cdtf($date); // Returns FALSE on failure + $timestamp = aggregator_parse_w3cdtf($date); // Aggregator_parse_w3cdtf() returns FALSE on failure. if (!$timestamp) { - $timestamp = time(); // better than nothing + // Better than nothing. + $timestamp = time(); } } @@ -810,11 +814,11 @@ function aggregator_save_item($edit) { if ($edit['iid'] && $edit['title']) { db_query("UPDATE {aggregator_item} SET title = '%s', link = '%s', author = '%s', description = '%s', guid = '%s', timestamp = %d WHERE iid = %d", $edit['title'], $edit['link'], $edit['author'], $edit['description'], $edit['guid'], $edit['timestamp'], $edit['iid']); } - else if ($edit['iid']) { + elseif ($edit['iid']) { db_query('DELETE FROM {aggregator_item} WHERE iid = %d', $edit['iid']); db_query('DELETE FROM {aggregator_category_item} WHERE iid = %d', $edit['iid']); } - else if ($edit['title'] && $edit['link']) { + elseif ($edit['title'] && $edit['link']) { db_query("INSERT INTO {aggregator_item} (fid, title, link, author, description, timestamp, guid) VALUES (%d, '%s', '%s', '%s', '%s', %d, '%s')", $edit['fid'], $edit['title'], $edit['link'], $edit['author'], $edit['description'], $edit['timestamp'], $edit['guid']); $edit['iid'] = db_last_insert_id('aggregator_item', 'iid'); // file the items in the categories indicated by the feed @@ -838,6 +842,7 @@ function aggregator_feed_load($fid) { if (!isset($feeds[$fid])) { $feeds[$fid] = db_fetch_array(db_query('SELECT * FROM {aggregator_feed} WHERE fid = %d', $fid)); } + return $feeds[$fid]; } @@ -854,6 +859,7 @@ function aggregator_category_load($cid) { if (!isset($categories[$cid])) { $categories[$cid] = db_fetch_array(db_query('SELECT * FROM {aggregator_category} WHERE cid = %d', $cid)); } + return $categories[$cid]; } |