Exemplo n.º 1
0
        /// <summary>
        /// Fully processes a page, applying all needed changes
        /// </summary>
        /// <param name="theArticle">Page to process</param>
        /// <param name="mainProcess">True if the page is being processed for save as usual,
        /// otherwise (Re-parse in context menu, prefetch, etc) false</param>
        private void ProcessPage(Article theArticle, bool mainProcess)
        {
            bool process = true;
            TypoStats = null;

            #if DEBUG
            Variables.Profiler.Start("ProcessPage(\"" + theArticle.Name + "\")");
            #endif

            try
            {
                // Must be performed regardless of general fixes, otherwise there may be breakage
                theArticle.AWBChangeArticleText("Fixes for Unicode compatibility",
                                                Parser.FixUnicode(theArticle.ArticleText),
                                                true);

                if (NoParse.Contains(theArticle.Name))
                    process = false;

                if (!IgnoreNoBots &&
                    !Parsers.CheckNoBots(theArticle.ArticleText, TheSession.User.Name))
                {
                    theArticle.AWBSkip("Restricted by {{bots}}/{{nobots}}");
                    return;
                }

                Variables.Profiler.Profile("Initial skip checks");

                if (CModule.ModuleUsable)
                {
                    theArticle.SendPageToCustomModule(CModule.Module);
                    if (theArticle.SkipArticle) return;
                }

                Variables.Profiler.Profile("Custom module");

                if (ExtProgram.ModuleEnabled)
                {
                    theArticle.SendPageToCustomModule(ExtProgram);
                    if (theArticle.SkipArticle) return;
                }

                Variables.Profiler.Profile("External Program");

                if (Plugin.AWBPlugins.Count > 0)
                {
                    foreach (KeyValuePair<string, IAWBPlugin> a in Plugin.AWBPlugins)
                    {
                        theArticle.SendPageToPlugin(a.Value, this);
                        if (theArticle.SkipArticle) return;
                    }
                }
                Variables.Profiler.Profile("Plugins");

                // unicodify whole article
                if (process && chkUnicodifyWhole.Checked)
                {
                    theArticle.HideMoreText(RemoveText);
                    Variables.Profiler.Profile("HideMoreText");

                    theArticle.Unicodify(Skip.SkipNoUnicode, Parser);
                    Variables.Profiler.Profile("Unicodify");

                    theArticle.UnHideMoreText(RemoveText);
                    Variables.Profiler.Profile("UnHideMoreText");
                }

                // find and replace before general fixes
                if (chkFindandReplace.Checked)
                {
                    theArticle.PerformFindAndReplace(FindAndReplace, SubstTemplates, RplcSpecial,
                                                     chkSkipWhenNoFAR.Checked, chkSkipOnlyMinorFaR.Checked, false);

                    Variables.Profiler.Profile("F&R");

                    theArticle.DoFaRSkips(FindAndReplace);
                    if (theArticle.SkipArticle)
                        return;
                }

                // RegexTypoFix
                if (chkRegExTypo.Checked && RegexTypos != null && !BotMode && !Namespace.IsTalk(theArticle.NameSpaceKey))
                {
                    if (!NoRetf.Contains(theArticle.Name))
                    {
                        theArticle.PerformTypoFixes(RegexTypos, chkSkipIfNoRegexTypo.Checked);
                        Variables.Profiler.Profile("Typos");
                        TypoStats = RegexTypos.GetStatistics();
                    }
                    else if (chkSkipIfNoRegexTypo.Checked)
                        TheArticle.Trace.AWBSkipped("No typo fixes (Title blacklisted from RegExTypoFix Typo Fixing)");

                    if (theArticle.SkipArticle)
                    {
                        if (mainProcess)
                        {
                            // update stats only if not called from e.g. 'Re-parse' than could be clicked repeatedly
                            OverallTypoStats.UpdateStats(TypoStats, true);
                            UpdateTypoCount();
                        }
                    }
                }

                // replace/add/remove categories
                if (cmboCategorise.SelectedIndex != 0)
                {
                    theArticle.Categorisation((WikiFunctions.Options.CategorisationOptions)
                                              cmboCategorise.SelectedIndex, Parser, chkSkipNoCatChange.Checked,
                                              txtNewCategory.Text.Trim(),
                                              txtNewCategory2.Text.Trim(), chkRemoveSortKey.Checked);
                    if (theArticle.SkipArticle)
                        return;
                    else if (!chkGeneralFixes.Checked)
                        theArticle.AWBChangeArticleText("Fix categories",
                                                        Parsers.FixCategories(theArticle.ArticleText), true);
                }

                Variables.Profiler.Profile("Categories");

                if (process)
                {
                    if (chkGeneralFixes.Checked)
                    {
                        theArticle.PerformUniversalGeneralFixes();
                        Variables.Profiler.Profile("Universal Genfixes");
                    }

                    if (theArticle.CanDoGeneralFixes)
                    {
                        if (chkGeneralFixes.Checked)
                        {
                            if (!TemplateRedirectsLoaded)
                            {
                                LoadTemplateRedirects();
                                Variables.Profiler.Profile("LoadTemplateRedirects");
                            }

                            if (!DatedTemplatesLoaded)
                            {
                                LoadDatedTemplates();
                                Variables.Profiler.Profile("LoadDatedTemplates");
                            }

                            if (!RenamedTemplateParametersLoaded)
                            {
                                LoadRenameTemplateParameters();
                                Variables.Profiler.Profile("LoadRenameTemplateParameters");
                            }

                            theArticle.PerformGeneralFixes(Parser, RemoveText, Skip,
                                                           replaceReferenceTagsToolStripMenuItem.Checked,
                                                           restrictDefaultsortChangesToolStripMenuItem.Checked,
                                                           noMOSComplianceFixesToolStripMenuItem.Checked);
                        }
                        Variables.Profiler.Profile("Mainspace Genfixes");

                        // auto tag
                        if (chkAutoTagger.Checked)
                        {
                            theArticle.AutoTag(Parser, Skip.SkipNoTag, restrictOrphanTaggingToolStripMenuItem.Checked);

                            if (mainProcess && theArticle.SkipArticle)
                                return;
                        }

                        Variables.Profiler.Profile("Auto-tagger");
                    }
                    else if (chkGeneralFixes.Checked)
                    {
                        if (theArticle.NameSpaceKey == Namespace.UserTalk)
                        {
                            if (!UserTalkWarningsLoaded)
                            {
                                LoadUserTalkWarnings();
                                Variables.Profiler.Profile("loadUserTalkWarnings");
                            }

                            theArticle.PerformUserTalkGeneralFixes(RemoveText, UserTalkTemplatesRegex,
                                                                   Skip.SkipNoUserTalkTemplatesSubstd);
                        }
                        else if (theArticle.CanDoTalkGeneralFixes)
                        {
                            theArticle.PerformTalkGeneralFixes(RemoveText);
                        }
                        Variables.Profiler.Profile("Talk Genfixes");
                    }
                }

                // find and replace after general fixes
                if (chkFindandReplace.Checked)
                {
                    theArticle.PerformFindAndReplace(FindAndReplace, SubstTemplates, RplcSpecial,
                                                     chkSkipWhenNoFAR.Checked, chkSkipOnlyMinorFaR.Checked, true);

                    theArticle.DoFaRSkips(FindAndReplace);

                    Variables.Profiler.Profile("F&R (2nd)");

                    if (theArticle.SkipArticle) return;
                }

                // append/prepend text
                if (chkAppend.Checked)
                {
                    // customized number of newlines
                    string newlines = "";
                    for (int i = 0; i < (int)udNewlineChars.Value; i++)
                        newlines += "\n";

                    if (rdoAppend.Checked)
                        theArticle.AWBChangeArticleText("Appended your message",
                                                        theArticle.ArticleText + newlines + Tools.ApplyKeyWords(theArticle.Name, txtAppendMessage.Text), false);
                    else
                        theArticle.AWBChangeArticleText("Prepended your message",
                                                        Tools.ApplyKeyWords(theArticle.Name, txtAppendMessage.Text) + newlines + theArticle.ArticleText, false);

                    if (chkAppendMetaDataSort.Checked)
                        theArticle.PerformMetaDataSort(Parser);
                }

                Variables.Profiler.Profile("Append Text");

                // replace/remove/comment out images
                if (cmboImages.SelectedIndex != 0)
                {
                    theArticle.UpdateImages((WikiFunctions.Options.ImageReplaceOptions)cmboImages.SelectedIndex,
                                            txtImageReplace.Text, txtImageWith.Text, chkSkipNoImgChange.Checked);
                    if (theArticle.SkipArticle)
                        return;
                }

                Variables.Profiler.Profile("Images");

                // disambiguation
                if (!preParseModeToolStripMenuItem.Checked && chkEnableDab.Checked && txtDabLink.Text.Trim().Length > 0 &&
                    txtDabVariants.Text.Trim().Length > 0)
                {
                    if (theArticle.Disambiguate(TheSession, txtDabLink.Text.Trim(), txtDabVariants.Lines, BotMode,
                                                (int)udContextChars.Value, chkSkipNoDab.Checked))
                    {
                        if (theArticle.SkipArticle)
                            return;
                    }
                    else
                    {
                        Abort = true;
                        Stop();
                        return;
                    }
                }
                Variables.Profiler.Profile("Disambiguate");
            }
            catch (Exception ex)
            {
                ErrorHandler.Handle(ex);

                // don't remove page over regex error – page itself is not at fault
                if (!ex.StackTrace.Contains("System.Text.RegularExpressions"))
                    theArticle.Trace.AWBSkipped("Exception:" + ex.Message);
                else
                    Skippable = false;
                Stop();
                StopDelayedAutoSaveTimer();
            }
            finally
            {
                Variables.Profiler.Flush();
            }
        }