Beispiel #1
0
        public static async Task RefreshPostsAsync(IEnumerable <Post> posts,
                                                   PostQueryOptions options, CancellationToken cancellationToken)
        {
            Debug.Assert(posts != null);
            // Fetch comment content.
            // You can even fetch posts from different sites.
            foreach (var sitePosts in posts.GroupBy(p => p.Site))
            {
                var site       = sitePosts.Key;
                var titleLimit = site.AccountInfo.HasRight(UserRights.ApiHighLimits)
                    ? 500
                    : 50;
                foreach (var partition in sitePosts.Partition(titleLimit))
                {
                    using (site.BeginActionScope(partition, options))
                    {
                        var postsNeedFetchingId = partition.Where(p => p.Id == 0).ToList();
                        if (postsNeedFetchingId.Count > 0)
                        {
                            site.Logger.LogDebug("Fetching page ID for {Count} comments.", postsNeedFetchingId.Count);
                        }
                        // Fetch last revisions to determine content and last editor
                        var pages            = partition.Select(p => new WikiPage(site, p.Id)).ToList();
                        var lastRevisionTask = pages.RefreshAsync(postLastRevisionQueryProvider, cancellationToken);
                        // Fetch the first revisions, when needed, to determine author.
                        Dictionary <int, Revision> firstRevisionDict = null;
                        if (partition.Count == 1 ||
                            (options & PostQueryOptions.ExactAuthoringInformation) == PostQueryOptions.ExactAuthoringInformation)
                        {
                            // We can only fetch for 1 page at a time, with rvdir = "newer"
                            firstRevisionDict = new Dictionary <int, Revision>();
                            foreach (var post in partition)
                            {
                                var generator = new RevisionsGenerator(site, post.Id)
                                {
                                    TimeAscending    = true,
                                    PaginationSize   = 1,
                                    PropertyProvider = postRevisionWithContentProvider
                                };
                                var rev = await generator.EnumItemsAsync().FirstAsync(cancellationToken);

                                firstRevisionDict[post.Id] = rev;
                            }
                        }
                        await lastRevisionTask;
                        var   lastRevisions = pages.ToDictionary(p => p.Id, p => p.LastRevision);
                        foreach (var post in partition)
                        {
                            var      lastRev  = lastRevisions[post.Id];
                            Revision firstRev = null;
                            firstRevisionDict?.TryGetValue(post.Id, out firstRev);
                            post.SetRevisions(firstRev, lastRev);
                        }
                    }
                }
            }
        }
Beispiel #2
0
        private async Task RevertArticleAsync(string article)
        {
            Console.Write($"Reverting recent ArtificerBot edits to {article}...");

            try
            {
                var generator = new RevisionsGenerator(Site)
                {
                    PageTitle     = article,
                    TimeAscending = false,
                    UserName      = ArtificerUser
                };

                var revs = await generator.EnumItemsAsync().ToList();

                if (revs.Count == 0)
                {
                    return;
                }

                var revision = revs.First();

                //https://www.mediawiki.org/wiki/API:Rollback
                var tokenQuery  = new { action = "query", meta = "tokens", titles = article };
                var tokenResult = await Site.InvokeMediaWikiApiAsync(new MediaWikiFormRequestMessage(tokenQuery), CancellationToken.None);

                string token = (string)tokenResult["query"]["tokens"]["csrftoken"];

                //action = rollback & title = Main % 20Page & user = Username & markbot & token = 094a45ddbbd5e90d55d79d2a23a8c921 % 2B\
                var editQuery = new
                {
                    action    = "edit",
                    title     = article,
                    bot       = true,
                    undo      = revision.Id,
                    undoafter = true,
                    summary   = "ArtificerBot reverting last mass edit on page due to user request.",
                    token
                };
                var result = await Site.InvokeMediaWikiApiAsync(new MediaWikiFormRequestMessage(editQuery), CancellationToken.None);

                Console.WriteLine("Done.");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error while reverting!");
                Console.WriteLine(ex.ToString());
            }
        }
Beispiel #3
0
        public async Task WpTestEnumRevisionsTest3()
        {
            var site = await WpTest2SiteAsync;
            // 5,100 revisions in total
            var page = new WikiPage(site, "Page:Edit_page_for_chrome");
            var t1   = new DateTime(2014, 10, 20, 10, 0, 0, DateTimeKind.Utc);
            var t2   = new DateTime(2014, 10, 22, 10, 0, 0, DateTimeKind.Utc);
            var gen  = new RevisionsGenerator(page.Site, page.Title)
            {
                TimeAscending  = true,
                StartTime      = t1,
                EndTime        = t2,
                PaginationSize = 30,
            };
            var revisions = await gen.EnumItemsAsync().Take(50).ToListAsync();

            Assert.True(revisions.SequenceEqual(revisions.OrderBy(r => r.TimeStamp)));
            Assert.True(revisions.First().TimeStamp >= t1);
            Assert.True(revisions.Last().TimeStamp <= t2);
            // This holds on 2016-12-09
            Assert.Equal(32, revisions.Count);
            ShallowTrace(revisions);
        }