Ejemplo n.º 1
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;
        }
Ejemplo n.º 2
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)
 {
 }