示例#1
0
        public async Task <IActionResult> OnGetDownloadSqliteDbAsync(int id)
        {
            using (HttpClient secureClient = new HttpClient())
            {
                LastResult = "";
                if (false == await PreparePoet())
                {
                    return(BadRequest(LastResult));
                }

                await GanjoorSessionChecker.PrepareClient(secureClient, Request, Response);

                var content = await secureClient.GetStreamAsync($"{APIRoot.Url}/api/ganjoor/sqlite/export/{id}");

                var contentType = "Application/octet-stream";
                var fileName    = $"{GPersianTextSync.Farglisize(Poet.Name)}.gdb";
                return(File(content, contentType, fileName));
            }
        }
示例#2
0
        private async Task <string> _ImportSQLiteCatChildren(RMuseumDbContext context, IDbConnection sqlite, int poetId, int sqliteParentCatId, GanjoorCat parentCat, string parentFullTitle, LongRunningJobProgressServiceEF jobProgressServiceEF, RLongRunningJobStatus job, int parentPagId)
        {
            try
            {
                string catHtmlText = "";
                foreach (var cat in await sqlite.QueryAsync($"SELECT * FROM cat WHERE parent_id = {sqliteParentCatId} ORDER BY id"))
                {
                    await jobProgressServiceEF.UpdateJob(job.Id, 0, $"Importing - {cat.text}");

                    var poetCatId = 1 + await context.GanjoorCategories.MaxAsync(c => c.Id);

                    string catTitle = cat.text;

                    string url = GPersianTextSync.Farglisize(catTitle);
                    if (catTitle.IndexOf('|') != -1)
                    {
                        string[] catParts = catTitle.Split('|', StringSplitOptions.RemoveEmptyEntries);
                        if (catParts.Length == 2)
                        {
                            catTitle = catParts[0].Trim();
                            url      = catParts[1].Trim();
                        }
                    }
                    else
                    {
                        switch (catTitle)
                        {
                        case "دیوان اشعار":
                            url = "divan";
                            break;

                        case "قصاید":
                        case "قصائد":
                        case "قصیده":
                        case "قصیده ها":
                            url = "ghaside";
                            break;

                        case "غزلیات":
                        case "غزل":
                        case "غزل ها":
                            url = "ghazal";
                            break;

                        case "قطعات":
                        case "مقطعات":
                        case "قطعه":
                            url = "ghete";
                            break;

                        case "مثنویات":
                        case "مثنوی":
                        case "مثنوی ها":
                            url = "masnavi";
                            break;

                        case "ترکیبات":
                        case "ترکیب بند":
                            url = "tarkib";
                            break;

                        case "ترجیعات":
                        case "ترجیع بند":
                            url = "tarjee";
                            break;

                        case "مسمطات":
                        case "مسمط":
                            url = "mosammat";
                            break;

                        case "مخمسات":
                        case "مخمس":
                            url = "mokhammas";
                            break;

                        case "رباعیات":
                        case "رباعی":
                        case "رباعی ها":
                            url = "robaee";
                            break;

                        case "ملمعات":
                        case "ملمع":
                            url = "molamma";
                            break;

                        case "هجویات":
                        case "هجو":
                            url = "hajv";
                            break;

                        case "هزلیات":
                        case "هزل":
                            url = "hazl";
                            break;

                        case "مراثی":
                        case "مرثیه":
                        case "رثا":
                        case "مرثیه ها":
                            url = "marsie";
                            break;

                        case "مفردات":
                            url = "mofradat";
                            break;

                        case "ملحقات":
                            url = "molhaghat";
                            break;

                        case "اشعار عربی":
                            url = "arabi";
                            break;

                        case "ماده تاریخ‌ها":
                        case "ماده تاریخها":
                        case "ماده تاریخ":
                            url = "tarikh";
                            break;

                        case "معمیات":
                            url = "moammiyat";
                            break;

                        case "چیستان":
                            url = "chistan";
                            break;

                        case "لغز":
                        case "لغزها":
                            url = "loghaz";
                            break;
                        }
                    }

                    GanjoorCat dbCat = new GanjoorCat()
                    {
                        Id                   = poetCatId,
                        PoetId               = poetId,
                        Title                = catTitle,
                        UrlSlug              = url,
                        FullUrl              = $"{parentCat.FullUrl}/{url}",
                        ParentId             = parentCat.Id,
                        TableOfContentsStyle = GanjoorTOC.Analyse,
                        Published            = true,
                    };
                    context.GanjoorCategories.Add(dbCat);

                    var maxPageId = await context.GanjoorPages.MaxAsync(p => p.Id);

                    if (await context.GanjoorPoems.MaxAsync(p => p.Id) > maxPageId)
                    {
                        maxPageId = await context.GanjoorPoems.MaxAsync(p => p.Id);
                    }

                    var catPageId = 1 + maxPageId;
                    while (await context.GanjoorPoems.Where(p => p.Id == catPageId).AnyAsync())
                    {
                        catPageId++;
                    }

                    GanjoorPage dbPageCat = new GanjoorPage()
                    {
                        Id = catPageId,
                        GanjoorPageType = GanjoorPageType.CatPage,
                        Published       = false,
                        PageOrder       = -1,
                        Title           = dbCat.Title,
                        FullTitle       = $"{parentFullTitle} » {dbCat.Title}",
                        UrlSlug         = dbCat.UrlSlug,
                        FullUrl         = dbCat.FullUrl,
                        HtmlText        = "",
                        PoetId          = poetId,
                        CatId           = poetCatId,
                        PostDate        = DateTime.Now,
                        ParentId        = parentPagId
                    };

                    context.GanjoorPages.Add(dbPageCat);

                    await context.SaveChangesAsync();

                    catHtmlText += $"<p><a href=\"{dbCat.FullUrl}\">{dbCat.Title}</a></p>{Environment.NewLine}";

                    var resChild = await _ImportSQLiteCatChildren(context, sqlite, poetId, (int)cat.id, dbCat, $"{parentFullTitle} » {dbCat.Title}", jobProgressServiceEF, job, dbPageCat.Id);

                    if (!string.IsNullOrEmpty(resChild))
                    {
                        return(resChild);
                    }
                }
                var maxPoemId = await context.GanjoorPoems.MaxAsync(p => p.Id);

                if (await context.GanjoorPages.MaxAsync(p => p.Id) > maxPoemId)
                {
                    maxPoemId = await context.GanjoorPages.MaxAsync(p => p.Id);
                }
                var poemId = 1 + maxPoemId;

                int poemNumber = 0;
                foreach (var poem in await sqlite.QueryAsync($"SELECT * FROM poem WHERE cat_id = {sqliteParentCatId} ORDER BY id"))
                {
                    poemNumber++;
                    await jobProgressServiceEF.UpdateJob(job.Id, poemNumber, "", false);

                    string title   = poem.title;
                    string urlSlug = $"sh{poemNumber}";
                    if (title.IndexOf('|') != -1)
                    {
                        string[] titleParts = title.Split('|', StringSplitOptions.RemoveEmptyEntries);
                        if (titleParts.Length == 2)
                        {
                            title   = titleParts[0].Trim();
                            urlSlug = titleParts[1].Trim();
                        }
                    }


                    GanjoorPoem dbPoem = new GanjoorPoem()
                    {
                        Id        = poemId,
                        CatId     = parentCat.Id,
                        Title     = title,
                        UrlSlug   = urlSlug,
                        FullTitle = $"{parentFullTitle} » {title}",
                        FullUrl   = $"{parentCat.FullUrl}/{urlSlug}",
                        Published = true,
                    };

                    List <GanjoorVerse> poemVerses = new List <GanjoorVerse>();
                    foreach (var verse in await sqlite.QueryAsync($"SELECT * FROM verse WHERE poem_id = {poem.id} ORDER BY vorder"))
                    {
                        int          vOrder   = int.Parse(verse.vorder.ToString());
                        int          position = int.Parse(verse.position.ToString());
                        string       text     = verse.text;
                        GanjoorVerse dbVerse  = new GanjoorVerse()
                        {
                            PoemId        = poemId,
                            VOrder        = vOrder,
                            VersePosition = (VersePosition)position,
                            Text          = text.Replace("ـ", "").Replace("  ", " ").ApplyCorrectYeKe().Trim()
                        };
                        poemVerses.Add(dbVerse);
                    }

                    if (poemVerses.Count == 0)
                    {
                        poemNumber--;
                        continue;
                    }

                    dbPoem.PlainText = PreparePlainText(poemVerses);
                    dbPoem.HtmlText  = PrepareHtmlText(poemVerses);

                    context.GanjoorPoems.Add(dbPoem);
                    await context.SaveChangesAsync();

                    foreach (var dbVerse in poemVerses)
                    {
                        context.GanjoorVerses.Add(dbVerse);
                        await context.SaveChangesAsync();//id set should be in order
                    }

                    await _FillPoemCoupletIndices(context, poemId);

                    try
                    {
                        var poemRhymeLettersRes = LanguageUtils.FindRhyme(poemVerses);
                        if (!string.IsNullOrEmpty(poemRhymeLettersRes.Rhyme))
                        {
                            dbPoem.RhymeLetters = poemRhymeLettersRes.Rhyme;
                            context.GanjoorPoems.Update(dbPoem);
                        }
                    }
                    catch
                    {
                    }



                    GanjoorPage dbPoemPage = new GanjoorPage()
                    {
                        Id = poemId,
                        GanjoorPageType = GanjoorPageType.PoemPage,
                        Published       = false,
                        PageOrder       = -1,
                        Title           = dbPoem.Title,
                        FullTitle       = dbPoem.FullTitle,
                        UrlSlug         = dbPoem.UrlSlug,
                        FullUrl         = dbPoem.FullUrl,
                        HtmlText        = dbPoem.HtmlText,
                        PoetId          = poetId,
                        CatId           = parentCat.Id,
                        PoemId          = poemId,
                        PostDate        = DateTime.Now,
                        ParentId        = parentPagId
                    };

                    context.GanjoorPages.Add(dbPoemPage);
                    await context.SaveChangesAsync();

                    catHtmlText += $"<p><a href=\"{dbPoemPage.FullUrl}\">{dbPoemPage.Title}</a></p>{Environment.NewLine}";

                    poemId++;
                }

                if (!string.IsNullOrEmpty(catHtmlText))
                {
                    var parentCatPage = await context.GanjoorPages.Where(p => p.FullUrl == parentCat.FullUrl).SingleAsync();

                    parentCatPage.HtmlText += catHtmlText;
                    context.GanjoorPages.Update(parentCatPage);
                }

                await context.SaveChangesAsync();
            }
            catch (Exception exp)
            {
                return(exp.ToString());
            }
            return("");
        }
        private async Task <RServiceResult <string> > _GenerateTableOfContents(RMuseumDbContext context, int catId, GanjoorTOC options)
        {
            string html    = "";
            var    subCats = await context.GanjoorCategories.Where(c => c.ParentId == catId).OrderBy(c => c.Id).AsNoTracking().ToArrayAsync();

            foreach (var cat in subCats)
            {
                html += $"<p><a href=\"{cat.FullUrl}\">{cat.Title}</a></p>{Environment.NewLine}";
            }

            var poems = await context.GanjoorPoems.Where(p => p.CatId == catId).OrderBy(p => p.Id).AsNoTracking().ToArrayAsync();

            if (poems.Length > 0)
            {
                if (options == GanjoorTOC.Analyse)
                {
                    if (poems.Where(p => !string.IsNullOrEmpty(p.RhymeLetters)).Count() * 100 / poems.Length > 50)
                    {
                        options = GanjoorTOC.AlphabeticWithFirstVerse;
                    }
                    else
                    {
                        options = GanjoorTOC.TitlesAndFirstVerse;
                    }
                }
            }

            if
            (
                options == GanjoorTOC.AlphabeticWithFirstCouplet
                ||
                options == GanjoorTOC.AlphabeticWithFirstVerse
                ||
                options == GanjoorTOC.AlphabeticWithSecondVerse
            )
            {
                var taggedPoems = poems.Where(p => !string.IsNullOrEmpty(p.RhymeLetters)).ToArray();
                if (taggedPoems.Length > 0)
                {
                    html += $"<p>فهرست شعرها به ترتیب آخر حرف قافیه گردآوری شده است. برای پیدا کردن یک شعر کافی است حرف آخر قافیهٔ آن را در نظر بگیرید تا بتوانید آن  را پیدا کنید.</p>{Environment.NewLine}";
                    var randomPoem       = taggedPoems[new Random(DateTime.Now.Millisecond).Next(taggedPoems.Length)];
                    var randomPoemVerses = await context.GanjoorVerses.AsNoTracking().Where(p => p.PoemId == randomPoem.Id).OrderBy(v => v.VOrder).ToArrayAsync();

                    if (randomPoemVerses.Length > 2)
                    {
                        html += $"<p>مثلاً برای پیدا کردن شعری که مصرع <em>{randomPoemVerses[1].Text}</em> مصرع دوم یکی از بیتهای آن است باید شعرهایی را نگاه کنید که آخر حرف قافیهٔ آنها «<em><a href=\"#{ GPersianTextSync.UniquelyFarglisize(randomPoem.RhymeLetters.Substring(randomPoem.RhymeLetters.Length - 1)) }\">{randomPoem.RhymeLetters.Substring(randomPoem.RhymeLetters.Length - 1)}</a></em>» است.</p>{Environment.NewLine}";
                    }

                    html += $"<h3><a id=\"index\">دسترسی سریع به حروف</a></h3>{Environment.NewLine}";
                    html += $"<p>{Environment.NewLine}";
                    string        lastChar         = "";
                    List <string> visitedLastChart = new List <string>();
                    foreach (var poem in taggedPoems)
                    {
                        string poemLastChar = poem.RhymeLetters.Substring(poem.RhymeLetters.Length - 1);
                        if (poemLastChar != lastChar)
                        {
                            if (visitedLastChart.IndexOf(poemLastChar) == -1)
                            {
                                if (lastChar != "")
                                {
                                    html += " | ";
                                }
                                html    += $"<a href=\"#{GPersianTextSync.UniquelyFarglisize(poemLastChar)}\">{poemLastChar}</a>";
                                lastChar = poemLastChar;

                                visitedLastChart.Add(poemLastChar);
                            }
                        }
                    }
                    html += $"</p>{Environment.NewLine}";
                }
            }

            string        last        = "";
            List <string> visitedLast = new List <string>();

            foreach (var poem in poems)
            {
                if
                (
                    options == GanjoorTOC.AlphabeticWithFirstCouplet
                    ||
                    options == GanjoorTOC.AlphabeticWithFirstVerse
                    ||
                    options == GanjoorTOC.AlphabeticWithSecondVerse
                )
                {
                    if (!string.IsNullOrEmpty(poem.RhymeLetters))
                    {
                        string poemLast = poem.RhymeLetters.Substring(poem.RhymeLetters.Length - 1);
                        if (poemLast != last)
                        {
                            if (visitedLast.IndexOf(poemLast) == -1)
                            {
                                html += $"<h3><a href=\"#index\" id=\"{GPersianTextSync.UniquelyFarglisize(poemLast)}\">{poemLast}</a></h3>{Environment.NewLine}";
                                last  = poemLast;
                                visitedLast.Add(poemLast);
                            }
                        }
                    }
                }

                html += $"<p><a href=\"{poem.FullUrl}\">{poem.Title}</a>";

                var verses = await context.GanjoorVerses.AsNoTracking().Where(p => p.PoemId == poem.Id).OrderBy(v => v.VOrder).ToArrayAsync();

                if (verses.Length > 0)
                {
                    if (options == GanjoorTOC.TitlesAndFirstVerse || options == GanjoorTOC.AlphabeticWithFirstVerse)
                    {
                        html += $": {verses[0].Text}";
                    }
                    else
                    if (options == GanjoorTOC.AlphabeticWithSecondVerse || options == GanjoorTOC.TitlesAndSecondVerse)
                    {
                        if (verses.Length > 1)
                        {
                            html += $": {verses[1].Text}";
                        }
                        else
                        {
                            html += $": {verses[0].Text}";
                        }
                    }
                    else
                    if (options == GanjoorTOC.AlphabeticWithFirstCouplet || options == GanjoorTOC.TitlesAndFirstCouplet)
                    {
                        if (verses.Length > 1)
                        {
                            html += $": {verses[0].Text} - {verses[1].Text}";
                        }
                        else
                        {
                            html += $": {verses[0].Text}";
                        }
                    }
                    else
                    if (options == GanjoorTOC.TitlesAndFirstCenteredVerse)
                    {
                        if (verses.Where(v => v.VersePosition == VersePosition.CenteredVerse1).Any())
                        {
                            html += $": {verses.Where(v => v.VersePosition == VersePosition.CenteredVerse1).First().Text}";
                        }
                        else
                        {
                            html += $": {verses[0].Text}";
                        }
                    }
                    else
                    if (options == GanjoorTOC.TitlesAndFirstCenteredCouplet)
                    {
                        if (
                            verses.Where(v => v.VersePosition == VersePosition.CenteredVerse1).Any()
                            &&
                            verses.Where(v => v.VersePosition == VersePosition.CenteredVerse2).Any()
                            )
                        {
                            html += $": {verses.Where(v => v.VersePosition == VersePosition.CenteredVerse1).First().Text} - {verses.Where(v => v.VersePosition == VersePosition.CenteredVerse2).First().Text}";
                        }
                        else
                        {
                            html += $": {verses[0].Text}";
                        }
                    }
                }


                html += $"</p>{Environment.NewLine}";
            }
            return(new RServiceResult <string>(html));
        }