<commit_before>using System;
using System.Net.Mail;
using System.Text.RegularExpressions;
using System.Web;
using umbraco.BusinessLogic;
using umbraco.cms.businesslogic.language;
using umbraco.cms.businesslogic.property;
using umbraco.cms.businesslogic.task;
using umbraco.cms.businesslogic.web;
using umbraco.IO;

namespace umbraco.cms.businesslogic.translation
{
    public class Translation
    {
        public static void MakeNew(CMSNode Node, User User, User Translator, Language Language, string Comment,
                                   bool IncludeSubpages, bool SendEmail)
        {
            // Create pending task
            Task t = new Task();
            t.Comment = Comment;
            t.Node = Node;
            t.ParentUser = User;
            t.User = Translator;
            t.Type = new TaskType("toTranslate");
            t.Save();

            // Add log entry
            Log.Add(LogTypes.SendToTranslate, User, Node.Id,
                    "Translator: " + Translator.Name + ", Language: " + Language.FriendlyName);

            // send it
            if (SendEmail)
            {
                string serverName = HttpContext.Current.Request.ServerVariables["SERVER_NAME"];
                int port = HttpContext.Current.Request.Url.Port;

                if(port != 80)
                    serverName += ":" + port.ToString();

                serverName += SystemDirectories.Umbraco;

                // Send mail
                string[] subjectVars = {serverName, Node.Text};
                string[] bodyVars = {
                                        Translator.Name, Node.Text, User.Name,
                                        serverName, Node.Id.ToString(),
                                        Language.FriendlyName
                                    };

                if (User.Email != "" && User.Email.Contains("@") && Translator.Email != "" &&
                    Translator.Email.Contains("@"))
                {
                    // create the mail message 
                    MailMessage mail = new MailMessage(User.Email, Translator.Email);

                    // populate the message
                    mail.Subject = ui.Text("translation", "mailSubject", subjectVars, Translator);
                    mail.IsBodyHtml = false;
                    mail.Body = ui.Text("translation", "mailBody", bodyVars, Translator);
                    try
                    {
                        SmtpClient sender = new SmtpClient();
                        sender.Send(mail);
                    }
                    catch (Exception ex)
                    {
                        Log.Add(LogTypes.Error, User, Node.Id,
                                string.Format("Error sending translation e-mail:{0}", ex.ToString()));
                    }
                }
                else
                    Log.Add(LogTypes.Error, User, Node.Id,
                            "Could not send translation e-mail because either user or translator lacks e-mail in settings");
            }

            if (IncludeSubpages)
            {
                //store children array here because iterating over an Array property object is very inneficient.
                var c = Node.Children;
                foreach (CMSNode n in c)
                {
                    MakeNew(n, User, Translator, Language, Comment, true, false);
                }
            }
        }

        public static int CountWords(int DocumentId)
        {
            Document d = new Document(DocumentId);

            int words = CountWordsInString(d.Text);
            var props = d.getProperties;
            foreach (Property p in props)
            {
                if (p.Value.GetType() == "".GetType())
                {
                    if (p.Value.ToString().Trim() != "")
                        words += CountWordsInString(p.Value.ToString());
                }
            }

            return words;
        }

        private static int CountWordsInString(string Text)
        {
            string pattern = @"<(.|\n)*?>";
            string tmpStr = Regex.Replace(Text, pattern, string.Empty);

            tmpStr = tmpStr.Replace("\t", " ").Trim();
            tmpStr = tmpStr.Replace("\n", " ");
            tmpStr = tmpStr.Replace("\r", " ");

            while (tmpStr.IndexOf("  ") != -1)
                tmpStr = tmpStr.Replace("  ", " ");

            return tmpStr.Split(' ').Length;
        }
    }
}<commit_msg>Fixing 25454: Exporting translation could end up in infinite loop<commit_after>using System;
using System.Net.Mail;
using System.Text.RegularExpressions;
using System.Web;
using umbraco.BusinessLogic;
using umbraco.cms.businesslogic.language;
using umbraco.cms.businesslogic.property;
using umbraco.cms.businesslogic.task;
using umbraco.cms.businesslogic.web;
using umbraco.IO;

