public void MWToDWContent(Site site, PageBE page, bool isOld) {
            try {

                // check for template content overrides
                if (OverrideTemplate(site, page)) {
                    return;
                }

                Plug converterUri = MediaWikiConverterContext.Current.MWConverterUri;
                if (null != converterUri) {
                    string interwikiInfo = String.Empty;
                    if (null != _MWInterwikiBySite) {
                        _MWInterwikiBySite.TryGetValue(site, out interwikiInfo);
                    }
                    XDoc xml = converterUri.With("title", page.Title.AsPrefixedDbPath()).With("lang", site.Language).With("site", site.Name).With("interwiki", interwikiInfo).With("text", page.GetText(DbUtils.CurrentSession)).PostAsForm().ToDocument();
                    xml.UsePrefix("mediawiki", "#mediawiki");

                    // if this is a redirect, set the page text accordingly]
                    if (page.IsRedirect) {
                        ParserResult result = DekiXmlParser.Parse(page, ParserMode.SAVE, -1, false);
                        if (result.RedirectsToTitle != null) {
                            page.SetText(String.Format(REDIRECT_TEXT, MWToDWTitle(site, Title.FromUIUri(null, xml["//mediawiki:link/@href"].Contents, true)).AsPrefixedDbPath().Replace("&", "&")));
                        } else {
                            page.SetText(String.Format(REDIRECT_TEXT, xml["//mediawiki:link/@href"].Contents.Replace("&", "&"), true));
                        }
                        page.ContentType = DekiMimeType.DEKI_XML0805;
                        return;
                    }

                    // remove extra paragraph tags from templates
                    if (page.Title.IsTemplate) {
                        List<XDoc> topLevelParagraphs = xml["/html/body/p"].ToList();
                        if (1 == topLevelParagraphs.Count) {
                            topLevelParagraphs[0].ReplaceWithNodes(topLevelParagraphs[0]);
                        }
                    }

                    // Map MediaWiki titles to MindTouch
                    MWToDWTitles(site, page, isOld, xml);

                    // Convert from MediaWiki output to MindTouch
                    WikiTextProcessor.Convert(site, xml, page.Title.IsTemplate);

                    // If the page is available in other languages, insert wiki.languages 
                    List<XDoc> languageList = xml["//mediawiki:meta[@type='language']"].ToList();
                    if (0 < languageList.Count) {
                        StringBuilder languageData = new StringBuilder("{{ wiki.languages( { ");
                        for (int i = 0; i < languageList.Count; i++) {
                            if (0 < i) {
                                languageData.Append(", ");
                            }
                            string relatedLanguage = languageList[i]["@language"].AsText;
                            Title relatedTitle = Title.FromUIUri(null, languageList[i].Contents, false);
                            Site relatedSite = MediaWikiConverterContext.Current.GetMWSite(relatedLanguage);
                            languageData.AppendFormat("{0}: {1}", StringUtil.QuoteString(relatedLanguage), StringUtil.QuoteString(MWToDWTitle(relatedSite, relatedTitle).AsPrefixedDbPath()));
                            languageList[i].Remove();
                        }
                        languageData.Append(" } ) }}");
                        xml["//body"].Value(languageData.ToString());
                    }

                    string contents = xml.ToString();
                    int first = contents.IndexOf("<body>");
                    int last = contents.LastIndexOf("</body>");
                    if ((first >= 0) && (last >= 0)) {
                        page.SetText(contents.Substring(first + 6, last - (first + 6)));
                        page.ContentType = DekiMimeType.DEKI_TEXT;
                    }
                }
            } catch (Exception e) {
                Console.Out.WriteLine("An unexpected exception has occurred:");
                Console.Out.WriteLine(e.GetCoroutineStackTrace());
                Console.Out.WriteLine("MediaWiki page text that produced the exception:");
                Console.Out.WriteLine(page.GetText(DbUtils.CurrentSession));
            }
        }
