Exemple #1
0
        public async Task WpTest2PagePurgeTest()
        {
            AssertModify();
            var site = await WpTest2SiteAsync;
            // We do not need to login.
            var page   = new WikiPage(site, "project:sandbox");
            var result = await page.PurgeAsync(PagePurgeOptions.ForceLinkUpdate | PagePurgeOptions.ForceRecursiveLinkUpdate);

            Assert.True(result);
            // Now an ArgumentException should be thrown from Page.ctor.
            //page = new Page(site, "special:");
            //result = AwaitSync(page.PurgeAsync());
            //Assert.False(result);
            page   = new WikiPage(site, "the page should be inexistent");
            result = await page.PurgeAsync();

            Assert.False(result);
        }
Exemple #2
0
        static async Task HelloWikiWorld()
        {
            var loggerFactory = new LoggerFactory();

            loggerFactory.AddConsole(LogLevel.Information, true);
            // Create a MediaWiki API client.
            var wikiClient = new WikiClient
            {
                // UA of Client Application. The UA of WikiClientLibrary will
                // be append to the end of this when sending requests.
                ClientUserAgent = "ConsoleTestApplication1/1.0",
                Logger          = loggerFactory.CreateLogger <WikiClient>(),
            };
            // Create a MediaWiki Site instance with the URL of API endpoint.
            var site = new WikiSite(wikiClient, "https://test2.wikipedia.org/w/api.php")
            {
                Logger = loggerFactory.CreateLogger <WikiSite>()
            };
            // Waits for the WikiSite to initialize
            await site.Initialization;

            // Access site information via Site.SiteInfo
            Console.WriteLine("API version: {0}", site.SiteInfo.Generator);
            // Access user information via Site.UserInfo
            Console.WriteLine("Hello, {0}!", site.AccountInfo.Name);
            // Site login
            Console.WriteLine("We will edit [[Project:Sandbox]].");
            if (Confirm($"Do you want to login into {site.SiteInfo.SiteName}?"))
            {
LOGIN_RETRY:
                try
                {
                    await site.LoginAsync(Input("User name"), Input("Password"));
                }
                catch (OperationFailedException ex)
                {
                    Console.WriteLine(ex.ErrorMessage);
                    goto LOGIN_RETRY;
                }
                Console.WriteLine("You have successfully logged in as {0}.", site.AccountInfo.Name);
                Console.WriteLine("You're in the following groups: {0}.", string.Join(",", site.AccountInfo.Groups));
            }
            // Find out more members in Site class, such as
            //  page.Namespaces
            //  page.InterwikiMap

            // Page Operations
            // Fetch information and content
            var page = new WikiPage(site, site.SiteInfo.MainPage);

            Console.WriteLine("Retrieving {0}...", page);
            await page.RefreshAsync(PageQueryOptions.FetchContent);

            Console.WriteLine("Last touched at {0}.", page.LastTouched);
            Console.WriteLine("Last revision {0} by {1} at {2}.", page.LastRevisionId,
                              page.LastRevision.UserName, page.LastRevision.TimeStamp);
            Console.WriteLine("Content length: {0} bytes ----------", page.ContentLength);
            Console.WriteLine(page.Content);
            // Purge the page
            if (await page.PurgeAsync())
            {
                Console.WriteLine("  The page has been purged successfully.");
            }
            // Edit the page
            page = new WikiPage(site, "Project:Sandbox");
            await page.RefreshAsync(PageQueryOptions.FetchContent);

            if (!page.Exists)
            {
                Console.WriteLine("Warning: The page {0} doesn't exist.", page);
            }
            page.Content += "\n\n'''Hello''' ''world''!";
            await page.UpdateContentAsync("Test edit from WikiClientLibrary.");

            Console.WriteLine("{0} has been saved. RevisionId = {1}.", page, page.LastRevisionId);
            // Find out more operations in Page class, such as
            //  page.MoveAsync()
            //  page.DeleteAsync()
            // Logout
            await site.LogoutAsync();

            Console.WriteLine("You have successfully logged out.");
        }