/// <summary> /// Returns all categories along with the uncategorized category /// </summary> /// <returns></returns> public CategoryCollection GetAllCachedCategories() { CategoryCollection cc = ZCache.Get <CategoryCollection>(CacheKey); if (cc == null) { cc = CategoryCollection.FetchAll(); bool foundUncategorized = false; foreach (Category c in cc) { if (c.Name == UncategorizedName) { foundUncategorized = true; break; } } if (!foundUncategorized) { Category uncategorizedCategory = new Category(); uncategorizedCategory.Name = UncategorizedName; uncategorizedCategory.LinkName = "uncategorized"; uncategorizedCategory.Save(); cc.Add(uncategorizedCategory); } ZCache.InsertCache(CacheKey, cc, 90); } return(cc); }
public static Feed AddFeed(Feed feed) { if (feed.Name == null) { throw new Exception("Must give the feed a name"); } if (feed.Id == Guid.Empty) { feed.Id = Guid.NewGuid(); } if (feed.RequestInterval == 0) { feed.RequestInterval = 60; } if (feed.Document == null) { feed.ReLoad(); } ObjectStore os = new ObjectStore(); os.UniqueId = feed.Id; os.Name = "Feed: " + feed.Name; os.Data = ObjectManager.ConvertToString(feed); os.ContentType = "feed/xml"; os.Type = typeof(Feed).FullName; os.Save(); ZCache.RemoveCache("Feed-Objects"); return(feed); }
/// <summary> /// Gets all tags /// </summary> /// <returns></returns> public TagCollection GetAllTags() { TagCollection tc = ZCache.Get <TagCollection>("AllTags"); if (tc == null) { tc = new TagCollection(); TagCollection temp = TagCollection.FetchAll(); foreach (Tag t in temp) { Tag tag = tc.Find( delegate(Tag tempTag) { return(tempTag.Name == t.Name); }); if (tag == null) { tc.Add(t); } } ZCache.InsertCache("AllTags", tc, 90); } return(tc); }
/// <summary> /// Deletes a user, and reassigns any content created by that user to another existing user /// </summary> public static bool DeleteUser(IGraffitiUser user, IGraffitiUser userToAssumeContent, out string errorMessage) { if (!controller.CanDeleteUsers) { errorMessage = "The membership system in use does not support deleting users."; return(false); } if (user == null) { throw new Exception("The supplied user object is null and cannot be deleted"); } // Check if the user has created any content PostCollection pc = new PostCollection(); Query q = Post.CreateQuery(); q.AndWhere(Post.Columns.UserName, user.Name); pc.LoadAndCloseReader(q.ExecuteReader()); if (pc != null && pc.Count > 0) { if (userToAssumeContent == null) { errorMessage = "The user you are trying to delete has created posts. Another existing user must be selected to assign these posts to."; return(false); } foreach (Post p in pc) { if (p.UserName == user.Name) { p.UserName = userToAssumeContent.Name; } if (p.ModifiedBy == user.Name) { p.ModifiedBy = userToAssumeContent.Name; } if (p.CreatedBy == user.Name) { p.CreatedBy = userToAssumeContent.Name; } } } // Remove from roles if (user.Roles != null && user.Roles.Length > 0) { foreach (string roleName in user.Roles) { controller.RemoveUserFromRole(user.Name, roleName); } ZCache.RemoveByPattern("usersByRole-"); } controller.DeleteUser(user); ZCache.RemoveCache("user-" + user.Name.ToLower()); errorMessage = string.Empty; return(true); }
protected PostCollection GetTaggedPosts(string tagName) { PostCollection pc = ZCache.Get <PostCollection>("Tags-ForRSS-" + tagName); if (pc == null) { pc = Post.FetchPostsByTag(TagName); PostCollection permissionsFiltered = new PostCollection(); foreach (Post post in pc) { permissionsFiltered.Add(post); } permissionsFiltered.AddRange(pc); foreach (Post p in pc) { if (!RolePermissionManager.GetPermissions(p.Category.Id, GraffitiUsers.Current).Read) { permissionsFiltered.Remove(p); } } pc.Clear(); int ctr = 0; foreach (Post post in permissionsFiltered) { if (ctr < Util.PageSize) { pc.Add(post); ctr++; } } ZCache.InsertCache("Tags-ForRSS-" + tagName, pc, 120); } return(pc); }
protected override void AfterCommit() { base.AfterCommit(); //Update the number of posts per category CategoryController.UpdatePostCounts(); //PostController.UpdateVersionCount(Id); //Save tags per post Tag.Destroy(Tag.Columns.PostId, Id); if (TagList != null) { foreach (string t in Util.ConvertStringToList(TagList)) { Tag tag = new Tag(); tag.Name = t.Trim(); tag.PostId = Id; tag.Save(); } } WritePages(); ZCache.RemoveByPattern("Posts-"); ZCache.RemoveCache("Post-" + Id); }
protected override void AfterRemove(bool isDestroy) { Core.Post.UpdateCommentCount(this.PostId); ZCache.RemoveCache("Comments-" + PostId); ZCache.RemoveByPattern("Comments-Recent"); }
protected override void AfterCommit() { base.AfterCommit(); Post.UpdateCommentCount(PostId); if (!DontSendEmail) { try { EmailTemplateToolboxContext etc = new EmailTemplateToolboxContext(); etc.Put("comment", this); EmailTemplate ef = new EmailTemplate(); ef.Context = etc; ef.Subject = "New Comment: " + Post.Title; ef.To = Post.User.Email; ef.TemplateName = "comment.view"; ef.ReplyTo = ((this.Email == "") ? Post.User.Email : this.Email); Emailer.Send(ef); Log.Info("Comment Sent", "Email sent to {0} ({1}) from the post \"{2}\" ({3}).", Post.User.ProperName, Post.User.Email, Post.Title, Post.Id); } catch (Exception ex) { Log.Error("Email Failure", ex.Message); } } ZCache.RemoveCache("Comments-" + PostId); ZCache.RemoveByPattern("Comments-Recent"); }
public override string RenderData() { if (PostIds == null || PostIds.Length == 0) { return(string.Empty); } PostCollection pc = ZCache.Get <PostCollection>(DataCacheKey); if (pc == null) { pc = new PostCollection(); foreach (int i in PostIds) { Post p = new Post(i); if (!p.IsNew && !p.IsDeleted && p.IsPublished) { pc.Add(p); } } ZCache.InsertCache(DataCacheKey, pc, 180); } StringBuilder sb = new StringBuilder("<ul>"); foreach (Post p in pc) { sb.AppendFormat("<li><a href=\"{0}\">{1}</a></li>", p.Url, p.Title); } sb.Append("</ul>"); return(sb.ToString()); }
/// <summary> /// Returns a Graffiti User Object, which is a wrapper around the ASP.Net membership system. /// /// Users are cached for 2 minutes using a lower case version of the username. /// </summary> /// <returns></returns> public static IGraffitiUser GetUser(string username, bool isOnline) { username = username.ToLower(); IGraffitiUser user = ZCache.Get <IGraffitiUser>("user-" + username); if (user == null) { user = controller.GetUser(username); if (user != null) { ZCache.InsertCache("user-" + username, user, 120); } } if (isOnline) { HttpContext context = HttpContext.Current; if (context != null) { context.Items["Current.GraffitiUser"] = user; } } return(user); }
/// <summary> /// Manages a single instance of GraffitiApplication which controls the events to be invoked /// </summary> public static GraffitiApplication Instance() { GraffitiApplication ga = ZCache.Get <GraffitiApplication>("GraffitiApplication"); if (ga == null) { lock (lockedOnly) { ga = ZCache.Get <GraffitiApplication>("GraffitiApplication"); if (ga == null) { ga = new GraffitiApplication(); var details = GetEvents(); foreach (EventDetails detail in details) { if (detail.Enabled) { detail.Event.Init(ga); } } ZCache.InsertCache("GraffitiApplication", ga, 1800); } } } return(ga); }
public static T Get <T>(string name) where T : class, new() { string cacheKey = "object-" + name; T t = ZCache.Get <T>(cacheKey); if (t == null) { ObjectStore os = ObjectStore.FetchByColumn(ObjectStore.Columns.Name, name); if (os.IsLoaded) { t = ConvertToObject <T>(os.Data); } else { t = new T(); } if (t == null) { throw new Exception("Type " + typeof(T) + " could not be found or created"); } ZCache.MaxCache(cacheKey, t); } return(t); }
public static void Delete(string name) { ObjectStore os = ObjectStore.FetchByColumn(ObjectStore.Columns.Name, name); ObjectStore.Destroy(os.Id); ZCache.RemoveCache("object-" + name); }
/// <summary> /// Returns all the users for a given role /// </summary> /// <param name="role"></param> /// <returns></returns> public static List <IGraffitiUser> GetUsers(string role) { var userList = ZCache.Get <string[]>("usersByRole-" + role); if (userList == null) { if (role != "*") { userList = controller.GetUsersInRole(role); } else { StringCollection sc = new StringCollection(); foreach (RolePermissions rp in RolePermissionManager.GetRolePermissions()) { var users = controller.GetUsersInRole(rp.RoleName); foreach (string u in users) { if (!sc.Contains(u)) { sc.Add(u.ToLower()); } } } var admimUsers = controller.GetUsersInRole(AdminRole); foreach (string u in admimUsers) { if (!sc.Contains(u)) { sc.Add(u.ToLower()); } } userList = new string[sc.Count]; sc.CopyTo(userList, 0); } ZCache.InsertCache("usersByRole-" + role, userList, 180); } var the_users = new List <IGraffitiUser>(); foreach (string username in userList) { the_users.Add(GetUser(username)); } the_users.Sort(delegate(IGraffitiUser u1, IGraffitiUser u2) { return (Comparer <string> .Default.Compare( u1.ProperName, u2.ProperName)); }); return(the_users); }
public PostCollection PostsByCategory(Category category, int numberOfPosts, bool filterHome) { if (category == null) { return(null); } const string CacheKey = "Posts-Categories-P:{0}-C:{1}-T:{2}-PS:{3}"; PostCollection pc = ZCache.Get <PostCollection>(string.Format(CacheKey, 1, category.Id, category.SortOrder, numberOfPosts)); if (pc == null) { pc = new PostCollection(); Query q = PostCollection.DefaultQuery(1, numberOfPosts, category.SortOrder); if (Category.IncludeChildPosts) { if (category.ParentId > 0) { q.AndWhere(Post.Columns.CategoryId, category.Id); } else { var ids = new List <int>(category.Children.Count + 1); foreach (Category child in category.Children) { ids.Add(child.Id); } ids.Add(category.Id); q.AndInWhere(Post.Columns.CategoryId, ids.ToArray()); } } else { q.AndWhere(Post.Columns.CategoryId, category.Id); } if (filterHome) { string where = GraffitiContext.Current["where"] as string; if (!string.IsNullOrEmpty(where) && where == "home" && Site.UseCustomHomeList) { q.AndWhere(Post.Columns.IsHome, true); } } pc.LoadAndCloseReader(q.ExecuteReader()); ZCache.InsertCache(string.Format(CacheKey, 1, category.Id, category.SortOrder, numberOfPosts), pc, 60); } return(pc); }
public static Post GetCachedPost(int id) { Post post = ZCache.Get <Post>("Post-" + id); if (post == null) { post = new Post(id); ZCache.InsertCache("Post-" + id, post, 10); } return(post); }
/// <summary> /// Gets all Posts by the specified tag /// </summary> /// <param name="tagName"></param> /// <returns></returns> public PostCollection PostsByTag(string tagName) { string TagName = Util.CleanForUrl(tagName); PostCollection pc = ZCache.Get <PostCollection>("Tags-" + TagName); if (pc == null) { pc = Post.FetchPostsByTag(TagName); ZCache.InsertCache("Tags-" + TagName, pc, 60); } return(pc); }
private static void UpdateFeed(Feed feed, bool resetCache) { ObjectStore os = ObjectStore.FetchByColumn(ObjectStore.Columns.UniqueId, feed.Id); os.Data = ObjectManager.ConvertToString(feed); os.Version++; os.Save(); if (resetCache) { ZCache.RemoveCache("Feed-Objects"); } }
public List <MigratorComment> GetComments(int postID) { var comments = ZCache.Get <List <MigratorComment> >("MigratorComments"); if (comments == null) { throw new Exception("The comment cache has expired. Please restart your import."); } var postComments = comments.FindAll( delegate(MigratorComment c) { return(c.PostID == postID); }); return(postComments); }
/// <summary> /// Gets the last x amount of comments from the specified category Id /// </summary> /// <param name="numberOfComments"></param> /// <param name="categoryId"></param> /// <returns></returns> public CommentCollection RecentComments(int numberOfComments, int categoryId) { CommentCollection cc = ZCache.Get <CommentCollection>("Comments-Recent-" + numberOfComments + "c:" + categoryId); if (cc == null) { cc = new CommentCollection(); Query q = Comment.CreateQuery(); q.AndWhere(Comment.Columns.IsDeleted, false); q.AndWhere(Comment.Columns.IsPublished, true); if (categoryId > 0) { Category category = new CategoryController().GetCachedCategory(categoryId, true); if (category != null) { if (category.ParentId > 0) { q.AndWhere(Post.Columns.CategoryId, categoryId); } else { List <int> ids = new List <int>(category.Children.Count + 1); foreach (Category child in category.Children) { ids.Add(child.Id); } ids.Add(category.Id); q.AndInWhere(Post.Columns.CategoryId, ids.ToArray()); } } else { //this should result in no data, but it will signal to //the end user to edit/remove this widget q.AndWhere(Post.Columns.CategoryId, categoryId); } } q.Top = numberOfComments.ToString(); q.OrderByDesc(Comment.Columns.Id); cc.LoadAndCloseReader(q.ExecuteReader()); ZCache.InsertCache("Comments-Recent-" + numberOfComments + "c:" + categoryId, cc, 60); } return(cc); }
public static void UpdateFeedData() { DateTime dt = DateTime.Now; foreach (Feed feed in GetFeeds().Values) { if (feed.LastRequested.AddMinutes(feed.RequestInterval) <= dt) { feed.ReLoad(); UpdateFeed(feed, false); } } ZCache.RemoveCache("Feed-Objects"); }
public static void Save(IGraffitiUser user, string modified_by) { if (user == null) { throw new Exception("The supplied user object is null and cannot be edited"); } if (user.UniqueId == Guid.Empty) { user.UniqueId = Guid.NewGuid(); } controller.Save(user, modified_by); ZCache.RemoveCache("user-" + user.Name.ToLower()); }
public static void RemoveFeedData() { ObjectStoreCollection osc = new ObjectStoreCollection(); Query q = ObjectStore.CreateQuery(); q.AndWhere(ObjectStore.Columns.ContentType, "feed/xml"); osc.LoadAndCloseReader(q.ExecuteReader()); foreach (ObjectStore os in osc) { ObjectStore.Destroy(os.Id); } ZCache.RemoveCache("Feed-Objects"); }
public static void UpdatePostStatus(int id, PostStatus status) { //UpdateVersionCount(id); QueryCommand command = new QueryCommand("Update graffiti_Posts Set Status = " + DataService.Provider.SqlVariable("Status") + " Where Id = " + DataService.Provider.SqlVariable("Id")); List <Parameter> parameters = Post.GenerateParameters(); command.Parameters.Add(Post.FindParameter(parameters, "Status")).Value = (int)status; command.Parameters.Add(Post.FindParameter(parameters, "Id")).Value = id; DataService.ExecuteNonQuery(command); ZCache.RemoveByPattern("Posts-"); ZCache.RemoveCache("Post-" + id); }
private static string LoadFile(string path) { string fileContent = ZCache.Get <string>(path); if (fileContent == null) { using (StreamReader sr = new StreamReader(path)) { fileContent = sr.ReadToEnd(); } ZCache.MaxCache(path, fileContent, new CacheDependency(path)); } return(fileContent); }
/// <summary> /// Creates a new user. UserName and Email must be unique. /// </summary> public static IGraffitiUser CreateUser(string username, string password, string email, string role) { username = username.ToLower(); controller.CreateUser(username, password, email, role); if (role != null) { ZCache.RemoveCache("usersByRole-" + role); ZCache.RemoveByPattern("usersByRole-"); } IGraffitiUser user = GetUser(username); Events.Instance().ExecuteAfterNewUser(user); return(user); }
/// <summary> /// Gets x amount of popular posts from the specified category Id /// </summary> /// <param name="numberOfPosts"></param> /// <param name="categoryId"></param> /// <returns></returns> public PostCollection PopularPosts(int numberOfPosts, int categoryId) { PostCollection pc = ZCache.Get <PostCollection>("Posts-Popular-" + numberOfPosts + "c:" + categoryId); if (pc == null) { Query q = PostCollection.DefaultQuery(); if (categoryId > 0) { Category category = new CategoryController().GetCachedCategory(categoryId, true); if (category != null) { if (category.ParentId > 0) { q.AndWhere(Post.Columns.CategoryId, categoryId); } else { List <int> ids = new List <int>(category.Children.Count + 1); foreach (Category child in category.Children) { ids.Add(child.Id); } ids.Add(category.Id); q.AndInWhere(Post.Columns.CategoryId, ids.ToArray()); } } else { //this should result in no data, but it will signal to //the end user to edit/remove this widget q.AndWhere(Post.Columns.CategoryId, categoryId); } } q.Orders.Clear(); q.OrderByDesc(Post.Columns.Views); q.Top = numberOfPosts.ToString(); pc = PostCollection.FetchByQuery(q); ZCache.InsertCache("Posts-Popular-" + numberOfPosts + "c:" + categoryId, pc, 60); } return(pc); }
protected virtual object GetCategoryPosts(string key, GraffitiContext graffitiContext) { Category category = new CategoryController().GetCachedCategory(CategoryID, false); int pageSize = SiteSettings.Get().PageSize; PostCollection pc = ZCache.Get <PostCollection>(string.Format(CacheKey, PageIndex, CategoryID, category.SortOrder, pageSize)); if (pc == null) { pc = new PostCollection(); Query q = PostCollection.DefaultQuery(PageIndex, pageSize, category.SortOrder); if (Category.IncludeChildPosts) { if (category.ParentId > 0) { q.AndWhere(Post.Columns.CategoryId, CategoryID); } else { var ids = new List <int>(category.Children.Count + 1); foreach (Category child in category.Children) { ids.Add(child.Id); } ids.Add(category.Id); q.AndInWhere(Post.Columns.CategoryId, ids.ToArray()); } } else { q.AndWhere(Post.Columns.CategoryId, CategoryID); } pc.LoadAndCloseReader(q.ExecuteReader()); ZCache.InsertCache(string.Format(CacheKey, PageIndex, CategoryID, category.SortOrder, pageSize), pc, 60); } graffitiContext.TotalRecords = category.PostCount; graffitiContext.PageIndex = PageIndex; graffitiContext.PageSize = SiteSettings.Get().PageSize; return(pc); }
/// <summary> /// Gets the post feedback from the specified postId /// </summary> /// <param name="PostId"></param> /// <returns></returns> public CommentCollection PostFeedback(int PostId) { CommentCollection cc = ZCache.Get <CommentCollection>("Feedback-" + PostId); if (cc == null) { cc = new CommentCollection(); Query q = Comment.CreateQuery(); q.AndWhere(Comment.Columns.PostId, PostId); q.AndWhere(Comment.Columns.IsPublished, true); q.AndWhere(Comment.Columns.IsDeleted, false); q.OrderByAsc(Comment.Columns.Published); cc.LoadAndCloseReader(q.ExecuteReader()); ZCache.InsertCache("Feedback-" + PostId, cc, 10); } return(cc); }
/// <summary> /// Gets all posts by the specified user in the specified category name /// </summary> /// <param name="user"></param> /// <param name="category"></param> /// <param name="numberOfPosts"></param> public PostCollection PostsByUserAndCategory(IGraffitiUser user, Category category, int numberOfPosts) { if (category == null || user == null) { return(null); } const string CacheKey = "Posts-Users-Categories-P:{0}-U:{1}-C:{2}-T:{3}-PS:{4}"; PostCollection pc = ZCache.Get <PostCollection>(string.Format(CacheKey, 1, user.UniqueId, category.Id, category.SortOrder, numberOfPosts)); if (pc == null) { pc = new PostCollection(); Query q = PostCollection.DefaultQuery(1, numberOfPosts, category.SortOrder); q.AndWhere(Post.Columns.UserName, user.Name); if (Category.IncludeChildPosts) { if (category.ParentId > 0) { q.AndWhere(Post.Columns.CategoryId, category.Id); } else { var ids = new List <int>(category.Children.Count + 1); foreach (Category child in category.Children) { ids.Add(child.Id); } ids.Add(category.Id); q.AndInWhere(Post.Columns.CategoryId, ids.ToArray()); } } else { q.AndWhere(Post.Columns.CategoryId, category.Id); } pc.LoadAndCloseReader(q.ExecuteReader()); ZCache.InsertCache(string.Format(CacheKey, 1, user.UniqueId, category.Id, category.SortOrder, numberOfPosts), pc, 60); } return(pc); }