public WikiArticleInfoViewModel(ControllerContext controllerContext, ArticleSlug articleSlug)
            : base(controllerContext, articleSlug, RouteNames.WikiArticleInfo, activeTab: HeaderTab.Info)
        {
            this.articleExistsHelper = new ArticleExistsHelper(this.CurrentRepository);

            var redirectsTargetArticleSlugs =
                CurrentRepository.QueryArticleRedirects(articleSlug).Select(x => x.TargetArticleSlug).ToList();

            this.Redirects =
                redirectsTargetArticleSlugs.Select(x => new ArticleSlug(x))
                    .Select(x => new KeyValuePair<string, string>(x.Slug, x.Title))
                    .ToList();

            this.Tags = this.ArticlePage.ArticleTags.ToList();

            var articleRevisions = CurrentRepository.QueryArticleRevisions(articleSlug).ToList();
            this.Revisions = articleRevisions.Select(x => new ArticleRevisionListItem(x)).ToList();

            var outboundArticleSlugs = (this.ArticlePage.ArticleRevisionOutboundArticleSlugs
                                        ?? Enumerable.Empty<string>());

            this.OutboundArticleSlugs = outboundArticleSlugs.Select(this.GetIncomingOutboundArticleSlug).ToList();

            var incomingArticleSlugs = this.CurrentRepository.GetArticleIncomingArticleSlugs(articleSlug).ToList();

            this.IncomingArticleSlugs = incomingArticleSlugs.Select(this.GetIncomingOutboundArticleSlug).ToList();
        }
        public ArticleRevisionDiffApiModel(
            ArticleSlug articleSlug,
            ArticleRevisionDate oldRevisionDate,
            ArticleRevisionDate newRevisionDate,
            Repository repository)
        {
            if (repository == null)
            {
                throw new ArgumentNullException("repository");
            }

            var oldRevision = repository.GetArticleRevision(articleSlug, oldRevisionDate);
            if (oldRevision == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            var newRevision = repository.GetArticleRevision(articleSlug, newRevisionDate);
            if (newRevision == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            var oldRevisionContent = MarkdownTextService.MakeText(oldRevision.MarkdownContent).ToHtmlLinebreaks();

            var newRevisionContent = MarkdownTextService.MakeText(newRevision.MarkdownContent).ToHtmlLinebreaks();

            var diff = new Helpers.HtmlDiff(oldRevisionContent ?? string.Empty, newRevisionContent ?? string.Empty);

            this.HtmlDiff = diff.Build();

            this.OldCreatedAt = oldRevision.CreatedAt.ToString(ArticleRevisionDate.FormattedDateTimeFormat);
            this.NewCreatedAt = newRevision.CreatedAt.ToString(ArticleRevisionDate.FormattedDateTimeFormat);
        }
 public WikiArticleEditViewModel(
     ControllerContext controllerContext,
     ArticleSlug articleSlug,
     ArticleRevisionDate articleRevisionDate = null)
     : base(controllerContext, articleSlug, RouteNames.WikiArticleEdit, articleRevisionDate, HeaderTab.Edit)
 {
     this.IsCreateMode = !this.ArticlePage.HasArticle;
 }
예제 #4
0
        public ArticleAdminApiModel(ArticleSlug articleSlug, Repository repository)
        {
            var article = repository.GetArticle(articleSlug);

            var articleAccess = (article != null) ? article.ArticleAccess : ArticleAccess.Default();

            this.CanRead = (int)articleAccess.CanRead;
            this.CanEdit = (int)articleAccess.CanEdit;
            this.CanAdmin = (int)articleAccess.CanAdmin;
        }
예제 #5
0
        public void AssertCanAccess(ArticleSlug articleSlug, IPrincipal principal, ArticleAccessType accessType)
        {
            if (articleSlug == null)
            {
                throw new ArgumentOutOfRangeException("articleSlug");
            }

            bool canAccess = this.GetCanAccess(principal, accessType);
            if (!canAccess)
            {
                throw new ArticleAccessException(articleSlug);
            }
        }
예제 #6
0
        private static void EnsureIsAuthorizedInternal(
            string slugValue,
            Repository repository,
            ArticleAccessType accessType,
            IPrincipal principal)
        {
            var articleSlug = new ArticleSlug(slugValue ?? string.Empty);
            var article = (articleSlug.HasValue) ? repository.GetArticle(articleSlug) : null;
            var articleAccess = (article != null) ? article.ArticleAccess : null;

            articleAccess = articleAccess ?? ArticleAccess.Default();

            articleAccess.AssertCanAccess(articleSlug, principal, accessType);
        }
예제 #7
0
        public void Save(ArticleSlug articleSlug, Repository repository)
        {
            var article = repository.GetArticle(articleSlug) ?? new Article(articleSlug);

            var canAdmin = TryGetArticleAccess(this.CanAdmin);
            var canEdit = TryGetArticleAccess(this.CanEdit);
            var canRead = TryGetArticleAccess(this.CanRead);

            article.ArticleAccess.CanAdmin = canAdmin;
            article.ArticleAccess.CanEdit = canEdit;
            article.ArticleAccess.CanRead = canRead;

            repository.SaveArticle(article);
        }
예제 #8
0
 public WikiArticleViewModel(
     ControllerContext controllerContext,
     ArticleSlug articleSlug,
     ArticleRevisionDate articleRevisionDate = null,
     bool shouldRedirect = false,
     bool handleIndexSlug = true)
     : this(controllerContext,
         articleSlug,
         articleRevisionDate,
         shouldRedirect,
         handleIndexSlug,
         WikiDownConfig.Instance)
 {
 }
예제 #9
0
        public WikiArticleViewModel(
            ControllerContext controllerContext,
            ArticleSlug articleSlug,
            ArticleRevisionDate articleRevisionDate,
            bool shouldRedirect,
            bool handleIndexSlug,
            WikiDownConfig config)
            : base(controllerContext,
                articleSlug,
                RouteNames.WikiArticle,
                articleRevisionDate,
                HeaderTab.Article,
                shouldRedirect)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            this.handleIndexSlug = handleIndexSlug;

            if (this.ArticlePage.HasRedirect && !this.ShouldRedirect)
            {
                return;
            }

            this.articleTags = (this.ArticlePage.HasArticle && this.ArticlePage.ArticleTags != null)
                                   ? this.ArticlePage.ArticleTags.ToList()
                                   : this.ArticleTags;

            if (!this.HasArticle)
            {
                return;
            }

            if (string.IsNullOrWhiteSpace(this.ArticlePage.ArticleRevisionMarkdownContent))
            {
                return;
            }

            string articleHtml = GetCachedArticleHtml(
                this.ArticlePage.ArticleRevisionId,
                this.ArticlePage.ArticleRevisionMarkdownContent,
                config);

            this.htmlContent = !string.IsNullOrWhiteSpace(articleHtml)
                                   ? new WikiDownArticleHtmlString(articleHtml, this.CurrentRepository)
                                   : null;
        }
예제 #10
0
        public ArticleRevisionApiModel(ArticleSlug articleSlug, ArticleRevision articleRevision)
        {
            this.Slug = articleSlug.Slug;

            if (articleRevision == null)
            {
                return;
            }

            this.MarkdownContent = articleRevision.MarkdownContent;

            var articleRevisionDate = new ArticleRevisionDate(articleRevision.CreatedAt);

            this.DateId = articleRevisionDate.DateTimeId;
            this.DateFormatted = articleRevisionDate.DateTimeFormatted;
        }
예제 #11
0
        public override object BindModel(
            Mvc.ControllerContext controllerContext,
            Mvc.ModelBindingContext bindingContext)
        {
            var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);

            var modelState = new Mvc.ModelState { Value = valueProviderResult };

            var value = (valueProviderResult != null) ? valueProviderResult.AttemptedValue : null;

            var model = new ArticleSlug(value ?? string.Empty);

            bindingContext.ModelState.Add(bindingContext.ModelName, modelState);

            return model;
        }
        protected WikiArticleViewModelBase(
            ControllerContext controllerContext,
            ArticleSlug articleSlug,
            string routeName,
            ArticleRevisionDate articleRevisionDate = null,
            HeaderTab activeTab = HeaderTab.None,
            bool shouldRedirect = false)
        {
            if (controllerContext == null)
            {
                throw new ArgumentNullException("controllerContext");
            }
            if (controllerContext.RequestContext == null)
            {
                throw new ArgumentOutOfRangeException(
                    "controllerContext",
                    "RequestContext in ControllerContext is null.");
            }
            if (articleSlug == null)
            {
                throw new ArgumentNullException("articleSlug");
            }
            if (routeName == null)
            {
                throw new ArgumentNullException("routeName");
            }

            this.RequestContext = controllerContext.RequestContext;
            this.RouteName = routeName;
            this.ArticleRevisionDate = articleRevisionDate;
            this.ActiveTab = activeTab;
            this.ShouldRedirect = shouldRedirect;

            this.CurrentRepository = this.RequestContext.HttpContext.GetRepository();
            this.CurrentPrincipal = this.RequestContext.HttpContext.User;
            this.UrlHelper = new UrlHelper(this.RequestContext);

            var revisionDate = (this.ArticleRevisionDate != null) ? this.ArticleRevisionDate.Value : (DateTime?)null;
            this.ArticlePage = this.CurrentRepository.GetArticlePage(articleSlug, revisionDate);

            this.alternativeActionResultLazy = new Lazy<ActionResult>(this.ResolveAlternativeActionResult);
        }
        public ListsSearchArticlesViewModel(ControllerContext controllerContext, string searchTerm)
        {
            this.SearchTerm = searchTerm ?? string.Empty;

            if (string.IsNullOrWhiteSpace(this.SearchTerm))
            {
                this.SearchResults = new List<SearchItem>(0);
                this.Suggestions = new List<string>(0);
                return;
            }

            var searchTermArticleSlug = new ArticleSlug(this.SearchTerm);

            this.SearchTermFormatted = Capitalize(searchTermArticleSlug.Title);

            var repository = controllerContext.HttpContext.GetRepository();

            var searchQuery = repository.QueryArticlesSearch(this.SearchTerm);
            var searchResult = new ArticleSearchResult(searchQuery);

            this.SearchResults = searchResult.Items.Select(x => new SearchItem(x)).ToList();
            this.Suggestions = searchResult.Suggestions;

            var articleRedirect = repository.GetArticleRedirect(searchTermArticleSlug);

            var articleSlugStatus = repository.GetArticleSlugStatus(searchTermArticleSlug);
            this.ArticleExists = articleSlugStatus.HasAnySlug || (articleRedirect != null);

            var redirectArticleSlug = (articleRedirect != null)
                                          ? new ArticleSlug(articleRedirect.TargetArticleSlug)
                                          : null;

            bool isRedirect = (redirectArticleSlug != null) && (redirectArticleSlug.Title != SearchTermFormatted);

            this.ArticleTitle = this.ArticleExists
                                    ? (isRedirect ? redirectArticleSlug.Title : this.SearchTermFormatted)
                                    : this.SearchTermFormatted;

            this.ArticleRedirectedFromTitle = (this.ArticleExists && isRedirect) ? this.SearchTermFormatted : null;
        }
