[3883] trunk/htdocs/system/classes: Return false if a tag doesn't exist, rather than assuming it does and throwing errors.

0 views
Skip to first unread message

svn-N...@habariproject.org

unread,
Dec 13, 2009, 7:11:08 PM12/13/09
to habar...@googlegroups.com
Revision
3883
Author
michaeltwofish
Date
2009-12-14 00:11:08 +0000 (Mon, 14 Dec 2009)

Log Message

Return false if a tag doesn't exist, rather than assuming it does and throwing errors. Add a require_match to tag rewrite rules to ensure that a tag exists, so that we 404 if a user tries to access /tag/nonexistenttag. (Perhaps need Tag::exists() ?)

Modified Paths

Diff

Modified: trunk/htdocs/system/classes/rewriterules.php (3882 => 3883)


--- trunk/htdocs/system/classes/rewriterules.php	2009-12-13 20:04:45 UTC (rev 3882)
+++ trunk/htdocs/system/classes/rewriterules.php	2009-12-14 00:11:08 UTC (rev 3883)
@@ -23,7 +23,7 @@
 			array( 'name' => 'display_home', 'parse_regex' => '%^(?:page/(?P<page>0|1))?/?$%', 'build_str' => '(page/{$page})', 'handler' => 'UserThemeHandler', 'action' => 'display_home', 'priority' => 1000, 'description' => 'Homepage (index) display' ),
 			array( 'name' => 'display_entries', 'parse_regex' => '%^(?:page/(?P<page>[2-9]|[1-9][0-9]+))/?$%', 'build_str' => '(page/{$page})', 'handler' => 'UserThemeHandler', 'action' => 'display_entries', 'priority' => 999, 'description' => 'Display multiple entries' ),
 			array( 'name' => 'display_entries_by_date', 'parse_regex' => '%^(?P<year>[1,2]{1}[\d]{3})(?:/(?P<month>[\d]{2}))?(?:/(?P<day>[\d]{2}))?(?:/page/(?P<page>\d+))?/?$%i', 'build_str' => '{$year}(/{$month})(/{$day})(/page/{$page})', 'handler' => 'UserThemeHandler', 'action' => 'display_date', 'priority' => 2, 'description' => 'Displays posts for a specific date.' ),
-			array( 'name' => 'display_entries_by_tag', 'parse_regex' => '%^tag/(?P<tag>[^/]*)(?:/page/(?P<page>\d+))?/?$%i', 'build_str' => 'tag/{$tag}(/page/{$page})', 'handler' => 'UserThemeHandler', 'action' => 'display_tag', 'priority' => 5, 'description' => 'Return posts matching specified tag.' ),
+			array( 'name' => 'display_entries_by_tag', 'parse_regex' => '%^tag/(?P<tag>[^/]*)(?:/page/(?P<page>\d+))?/?$%i', 'build_str' => 'tag/{$tag}(/page/{$page})', 'handler' => 'UserThemeHandler', 'action' => 'display_tag', 'priority' => 5, 'description' => 'Return posts matching specified tag.', 'parameters' => serialize( array( 'require_match' => array('Tag', 'rewrite_tag_exists') ) ) ),
 			array( 'name' => 'display_entry', 'parse_regex' => '%^(?P<slug>[^/]+)(?:/page/(?P<page>\d+))?/?$%i', 'build_str' => '{$slug}(/page/{$page})', 'handler' => 'UserThemeHandler', 'action' => 'display_post', 'priority' => 100, 'description' => 'Return entry matching specified slug', 'parameters' => serialize( array( 'require_match' => array('Posts', 'rewrite_match_type'), 'content_type'=>'entry' ) ) ),
 			array( 'name' => 'display_page', 'parse_regex' => '%^(?P<slug>[^/]+)(?:/page/(?P<page>\d+))?/?$%i', 'build_str' => '{$slug}(/page/{$page})', 'handler' => 'UserThemeHandler', 'action' => 'display_post', 'priority' => 100, 'description' => 'Return page matching specified slug', 'parameters' => serialize( array( 'require_match' => array('Posts', 'rewrite_match_type'), 'content_type'=>'page' ) ) ),
 			array( 'name' => 'display_search', 'parse_regex' => '%^search(?:/(?P<criteria>[^/]+))?(?:/page/(?P<page>\d+))?/?$%i', 'build_str' => 'search(/{$criteria})(/page/{$page})', 'handler' => 'UserThemeHandler', 'action' => 'search', 'priority' => 8, 'description' => 'Searches posts' ),
