Skip to content

Instantly share code, notes, and snippets.

@KevinJump
Created June 28, 2023 16:01
Show Gist options
  • Save KevinJump/db21a4c6175352048baf4c203cf862bd to your computer and use it in GitHub Desktop.
Save KevinJump/db21a4c6175352048baf4c203cf862bd to your computer and use it in GitHub Desktop.
v8 - word count controller - does a hacky version of the full child page counting we do in v10/11/12
using System.Web.Http;
using Jumoo.TranslationManager.Core.Controllers;
using Newtonsoft.Json;
using Umbraco.Core.Services;
using Umbraco.Web.Mvc;
using Umbraco.Web.WebApi;
namespace WordCount
{
[PluginController("Testing")]
public class WordCountController : UmbracoAuthorizedApiController
{
private readonly TranslationNodeApiController _translationNodeApiController;
private readonly IContentService _contentService;
public WordCountController(TranslationNodeApiController translationNodeApiController, IContentService contentService)
{
_translationNodeApiController = translationNodeApiController;
_contentService = contentService;
}
[HttpGet]
public Info GetWordCounts(int id)
{
var wordInfo = new Info();
long pageNumber = 0;
int pageSize = 100;
long total;
do
{
var descendantsOrSelf = _contentService.GetPagedDescendants(id, pageNumber, pageSize, out total);
foreach (var item in descendantsOrSelf)
{
var response = _translationNodeApiController.GetWordCountInfo(item.Id);
var info = JsonConvert.DeserializeObject<Info>(JsonConvert.SerializeObject(response, Formatting.Indented));
wordInfo.Characters += info.Characters;
wordInfo.Words += info.Words;
wordInfo.Properties += info.Properties;
wordInfo.Message += info.Message;
wordInfo.Pages += 1;
}
pageNumber++;
} while (total > pageNumber * pageSize);
return wordInfo;
}
public class Info
{
public int Pages;
public int Words;
public int Characters;
public int Properties;
public string Message;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment