コード例 #1
0
        /// <summary>
        /// Convert Html to WikiText format
        /// </summary>
        /// <param name="html">Input Html</param>
        /// <returns>Converted WikiText</returns>
        public string HtmlToWikiText(string html)
        {
            if (string.IsNullOrWhiteSpace(html))
            {
                throw new ArgumentNullException(nameof(html));
            }

            // construct multi-part form data
            string boundaryName = $"----------{Guid.NewGuid().ToString("N")}";

            StringBuilder requestContent = new StringBuilder();

            requestContent.Append("--").Append(boundaryName)
            .Append("\r\nContent-Disposition: form-data; name=\"scrub_wikitext\"\r\n\r\nfalse");
            requestContent.Append("--").Append(boundaryName)
            .Append("\r\nContent-Disposition: form-data; name=\"html\"\r\n\r\n")
            .Append(html);

            RestApiClient client = new RestApiClient(new Uri($"{ENDPOINT_BASE_URI}/transform/html/to/wikitext"))
            {
                RequestBodyString = requestContent.ToString()
            };

            HttpResponseMessage responseMessage = client.CallApiMethod(HttpMethod.Post, $"multipart/form-data; boundary={boundaryName}");

            if (!responseMessage.IsSuccessStatusCode)
            {
                return(null);
            }

            return(responseMessage.Content.ReadAsStringAsync().Result);
        }
コード例 #2
0
        /// <summary>
        /// Adds a page revision using Html-format content
        /// </summary>
        /// <param name="pageName">Exact title for the page (spaces will be converted to underscores '_' by the function)</param>
        /// <param name="contentHtml">New content (full content) for the page in Html format</param>
        /// <param name="editComments">Comments to be used to commit the edits</param>
        /// <param name="isMinorRevision">If true, adds a minor version to the page, otherwise increments the major version (major versions may require approvals before being public)</param>
        /// <returns>Must return "Content Unmodified" or "Accepted". Anything else is an error.</returns>
        public string AddPageRevisionHtml(string pageName, string contentHtml, string editComments, bool isMinorRevision = true)
        {
            if (string.IsNullOrWhiteSpace(pageName))
            {
                throw new ArgumentNullException(nameof(pageName));
            }

            if (string.IsNullOrWhiteSpace(contentHtml))
            {
                throw new ArgumentNullException(nameof(contentHtml));
            }

            if (string.IsNullOrWhiteSpace(editComments))
            {
                throw new ArgumentNullException(nameof(editComments));
            }

            StringBuilder uri = new StringBuilder();

            uri.Append(ENDPOINT_BASE_URI).Append("/page/html/").Append(pageName.Replace(" ", "_"));

            PageHtmlRevisionUpdateItem updateItem = new PageHtmlRevisionUpdateItem()
            {
                CrossSiteAntiForgeryToken = Guid.NewGuid().ToString("d"),
                BaseEtag       = string.Empty,
                CommitComments = editComments,
                ContentHtml    = contentHtml,
                IsBotUser      = true,
                IsMinorEdit    = isMinorRevision
            };

            RestApiClient client = new RestApiClient(new Uri(uri.ToString()));

            client.RequestHeaders.Add("Accept", "application/json");
            client.RequestBodyString = JsonSerializer.Serialize(updateItem);

            HttpResponseMessage responseMessage = client.CallApiMethod(HttpMethod.Post, "multipart/form-data");

            switch (responseMessage.StatusCode)
            {
            case System.Net.HttpStatusCode.OK:
                return("Content Unmodified");

            case System.Net.HttpStatusCode.Created:
                return("Accepted");
            }

            Dictionary <string, string> messages = JsonSerializer.Deserialize <Dictionary <string, string> >(responseMessage.Content.ReadAsStringAsync().Result);

            return(((messages != null) && (messages.ContainsKey("detail"))) ? messages["detail"] : "Unknown Error");
        }