@@ -48,7 +48,7 @@
 			array( 'name' => 'atom_entry', 'parse_regex' => '%^(?P<slug>[^/]+)/atom/?$%i', 'build_str' => '{$slug}/atom', 'handler' => 'AtomHandler', 'action' => 'entry', 'priority' => 8, 'description' => 'Atom Publishing Protocol' ),
 			array( 'name' => 'atom_feed', 'parse_regex' => '%^atom/(?P<index>[^/]+)(?:/page/(?P<page>\d+))?/?$%i', 'build_str' => 'atom/{$index}(/page/{$page})', 'handler' => 'AtomHandler', 'action' => 'collection', 'priority' => 8, 'description' => 'Atom collection' ),
 			array( 'name' => 'atom_feed_comments', 'parse_regex' => '%^atom/comments(?:/page/(?P<page>\d+))?/?$%i', 'build_str' => 'atom/comments(/page/{$page})', 'handler' => 'AtomHandler', 'action' => 'comments', 'priority' => 7, 'description' => 'Entries comments' ),
-			array( 'name' => 'atom_feed_tag', 'parse_regex' => '%^tag/(?P<tag>[^/]+)/atom(?:/page/(?P<page>\d+))?/?$%i', 'build_str' => 'tag/{$tag}/atom(/page/{$page})', 'handler' => 'AtomHandler', 'action' => 'tag_collection', 'priority' => 8, 'description' => 'Atom Tag Collection' ),
+			array( 'name' => 'atom_feed_tag', 'parse_regex' => '%^tag/(?P<tag>[^/]+)/atom(?:/page/(?P<page>\d+))?/?$%i', 'build_str' => 'tag/{$tag}/atom(/page/{$page})', 'handler' => 'AtomHandler', 'action' => 'tag_collection', 'priority' => 8, 'description' => 'Atom Tag Collection', 'parameters' => serialize( array( 'require_match' => array('Tag', 'rewrite_tag_exists') ) ) ),
 			array( 'name' => 'atom_feed_entry_comments', 'parse_regex' => '%^(?P<slug>[^/]+)/atom/comments(?:/page/(?P<page>\d+))?/?$%i', 'build_str' => '{$slug}/atom/comments(/page/{$page})', 'handler' => 'AtomHandler', 'action' => 'entry_comments', 'priority' => 8, 'description' => 'Entry comments' ),
 			array( 'name' => 'atom_feed_page_comments', 'parse_regex' => '%^(?P<slug>[^/]+)/atom/comments(?:/page/(?P<page>\d+))?/?$%i', 'build_str' => '{$slug}/atom/comments(/page/{$page})', 'handler' => 'AtomHandler', 'action' => 'entry_comments', 'priority' => 8, 'description' => 'Page comments' ),
 
@@ -151,4 +151,4 @@
 	}
 }
 
-?>
\ No newline at end of file
+?>

Modified: trunk/htdocs/system/classes/tag.php (3882 => 3883)


--- trunk/htdocs/system/classes/tag.php	2009-12-13 20:04:45 UTC (rev 3882)
+++ trunk/htdocs/system/classes/tag.php	2009-12-14 00:11:08 UTC (rev 3883)
@@ -72,6 +72,17 @@
 	}
 
 	/**
+	 * Check if a tag exists, to see if we should match this rewrite rule
+	 *
+	 * @return Boolean
+	 **/
+	public static function rewrite_tag_exists($rule, $slug, $parameters)
+	{
+		$tag = Tag::get($rule->named_arg_values['tag']);
+		return $tag instanceOf Tag;
+	}
+
+	/**
 	 * Create a tag and save it.
 	 *
 	 * @param array $paramarray An associative array of tag fields

Modified: trunk/htdocs/system/classes/tags.php (3882 => 3883)


--- trunk/htdocs/system/classes/tags.php	2009-12-13 20:04:45 UTC (rev 3882)
+++ trunk/htdocs/system/classes/tags.php	2009-12-14 00:11:08 UTC (rev 3883)
@@ -36,6 +36,9 @@
 	public static function get_one( $tag )
 	{
 		$term = Tags::vocabulary()->get_term( $tag );
+		if ( !$term instanceOf Term ) {
+			return FALSE;
+		}
 		$tag = new Tag( array( 'tag_text' => $term->term_display, 'tag_slug' => $term->term, 'id' => $term->id ) );
 		return $tag;
 	}
Reply all
Reply to author
Forward
0 new messages