Example #2
0
        public static OldBE Save(PageBE page, OldBE previous, string userComment, string text, string contentType, string displayName, string language, int section, string xpath, DateTime timeStamp, ulong restoredPageId, bool loggingEnabled, bool removeIllegalElements, Title relToTitle, bool overwrite, uint authorId, out bool conflict) {

            // NOTE (steveb):
            //  page: most recent page about to be overwritten
            //  previous: (optional) possible earlier page on which the current edit is based upon

            conflict = false;
            bool isNewForEventContext = page.ID == 0 || page.IsRedirect;

            // check save permissions
            IsAccessAllowed(page, 0 == page.ID ? Permissions.CREATE : Permissions.UPDATE, false);

            // validate the save
            if((0 == page.ID) && ((-1 != section) || (null != xpath))) {
                throw new ArgumentException(DekiResources.SECTION_EDIT_EXISTING_PAGES_ONLY);
            }

            // displaynames entered by user are trimmed
            if(displayName != null) {
                displayName = displayName.Trim();
            }

            if(!Title.FromDbPath(page.Title.Namespace, page.Title.AsUnprefixedDbPath(), displayName).IsValid) {
                throw new DreamAbortException(DreamMessage.Conflict(DekiResources.INVALID_TITLE));
            }

            // load old contents into current page when a section is edited
            ParserResult alternate = new ParserResult();
            ParserResult original = new ParserResult();
            if(previous != null) {

                // parse most recent version as alternate
                alternate = DekiXmlParser.Parse(page, ParserMode.RAW);

                // parse base version for three way diff
                string pageContentType = page.ContentType;
                string pageText = page.GetText(DbUtils.CurrentSession);
                page.ContentType = previous.ContentType;
                page.SetText(previous.Text);
                original = DekiXmlParser.Parse(page, ParserMode.RAW);
                page.ContentType = pageContentType;
                page.SetText(pageText);
            }

            // ensure the parent exists
            PageBE parent = EnsureParent(DekiXmlParser.REDIRECT_REGEX.IsMatch(text), page.Title);
            if(null != parent) {
                page.ParentID = parent.Title.IsRoot ? 0 : parent.ID;
            }

            // Explicitly setting the language of a talk page is not valid
            if(page.Title.IsTalk && !string.IsNullOrEmpty(language)) {
                throw new Exceptions.TalkPageLanguageCannotBeSet();
            }

            // Language is set in this order: explicitly given, already set, language of parent
            language = language ?? page.Language ?? (null != parent ? parent.Language : String.Empty);

            // talk pages always get their language from their corresponding front page
            if(page.Title.IsTalk) {
                PageBE frontPage = PageBL.GetPageByTitle(page.Title.AsFront());
                if(frontPage != null && frontPage.ID != 0) {
                    language = frontPage.Language;
                }
            }

            string nativeName = ValidatePageLanguage(language);

            // parse the content
            ParserResult parserResult = DekiXmlParser.ParseSave(page, contentType, language, text, section, xpath, removeIllegalElements, relToTitle);
            OldBE old = null;
            string comment = userComment ?? string.Empty;

            // check if this is a new page
            if(0 == page.ID) {
                AuthorizePage(DekiContext.Current.User, Permissions.CREATE, parent, false);

                if(0 == restoredPageId) {
                    if(!string.IsNullOrEmpty(comment)) {
                        comment += "; ";
                    }
                    comment += string.Format(DekiResources.PAGE_CREATED);
                    if((null == parserResult.RedirectsToTitle) && (null == parserResult.RedirectsToUri)) {
                        comment += string.Format(", " + DekiResources.PAGE_DIFF_SUMMARY_ADDED, Utils.GetPageWordCount(parserResult.MainBody));
                    }
                    page.MinorEdit = false;
                    page.IsNew = true;
                }
            }

            // if this is an existing page, ensure the content has changed and save the current page information 
            else {

                // prevent creating a redirect on a page that has non-redirect children
                if((null != parserResult.RedirectsToTitle) || (null != parserResult.RedirectsToUri)) {
                    IList<PageBE> children = DbUtils.CurrentSession.Pages_GetChildren((uint)page.ID, page.Title.Namespace, true);
                    if(0 < children.Count) {
                        throw new DreamAbortException(DreamMessage.Conflict(DekiResources.INVALID_REDIRECT));
                    }
                }

                bool displayNameChanged = page.Title.DisplayName != displayName && displayName != null;
                bool languageChanged = !StringUtil.EqualsInvariant(page.Language, language);
                if(parserResult.ContentType == page.ContentType) {
                    if(StringUtil.EqualsInvariant(parserResult.BodyText, page.GetText(DbUtils.CurrentSession))) {
                        if(!displayNameChanged && !languageChanged && !overwrite) {
                            return null;
                        }
                    } else {

                        // merge changes
                        if(previous != null) {
                            conflict = true;
                            try {
                                XDoc mergeBody = XDocDiff.Merge(original.MainBody, alternate.MainBody, parserResult.MainBody, Utils.MAX_DIFF_SIZE, ArrayMergeDiffPriority.Right, out conflict);
                                parserResult = DekiXmlParser.ParseSave(page, page.ContentType, page.Language, mergeBody.ToInnerXHtml(), -1, null, removeIllegalElements, relToTitle);
                            } catch(Exception e) {
                                _log.Error("Save", e);
                            }
                        }
                        if(!string.IsNullOrEmpty(comment)) {
                            comment += "; ";
                        }
                        if(page.IsRedirect) {
                            comment += string.Format(DekiResources.PAGE_DIFF_SUMMARY_ADDED, Utils.GetPageWordCount(parserResult.MainBody));
                        } else {
                            comment += Utils.GetPageDiffSummary(page, page.GetText(DbUtils.CurrentSession), page.ContentType, parserResult.BodyText, parserResult.ContentType);
                        }
                    }
                } else {
                    if(!string.IsNullOrEmpty(comment)) {
                        comment += "; ";
                    }
                    comment += string.Format(DekiResources.PAGE_CONTENTTYPE_CHANGED, parserResult.ContentType);
                }

                if(displayNameChanged) {
                    if(!string.IsNullOrEmpty(comment)) {
                        comment += "; ";
                    }
                    comment += string.Format(DekiResources.PAGE_DISPLAYNAME_CHANGED, displayName);

                }

                if(languageChanged) {
                    if(!string.IsNullOrEmpty(comment)) {
                        comment += "; ";
                    }
                    comment += string.Format(DekiResources.PAGE_LANGUAGE_CHANGED, nativeName);

                    // set the language on the talk page as well.
                    if(!page.Title.IsTalk) {
                        PageBE talkPage = PageBL.GetPageByTitle(page.Title.AsTalk());
                        if(talkPage != null && talkPage.ID != 0) {
                            SetPageLanguage(talkPage, language, true);
                        }
                    }
                }
                old = InsertOld(page, 0);
                page.MinorEdit = (page.UserID == DekiContext.Current.User.ID) && (DateTime.Now < page.TimeStamp.AddMinutes(15));
                page.IsNew = false;
            }

            // update the page information to reflect the new content
            page.Comment = comment;
            page.ContentType = parserResult.ContentType;
            page.Language = language;
            page.UserID = authorId;
            var bodyText = string.Empty;
            if(parserResult.RedirectsToTitle != null) {
                page.IsRedirect = true;
                bodyText = "#REDIRECT [[" + parserResult.RedirectsToTitle.AsPrefixedDbPath() + "]]";
            } else if(parserResult.RedirectsToUri != null) {
                page.IsRedirect = true;
                bodyText = "#REDIRECT [[" + parserResult.RedirectsToUri + "]]";
            } else {
                page.IsRedirect = false;
                bodyText = parserResult.BodyText;
            }
            page.SetText(bodyText);
            page.UseCache = !parserResult.HasScriptContent;
            page.TIP = parserResult.Summary;
            page.Touched = page.TimeStamp = timeStamp;

            // nametype and displayname logic
            if(string.IsNullOrEmpty(displayName) && (page.ID == 0)) {

                // new page created without a title: title comes from the name
                page.Title.DisplayName = page.Title.AsUserFriendlyDisplayName();
            } else if(!string.IsNullOrEmpty(displayName)){

                // title is provided: title set from provided value
                page.Title.DisplayName = displayName;
            } else {
                // preserve the display name of the page
            }

            // Note (arnec): Using Encoding.UTF8 because the default is Encoding.Unicode which produces a different md5sum than expected from ascii
            page.Etag = StringUtil.ComputeHashString(bodyText, Encoding.UTF8);

            // insert or update the page
            if(0 == page.ID) {
                if(restoredPageId == 0) {

                    // only set the revision if this isn't a restore.
                    page.Revision = 1;
                }
                ulong pageId = DbUtils.CurrentSession.Pages_Insert(page, restoredPageId);
                if(pageId != 0) {

                    //TODO Max: the existing page object is being modified with the ID instead of using the new object
                    page.ID = pageId;
                    if(loggingEnabled) {
                        RecentChangeBL.AddNewPageRecentChange(page.TimeStamp, page, DekiContext.Current.User, comment);
                    }

                    // Copy permissions from parent if the page is a child of homepage or Special:
                    ulong parentPageId = page.ParentID;
                    if((parentPageId == 0) && (parent != null) && (page.Title.IsMain || page.Title.IsSpecial)) {
                        parentPageId = parent.ID;
                    }
                    if(parentPageId > 0) {
                        DbUtils.CurrentSession.Grants_CopyToPage(parentPageId, page.ID);
                    }
                    
                    // never log creation of userhomepages to recentchanges
                    if(loggingEnabled && page.Title.IsUser && page.Title.GetParent().IsHomepage) {
                        loggingEnabled = false;
                    }
                }
            } else {
                page.Revision++;
                DbUtils.CurrentSession.Pages_Update(page);
                if(loggingEnabled) {
                    RecentChangeBL.AddEditPageRecentChange(page.TimeStamp, page, DekiContext.Current.User, comment, old);
                }
            }
            try {
                if(null != parserResult.Templates) {
                    ImportTemplatePages(page, parserResult.Templates.ToArray());
                }
                if(null != parserResult.Tags) {
                    ImportPageTags(page, parserResult.Tags.ToArray());
                }
                if(null != parserResult.Links) {
                    UpdateLinks(page, parserResult.Links.ToArray());
                }
            } finally {
                if(isNewForEventContext) {
                    DekiContext.Current.Instance.EventSink.PageCreate(DreamContext.Current.StartTime, page, DekiContext.Current.User);
                } else {
                    DekiContext.Current.Instance.EventSink.PageUpdate(DreamContext.Current.StartTime, page, DekiContext.Current.User);
                }
            }
            return old;
        }
        public bool OverrideTemplate(Site site, PageBE page) {
            if (page.Title.IsTemplate && !String.IsNullOrEmpty(MediaWikiConverterContext.Current.MWTemplatePath)) {
                string templateFilename = GetTemplateFilename(page.Title);
                string templateOriginal = Path.Combine(OriginalTemplateDir, site.Language + "-" + templateFilename);

                // check the template against its base version to see if it's changed
                if (!File.Exists(templateOriginal)) {
                    #region logging
                    if (MediaWikiConverterContext.Current.LoggingEnabled) {
                        log["/html/body/table[@id='outdatedtemplates']"].Add(new XDoc("tr").Elem("td", MWToDWTitle(site, page.Title).AsPrefixedDbPath()));
                    }
                    #endregion
                    File.WriteAllLines(templateOriginal, new string[] { page.TimeStamp.ToString() });
                    File.AppendAllText(templateOriginal, page.GetText(DbUtils.CurrentSession));
                } else {
                    DateTime baseTimestamp = DateTime.MinValue;
                    string[] lines = File.ReadAllLines(templateOriginal);
                    if (0 < lines.Length) {
                        DateTime.TryParse(lines[0], out baseTimestamp);
                    }
                    if (DateTime.MinValue == baseTimestamp || baseTimestamp < page.TimeStamp) {
                        #region logging
                        if (MediaWikiConverterContext.Current.LoggingEnabled) {
                            log["/html/body/table[@id='outdatedtemplates']"].Add(new XDoc("tr").Elem("td", MWToDWTitle(site, page.Title).AsPrefixedDbPath()));
                        }
                        #endregion
                        File.WriteAllLines(templateOriginal, new string[] { page.TimeStamp.ToString() });
                        File.AppendAllText(templateOriginal, page.GetText(DbUtils.CurrentSession));

                    }
                }

                // check if the template's content has been overriden
                string templateOverride = Path.Combine(OverrideTemplateDir, templateFilename);
                if (File.Exists(templateOverride)) {
                    page.SetText(File.ReadAllText(templateOverride));
                    page.ContentType = DekiMimeType.DEKI_TEXT;
                    ParserResult parserResult = DekiXmlParser.Parse(page, ParserMode.SAVE, -1, false);
                    page.IsRedirect = (null != parserResult.RedirectsToTitle) || (null != parserResult.RedirectsToUri);
                    return true;
                }
            }
            return false;
        }
