<commit_before>﻿using Dnn.PersonaBar.Library.Prompt;
using Dnn.PersonaBar.Library.Prompt.Attributes;
using Dnn.PersonaBar.Library.Prompt.Models;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Entities.Tabs;
using DotNetNuke.Entities.Users;

namespace Dnn.PersonaBar.Recyclebin.Components.Prompt.Commands
{
    [ConsoleCommand("restore-page", Constants.RecylcleBinCategory, "Prompt_RestorePage_Description")]
    public class RestorePage : ConsoleCommandBase
    {
        public override string LocalResourceFile => Constants.LocalResourcesFile;

        [FlagParameter("name", "Prompt_RestorePage_FlagName", "String")]
        private const string FlagName = "name";

        [FlagParameter("parentid", "Prompt_RestorePage_FlagParentId", "Integer")]
        private const string FlagParentId = "parentid";

        [FlagParameter("id", "Prompt_RestorePage_FlagId", "Integer")]
        private const string FlagId = "id";

        private int PageId { get; set; }
        private string PageName { get; set; }
        private int ParentId { get; set; }

        public override void Init(string[] args, PortalSettings portalSettings, UserInfo userInfo, int activeTabId)
        {
            
            PageId = GetFlagValue(FlagId, "Page Id", -1, false, true);
            ParentId = GetFlagValue(FlagParentId, "Parent Id", -1);
            PageName = GetFlagValue(FlagName, "Page Name", string.Empty);
            if (PageId <= 0 && string.IsNullOrEmpty(PageName))
            {
                AddMessage(LocalizeString("Prompt_RestorePageNoParams"));
            }
        }

        public override ConsoleResultModel Run()
        {
            TabInfo tab;
            if (PageId > 0)
            {
                tab = TabController.Instance.GetTab(PageId, PortalId);
                if (tab == null)
                {
                    return new ConsoleErrorResultModel(string.Format(LocalizeString("PageNotFound"), PageId));
                }
            }
            else if (!string.IsNullOrEmpty(PageName))
            {
                tab = ParentId > 0 ? TabController.Instance.GetTabByName(PageName, PortalId, ParentId) : TabController.Instance.GetTabByName(PageName, PortalId);
                if (tab == null)
                    return
                        new ConsoleErrorResultModel(
                            string.Format(
                                LocalizeString("PageNotFoundWithName"),
                                PageName));
            }
            else
            {
                return new ConsoleErrorResultModel(LocalizeString("Prompt_RestorePageNoParams"));
            }
            string message;
            RecyclebinController.Instance.RestoreTab(tab, out message);
            return string.IsNullOrEmpty(message) ? new ConsoleResultModel(string.Format(LocalizeString("Prompt_PageRestoredSuccessfully"), tab.TabID, tab.TabName)) { Records = 1 } : new ConsoleErrorResultModel(message);
        }
    }
}<commit_msg>DNN-19712 : "restore-page" is working regardless of the CrossPortalId specified in rest request - check for Id & name paramter<commit_after>﻿using Dnn.PersonaBar.Library.Helper;
using Dnn.PersonaBar.Library.Prompt;
using Dnn.PersonaBar.Library.Prompt.Attributes;
using Dnn.PersonaBar.Library.Prompt.Models;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Entities.Tabs;
using DotNetNuke.Entities.Users;

namespace Dnn.PersonaBar.Recyclebin.Components.Prompt.Commands
{
    [ConsoleCommand("restore-page", Constants.RecylcleBinCategory, "Prompt_RestorePage_Description")]
    public class RestorePage : ConsoleCommandBase
    {
        public override string LocalResourceFile => Constants.LocalResourcesFile;

        [FlagParameter("name", "Prompt_RestorePage_FlagName", "String")]
        private const string FlagName = "name";

        [FlagParameter("parentid", "Prompt_RestorePage_FlagParentId", "Integer")]
        private const string FlagParentId = "parentid";

        [FlagParameter("id", "Prompt_RestorePage_FlagId", "Integer")]
        private const string FlagId = "id";

        private int PageId { get; set; }
        private string PageName { get; set; }
        private int ParentId { get; set; }

        public override void Init(string[] args, PortalSettings portalSettings, UserInfo userInfo, int activeTabId)
        {
            
            PageId = GetFlagValue(FlagId, "Page Id", -1, false, true);
            ParentId = GetFlagValue(FlagParentId, "Parent Id", -1);
            PageName = GetFlagValue(FlagName, "Page Name", string.Empty);
            if (PageId <= 0 && string.IsNullOrEmpty(PageName))
            {
                AddMessage(LocalizeString("Prompt_RestorePageNoParams"));
            }
        }

        public override ConsoleResultModel Run()
        {
            TabInfo tab;
            string message = string.Format(LocalizeString("PageNotFound"), PageId);

            if (PageId > 0)
            {
                tab = TabController.Instance.GetTab(PageId, PortalId);
                if (tab == null)
                {                  
                    return new ConsoleErrorResultModel(message);
                }
            }
            else if (!string.IsNullOrEmpty(PageName))
            {
                tab = ParentId > 0 
                    ? TabController.Instance.GetTabByName(PageName, PortalId, ParentId) 
                    : TabController.Instance.GetTabByName(PageName, PortalId);

                if (tab == null)
                {
                    message = string.Format(LocalizeString("PageNotFoundWithName"), PageName);
                    return new ConsoleErrorResultModel(message);
                }
            }
            else
            {
                return new ConsoleErrorResultModel(LocalizeString("Prompt_RestorePageNoParams"));
            }

            if (!PortalHelper.IsContentExistsForRequestedPortal(tab.PortalID, PortalSettings))
            {
                return new ConsoleErrorResultModel(message);
            }

            RecyclebinController.Instance.RestoreTab(tab, out message);
            return string.IsNullOrEmpty(message) ? new ConsoleResultModel(string.Format(LocalizeString("Prompt_PageRestoredSuccessfully"), tab.TabID, tab.TabName)) { Records = 1 } : new ConsoleErrorResultModel(message);
        }
    }
}
{'fraction_non_alphanumeric': 0.09357740255641471, 'fraction_numerical': 0.00268265740886855, 'mean_word_length': 2.2686952037132544, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '6444358', 'n_tokens_mistral': 1693, 'n_tokens_neox': 1638, 'n_words': 337}