namespace umbraco.cms.businesslogic.translation
{
    public class Translation
    {
        public static void MakeNew(CMSNode Node, User User, User Translator, Language Language, string Comment,
                                   bool IncludeSubpages, bool SendEmail)
        {
            // Create pending task
            Task t = new Task();
            t.Comment = Comment;
            t.Node = Node;
            t.ParentUser = User;
            t.User = Translator;
            t.Type = new TaskType("toTranslate");
            t.Save();

            // Add log entry
            Log.Add(LogTypes.SendToTranslate, User, Node.Id,
                    "Translator: " + Translator.Name + ", Language: " + Language.FriendlyName);

            // send it
            if (SendEmail)
            {
                string serverName = HttpContext.Current.Request.ServerVariables["SERVER_NAME"];
                int port = HttpContext.Current.Request.Url.Port;

                if(port != 80)
                    serverName += ":" + port.ToString();

                serverName += SystemDirectories.Umbraco;

                // Send mail
                string[] subjectVars = {serverName, Node.Text};
                string[] bodyVars = {
                                        Translator.Name, Node.Text, User.Name,
                                        serverName, Node.Id.ToString(),
                                        Language.FriendlyName
                                    };

                if (User.Email != "" && User.Email.Contains("@") && Translator.Email != "" &&
                    Translator.Email.Contains("@"))
                {
                    // create the mail message 
                    MailMessage mail = new MailMessage(User.Email, Translator.Email);

                    // populate the message
                    mail.Subject = ui.Text("translation", "mailSubject", subjectVars, Translator);
                    mail.IsBodyHtml = false;
                    mail.Body = ui.Text("translation", "mailBody", bodyVars, Translator);
                    try
                    {
                        SmtpClient sender = new SmtpClient();
                        sender.Send(mail);
                    }
                    catch (Exception ex)
                    {
                        Log.Add(LogTypes.Error, User, Node.Id,
                                string.Format("Error sending translation e-mail:{0}", ex.ToString()));
                    }
                }
                else
                    Log.Add(LogTypes.Error, User, Node.Id,
                            "Could not send translation e-mail because either user or translator lacks e-mail in settings");
            }

            if (IncludeSubpages)
            {
                //store children array here because iterating over an Array property object is very inneficient.
                var c = Node.Children;
                foreach (CMSNode n in c)
                {
                    MakeNew(n, User, Translator, Language, Comment, true, false);
                }
            }
        }

        public static int CountWords(int DocumentId)
        {
            Document d = new Document(DocumentId);

            int words = CountWordsInString(d.Text);
            var props = d.getProperties;
            foreach (Property p in props)
            {
                if (p.Value.GetType() == "".GetType())
                {
                    if (p.Value.ToString().Trim() != "")
                        words += CountWordsInString(p.Value.ToString());
                }
            }

            return words;
        }

        private static int CountWordsInString(string Text)
        {
            string pattern = @"<(.|\n)*?>";
            string tmpStr = Regex.Replace(Text, pattern, string.Empty);

            tmpStr = tmpStr.Replace("\t", " ").Trim();
            tmpStr = tmpStr.Replace("\n", " ");
            tmpStr = tmpStr.Replace("\r", " ");

            MatchCollection collection = Regex.Matches(tmpStr, @"[\S]+");
            return collection.Count; 
        }
    }
}
{'fraction_non_alphanumeric': 0.09912061665400065, 'fraction_numerical': 0.0013027901422212572, 'mean_word_length': 1.2235095341539948, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5708214', 'n_tokens_mistral': 2360, 'n_tokens_neox': 2112, 'n_words': 513}