Example #4
0
        private static void MovePageProcessRedirects(List<PageBE> pagesToMove, Dictionary<ulong, Title> newTitlesByPageId, List<PageBE> childRedirectsToRecreate) {

            //Update redirects pointing to the old locations to point to the new
            Dictionary<ulong, IList<PageBE>> redirectsByPages =
                DbUtils.CurrentSession.Pages_GetRedirects(new List<ulong>(from p in pagesToMove select p.ID));

            foreach(PageBE pageToMove in pagesToMove) {
                IList<PageBE> redirects = null;
                if(redirectsByPages.TryGetValue(pageToMove.ID, out redirects)) {
                    foreach(PageBE redirect in redirects) {
                        redirect.SetText("#REDIRECT [[" + newTitlesByPageId[pageToMove.ID].AsPrefixedDbPath() + "]]");
                        DbUtils.CurrentSession.Pages_Update(redirect);
                    }
                }
            }

            // redirects that are children of the moved root page are deleted by Pages_UpdateTitlesForMove and need to be recreated
            pagesToMove.AddRange(childRedirectsToRecreate);

            //NOTE: Pages are sorted before creating redirects to intermediate parent pages arent automatically created
            pagesToMove = pagesToMove.OrderBy(p => p.Title).ToList();
 
            foreach(PageBE movedPage in pagesToMove) {
                string text = string.Empty;
                if(movedPage.IsRedirect) {
                    //This redirect is simply recreated any may be a direct link or in the case when it's pointing to a page that was just moved,
                    //it will point to the previous title for which there's a new redirect.
                    text = movedPage.GetText(DbUtils.CurrentSession);
                } else {
                    text = "#REDIRECT [[" + newTitlesByPageId[movedPage.ID].AsPrefixedDbPath() + "]]";
                }
                PageBE oldPageRedirect = new PageBE();
                oldPageRedirect.Title = movedPage.Title;
                oldPageRedirect.SetText(text);
                PageBL.Save(oldPageRedirect, oldPageRedirect.GetText(DbUtils.CurrentSession), DekiMimeType.DEKI_TEXT, null);
                DbUtils.CurrentSession.Grants_CopyToPage(movedPage.ID, oldPageRedirect.ID);
                DekiContext.Current.Instance.EventSink.PageAliasCreate(DreamContext.Current.StartTime, oldPageRedirect, DekiContext.Current.User);
            }
        }
