/// <summary> /// Creates a Metaweblog Post object from the XML struct /// </summary> /// <param name="node">XML contains a Metaweblog Post Struct</param> /// <returns>Metaweblog Post Struct Obejct</returns> private MWAPost GetPost(XmlNode node) { MWAPost temp = new MWAPost(); List<string> cats = new List<string>(); List<string> tags = new List<string>(); // Require Title and Description try { temp.title = node.SelectSingleNode("value/struct/member[name='title']").LastChild.InnerText; temp.description = node.SelectSingleNode("value/struct/member[name='description']").LastChild.InnerText; } catch (Exception ex) { throw new MetaWeblogException("05", "Post Struct Element, Title or Description, not Sent. (" + ex.Message + ")"); } if (node.SelectSingleNode("value/struct/member[name='link']") == null) temp.link = ""; else temp.link = node.SelectSingleNode("value/struct/member[name='link']").LastChild.InnerText; if (node.SelectSingleNode("value/struct/member[name='mt_allow_comments']") == null) temp.commentPolicy = ""; else temp.commentPolicy = node.SelectSingleNode("value/struct/member[name='mt_allow_comments']").LastChild.InnerText; if (node.SelectSingleNode("value/struct/member[name='mt_excerpt']") == null) temp.excerpt = ""; else temp.excerpt = node.SelectSingleNode("value/struct/member[name='mt_excerpt']").LastChild.InnerText; if (node.SelectSingleNode("value/struct/member[name='wp_slug']") == null) temp.slug = ""; else temp.slug = node.SelectSingleNode("value/struct/member[name='wp_slug']").LastChild.InnerText; if (node.SelectSingleNode("value/struct/member[name='wp_author_id']") == null) temp.author = ""; else temp.author = node.SelectSingleNode("value/struct/member[name='wp_author_id']").LastChild.InnerText; if (node.SelectSingleNode("value/struct/member[name='categories']") != null) { XmlNode categoryArray = node.SelectSingleNode("value/struct/member[name='categories']").LastChild; foreach (XmlNode catnode in categoryArray.SelectNodes("array/data/value/string")) { cats.Add(catnode.InnerText); } } temp.categories = cats; // postDate has a few different names to worry about if (node.SelectSingleNode("value/struct/member[name='dateCreated']") != null) { try { string tempDate = node.SelectSingleNode("value/struct/member[name='dateCreated']").LastChild.InnerText; temp.postDate = DateTime.ParseExact(tempDate, "yyyyMMdd'T'HH':'mm':'ss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal); } catch { // Ignore PubDate Error } } else if (node.SelectSingleNode("value/struct/member[name='pubDate']") != null) { try { string tempPubDate = node.SelectSingleNode("value/struct/member[name='pubDate']").LastChild.InnerText; temp.postDate = DateTime.ParseExact(tempPubDate, "yyyyMMdd'T'HH':'mm':'ss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal); } catch { // Ignore PubDate Error } } // WLW tags implementation using mt_keywords if (node.SelectSingleNode("value/struct/member[name='mt_keywords']") != null) { string tagsList = node.SelectSingleNode("value/struct/member[name='mt_keywords']").LastChild.InnerText; foreach (string item in tagsList.Split(',')) { if (string.IsNullOrEmpty(tags.Find(delegate(string t) { return t.Equals(item.Trim(), StringComparison.OrdinalIgnoreCase); }))) { tags.Add(item.Trim()); } } } temp.tags = tags; return temp; }
/// <summary> /// Loads object properties with contents of passed xml /// </summary> /// <param name="xml">xml doc with methodname and parameters</param> private void LoadXMLRequest(string xml) { XmlDocument request = new XmlDocument(); try { if (!(xml.StartsWith("<?xml") || xml.StartsWith("<method"))) { xml = xml.Substring(xml.IndexOf("<?xml")); } request.LoadXml(xml); } catch (Exception ex) { throw new MetaWeblogException("01", "Invalid XMLRPC Request. (" + ex.Message + ")"); } // Method name is always first _methodName = request.DocumentElement.ChildNodes[0].InnerText; // Parameters are next (and last) _inputParams = new List<XmlNode>(); foreach (XmlNode node in request.SelectNodes("/methodCall/params/param")) { _inputParams.Add(node); } // Determine what params are what by method name switch (_methodName) { case "metaWeblog.newPost": _blogID = _inputParams[0].InnerText; _userName = _inputParams[1].InnerText; _password = _inputParams[2].InnerText; _post = GetPost(_inputParams[3]); if (_inputParams[4].InnerText == "0" || _inputParams[4].InnerText == "false") _publish = false; else _publish = true; break; case "metaWeblog.editPost": _postID = _inputParams[0].InnerText; _userName = _inputParams[1].InnerText; _password = _inputParams[2].InnerText; _post = GetPost(_inputParams[3]); if (_inputParams[4].InnerText == "0" || _inputParams[4].InnerText == "false") _publish = false; else _publish = true; break; case "metaWeblog.getPost": _postID = _inputParams[0].InnerText; _userName = _inputParams[1].InnerText; _password = _inputParams[2].InnerText; break; case "metaWeblog.newMediaObject": _blogID = _inputParams[0].InnerText; _userName = _inputParams[1].InnerText; _password = _inputParams[2].InnerText; _media = GetMediaObject(_inputParams[3]); break; case "metaWeblog.getCategories": case "wp.getAuthors": case "wp.getPageList": case "wp.getPages": case "wp.getTags": _blogID = _inputParams[0].InnerText; _userName = _inputParams[1].InnerText; _password = _inputParams[2].InnerText; break; case "metaWeblog.getRecentPosts": _blogID = _inputParams[0].InnerText; _userName = _inputParams[1].InnerText; _password = _inputParams[2].InnerText; _numberOfPosts = Int32.Parse(_inputParams[3].InnerText, CultureInfo.InvariantCulture); break; case "blogger.getUsersBlogs": case "metaWeblog.getUsersBlogs": _appKey = _inputParams[0].InnerText; _userName = _inputParams[1].InnerText; _password = _inputParams[2].InnerText; break; case "blogger.deletePost": _appKey = _inputParams[0].InnerText; _postID = _inputParams[1].InnerText; _userName = _inputParams[2].InnerText; _password = _inputParams[3].InnerText; if (_inputParams[4].InnerText == "0" || _inputParams[4].InnerText == "false") _publish = false; else _publish = true; break; case "blogger.getUserInfo": _appKey = _inputParams[0].InnerText; _userName = _inputParams[1].InnerText; _password = _inputParams[2].InnerText; break; case "wp.newPage": _blogID = _inputParams[0].InnerText; _userName = _inputParams[1].InnerText; _password = _inputParams[2].InnerText; _page = GetPage(_inputParams[3]); if (_inputParams[4].InnerText == "0" || _inputParams[4].InnerText == "false") _publish = false; else _publish = true; break; case "wp.getPage": _blogID = _inputParams[0].InnerText; _pageID = _inputParams[1].InnerText; _userName = _inputParams[2].InnerText; _password = _inputParams[3].InnerText; break; case "wp.editPage": _blogID = _inputParams[0].InnerText; _pageID = _inputParams[1].InnerText; _userName = _inputParams[2].InnerText; _password = _inputParams[3].InnerText; _page = GetPage(_inputParams[4]); if (_inputParams[5].InnerText == "0" || _inputParams[5].InnerText == "false") _publish = false; else _publish = true; break; case "wp.deletePage": _blogID = _inputParams[0].InnerText; _userName = _inputParams[1].InnerText; _password = _inputParams[2].InnerText; _pageID = _inputParams[3].InnerText; break; default: throw new MetaWeblogException("02", "未知方法. (" + _methodName + ")"); } }
/// <summary> /// 添加或修改文章 /// </summary> /// <param name="blogID"></param> /// <param name="postID"></param> /// <param name="userName"></param> /// <param name="password"></param> /// <param name="sentPost"></param> /// <param name="publish"></param> /// <param name="operate"></param> /// <returns></returns> private int NewOrUpdatePost(string blogID, string postID, string userName, string password, MWAPost sentPost, bool publish, OperateType operate) { ValidateRequest(userName, password); PostInfo post = new PostInfo(); if (operate == OperateType.Update) { post = PostManager.GetPost(StringHelper.StrToInt(postID, 0)); } else { post.CommentCount = 0; post.ViewCount = 0; post.CreateDate = DateTime.Now; UserInfo user = UserManager.GetUser(userName); if (user != null) { post.UserId = user.UserId; } } post.Title = StringHelper.HtmlEncode(sentPost.title); post.Content = sentPost.description; post.Status = publish == true ? 1 : 0; post.Slug = PageUtils.FilterSlug(sentPost.slug, "post", true); post.Summary = sentPost.excerpt; post.UrlFormat = (int)PostUrlFormat.Default; post.Template = string.Empty; post.Recommend = 0; post.TopStatus = 0; post.HideStatus = 0; post.UpdateDate = DateTime.Now; if (sentPost.commentPolicy != "") { if (sentPost.commentPolicy == "1") post.CommentStatus = 1; else post.CommentStatus = 0; } foreach (string item in sentPost.categories) { CategoryInfo cat; if (LookupCategoryGuidByName(item, out cat)) { post.CategoryId = cat.CategoryId; } else { CategoryInfo newcat = new CategoryInfo(); newcat.Count = 0; newcat.CreateDate = DateTime.Now; newcat.Description = "由离线工具创建"; newcat.Displayorder = 1000; newcat.Name = StringHelper.HtmlEncode(item); newcat.Slug = PageUtils.FilterSlug(item, "cate", false); newcat.CategoryId = CategoryManager.InsertCategory(newcat); post.CategoryId = newcat.CategoryId; } } post.Tag = GetTagIdList(sentPost.tags); if (operate == OperateType.Update) { PostManager.UpdatePost(post); } else { post.PostId = PostManager.InsertPost(post); // SendEmail(p); } return post.PostId; }
/// <summary> /// metaWeblog.newPost /// </summary> /// <param name="blogID">always 1000 in BlogEngine since it is a singlar blog instance</param> /// <param name="userName">login username</param> /// <param name="password">login password</param> /// <param name="sentPost">struct with post details</param> /// <param name="publish">mark as published?</param> /// <returns>postID as string</returns> internal string NewPost(string blogID, string userName, string password, MWAPost sentPost, bool publish) { ValidateRequest(userName, password); return NewOrUpdatePost(blogID, "", userName, password, sentPost, publish, OperateType.Insert).ToString(); }
/// <summary> /// metaWeblog.getRecentPosts /// </summary> /// <param name="blogID">always 1000 in BlogEngine since it is a singlar blog instance</param> /// <param name="userName">login username</param> /// <param name="password">login password</param> /// <param name="numberOfPosts">number of posts to return</param> /// <returns>array of post structs</returns> internal List<MWAPost> GetRecentPosts(string blogID, string userName, string password, int numberOfPosts) { ValidateRequest(userName, password); List<MWAPost> sendPosts = new List<MWAPost>(); int userid = 0; UserInfo user = UserManager.GetUser(userName); if (user != null) { userid = user.UserId; } List<PostInfo> posts = PostManager.GetPostList(numberOfPosts, -1, userid, -1, -1, -1, -1); foreach (PostInfo post in posts) { MWAPost tempPost = new MWAPost(); List<string> tempCats = new List<string>(); List<string> tempTags = new List<string>(); tempPost.postID = post.PostId.ToString(); tempPost.postDate = post.CreateDate; tempPost.title = StringHelper.HtmlDecode(post.Title); tempPost.description = post.Content; tempPost.link = post.Url; tempPost.slug = StringHelper.HtmlDecode(post.Slug); tempPost.excerpt = post.Summary; if (post.CommentStatus == 1) tempPost.commentPolicy = ""; else tempPost.commentPolicy = "0"; tempPost.publish = post.Status == 1 ? true : false; tempCats.Add(StringHelper.HtmlDecode(post.Category.Name)); tempPost.categories = tempCats; for (int i = 0; i < post.Tags.Count; i++) { tempTags.Add(StringHelper.HtmlDecode(post.Tags[i].Name)); } tempPost.tags = tempTags; sendPosts.Add(tempPost); } return sendPosts; }
/// <summary> /// metaWeblog.getPost /// </summary> /// <param name="postID">post guid in string format</param> /// <param name="userName">login username</param> /// <param name="password">login password</param> /// <returns>struct with post details</returns> internal MWAPost GetPost(string postID, string userName, string password) { ValidateRequest(userName, password); MWAPost sendPost = new MWAPost(); PostInfo post = PostManager.GetPost(StringHelper.StrToInt(postID, 0)); sendPost.postID = post.PostId.ToString(); sendPost.postDate = post.CreateDate; sendPost.title = StringHelper.HtmlDecode(post.Title); sendPost.description = post.Content; sendPost.link = post.Url; sendPost.slug = StringHelper.HtmlDecode(post.Slug); sendPost.excerpt = post.Summary; if (post.CommentStatus == 1) { sendPost.commentPolicy = "1"; } else { sendPost.commentPolicy = "0"; } sendPost.publish = post.Status == 1 ? true : false; List<string> cats = new List<string>(); cats.Add(StringHelper.HtmlDecode(post.Category.Name)); sendPost.categories = cats; List<string> tags = new List<string>(); for (int i = 0; i < post.Tags.Count; i++) { tags.Add(StringHelper.HtmlDecode(post.Tags[i].Name)); } sendPost.tags = tags; return sendPost; }
/// <summary> /// metaWeblog.editPost /// </summary> /// <param name="postID">post guid in string format</param> /// <param name="userName">login username</param> /// <param name="password">login password</param> /// <param name="sentPost">struct with post details</param> /// <param name="publish">mark as published?</param> /// <returns>1 if successful</returns> internal bool EditPost(string postID, string userName, string password, MWAPost sentPost, bool publish) { ValidateRequest(userName, password); NewOrUpdatePost("", postID, userName, password, sentPost, publish, OperateType.Update); return true; }