#215 find and fix comments with long urls embedded in their text

This commit is contained in:
Hamid Reza Mohammadi 2021-10-02 11:56:43 +03:30
parent 639ec9e4aa
commit 080c5a173b
5 changed files with 141 additions and 2 deletions

View File

@ -8,8 +8,8 @@
},
"AllowedHosts": "*",
"GoogleAnalyticsCode": "",
"APIRoot": "https://ganjgah.ir",
"GlobalAPIRoot": "https://ganjgah.ir",
"APIRoot": "http://localhost:3439",
"GlobalAPIRoot": "http://localhost:3439",
"SiteUrl": "http://localhost:33081",
"ReadOnlyMode": "False"
}

View File

@ -1792,6 +1792,24 @@ namespace RMuseum.Controllers
return BadRequest(res.ExceptionString);
}
/// <summary>
/// examine comments for long links
/// </summary>
/// <returns></returns>
[HttpPost]
[Route("fixlongurlsincomments")]
[Authorize(Policy = RMuseumSecurableItem.GanjoorEntityShortName + ":" + RMuseumSecurableItem.ImportOperationShortName)]
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(bool))]
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
public IActionResult FindAndFixLongUrlsInComments()
{
RServiceResult<bool> res =
_ganjoorService.FindAndFixLongUrlsInComments();
if (res.Result)
return Ok();
return BadRequest(res.ExceptionString);
}
/// <summary>
/// separate verses in poem.PlainText with Environment.NewLine instead of SPACE
/// </summary>

View File

@ -1186,6 +1186,12 @@
</summary>
<returns></returns>
</member>
<member name="M:RMuseum.Controllers.GanjoorController.FindAndFixLongUrlsInComments">
<summary>
examine comments for long links
</summary>
<returns></returns>
</member>
<member name="M:RMuseum.Controllers.GanjoorController.RegerneratePoemsPlainText">
<summary>
separate verses in poem.PlainText with Environment.NewLine instead of SPACE
@ -8570,6 +8576,12 @@
</summary>
<returns></returns>
</member>
<member name="M:RMuseum.Services.IGanjoorService.FindAndFixLongUrlsInComments">
<summary>
examine comments for long links
</summary>
<returns></returns>
</member>
<member name="M:RMuseum.Services.IGanjoorService.RegerneratePoemsPlainText">
<summary>
separate verses in poem.PlainText with Environment.NewLine instead of SPACE
@ -9896,6 +9908,12 @@
</summary>
<returns></returns>
</member>
<member name="M:RMuseum.Services.Implementation.GanjoorService.FindAndFixLongUrlsInComments">
<summary>
examine comments for long links
</summary>
<returns></returns>
</member>
<member name="M:RMuseum.Services.Implementation.GanjoorService.StartUpdatingMundexPage(System.Guid)">
<summary>
start updating stats page

View File

@ -440,6 +440,12 @@ namespace RMuseum.Services
/// <returns></returns>
RServiceResult<bool> HealthCheckContents();
/// <summary>
/// examine comments for long links
/// </summary>
/// <returns></returns>
RServiceResult<bool> FindAndFixLongUrlsInComments();
/// <summary>
/// separate verses in poem.PlainText with Environment.NewLine instead of SPACE
/// </summary>

View File

@ -506,5 +506,102 @@ namespace RMuseum.Services.Implementation
return new RServiceResult<bool>(true);
}
/// <summary>
/// examine comments for long links
/// </summary>
/// <returns></returns>
public RServiceResult<bool> FindAndFixLongUrlsInComments()
{
_backgroundTaskQueue.QueueBackgroundWorkItem
(
async token =>
{
using (RMuseumDbContext context = new RMuseumDbContext(new DbContextOptions<RMuseumDbContext>())) //this is long running job, so _context might be already been freed/collected by GC
{
LongRunningJobProgressServiceEF jobProgressServiceEF = new LongRunningJobProgressServiceEF(context);
var job = (await jobProgressServiceEF.NewJob("FindAndFixLongUrlsInComments", "Query data")).Result;
try
{
var comments = await context.GanjoorComments.Where(c => c.HtmlComment.Contains("href=")).ToArrayAsync();
await jobProgressServiceEF.UpdateJob(job.Id, 0, $"Examining {comments.Length} Comments");
int percent = 0;
for (int i = 0; i < comments.Length; i++)
{
if (i * 100 / comments.Length > percent)
{
percent++;
await jobProgressServiceEF.UpdateJob(job.Id, percent);
}
var comment = comments[i];
string commentText = comment.HtmlComment;
int index = commentText.IndexOf("href=");
while(index != -1)
{
index += "href=\"".Length;
commentText = commentText.Replace("'", "\"");
if(commentText.IndexOf("\"", index) != -1)
{
int closeIndex = commentText.IndexOf("\"", index);
if(closeIndex == -1)
{
continue;
}
string url = commentText.Substring(index, closeIndex - index);
closeIndex = commentText.IndexOf(">", index);
if (closeIndex != -1 && commentText.IndexOf("</a>", closeIndex) != -1)
{
closeIndex += ">".Length;
string urlText = commentText.Substring(closeIndex, commentText.IndexOf("</a>", closeIndex) - closeIndex);
if(urlText == url)
{
bool textFixed = false;
if(urlText.IndexOf("http://ganjoor.net") == 0 || urlText.IndexOf("https://ganjoor.net") == 0)
{
urlText = urlText.Replace("http://ganjoor.net", "").Replace("https://ganjoor.net", "");
if (urlText.Length > 0 && urlText[urlText.Length - 1] == '/')
urlText = urlText.Substring(0, urlText.Length - 1);
var page = await context.GanjoorPages.AsNoTracking().Where(p => p.FullUrl == urlText).FirstOrDefaultAsync();
if(page != null)
{
commentText = commentText.Substring(0, closeIndex) + page.FullTitle + commentText.Substring(commentText.IndexOf("</a>", closeIndex));
textFixed = true;
}
}
if(!textFixed)
commentText = commentText.Substring(0, closeIndex) + "پیوند به وبگاه بیرونی" + commentText.Substring(commentText.IndexOf("</a>", closeIndex));
}
}
}
index = commentText.IndexOf("href=\"", index);
}
if(commentText != comment.HtmlComment)
{
comment.HtmlComment = commentText;
context.Update(comment);
await context.SaveChangesAsync();
}
}
await jobProgressServiceEF.UpdateJob(job.Id, 100, "", true);
}
catch (Exception exp)
{
await jobProgressServiceEF.UpdateJob(job.Id, 100, "", false, exp.ToString());
}
}
}
);
return new RServiceResult<bool>(true);
}
}
}