Example #5
0
 public static void CopyPageToOld(PageBE page, OldBE old) {
     old.Text = page.GetText(DbUtils.CurrentSession);
     old.ContentType = page.ContentType;
     old.UserID = page.UserID;
     old.TimeStamp = page.TimeStamp;
     old.MinorEdit = page.MinorEdit;
     old.Comment = page.Comment;
     old.Language = page.Language;
     old.IsHidden = page.IsHidden;
     old.Meta = page.Meta;
     old.Revision = page.Revision;
     old.PageID = page.ID;
     old.DisplayName = page.Title.DisplayName;
 }
Example #6
0
 public static ParserResult Parse(PageBE page, ParserMode mode) {
     return Parse(page, page.ContentType, page.Language, page.GetText(DbUtils.CurrentSession), mode, false, -1, null, null);
 }
Example #7
0
 public static XDoc CreateParserDocument(PageBE page, ParserMode mode) {
     return CreateParserDocument(page, page.ContentType, page.Language, page.GetText(DbUtils.CurrentSession), mode);
 }
Example #8
0
 public static ParserResult Parse(PageBE page, ParserMode mode, int section, bool isInclude) {
     return Parse(page, page.ContentType, page.Language, page.GetText(DbUtils.CurrentSession), mode, isInclude, section, null, null);
 }
