/// <summary> /// Create the XML for the subscribedUsers list /// </summary> /// <param name="userID">ID of user whose list we want</param> /// <param name="skip">skip to this index in the list</param> /// <param name="show">show this many items in the list</param> /// <returns></returns> public bool CreateSubscribingUsersList(int userID, int skip, int show) { // check object is not already initialised if (userID <= 0 || show <= 0) { return false; } int count = show; XmlElement SubscribingUsersList = AddElementTag(RootElement, "SUBSCRIBINGUSERS-LIST"); AddAttribute(SubscribingUsersList, "SKIP", skip); AddAttribute(SubscribingUsersList, "SHOW", show); using (IDnaDataReader dataReader = GetSubscribingUsersList(userID, skip, show + 1)) // Get +1 so we know if there are more left { // Check to see if we found anything string userName = String.Empty; if (dataReader.HasRows && dataReader.Read()) { User subscribedTo = new User(InputContext); subscribedTo.AddPrefixedUserXMLBlock(dataReader, userID, "subscribedTo", SubscribingUsersList); _userAcceptsSubscriptions = dataReader.GetBoolean("SubscribedToAcceptSubscriptions"); //Get the list from the second recordset dataReader.NextResult(); if (dataReader.HasRows && dataReader.Read()) { XmlElement users = CreateElement("USERS"); do { User subscribingUser = new User(InputContext); int subscribingUserID = dataReader.GetInt32NullAsZero("UserID"); subscribingUser.AddUserXMLBlock(dataReader, subscribingUserID, users); count--; } while (count > 0 && dataReader.Read()); // dataReader.Read won't get called if count == 0 SubscribingUsersList.AppendChild(users); // See if there's an extra row waiting if (count == 0 && dataReader.Read()) { AddAttribute(SubscribingUsersList, "MORE", 1); } } } } //AddAttribute(UserSubscriptionsList, "COUNT", count); return true; }
/* <FORUMSOURCE> <JOURNAL> <USER> <USERID>6</USERID> <USERNAME>Jim Lynn</USERNAME> </USER> </JOURNAL> </FORUMSOURCE> or <FORUMSOURCE> <ARTICLE> <H2G2ID>12345</H2G2ID> <SUBJECT>Hello there</SUBJECT> </ARTICLE> </FORUMSOURCE> or <FORUMSOURCE> <USERPAGE> <USER> <USERID>6</USERID> <USERNAME>Jim Lynn</USERNAME> </USER> </USERPAGE> </FORUMSOURCE> or <FORUMSOURCE> <CLUB ID= '1'> <NAME>Dharmesh Club</NAME> </CLUB> </FORUMSOURCE> */ /// <summary> /// Builds an XML representation of the forum source, vis: /// </summary> /// <param name="forumID">ID of forum</param> /// <param name="threadID">ID of forum thread</param> /// <param name="includeArticle">flag to include article data</param> /// <param name="type">Type of forum</param> /// <param name="ID">ID either H2G2ID or club ID</param> /// <param name="title">Title</param> /// <param name="siteID">Site ID</param> /// <param name="url">URL involved</param> public void GetTitle(int forumID, int threadID, bool includeArticle, ref int type, ref int ID, ref string title, ref int siteID, ref string url) { string subject = ""; // Subject based on type found int userID = 0; int reviewForumID = 0; int alertInstantly = 0; int hiddenStatus = 0; int articleStatus = 0; int realForumID = 0; using (IDnaDataReader dataReader = InputContext.CreateDnaDataReader("getforumsource")) { dataReader.AddParameter("ForumID", forumID); dataReader.AddParameter("ThreadID", threadID); dataReader.AddParameter("CurrentSiteID", InputContext.CurrentSite.SiteID); dataReader.Execute(); if (dataReader.HasRows && dataReader.Read()) { // Fill in return values ID = dataReader.GetInt32NullAsZero("h2g2ID"); type = dataReader.GetInt32NullAsZero("Type"); hiddenStatus = dataReader.GetInt32NullAsZero("HiddenStatus"); articleStatus = dataReader.GetInt32NullAsZero("ArticleStatus"); alertInstantly = dataReader.GetInt32NullAsZero("AlertInstantly"); siteID = dataReader.GetInt32NullAsZero("SiteID"); realForumID = dataReader.GetInt32NullAsZero("ForumID"); reviewForumID = 0; userID = 0; if (dataReader.GetInt32NullAsZero("JournalOwner") > 0) { title = dataReader.GetStringNullAsEmpty("JournalUserName"); userID = dataReader.GetInt32NullAsZero("JournalOwner"); } else if (dataReader.GetInt32NullAsZero("UserID") > 0) { title = dataReader.GetStringNullAsEmpty("UserName"); userID = dataReader.GetInt32NullAsZero("UserID"); } else if (!dataReader.IsDBNull("ReviewForumID")) { title = dataReader.GetStringNullAsEmpty("ReviewForumName"); reviewForumID = dataReader.GetInt32NullAsZero("ReviewForumID"); } else if (!dataReader.IsDBNull("URL")) { url = dataReader.GetStringNullAsEmpty("URL"); } else { title = dataReader.GetStringNullAsEmpty("Subject"); } XmlElement forumSource = AddElementTag(RootElement, "FORUMSOURCE"); if (type == 0) { AddAttribute(forumSource, "TYPE", "journal"); User journalOwner = new User(InputContext); journalOwner.AddPrefixedUserXMLBlock(dataReader, userID, "Journal", forumSource); } else if (type == 3) { // It's a review forum AddAttribute(forumSource, "TYPE", "reviewforum"); XmlElement reviewForum = AddElementTag(forumSource, "REVIEWFORUM"); AddAttribute(reviewForum, "ID", reviewForumID); AddTextTag(reviewForum, "REVIEWFORUMNAME", subject); AddTextTag(reviewForum, "URLFRIENDLYNAME", "RF" + reviewForumID.ToString()); } else if (type == 1) { AddAttribute(forumSource, "TYPE", "article"); } /*else if (type == 5) { AddAttribute(forumSource, "TYPE", "club"); XmlElement club = AddElementTag(forumSource, "CLUB"); AddAttribute(club, "ID", clubID); AddTextTag(club, "NAME", subject); } else if (type == 6) { AddAttribute(forumSource, "TYPE", "clubforum"); XmlElement club = AddElementTag(forumSource, "CLUB"); AddAttribute(club, "ID", clubID); AddTextTag(club, "NAME", subject); } else if (type == 7) { AddAttribute(forumSource, "TYPE", "clubjournal"); XmlElement club = AddElementTag(forumSource, "CLUB"); AddAttribute(club, "ID", clubID); AddTextTag(club, "NAME", subject); }*/ else if (type == 4) { AddAttribute(forumSource, "TYPE", "privateuser"); XmlElement userPage = AddElementTag(forumSource, "USERPAGE"); User user = new User(InputContext); user.AddUserXMLBlock(dataReader, userID, userPage); } else if (type == 8) { AddAttribute(forumSource, "TYPE", "noticeboard"); } else { AddAttribute(forumSource, "TYPE", "userpage"); XmlElement userPage = AddElementTag(forumSource, "USERPAGE"); User user = new User(InputContext); user.AddUserXMLBlock(dataReader, userID, userPage); } if (ID > 0) { if (includeArticle) { GuideEntrySetup guideEntrySetup = new GuideEntrySetup(ID); guideEntrySetup.ShowEntryData = true; guideEntrySetup.ShowPageAuthors = true; guideEntrySetup.ShowReferences = true; guideEntrySetup.SafeToCache = true; GuideEntry guideEntry = new GuideEntry(InputContext, guideEntrySetup); if (!guideEntry.Initialise()) { throw new DnaException("The article doesn't exist. " + ID.ToString()); } if (!guideEntry.IsDeleted) { AddInside(guideEntry, "FORUMSOURCE"); XmlElement article = (XmlElement)forumSource.SelectSingleNode("ARTICLE"); AddIntElement(article, "H2G2ID", ID); } else { XmlElement deletedArticle = AddElementTag(forumSource, "ARTICLE"); AddIntElement(deletedArticle, "H2G2ID", ID); AddTextTag(deletedArticle, "SUBJECT", "Deleted"); AddIntElement(deletedArticle, "HIDDEN", hiddenStatus); XmlNode statusNode = AddTextTag(deletedArticle, "STATUS", GuideEntry.GetDescriptionForStatusValue(articleStatus)); AddAttribute(statusNode, "TYPE", articleStatus); } } else { XmlElement defaultArticle = AddElementTag(forumSource, "ARTICLE"); AddIntElement(defaultArticle, "H2G2ID", ID); AddTextTag(defaultArticle, "SUBJECT", subject); AddIntElement(defaultArticle, "HIDDEN", hiddenStatus); XmlNode statusNode = AddTextTag(defaultArticle, "STATUS", GuideEntry.GetDescriptionForStatusValue(articleStatus)); AddAttribute(statusNode, "TYPE", articleStatus); } AddIntElement(forumSource, "ALERTINSTANTLY", alertInstantly); } } } }
/// <summary> /// Gets the journal forum /// </summary> /// <param name="journalID">The ID of the journal you want to get the list for</param> /// <param name="show">The number of threads you want to pull out</param> /// <param name="skip">The number of threads you want to skip</param> /// <param name="showUserHidden">Whether to show user hidden</param> public void GetJournal(int journalID, int show, int skip, bool showUserHidden) { if (journalID == 0) { // Now create the xml tree XmlElement journalPosts = AddElementTag(RootElement, "JOURNALPOSTS"); // Add all the attributes for the JournalPosts node AddAttribute(journalPosts, "FORUMID", journalID); AddAttribute(journalPosts, "SKIPTO", skip); AddAttribute(journalPosts, "COUNT", show); AddAttribute(journalPosts, "TOTALTHREADS", 0); AddAttribute(journalPosts, "CANREAD", 1); AddAttribute(journalPosts, "CANWRITE", 0); return; } // Check to make sure we've been given acceptable values if (show > 200) { show = 200; } // Get the site that the forum belongs to int siteID = GetSiteIDForForum(journalID); // Get the most recent thread date DateTime expiryDate = GetMostRecentThreadDate(journalID); // Check to see if the journal is cached _cachedFileName = "J" + journalID.ToString() + "-" + skip.ToString() + "-" + Convert.ToString(skip + show - 1) + "-" + showUserHidden.ToString() + ".txt"; string cachedJournal = ""; if (InputContext.FileCacheGetItem("journal", _cachedFileName, ref expiryDate, ref cachedJournal) && cachedJournal.Length > 0) { // Create the journal from the cache CreateAndInsertCachedXML(cachedJournal, "JOURNALPOSTS", true); // Set the siteid from the tree if (RootElement.SelectSingleNode("//SITEID") != null) { _siteID = Convert.ToInt32(RootElement.SelectSingleNode("//SITEID").Value); } // Set the forum field _forumID = journalID; // Update the users permissions FilterOnJournalPostPermissions(ForumID); // Finally update the relative dates and return UpdateRelativeDates(); return; } // Not cached. Get the journal from the database and cache it if it's valid bool cacheResult = true; using (IDnaDataReader dataReader = InputContext.CreateDnaDataReader("forumlistjournalthreadswithreplies")) { dataReader.AddParameter("ForumID", journalID); dataReader.Execute(); cacheResult = dataReader.HasRows; bool moreData = false; bool defaultCanRead = true; bool defaultCanWrite = true; int moderationStatus = 0; int totalThreads = 0; if (dataReader.Read()) { moreData = true; defaultCanRead = dataReader.GetBoolean("CanRead"); defaultCanWrite = dataReader.GetBoolean("CanWrite"); moderationStatus = dataReader.GetInt32NullAsZero("ModerationStatus"); totalThreads = dataReader.GetInt32("ThreadCount"); } // Now create the xml tree XmlElement journalPosts = AddElementTag(RootElement, "JOURNALPOSTS"); // Add all the attributes for the JournalPosts node AddAttribute(journalPosts, "FORUMID", journalID); AddAttribute(journalPosts, "SKIPTO", skip); AddAttribute(journalPosts, "COUNT", show); AddAttribute(journalPosts, "TOTALTHREADS", totalThreads); AddAttribute(journalPosts, "CANREAD", Convert.ToInt32(defaultCanRead)); AddAttribute(journalPosts, "CANWRITE", Convert.ToInt32(defaultCanWrite)); if (skip > 0) { //Read/skip over the skip number of rows so that the row that the first row that in the do below is //the one required for (int i = 0; i < skip; i++) { moreData = dataReader.Read(); } } // Now add all the other info //XmlNode moderationNode = AddIntElement(forumThreadsNode, "MODERATIONSTATUS", moderationStatus); //AddAttribute(moderationNode, "ID", forumID); int numberOfPosts = show; while (moreData && numberOfPosts > 0 && totalThreads > 0) { int threadID = dataReader.GetInt32NullAsZero("ThreadID"); int postID = dataReader.GetInt32NullAsZero("EntryID"); int userID = dataReader.GetInt32NullAsZero("UserID"); int count = dataReader.GetInt32NullAsZero("Cnt"); int hidden = dataReader.GetInt32NullAsZero("Hidden"); string subject = ""; string text = ""; //Allow author to view their own user-hidden items. bool showHidden = (hidden == 7 && showUserHidden && InputContext.ViewingUser.UserID == userID); if (hidden > 0 && !showHidden) { subject = "Hidden"; text = "This post has been hidden"; } else { subject = dataReader.GetStringNullAsEmpty("Subject"); text = dataReader.GetStringNullAsEmpty("text"); } // Add the text, checking to see what style it is int postStyle = 0; if (!dataReader.IsDBNull("PostStyle")) { postStyle = dataReader.GetTinyIntAsInt("PostStyle"); } if (postStyle != 1) { text = ThreadPost.FormatPost(text, (CommentStatus.Hidden)hidden, true, false); //text = StringUtils.ConvertPlainText(text); } else { //TODO Do we need Rich Post stuff for the post style?? string temp = "<RICHPOST>" + HtmlUtils.ReplaceCRsWithBRs(text) + "</RICHPOST>"; //Regex regex = new Regex(@"(<[^<>]+)<BR \/>"); //while (regex.Match(temp).Success) //{ // temp = regex.Replace(temp, @"$1 "); //} //text = temp; text = HtmlUtils.TryParseToValidHtml(temp); } XmlElement post = AddElementTag(journalPosts, "POST"); AddAttribute(post, "POSTID", postID); AddAttribute(post, "THREADID", threadID); AddAttribute(post, "HIDDEN", hidden); AddTextTag(post, "SUBJECT", subject); AddDateXml(dataReader, post, "DATEPOSTED", "DatePosted"); if (count > 0) { AddDateXml(dataReader, post, "LASTREPLY", "LastReply"); XmlElement lastReplyTag = (XmlElement)post.SelectSingleNode("LASTREPLY"); AddAttribute(lastReplyTag, "COUNT", count); } User tempUser = new User(InputContext); tempUser.AddUserXMLBlock(dataReader, userID, post); AddXmlTextTag(post, "TEXT", text); // Now check to see if we've got more posts moreData = dataReader.Read(); numberOfPosts--; } if ((totalThreads > (skip + show)) && moreData) { AddAttribute(journalPosts, "MORE", 1); } // Check to see if we're needed to cache the journal if (cacheResult) { InputContext.FileCachePutItem("journal", _cachedFileName, journalPosts.OuterXml); } FilterOnJournalPostPermissions(journalID); UpdateRelativeDates(); } }
/// <summary> /// Produce XML for users posts tagged to the specified node. /// </summary> /// <param name="nodeID">The node ID in question</param> /// <returns>An XML Element contain the list of users for a node</returns> public XmlElement GetUsersForNodeID(int nodeID) { // Check the NodeID! if (nodeID == 0) { throw new DnaException("Category - NoNodeIDGiven - Illegal NodeID (0)"); } XmlElement userMemberList = CreateElement("USERMEMBERLIST"); // Now get the list for the given list id using (IDnaDataReader reader = InputContext.CreateDnaDataReader("getusersfornode")) { reader.AddParameter("nodeID", nodeID); reader.AddParameter("siteID", InputContext.CurrentSite.SiteID); reader.Execute(); if (reader.HasRows) { int userMemberCount = 0; while (reader.Read()) { User user = new User(InputContext); int userID = reader.GetInt32NullAsZero("UserID"); user.AddUserXMLBlock(reader, userID, userMemberList); userMemberCount++; } AddIntElement(userMemberList, "USERMEMBERCOUNT", userMemberCount); } } return userMemberList; }
/// <summary> /// Method to generate POST XML and add to a parent node /// </summary> /// <param name="reader">DataReader result of stored procedure returning forum post data.</param> /// <param name="component">DnaComponent to add posts to.</param> /// <param name="parentNode">Parent Node to xml the generated xml to.</param> /// <param name="context">The Input Context</param> public static void AddPostXml(IDnaDataReader reader, DnaComponent component, XmlNode parentNode, IInputContext context) { int hidden = 0; int inReplyTo = 0; int prevSibling = 0; int nextSibling = 0; int firstChild = 0; int userID = 0; int entryId = 0; int threadId = 0; int postIndex = 0; string subject = String.Empty; string datePosted = String.Empty; string bodyText = String.Empty; string hostPageUrl = String.Empty; string commentForumTitle = String.Empty; //get from db if (reader.DoesFieldExist("parent")) { inReplyTo = reader.GetInt32NullAsZero("parent"); } if (reader.DoesFieldExist("prevSibling")) { prevSibling = reader.GetInt32NullAsZero("prevSibling"); } if (reader.DoesFieldExist("nextSibling")) { nextSibling = reader.GetInt32NullAsZero("nextSibling"); } if (reader.DoesFieldExist("firstChild")) { firstChild = reader.GetInt32NullAsZero("firstChild"); } if (reader.DoesFieldExist("userID")) { userID = reader.GetInt32NullAsZero("userID"); } if (reader.DoesFieldExist("hidden")) { hidden = reader.GetInt32NullAsZero("hidden"); } subject = String.Empty; if (hidden == 3) // 3 means premoderated! - hidden! { subject = "Hidden"; } else if (hidden > 0) { subject = "Removed"; } else { subject = reader["subject"] as string; subject = StringUtils.EscapeAllXml(subject); } datePosted = String.Empty; if (reader["datePosted"] != DBNull.Value) { datePosted = reader["datePosted"].ToString(); } bodyText = String.Empty; if (hidden == 3) // 3 means premoderated! - hidden! { bodyText = "This post has been hidden"; } else if (hidden > 0) { bodyText = "This post has been Removed"; } else { bodyText = reader.GetStringNullAsEmpty("text"); } byte postStyle = reader.GetByteNullAsZero("postStyle"); if (postStyle != 1) { bodyText = StringUtils.ConvertPlainText(bodyText); } else { //TODO Do we need Rich Post stuff for the post style?? string temp = "<RICHPOST>" + bodyText.Replace("\r\n", "<BR />").Replace("\n", "<BR />") + "</RICHPOST>"; //Regex regex = new Regex(@"(<[^<>]+)<BR \/>"); //while (regex.Match(temp).Success) //{ // temp = regex.Replace(temp,@"$1 "); //} //bodyText = temp; bodyText = HtmlUtils.TryParseToValidHtml(temp); } if (reader.DoesFieldExist("hostpageurl")) { hostPageUrl = reader.GetStringNullAsEmpty("hostpageurl"); } if (reader.DoesFieldExist("commentforumtitle")) { commentForumTitle = reader.GetStringNullAsEmpty("commentforumtitle"); } if (reader.DoesFieldExist("entryID")) { entryId = reader.GetInt32NullAsZero("entryID"); } if (reader.DoesFieldExist("threadID")) { threadId = reader.GetInt32NullAsZero("threadID"); } if (reader.DoesFieldExist("postindex")) { postIndex = reader.GetInt32NullAsZero("postindex"); } string canRead = String.Empty; if (reader.DoesFieldExist("canRead")) { canRead = reader.GetByteNullAsZero("canRead").ToString(); } string canWrite = String.Empty; if (reader.DoesFieldExist("canWrite")) { canWrite = reader.GetByteNullAsZero("canWrite").ToString(); } XmlNode post = component.CreateElementNode("POST"); User user = new User(context); user.AddUserXMLBlock(reader, userID, post); AddPostXmlInternal(component, parentNode, context, hidden, inReplyTo, prevSibling, nextSibling, firstChild, userID, subject, datePosted, bodyText, hostPageUrl, commentForumTitle, entryId, threadId, postIndex, canRead, canWrite, post.ChildNodes[0]); }
/// <summary> /// Helper method to create the list after a specific stored procedure /// has been called to return an appropriate results set. /// </summary> /// <param name="dataReader">The data reader containing results</param> /// <param name="userlistXML">returned XmlElement</param> /// <param name="maxNumber">max number of rows to add</param> /// <param name="skip">number of rows to skip</param> /// <returns>true if successful, false if not.</returns> private bool CreateList(IDnaDataReader dataReader, ref XmlElement userlistXML, int maxNumber, int skip) { if (!dataReader.HasRows) { return false; } int numberShown = 0; if (skip > 0) { //Read/skip over the skip number of rows for (int i = 0; i < skip; i++) { dataReader.Read(); } } //add rows, assume that sp has limited the amount returned while (maxNumber > numberShown && dataReader.Read()) { User user = new User(InputContext); user.AddUserXMLBlock(dataReader, dataReader.GetInt32NullAsZero("USERID"), userlistXML); numberShown++; } if (dataReader.Read()) { AddAttribute(userlistXML, "MORE", 1); } AddAttribute(userlistXML, "COUNT", numberShown); return true; }