Example #1
0
        /// <summary>
        /// Create a wiki site, login if necessary.
        /// </summary>
        private async Task <WikiSite> CreateWikiSiteAsync(IWikiClient wikiClient, string url)
        {
            WikiSite site;

            if (url.Contains(".wikia.com"))
            {
                var uri     = new Uri(url, UriKind.Absolute);
                var options = new WikiaSiteOptions(uri.GetLeftPart(UriPartial.Authority) + "/")
                {
                    AccountAssertion = AccountAssertionBehavior.AssertAll,
                };
                site = new WikiaSite(wikiClient, options)
                {
                    Logger = OutputLoggerFactory.CreateLogger <WikiaSite>()
                };
            }
            else
            {
                var options = new SiteOptions(url)
                {
                    AccountAssertion = AccountAssertionBehavior.AssertAll,
                };
                site = new WikiSite(wikiClient, options)
                {
                    Logger = OutputLoggerFactory.CreateLogger <WikiSite>()
                };
            }
            await site.Initialization;

            if (sitesNeedsLogin.Contains(url))
            {
                await CredentialManager.LoginAsync(site);
            }
            return(site);
        }
        /// <summary>
        /// Use predefined credential routine, return a logged-in WikiSite instance.
        /// </summary>
        public static async Task <WikiSite> EarlyLoginAsync(IWikiClient wikiClient, SiteOptions options)
        {
            if (wikiClient == null)
            {
                throw new ArgumentNullException(nameof(wikiClient));
            }
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            if (EarlyLoginCoreAsyncHandler == null)
            {
                throw new NotSupportedException("To enable login feature, you should set `EarlyLoginCoreAsyncHandler` in `Initialize` private function. See http://github.com/cxuesong/WikiClientLibrary for more information.");
            }
            var site = await EarlyLoginCoreAsyncHandler(wikiClient, options);

            if (!site.Initialization.IsCompleted)
            {
                throw new InvalidOperationException("You forgot to await WikiSite.Initialization in your EarlyLoginCoreAsyncHandler implementation.");
            }
            if (site == null)
            {
                throw new NotSupportedException("Your EarlyLoginCoreAsyncHandler implementation returned null for site: " + options.ApiEndpoint + ".");
            }
            if (!site.AccountInfo.IsUser)
            {
                throw new NotSupportedException("Failed to login into: " + site + " . Check your EarlyLoginCoreAsyncHandler implementation.");
            }
            return(site);
        }
Example #3
0
        /// <summary>
        /// Initializes a <see cref="WikiSite"/> instance with the specified settings
        /// and optional login before fetching for site information.
        /// </summary>
        /// <param name="wikiClient">WikiClient instance.</param>
        /// <param name="options">Site options.</param>
        /// <param name="userName">The user name used to login before fetching for site information. Pass <c>null</c> to fetch site information without login first.</param>
        /// <param name="password">The password used to login before fetching for site information.</param>
        /// <exception cref="ArgumentNullException"><paramref name="wikiClient"/> or <paramref name="options"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException">One or more settings in <paramref name="options"/> is invalid.</exception>
        /// <remarks>
        /// <para>For the private wiki where anonymous users cannot access query API, you can use this
        /// overload to login to the site before any querying API invocations are issued.</para>
        /// </remarks>
        public WikiSite(IWikiClient wikiClient, SiteOptions options, string userName, string password)
        {
            if (wikiClient == null)
            {
                throw new ArgumentNullException(nameof(wikiClient));
            }
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            if (string.IsNullOrEmpty(options.ApiEndpoint))
            {
                throw new ArgumentException(Prompts.ExceptionInvalidMediaWikiApiEndpointUrl, nameof(options));
            }
            WikiClient    = wikiClient;
            this.options  = options.Clone();
            tokensManager = new TokensManager(this);
            DisambiguationTemplatesAsync = new AsyncLazy <ICollection <string> >(async() =>
            {
                if (this.options.DisambiguationTemplates == null)
                {
                    var dabPages = await RequestHelper
                                   .EnumLinksAsync(this, "MediaWiki:Disambiguationspage", new[] { BuiltInNamespaces.Template })
                                   .ToListAsync();
                    if (dabPages.Count == 0)
                    {
                        // Try to fetch from mw messages
                        var msg = await GetMessageAsync("disambiguationspage");
                        if (msg != null)
                        {
                            dabPages.Add(msg);
                        }
                    }
                    dabPages.Add(SiteOptions.DefaultDisambiguationTemplate);
                    return(dabPages);
                }
                return(this.options.DisambiguationTemplates);
            });

            async Task InitializeAsync()
            {
                if (userName != null)
                {
                    await LoginAsync(userName, password);
                    await RefreshSiteInfoAsync();
                }
                else
                {
                    var   refSi = RefreshSiteInfoAsync();
                    var   refAi = RefreshAccountInfoAsync();
                    await refSi;
                    await refAi;
                }
            }

            localInitialization = InitializeAsync();
            Initialization      = localInitialization;
        }