Example #9
0
        public static ParserResult ParseSave(PageBE page, string contentType, string language, string content, int section, string xpath, bool removeIllegalElements, Title relToTitle) {
            XDoc result;
            XDoc contentDoc = ParseContent(content, DekiMimeType.DEKI_TEXT);
            if(null == contentDoc) {
                throw new DekiXmlParserInvalidContentException();
            }

            // convert <nowiki> to <span class="nowiki">
            foreach(XDoc nowiki in contentDoc[".//nowiki"]) {
                nowiki.Rename("span").Attr("class", "nowiki");
            }

            // HACK HACK HACK (steveb): inspect onclick, onfocus, etc. attributes for bogus prefixes (stupid Xinha bug)
            foreach(XDoc attr in contentDoc[".//@onblur | .//@onclick | .//@ondblclick | .//@onfocus | .//@onkeydown | .//@onkeypress | .//@onkeyup | .//@onmousedown | .//@onmousemove | .//@onmouseout | .//@onmouseover | .//@onmouseup"]) {
                string value = attr.Contents;
                while(value.StartsWithInvariantIgnoreCase(UNWANTED_XINHA_CODE_IN_ATTRIBUTES)) {
                    value = value.Substring(UNWANTED_XINHA_CODE_IN_ATTRIBUTES.Length);
                }
                attr.ReplaceValue(value);
            }

            // denormalize links if needed (for import/export)
            if(null != relToTitle) {
                foreach(XDoc addr in contentDoc[".//a[@href.path] | .//img[@src.path]"]) {
                    if(!ExcludedTags.Contains(addr.AsXmlNode.ParentNode, true)) {
                        Title title;
                        if(addr.HasName("a")) {
                            title = Title.FromRelativePath(relToTitle, addr["@href.path"].Contents);
                            addr.RemoveAttr("href.path");
                            title.Anchor = addr["@href.anchor"].AsText;
                            addr.RemoveAttr("href.anchor");
                            title.Query = addr["@href.query"].AsText;
                            addr.RemoveAttr("href.query");
                            title.Filename = addr["@href.filename"].AsText;
                            addr.RemoveAttr("href.filename");
                            addr.Attr("href", title.AsUiUriPath());
                        } else if(addr.HasName("img")) {
                            title = Title.FromRelativePath(relToTitle, addr["@src.path"].Contents);
                            addr.RemoveAttr("src.path");
                            title.Filename = addr["@src.filename"].AsText;
                            addr.RemoveAttr("src.filename");
                            addr.Attr("src", title.AsUiUriPath());
                        } else {
                            DekiContext.Current.Instance.Log.Warn("ParseSave: unexpected element");
                        }
                    }
                }
            }

            // remove local prefixes from links
            foreach(XDoc addr in contentDoc[".//a[@href] | .//img[@src]"]) {
                if(!ExcludedTags.Contains(addr.AsXmlNode.ParentNode, true)) {
                    string attrName = addr.HasName("a") ? "href" : "src";
                    string link = addr["@" + attrName].Contents.Trim();

                    bool hasLocalPrefix = true;
                    if(link.StartsWithInvariantIgnoreCase(Utils.MKS_PATH)) {
                        link = link.Substring(Utils.MKS_PATH.Length);
                    } else if(link.StartsWithInvariantIgnoreCase(DekiContext.Current.UiUri.Uri.SchemeHostPort + "/")) {
                        link = link.Substring(DekiContext.Current.UiUri.Uri.SchemeHostPort.Length + 1);
                    } else {
                        hasLocalPrefix = false;
                    }
                    if(hasLocalPrefix) {
                        if(!link.StartsWith("/")) {
                            link = "/" + link;
                        }
                        addr.Attr(attrName, link);
                    }
                }
            }

            // pass the input through the xml validator
            DekiScriptLibrary.ValidateXHtml(contentDoc["body"], DekiContext.Current.Instance.SafeHtml && !PermissionsBL.IsUserAllowed(DekiContext.Current.User, Permissions.UNSAFECONTENT), removeIllegalElements);
            XDoc validatedDoc = CreateParserDocument(page, contentType, language, string.Empty, ParserMode.SAVE);
            validatedDoc["content/@type"].ReplaceValue(DekiMimeType.DEKI_TEXT);
            validatedDoc[".//body"].Replace(contentDoc[".//body"]);

            // check if we need to remove a trailing <br /> element from <body>
            XDoc body = validatedDoc[".//body"];
            if(!body.IsEmpty) {
                XmlNode last = body.AsXmlNode.LastChild;

                // check if last node is a <table> or <pre> element
                if((last != null) && (last.NodeType == XmlNodeType.Element)) {
                    if(last.LocalName.EqualsInvariant("br")) {

                        // remove <br /> element
                        body[last].Remove();
                    } else if(
                            last.LocalName.EqualsInvariant("p") && (
                                (last.ChildNodes.Count == 0) || (
                                    (last.ChildNodes.Count == 1) && (
                                        ((last.ChildNodes[0].NodeType == XmlNodeType.Text) && (last.ChildNodes[0].Value.Trim().Length == 0)) ||
                                        (last.ChildNodes[0].NodeType == XmlNodeType.SignificantWhitespace) ||
                                        (last.ChildNodes[0].NodeType == XmlNodeType.Whitespace)
                                    )
                                )
                            )
                    ) {

                        // remove <p></p> element
                        body[last].Remove();
                    }
                }
            }

            // if section is negative, update the entire page contents
            if((section < 0) && (null == xpath)) {
                result = validatedDoc;
            } else {

                // convert the existing page and new section contents to a consistent format
                result = CreateParserDocument(page, page.ContentType, page.Language, page.GetText(DbUtils.CurrentSession), ParserMode.RAW);
                XDoc headingDoc = validatedDoc;
                if(DekiMimeType.DEKI_TEXT == contentType) {
                    WikiConverter_TextToXml.Convert(headingDoc);
                }

                if(section < 0) {

                    // if no section was specified, replace the node identified by the xpath
                    if(xpath.StartsWith("/")) {
                        xpath = "." + xpath;
                    }
                    try {
                        result[MAIN_BODY_XPATH][xpath].ReplaceWithNodes(headingDoc[MAIN_BODY_XPATH]);
                    } catch {
                        throw new DekiXmlParserInvalidXPathException();
                    }
                } else if(0 == section) {

                    // append the section to the end of the document
                    result[MAIN_BODY_XPATH].AddNodes(headingDoc[MAIN_BODY_XPATH]);
                } else {

                    // find the specified section number
                    XmlNode startNode, endNode;
                    FindSection(result, section, out startNode, out endNode);
                    if(null == startNode) {
                        throw new SectionParamInvalidArgumentException();
                    }

                    // replace the section contents
                    AddChildrenBefore(startNode, headingDoc[MAIN_BODY_XPATH].AsXmlNode);
                    XmlNode currentNode = startNode;
                    while(currentNode != null && currentNode != endNode) {
                        XmlNode nodeToDelete = currentNode;
                        currentNode = currentNode.NextSibling;
                        nodeToDelete.ParentNode.RemoveChild(nodeToDelete);
                    }
                }
            }
            return Parse(page, result, ParserMode.SAVE, false, null, relToTitle);
        }
