Index: feed-atom.php
===================================================================
--- feed-atom.php	(revision 11370)
+++ feed-atom.php	(working copy)
@@ -13,20 +13,12 @@
   xmlns="http://www.w3.org/2005/Atom"
   xmlns:thr="http://purl.org/syndication/thread/1.0"
   xml:lang="<?php echo get_option('rss_language'); ?>"
-  xml:base="<?php bloginfo_rss('home') ?>/wp-atom.php"
+  xml:base="<?php bloginfo_rss('url') ?>/wp-atom.php"
   <?php do_action('atom_ns'); ?>
  >
 	<title type="text"><?php bloginfo_rss('name'); wp_title_rss(); ?></title>
-	<subtitle type="text"><?php bloginfo_rss("description") ?></subtitle>
-
+	<?php atom_header(); the_generator('atom'); do_action('atom_head'); ?>
 	<updated><?php echo mysql2date('Y-m-d\TH:i:s\Z', get_lastpostmodified('GMT'), false); ?></updated>
-	<?php the_generator( 'atom' ); ?>
-
-	<link rel="alternate" type="text/html" href="<?php bloginfo_rss('home') ?>" />
-	<id><?php bloginfo('atom_url'); ?></id>
-	<link rel="self" type="application/atom+xml" href="<?php self_link(); ?>" />
-
-	<?php do_action('atom_head'); ?>
 	<?php while (have_posts()) : the_post(); ?>
 	<entry>
 		<author>
@@ -52,4 +44,4 @@
 		<thr:total><?php echo get_comments_number()?></thr:total>
 	</entry>
 	<?php endwhile ; ?>
-</feed>
+</feed>
\ No newline at end of file
Index: feed.php
===================================================================
--- feed.php	(revision 11370)
+++ feed.php	(working copy)
@@ -559,3 +559,117 @@
 
 	return $feed;
 }