Example #4
0
 /// <summary>
 /// Initializes the instance with a <see cref="Client.WikiClient"/> and family name.
 /// </summary>
 /// <exception cref="ArgumentNullException"><paramref name="wikiClient"/> is <c>null</c>.</exception>
 public WikiFamily(IWikiClient wikiClient, string name)
 {
     if (wikiClient == null)
     {
         throw new ArgumentNullException(nameof(wikiClient));
     }
     WikiClient = wikiClient;
     Name       = name;
 }
        /// <summary>
        /// Create a wiki site, login if necessary.
        /// </summary>
        private async Task <WikiSite> CreateWikiSiteAsync(IWikiClient wikiClient, string url)
        {
            WikiSite site;

            if (url.Contains(".wikia.com") || url.Contains(".wikia.org") || url.Contains(".fandom.com"))
            {
                var uri     = new Uri(url, UriKind.Absolute);
                var rootUrl = new Uri(uri, ".").ToString();
                var options = new WikiaSiteOptions(rootUrl)
                {
                    AccountAssertion = AccountAssertionBehavior.AssertAll,
                };
                site = new WikiaSite(wikiClient, options)
                {
                    Logger = OutputLoggerFactory.CreateLogger <WikiaSite>()
                };
            }
            else
            {
                var options = new SiteOptions(url)
                {
                    AccountAssertion = AccountAssertionBehavior.AssertAll,
                };
                site = new WikiSite(wikiClient, options)
                {
                    Logger = OutputLoggerFactory.CreateLogger <WikiSite>()
                };
            }
            await site.Initialization;

            if (sitesNeedsLogin.Contains(url))
            {
                await CredentialManager.LoginAsync(site);
            }
            return(site);
        }
Example #6
0
 /// <summary>
 /// Initializes the instance with a <see cref="Client.WikiClient"/> and family name.
 /// </summary>
 public WikiFamily(IWikiClient wikiClient) : this(wikiClient, null)
 {
 }
Example #7
0
 /// <inheritdoc cref="WikiSite(IWikiClient,SiteOptions,string,string)"/>
 /// <summary>
 /// Initializes a <see cref="WikiSite"/> instance with the specified API endpoint.
 /// </summary>
 /// <exception cref="UnauthorizedOperationException">Cannot access query API module due to target site permission settings. You may need to use <see cref="WikiSite(IWikiClient,SiteOptions,string, string)"/> to login before any other API requests.</exception>
 /// <remarks></remarks>
 public WikiSite(IWikiClient wikiClient, string apiEndpoint)
     : this(wikiClient, new SiteOptions(apiEndpoint), null, null)
 {
 }
Example #8
0
 /// <inheritdoc cref="WikiSite(IWikiClient,SiteOptions,string,string)"/>
 /// <summary>
 /// Initializes a <see cref="WikiSite"/> instance with the specified settings.
 /// </summary>
 /// <exception cref="UnauthorizedOperationException">Cannot access query API module due to target site permission settings. You may need to use <see cref="WikiSite(IWikiClient,SiteOptions,string, string)"/> to login before any other API requests.</exception>
 /// <remarks></remarks>
 public WikiSite(IWikiClient wikiClient, SiteOptions options)
     : this(wikiClient, options, null, null)
 {
 }
 public void TestInitialize()
 {
     _client    = Substitute.For <IWikiClient>();
     _processor = new WikipediaProcessor(_client);
 }
Example #10
0
 /// <inheritdoc />
 public WikiaSite(IWikiClient wikiClient, WikiaSiteOptions options, string userName, string password)
     : base(wikiClient, options, userName, password)
 {
 }
Example #11
0
 /// <inheritdoc />
 /// <summary>
 /// Initializes a new <see cref="WikiaSite"/> instance from the Wikia site root URL.
 /// </summary>
 /// <param name="siteRootUrl">Wikia site root URL, with the ending slash. e.g. <c>http://community.wikia.com/</c>.</param>
 public WikiaSite(IWikiClient wikiClient, string siteRootUrl) : this(wikiClient, new WikiaSiteOptions(siteRootUrl), null, null)
 {
 }