Example #10
0
        public static ParserResult ParseGlobalTemplate(PageBE contextPage, PageBE templatePage) {

            // update the parse state to indicate the page is in the process of being parsed
            ParserState parseState = GetParseState();
            string parseStateKey = GetParserCacheKey(contextPage, ParserMode.VIEW_NO_EXECUTE);
            parseState.ProcessedPages[parseStateKey] = null;
            parseState.ProcessingStack.Add(contextPage);

            // set current page in context
            DreamContext context = DreamContext.Current;
            PageBE prevBasePage = context.GetState<PageBE>("CurrentPage");
            CultureInfo prevCulture = context.Culture;
            context.SetState("CurrentPage", contextPage);
            context.Culture = HttpUtil.GetCultureInfoFromHeader(contextPage.Language, context.Culture);

            // parse the page
            ParserResult result = Parse(templatePage, templatePage.ContentType, templatePage.Language, templatePage.GetText(DbUtils.CurrentSession), ParserMode.VIEW, true, -1, DekiScriptNil.Value, null);

            // restore previous page context
            context.SetState("CurrentPage", prevBasePage);
            context.Culture = prevCulture;

            // update the parse state to indicate the page as been successfully parsed
            parseState.ProcessedPages.Remove(parseStateKey);
            parseState.ProcessingStack.RemoveAt(parseState.ProcessingStack.Count - 1);

            // return result
            return result;
        }
 public static void UpdateDWPageData(PageBE page) {
     MediaWikiConverterContext.Current.DWCatalog.NewQuery(String.Format("UPDATE pages set page_usecache={0}, page_tip='{1}', page_content_type='{2}', page_text='{3}' where page_id={4}", page.UseCache, DataCommand.MakeSqlSafe(page.TIP), DataCommand.MakeSqlSafe(page.ContentType), DataCommand.MakeSqlSafe(page.GetText(DbUtils.CurrentSession)), page.ID)).Execute();
 }