+
+function echo_atom_feed_id($feed_id) {
+	echo "<id>$feed_id</id>\n";
+}
+function echo_atom_feed_subtitle($feed_subtitle) {
+	if(!empty($feed_subtitle)) {
+		echo '<subtitle type="text">'.$feed_subtitle.'</subtitle>'."\n";
+	}
+}
+function echo_atom_feed_links($self_link,$alternate_link,$replies_link) {
+	echo '<link rel="self" type="application/atom+xml" href="'.$self_link.'" />'."\n".'<link rel="alternate" type="'.get_option('html_type').'" href="'.$alternate_link.'" />'."\n".'<link rel="replies" type="application/atom+xml" href="'.$replies_link.'" />'."\n";
+}
+function atom_header() {
+	$feed_type="atom";
+	if(is_category()) {
+		$cat_id=get_query_object_id();
+		echo_atom_feed_id(get_category_feed_link($cat_id,$feed_type,true));
+		echo_atom_feed_links(get_category_feed_link($cat_id,$feed_type),get_category_link($cat_id),get_category_comments_feed_link($cat_id,$feed_type));
+	}
+	elseif(is_tag()) {
+		$tag_atom_id=get_query_object_id();
+		echo_atom_feed_id(get_tag_feed_link($tag_id,$feed_type,true));
+		echo_atom_feed_links(get_tag_feed_link($tag_id,$feed_type),get_tag_link($tag_id),get_tag_comments_feed_link($tag_id,$feed_type));
+	}
+	elseif(is_author()) {
+		$author_id=get_query_object_id();
+		echo_atom_feed_id(get_author_feed_link($author_id,$feed_type,true));
+		echo_atom_feed_links(get_author_feed_link($author_id,$feed_type),get_author_posts_url($author_id),get_author_comments_feed_link($author_id,$feed_type));
+	}
+	elseif(is_date()) {
+		$year=$month=$day=$hour=$minute=$second=$canonical_feed_link=$alternate_link=$comments_feed_link=null;
+		$year_string=get_query_var('year');
+		$month_string=get_query_var('monthnum');
+		$day_string=get_query_var('day');
+		$hour_string=get_query_var('hour');
+		$minute_string=get_query_var('minute');
+		$second_string=get_query_var('second');
+		$m=get_query_var('m');
+		$week=get_query_var('w');
+		if(!empty($week)) {
+			if(!empty($year_string)) {
+				$year=$year_string;
+			}
+			elseif(!empty($m)&&strlen($m)>=4) {
+				$year=substr($m,0,4);
+			}
+			$canonical_feed_link=get_week_feed_link($year,$week,$feed_type);
+			$alternate_link=get_week_link($year,$week);
+			$comments_feed_link=get_week_comments_feed_link($year,$week,$feed_type);
+		}
+		elseif(empty($m)) {
+			if(!empty($year_string)) {
+				$year=$year_string;
+			}
+			if(!empty($month_string)) {
+				$month=$month_string;
+			}
+			if(!empty($day_string)) {
+				$day=$day_string;
+			}
+			if(!empty($hour_string)) {
+				$hour=$hour_string;
+			}
+			if(!empty($minute_string)) {
+				$minute=$minute_string;
+			}
+			if(!empty($second_string)) {
+				$second=$second_string;
+			}
+		}
+		else {
+			$m_length=strlen($m);
+			switch($m_length) {
+				case 14:
+					$second=substr($m,12,2);
+				case 12:
+					$minute=substr($m,10,2);
+				case 10:
+					$hour=substr($m,8,2);
+				case 8:
+					$day=substr($m,6,2);
+				case 6:
+					$month=substr($m,4,2);
+				case 4:
+					$year=substr($m,0,4);
+					break;
+				default:
+					return false;
+			}
+		}
+		if($canonical_feed_link===null) {
+			$canonical_feed_link=get_archive_feed_link($year,$month,$day,$hour,$minute,$second,$feed_type);
+		}
+		if($alternate_link===null) {
+			$alternate_link=get_archive_link($year,$month,$day,$hour,$minute,$second);
+		}
+		if($comments_feed_link===null) {
+			$comments_feed_link=get_archive_comments_feed_link($year,$month,$day,$hour,$minute,$second,$feed_type);
+		}
+		echo_atom_feed_id($canonical_feed_link);
+		echo_atom_feed_links($canonical_feed_link,$alternate_link,$comments_feed_link);
+	}
+	else if(is_search()) {
+		$search_query=get_search_query();
+		$canonical_feed_link=get_search_feed_link($search_query,$feed_type);
+		echo_atom_feed_id($canonical_feed_link);
+		echo_atom_feed_links($canonical_feed_link,get_search_link($search_query),get_search_comments_feed_link($search_query,$feed_type));
+	}
+	else {
+		echo_atom_feed_subtitle(get_bloginfo_rss("description"));
+		echo_atom_feed_id(get_feed_link('atom',true));
+		echo_atom_feed_links(get_bloginfo('atom_url'),trailingslashit(get_bloginfo_rss('url')),get_bloginfo_rss('comments_atom_url'));
+	}
+}
Index: link-template.php
===================================================================
--- link-template.php	(revision 11370)
+++ link-template.php	(working copy)
@@ -359,11 +359,20 @@
  * @param string $feed Optional, defaults to default feed. Feed type.
  * @return string
  */
