/// <summary> /// Edits the specified page. /// </summary> /// <param name="Article">The article to edit.</param> /// <param name="NewText">The new wikitext for the article.</param> /// <param name="Summary">The edit summary to use for this edit.</param> /// <param name="Minor">Whether or not to flag the edit as minor.</param> /// <param name="Watch">Whether article should be added to your watchlist</param> /// <returns>An EditPageRetvals object</returns> public EditPageRetvals EditPageEx(String Article, String NewText, String Summary, bool Minor, bool Watch) { HttpWebRequest wr = Variables.PrepareWebRequest(m_indexpath + "index.php?title=" + Tools.WikiEncode(Article) + "&action=submit", UserAgent); string editpagestr = GetEditPage(Article); Match m = Edittime.Match(editpagestr); string wpEdittime = m.Groups[1].Value; m = EditToken.Match(editpagestr); string wpEditkey = HttpUtility.UrlEncode(m.Groups[1].Value); wr.CookieContainer = new CookieContainer(); foreach (Cookie cook in logincookies) wr.CookieContainer.Add(cook); //Create poststring string poststring = string.Format( "wpSection=&wpStarttime={0}&wpEdittime={1}&wpScrolltop=&wpTextbox1={2}&wpSummary={3}&wpSave=Save%20Page&wpEditToken={4}", new[] { DateTime.Now.ToUniversalTime().ToString("yyyyMMddHHmmss"), wpEdittime, HttpUtility.UrlEncode(NewText), HttpUtility.UrlEncode(Summary), wpEditkey }); if (Minor) poststring = poststring.Insert(poststring.IndexOf("wpSummary"), "wpMinoredit=1&"); if (Watch || editpagestr.Contains("type='checkbox' name='wpWatchthis' checked='checked' accesskey=\"w\" id='wpWatchthis' />")) poststring += "&wpWatchthis=1"; wr.Method = "POST"; wr.ContentType = "application/x-www-form-urlencoded"; //poststring = HttpUtility.UrlEncode(poststring); byte[] bytedata = Encoding.UTF8.GetBytes(poststring); wr.ContentLength = bytedata.Length; Stream rs = wr.GetRequestStream(); rs.Write(bytedata, 0, bytedata.Length); rs.Close(); WebResponse resps = wr.GetResponse(); StreamReader sr = new StreamReader(resps.GetResponseStream()); EditPageRetvals retval = new EditPageRetvals(); retval.Article = Article; retval.responsetext = sr.ReadToEnd(); Match permalinkmatch = permalinkrx.Match(retval.responsetext); //From the root directory. retval.difflink = m_indexpath.Substring(0, m_indexpath.IndexOf("/", 9)) + permalinkmatch.Groups[1].Value + "&diff=prev"; retval.difflink = retval.difflink.Replace("&", "&"); // TODO: Check our submission worked and we have a valid difflink; throw an exception if not. Also check for the sorry we could not process because of loss of session data message return retval; }
/// <summary> /// Edits the specified page. /// </summary> /// <param name="Article">The article to edit.</param> /// <param name="NewText">The new wikitext for the article.</param> /// <param name="Summary">The edit summary to use for this edit.</param> /// <param name="Minor">Whether or not to flag the edit as minor.</param> /// <param name="Watch">Whether article should be added to your watchlist</param> /// <returns>An EditPageRetvals object</returns> public EditPageRetvals EditPageEx(String Article, String NewText, String Summary, bool Minor, bool Watch) { HttpWebRequest wr = Variables.PrepareWebRequest(m_indexpath + "index.php?title=" + Tools.WikiEncode(Article) + "&action=submit", UserAgent); string editpagestr = GetEditPage(Article); Match m = Edittime.Match(editpagestr); string wpEdittime = m.Groups[1].Value; m = EditToken.Match(editpagestr); string wpEditkey = HttpUtility.UrlEncode(m.Groups[1].Value); wr.CookieContainer = new CookieContainer(); foreach (Cookie cook in logincookies) { wr.CookieContainer.Add(cook); } //Create poststring string poststring = string.Format( "wpSection=&wpStarttime={0}&wpEdittime={1}&wpScrolltop=&wpTextbox1={2}&wpSummary={3}&wpSave=Save%20Page&wpEditToken={4}", new[] { DateTime.Now.ToUniversalTime().ToString("yyyyMMddHHmmss"), wpEdittime, HttpUtility.UrlEncode(NewText), HttpUtility.UrlEncode(Summary), wpEditkey }); if (Minor) { poststring = poststring.Insert(poststring.IndexOf("wpSummary"), "wpMinoredit=1&"); } if (Watch || editpagestr.Contains("type='checkbox' name='wpWatchthis' checked='checked' accesskey=\"w\" id='wpWatchthis' />")) { poststring += "&wpWatchthis=1"; } wr.Method = "POST"; wr.ContentType = "application/x-www-form-urlencoded"; //poststring = HttpUtility.UrlEncode(poststring); byte[] bytedata = Encoding.UTF8.GetBytes(poststring); wr.ContentLength = bytedata.Length; Stream rs = wr.GetRequestStream(); rs.Write(bytedata, 0, bytedata.Length); rs.Close(); WebResponse resps = wr.GetResponse(); StreamReader sr = new StreamReader(resps.GetResponseStream()); EditPageRetvals retval = new EditPageRetvals(); retval.article = Article; retval.responsetext = sr.ReadToEnd(); Match permalinkmatch = permalinkrx.Match(retval.responsetext); //From the root directory. retval.difflink = m_indexpath.Substring(0, m_indexpath.IndexOf("/", 9)) + permalinkmatch.Groups[1].Value + "&diff=prev"; retval.difflink = retval.difflink.Replace("&", "&"); // TODO: Check our submission worked and we have a valid difflink; throw an exception if not. Also check for the sorry we could not process because of loss of session data message return(retval); }