<commit_before>﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Threading;
using GSoft.Dynamite.Globalization.Variations;
using GSoft.Dynamite.Logging;
using GSoft.Dynamite.TimerJobs;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;
using Microsoft.SharePoint.Utilities;

namespace GSoft.Dynamite.Helpers
{
    /// <summary>
    /// SharePoint variations helpers
    /// </summary>
    public class VariationHelper : IVariationHelper
    {
        private readonly ILogger logger;
        private readonly ITimerJobHelper timerJobHelper;

        /// <summary>
        /// Default constructor with dependency injection
        /// </summary>
        /// <param name="logger">The logger</param>
        /// <param name="timerJobHelper">The timer job helper</param>
        public VariationHelper(ILogger logger, ITimerJobHelper timerJobHelper)
        {
            this.logger = logger;
            this.timerJobHelper = timerJobHelper;
        }

        /// <summary>
        /// Determines whether [the specified web] [is current web source label].
        /// </summary>
        /// <param name="web">The web.</param>
        /// <returns>A boolean value which indicates if the current web is the source variation label.</returns>
        public bool IsCurrentWebSourceLabel(SPWeb web)
        {
            var sourceLabel = Microsoft.SharePoint.Publishing.Variations.GetLabels(web.Site).FirstOrDefault(x => x.IsSource);
            if (sourceLabel != null)
            {
                // Compare absolute URL values
                return web.Url.StartsWith(sourceLabel.TopWebUrl, StringComparison.OrdinalIgnoreCase);
            }

            return false;
        }

        /// <summary>
        /// Determines if variations are enabled on a site.
        /// </summary>
        /// <param name="site">The site.</param>
        /// <returns>A boolean value which indicates if the current site has variations enabled.</returns>
        public bool IsVariationsEnabled(SPSite site)
        {
            bool isEnabled = Microsoft.SharePoint.Publishing.Variations.GetLabels(site).Count > 0;

            return isEnabled;
        }

        /// <summary>
        /// Get the variations labels for the site collection.
        /// NOTE: Also possible with the static Microsoft.SharePoint.Publishing Variations object by faking a SPContext
        /// </summary>
        /// <param name="site">The site.</param>
        /// <param name="labelToSync">The label name to Sync. example: <c>"en"</c> or <c>"fr"</c>.</param>
        /// <returns>A collection of unique label.</returns>
        public ReadOnlyCollection<Microsoft.SharePoint.Publishing.VariationLabel> GetVariationLabels(SPSite site, string labelToSync)
        {
            this.logger.Info("Start method 'GetVariationLabels' for site url: '{0}' with label '{1}'", site.Url, labelToSync);

            var web = site.RootWeb;<commit_msg>- [VariationsHelper] Prise en compte du cas où les variations ne sont pas activées sur le site. Dans ce cas, le web correspond forcément au label source.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Threading;
using GSoft.Dynamite.Globalization.Variations;
using GSoft.Dynamite.Logging;
using GSoft.Dynamite.TimerJobs;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;
using Microsoft.SharePoint.Utilities;

namespace GSoft.Dynamite.Helpers
{
    /// <summary>
    /// SharePoint variations helpers
    /// </summary>
    public class VariationHelper : IVariationHelper
    {
        private readonly ILogger logger;
        private readonly ITimerJobHelper timerJobHelper;

        /// <summary>
        /// Default constructor with dependency injection
        /// </summary>
        /// <param name="logger">The logger</param>
        /// <param name="timerJobHelper">The timer job helper</param>
        public VariationHelper(ILogger logger, ITimerJobHelper timerJobHelper)
        {
            this.logger = logger;
            this.timerJobHelper = timerJobHelper;
        }

        /// <summary>
        /// Determines whether [the specified web] [is current web source label].
        /// </summary>
        /// <param name="web">The web.</param>
        /// <returns>A boolean value which indicates if the current web is the source variation label.</returns>
        public bool IsCurrentWebSourceLabel(SPWeb web)
        {
            var isSourceLabel = true;

            // If the site doesn't have variatiosn enabled, by default the web is the source label
            if (Microsoft.SharePoint.Publishing.Variations.Current != null)
            {
                var labels = Microsoft.SharePoint.Publishing.Variations.GetLabels(web.Site);

                if (labels.Count > 0)
                {
                    var sourceLabel = labels.FirstOrDefault(x => x.IsSource);
                    if (sourceLabel != null)
                    {
                        // Compare absolute URL values
                        return web.Url.StartsWith(sourceLabel.TopWebUrl, StringComparison.OrdinalIgnoreCase);
                    }
                    else
                    {
                        isSourceLabel = false;
                    }
                }
            }

            return isSourceLabel;
        }

        /// <summary>
        /// Determines if variations are enabled on a site.
        /// </summary>
        /// <param name="site">The site.</param>
        /// <returns>A boolean value which indicates if the current site has variations enabled.</returns>
        public bool IsVariationsEnabled(SPSite site)
        {
            bool isEnabled = Microsoft.SharePoint.Publishing.Variations.GetLabels(site).Count > 0;

            return isEnabled;
        }

        /// <summary>
        /// Get the variations labels for the site collection.
        /// NOTE: Also possible with the static Microsoft.SharePoint.Publishing Variations object by faking a SPContext
        /// </summary>
        /// <param name="site">The site.</param>
        /// <param name="labelToSync">The label name to Sync. example: <c>"en"</c> or <c>"fr"</c>.</param>
        /// <returns>A collection of unique label.</returns>
        public ReadOnlyCollection<Microsoft.SharePoint.Publishing.VariationLabel> GetVariationLabels(SPSite site, string labelToSync)
        {
            this.logger.Info("Start method 'GetVariationLabels' for site url: '{0}' with label '{1}'", site.Url, labelToSync);

            var web = site.RootWeb;
{'fraction_non_alphanumeric': 0.10397643593519883, 'fraction_numerical': 0.0010309278350515464, 'mean_word_length': 2.220009483167378, 'pattern_counts': {'":': 0, '<': 69, '<?xml version=': 0, '>': 74, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6888932', 'n_tokens_mistral': 1739, 'n_tokens_neox': 1698, 'n_words': 485}