-function get_feed_link($feed = '') {
+function get_feed_link($feed = '', $return_canonical_form = false) {
 	global $wp_rewrite;
 
 	$permalink = $wp_rewrite->get_feed_permastruct();
-	if ( '' != $permalink ) {
+	if ($return_canonical_form==true||empty($permalink)) {
+		if ( empty($feed) )
+			$feed = get_default_feed();
+
+		if ( false !== strpos($feed, 'comments_') )
+			$feed = str_replace('comments_', 'comments-', $feed);
+
+		$output = trailingslashit(get_option('home')) . "?feed={$feed}";
+	}
+	else {
 		if ( false !== strpos($feed, 'comments_') ) {
 			$feed = str_replace('comments_', '', $feed);
 			$permalink = $wp_rewrite->get_comment_feed_permastruct();
@@ -375,16 +384,8 @@
 		$permalink = str_replace('%feed%', $feed, $permalink);
 		$permalink = preg_replace('#/+#', '/', "/$permalink");
 		$output =  get_option('home') . user_trailingslashit($permalink, 'feed');
-	} else {
-		if ( empty($feed) )
-			$feed = get_default_feed();
-
-		if ( false !== strpos($feed, 'comments_') )
-			$feed = str_replace('comments_', 'comments-', $feed);
-
-		$output = trailingslashit(get_option('home')) . "?feed={$feed}";
 	}
-
+	
 	return apply_filters('feed_link', $output, $feed);
 }
 
@@ -397,7 +398,7 @@
  * @param string $feed Optional. Feed type.
  * @return string
  */
-function get_post_comments_feed_link($post_id = '', $feed = '') {
+function get_post_comments_feed_link($post_id = '', $feed = '', $return_canonical_form = false) {
 	global $id;
 
 	if ( empty($post_id) )
@@ -406,19 +407,20 @@
 	if ( empty($feed) )
 		$feed = get_default_feed();
 
-	if ( '' != get_option('permalink_structure') ) {
-		$url = trailingslashit( get_permalink($post_id) ) . 'feed';
-		if ( $feed != get_default_feed() )
-			$url .= "/$feed";
-		$url = user_trailingslashit($url, 'single_feed');
-	} else {
+	if ($return_canonical_form==true||empty($permalink)) {
 		$type = get_post_field('post_type', $post_id);
 		if ( 'page' == $type )
 			$url = trailingslashit(get_option('home')) . "?feed=$feed&amp;page_id=$post_id";
 		else
 			$url = trailingslashit(get_option('home')) . "?feed=$feed&amp;p=$post_id";
 	}
-
+	else {
+		$url = trailingslashit( get_permalink($post_id) ) . 'feed';
+		if ( $feed != get_default_feed() )
+			$url .= "/$feed";
+		$url = user_trailingslashit($url, 'single_feed');
+	}
+	
 	return apply_filters('post_comments_feed_link', $url);
 }
 
@@ -438,8 +440,8 @@
  * @param string $feed Optional. Feed format.
  * @return string Link to the comment feed for the current post.
 */
-function post_comments_feed_link( $link_text = '', $post_id = '', $feed = '' ) {
-	$url = get_post_comments_feed_link($post_id, $feed);
+function post_comments_feed_link( $link_text = '', $post_id = '', $feed = '', $return_canonical_form = false) {
+	$url = get_post_comments_feed_link($post_id, $feed, $return_canonical_form);
 	if ( empty($link_text) )
 		$link_text = __('Comments Feed');
 
@@ -460,16 +462,17 @@
  * @param string $feed Optional. Feed type.
  * @return string Link to the feed for the author specified by $author_id.
 */
-function get_author_feed_link( $author_id, $feed = '' ) {
+function get_author_feed_link( $author_id, $feed = '', $return_canonical_form = false) {
 	$author_id = (int) $author_id;
 	$permalink_structure = get_option('permalink_structure');
 
 	if ( empty($feed) )
 		$feed = get_default_feed();
 
-	if ( '' == $permalink_structure ) {
+	if ($return_canonical_form==true||empty($permalink_structure)) {
 		$link = trailingslashit(get_option('home')) . "?feed=$feed&amp;author=" . $author_id;
-	} else {
+	}
+	else {
 		$link = get_author_posts_url($author_id);
 		if ( $feed == get_default_feed() )
 			$feed_link = 'feed';
@@ -484,6 +487,31 @@
 	return $link;
 }
 
+function get_author_comments_feed_link($author_id, $feed = '', $return_canonical_form = false) {
+	$author_id = (int) $author_id;
+	$permalink_structure = get_option('permalink_structure');
+
+	if ( empty($feed) )
+		$feed = get_default_feed();
+
+	if ($return_canonical_form==true||empty($permalink_structure)) {
+		$link = trailingslashit(get_option('home')) . "?feed=comments-$feed&amp;author=" . $author_id;
+	}
+	else {
+		$link = get_author_posts_url($author_id);
+		if ( $feed == get_default_feed() )
+			$feed_link = 'comments/feed';
+		else
+			$feed_link = "comments/feed/$feed";
+
+		$link = trailingslashit($link) . user_trailingslashit($feed_link, 'feed');
+	}
+
+	$link = apply_filters('author_comments_feed_link', $link, $feed);
+
+	return $link;
+}
+
 /**
  * Retrieve the feed link for a category.
  *
@@ -498,7 +526,7 @@
  * @param string $feed Optional. Feed type.
  * @return string Link to the feed for the category specified by $cat_id.
 */
-function get_category_feed_link($cat_id, $feed = '') {
+function get_category_feed_link($cat_id, $feed = '', $return_canonical_form = false) {
 	$cat_id = (int) $cat_id;
 
 	$category = get_category($cat_id);
@@ -511,9 +539,10 @@
 
 	$permalink_structure = get_option('permalink_structure');
 
-	if ( '' == $permalink_structure ) {
+	if ($return_canonical_form==true||empty($permalink_structure)) {
 		$link = trailingslashit(get_option('home')) . "?feed=$feed&amp;cat=" . $cat_id;
-	} else {
+	}
+	else {
 		$link = get_category_link($cat_id);
 		if( $feed == get_default_feed() )
 			$feed_link = 'feed';
@@ -528,6 +557,37 @@
 	return $link;
 }
 
+function get_category_comments_feed_link($cat_id, $feed = '', $return_canonical_form = false) {
+	$cat_id = (int) $cat_id;
+
+	$category = get_category($cat_id);
+
+	if ( empty($category) || is_wp_error($category) )
+		return false;
+
+	if ( empty($feed) )
+		$feed = get_default_feed();
+
+	$permalink_structure = get_option('permalink_structure');
+
+	if ($return_canonical_form==true||empty($permalink_structure)) {
+		$link = trailingslashit(get_option('home')) . "?feed=comments-$feed&amp;cat=" . $cat_id;
+	}
+	else {
+		$link = get_category_link($cat_id);
+		if( $feed == get_default_feed() )
+			$feed_link = 'comments/feed';
+		else
+			$feed_link = "comments/feed/$feed";
+
+		$link = trailingslashit($link) . user_trailingslashit($feed_link, 'feed');
+	}
+
+	$link = apply_filters('category_comments_feed_link', $link, $feed);
+
+	return $link;
+}
+
 /**
  * Retrieve permalink for feed of tag.
  *
@@ -537,7 +597,7 @@
  * @param string $feed Optional. Feed type.
  * @return string
  */
-function get_tag_feed_link($tag_id, $feed = '') {
+function get_tag_feed_link($tag_id, $feed = '', $return_canonical_form = false) {
 	$tag_id = (int) $tag_id;
 
 	$tag = get_tag($tag_id);
@@ -550,9 +610,10 @@
 	if ( empty($feed) )
 		$feed = get_default_feed();
 
-	if ( '' == $permalink_structure ) {
+	if ($return_canonical_form==true||empty($permalink_structure)) {
 		$link = trailingslashit(get_option('home')) . "?feed=$feed&amp;tag=" . $tag->slug;
-	} else {
+	}
+	else {
 		$link = get_tag_link($tag->term_id);
 		if ( $feed == get_default_feed() )
 			$feed_link = 'feed';
@@ -566,6 +627,36 @@
 	return $link;
 }
 
+function get_tag_comments_feed_link($tag_id, $feed = '', $return_canonical_form = false) {
+	$tag_id = (int) $tag_id;
+
+	$tag = get_tag($tag_id);
+
+	if ( empty($tag) || is_wp_error($tag) )
+		return false;
+
+	$permalink_structure = get_option('permalink_structure');
+
+	if ( empty($feed) )
+		$feed = get_default_feed();
+
+	if ($return_canonical_form==true||empty($permalink_structure)) {
+		$link = trailingslashit(get_option('home')) . "?feed=comments-$feed&amp;tag=" . $tag->slug;
+	}
+	else {
+		$link = get_tag_link($tag->term_id);
+		if ( $feed == get_default_feed() )
+			$feed_link = 'comments/feed';
+		else
+			$feed_link = "comments/feed/$feed";
+		$link = trailingslashit($link) . user_trailingslashit($feed_link, 'feed');
+	}
+
+	$link = apply_filters('tag_comments_feed_link', $link, $feed);
+
+	return $link;
+}
+
 /**
  * Retrieve edit tag link.
  *
@@ -608,6 +699,18 @@
 	echo $before . apply_filters( 'edit_tag_link', $link, $tag->term_id ) . $after;
 }
 
+function get_search_link($search_query) {
+	if (empty($search_query)) {
+		$search=attribute_escape(get_search_query());
+	}
+	else {
+		$search=attribute_escape(stripslashes($search_query));
+	}
+	$link=trailingslashit(get_option('home'))."?s=$search";
+	$link=apply_filters('search_link',$link);
+	return $link;
+}
+
 /**
  * Retrieve the permalink for the feed of the search results.
  *
@@ -658,6 +761,129 @@
 	return $link;
 }
 
+function _archive_array_to_link($array) {
+	$link=$array[0];
+	$array_length=count($array);
+	if($array_length>1) {
+		$link.="?$array[1]";
+		if($array_length>2) {
+			for($i=2;$i<$array_length;$i++) {
+				$link.="&amp;$array[$i]";
+			}
+		}
+	}
+	return $link;
+}
+function get_archive_link($year,$month=null,$day=null,$hour=null,$minute=null,$second=null,$return_array=false) {
+	$array=array(trailingslashit(get_option('home')));
+	if($year!==null) {
+		$year=(int)$year;
+		if($year<=0) {
+			return false;
+		}
+		$array[]="year=$year";
+	}
+	if($month!==null) {
+		$month=(int)$month;
+		if($month<1||$month>12) {
+			return false;
+		}
+		$array[]="monthnum=$month";
+	}
+	if($day!==null) {
+		$day=(int)$day;
+		if($day<1||$day>31) {
+			return false;
+		}
+		$array[]="day=$day";
+	}
+	if($hour!==null) {
+		$hour=(int)$hour;
+		if($hour<0||$hour>23) {
+			return false;
+		}
+		$array[]="hour=$hour";
+	}
+	if($minute!==null) {
+		$minute=(int)$minute;
+		if($minute<0||$minute>60) {
+			return false;
+		}
+		$array[]="minute=$minute";
+	}
+	if($second!==null) {
+		$second=(int)$second;
+		if($second<0||$second>60) {
+			return false;
+		}
+		$array[]="second=$second";
+	}
+	if($return_array==true) {
+		return $array;
+	}
+	else {
+		return apply_filters('archive_link',_archive_array_to_link($array));
+	}
+}
+function get_archive_feed_link($year,$month=null,$day=null,$hour=null,$minute=null,$second=null,$feed='') {
+	$array=get_archive_link($year,$month,$day,$hour,$minute,$second,true);
+	if($array===false) {
+		return false;
+	}
+	if(empty($feed)) {
+		$feed=get_default_feed();
+	}
+	$array[]="feed=$feed";
+	return apply_filters('archive_feed_link',_archive_array_to_link($array),$feed);
+}
+function get_archive_comments_feed_link($year,$month=null,$day=null,$hour=null,$minute=null,$second=null,$feed='') {
+	$array=get_archive_link($year,$month,$day,$hour,$minute,$second,true);
+	if($array===false) {
+		return false;
+	}
+	if(empty($feed)) {
+		$feed=get_default_feed();
+	}
+	$array[]="feed=comments-$feed";
+	return apply_filters('archive_comments_feed_link',_archive_array_to_link($array),$feed);
+}
+function get_week_link($year,$week,$return_array=false) {
+	$array=get_archive_link($year,null,null,null,null,null,true);
+	$week=(int)$week;
+	if($array===false||$week<0||$week>53) {
+		return false;
+	}
+	$array[]="w=$week";
+	if($return_array==true) {
+		return $array;
+	}
+	else {
+		return apply_filters('week_link',_archive_array_to_link($array));
+	}
+}
+function get_week_feed_link($year,$week,$feed='') {
+	$array=get_week_link($year,$week,true);
+	if($array===false) {
+		return false;
+	}
+	if(empty($feed)) {
+		$feed=get_default_feed();
+	}
+	$array[]="feed=$feed";
+	return apply_filters('week_feed_link',_archive_array_to_link($array),$feed);
+}
+function get_week_comments_feed_link($year,$week,$feed='') {
+	$array=get_week_link($year,$week,true);
+	if($array===false) {
+		return false;
+	}
+	if(empty($feed)) {
+		$feed=get_default_feed();
+	}
+	$array[]="feed=comments-$feed";
+	return apply_filters('week_comments_feed_link',_archive_array_to_link($array),$feed);
+}
+
 /**
  * Retrieve edit posts link for post.
  *
Index: query.php
===================================================================
--- query.php	(revision 11370)
+++ query.php	(working copy)
@@ -11,6 +11,11 @@
  * @subpackage Query
  */
 
+function get_query_object_id() {
+	global $wp_query;
+	return $wp_query->get_queried_object_id();
+}
+
 /**
  * Retrieve variable in the WP_Query class.
  *