Example #12
0
        public static XDoc RetrievePageXDoc(PageBE page, uint pageId, ParserMode mode, string language, bool isInclude, int section, Title relToTitle, bool xhtml, out ParserResult parserResult) {
            uint contextPageId = pageId;
            if((mode == ParserMode.VIEW) && (contextPageId != uint.MaxValue) && page.Title.IsTemplate) {

                // NOTE (steveb): page being rendered is a template and a contextual page was specified; this means we're rendering a global template page
                PageBE contextPage = GetPageById(contextPageId);
                if(contextPage == null) {
                    parserResult = new ParserResult();
                    return null;
                }
                parserResult = DekiXmlParser.ParseGlobalTemplate(contextPage, page);
            } else {
                parserResult = DekiXmlParser.Parse(page, page.ContentType, language ?? page.Language, page.GetText(DbUtils.CurrentSession), mode, isInclude, section, null, relToTitle);
            }
            if(page.Title.IsTemplate && isInclude) {
                DekiXmlParser.PostProcessTemplateInsertBody(parserResult, page);
            }

            // post process tail element
            DekiXmlParser.PostProcessParserResults(parserResult);

            // BUGBUGBUG (steveb): we cannot properly restore an old title unless it had a display title set

            // wrap the result in a content tag and return it to the user
            XDoc result = new XDoc("content")
                    .Attr("type", parserResult.ContentType)
                    .Attr("etag", page.Etag)
                    .Attr("title", page.CustomTitle ?? page.Title.AsUserFriendlyName());

            // check if page contains unsafe content
            if(mode == ParserMode.EDIT) {
                result.Attr("unsafe", !DekiScriptLibrary.VerifyXHtml(parserResult.MainBody, true));
            }

            if(xhtml) {
                result.AddNodes(parserResult.Content);
            } else {

                // encode the result as nodes of text
                foreach(XDoc entry in parserResult.Content.Elements) {
                    if(entry.HasName("body")) {
                        result.Start("body").Attr("target", entry["@target"].AsText).Value(entry.ToInnerXHtml()).End();
                    } else {
                        result.Elem(entry.Name, entry.ToInnerXHtml());
                    }
                }
            }
            return result;
        }