예제 #14
0
 public ArticleAccessException(ArticleSlug articleSlug, string message = null, Exception innerException = null)
     : base(articleSlug, HttpStatusCode.Unauthorized, message ?? GetDefaultMessage(articleSlug), innerException)
 {
 }
        private ArticleRevision GetEnsuredArticleRevision(ArticleSlug slug, ArticleRevisionDate articleRevisionDate)
        {
            var articleRevision = this.CurrentRepository.GetArticleRevision(slug, articleRevisionDate.DateTime);
            if (articleRevision == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            return articleRevision;
        }
예제 #16
0
        public static string WikiArticle(
            this UrlHelper urlHelper,
            ArticleSlug articleSlug,
            ArticleRevisionDate revisionDate = null,
            bool shouldRedirect = true,
            bool includeHash = false)
        {
            var noRedirectValue = shouldRedirect ? null : "0";

            var url = urlHelper.RouteUrl(
                RouteNames.WikiArticle,
                new { slug = articleSlug.Slug, revisionDate, redirect = noRedirectValue });

            return includeHash ? AppendHash(url, articleSlug) : url;
        }
 public override string RedirectUrlFactory(ArticleSlug articleSlug)
 {
     return this.UrlHelper.WikiArticleEdit(articleSlug, this.ArticleRevisionDate);
 }
 public abstract string RedirectUrlFactory(ArticleSlug articleSlug);
        private KeyValuePair<ArticleSlug, bool> GetIncomingOutboundArticleSlug(string slug)
        {
            var articleSlug = new ArticleSlug(slug);
            bool articleExists = this.articleExistsHelper.GetExists(articleSlug.Slug);

            return new KeyValuePair<ArticleSlug, bool>(articleSlug, articleExists);
        }
예제 #20
0
 public static string WikiArticleComments(this UrlHelper urlHelper, ArticleSlug articleSlug)
 {
     return null;
     //return urlHelper.RouteUrl(RouteNames.WikiArticleComments, new { slug = articleSlug.Slug });
 }
예제 #21
0
 public static string WikiArticleEdit(
     this UrlHelper urlHelper,
     ArticleSlug articleSlug,
     ArticleRevisionDate revisionDate = null)
 {
     return urlHelper.RouteUrl(RouteNames.WikiArticleEdit, new { slug = articleSlug.Slug, revisionDate });
 }
예제 #22
0
 public static string ListsTaggedArticles(this UrlHelper urlHelper, ArticleSlug tagName, int? pageIndex = null)
 {
     return urlHelper.RouteUrl(RouteNames.ListsTaggedArticles, new { tagName, pageIndex });
 }
예제 #23
0
 private static string AppendHash(string url, ArticleSlug articleSlug)
 {
     return !string.IsNullOrWhiteSpace(articleSlug.Hash) ? string.Format("{0}#{1}", url, articleSlug.Hash) : url;
 }
예제 #24
0
 public static string WikiArticleInfo(this UrlHelper urlHelper, ArticleSlug articleSlug)
 {
     return urlHelper.RouteUrl(RouteNames.WikiArticleInfo, new { slug = articleSlug.Slug });
 }
예제 #25
0
 private static string GetDefaultMessage(ArticleSlug articleSlug)
 {
     return (articleSlug != null && articleSlug.HasValue)
                ? string.Format("Cannot access article with Slug '{0}'.", articleSlug.Slug)
                : "Cannot access article.";
 }
 public override string RedirectUrlFactory(ArticleSlug articleSlug)
 {
     return this.UrlHelper.WikiArticleInfo(articleSlug);
 }