/// <summary>
 /// Asynchronously purges a sequence of pages with the given options.
 /// </summary>
 /// <returns>A collection of pages that haven't been successfully purged, because of either missing or invalid titles.</returns>
 public static Task <IReadOnlyCollection <WikiPage> > PurgeAsync(this IEnumerable <WikiPage> pages,
                                                                 PagePurgeOptions options, CancellationToken cancellationToken)
 {
     return(RequestHelper.PurgePagesAsync(pages, options, cancellationToken));
 }
示例#2
0
        /// <summary>
        /// Asynchronously purges the pages.
        /// </summary>
        /// <returns>A collection of pages that haven't been successfully purged, because of either missing or invalid titles.</returns>
        public static async Task <IReadOnlyCollection <PurgeFailureInfo> > PurgePagesAsync(IEnumerable <WikiPage> pages, PagePurgeOptions options, CancellationToken cancellationToken)
        {
            if (pages == null)
            {
                throw new ArgumentNullException(nameof(pages));
            }
            List <PurgeFailureInfo> failedPages = null;

            // You can even purge pages from different sites.
            foreach (var sitePages in pages.GroupBy(p => new WikiPageGroupKey(p)))
            {
                var site       = sitePages.Key.Site;
                var titleLimit = site.AccountInfo.HasRight(UserRights.ApiHighLimits)
                    ? 500
                    : 50;
                using (site.BeginActionScope(sitePages, options))
                {
                    foreach (var partition in sitePages.Partition(titleLimit).Select(partition => partition.ToList()))
                    {
                        string titles;
                        string ids;
                        if (sitePages.Key.HasTitle)
                        {
                            // If a page has both title and ID information,
                            // we will use title anyway.
                            site.Logger.LogDebug("Purging {Count} pages by title.", partition.Count);
                            titles = MediaWikiHelper.JoinValues(partition.Select(p => p.Title));
                            ids    = null;
                        }
                        else
                        {
                            site.Logger.LogDebug("Purging {Count} pages by ID.", partition.Count);
                            Debug.Assert(sitePages.All(p => p.PageStub.HasId));
                            titles = null;
                            ids    = MediaWikiHelper.JoinValues(partition.Select(p => p.Id));
                        }
                        try
                        {
                            var jresult = await site.InvokeMediaWikiApiAsync(new MediaWikiFormRequestMessage(new
                            {
                                action = "purge",
                                titles = titles,
                                pageids = ids,
                                forcelinkupdate = (options & PagePurgeOptions.ForceLinkUpdate) == PagePurgeOptions.ForceLinkUpdate,
                                forcerecursivelinkupdate = (options & PagePurgeOptions.ForceRecursiveLinkUpdate) == PagePurgeOptions.ForceRecursiveLinkUpdate,
                            }), cancellationToken);

                            // Now check whether the pages have been purged successfully.
                            foreach (var jitem in jresult["purge"])
                            {
                                if (jitem["missing"] != null || jitem["invalid"] != null)
                                {
                                    if (failedPages == null)
                                    {
                                        failedPages = new List <PurgeFailureInfo>();
                                    }
                                    failedPages.Add(new PurgeFailureInfo(MediaWikiHelper.PageStubFromJson((JObject)jitem), (string)jitem["invalidreason"]));
                                }
                            }
                        }
                        catch (OperationFailedException ex)
                        {
                            if (ex.ErrorCode == "cantpurge")
                            {
                                throw new UnauthorizedOperationException(ex);
                            }
                            throw;
                        }
                    }
                }
            }
            return(failedPages ?? emptyPurgeFailures);
        }
 /// <summary>
 /// Asynchronously purges a sequence of pages with the given options.
 /// </summary>
 /// <returns>A collection of pages that haven't been successfully purged, because of either missing or invalid titles.</returns>
 public static Task <IReadOnlyCollection <WikiPage> > PurgeAsync(this IEnumerable <WikiPage> pages,
                                                                 PagePurgeOptions options)
 {
     return(PurgeAsync(pages, options, new CancellationToken()));
 }
示例#4
0
 /// <summary>
 /// Asynchronously purges a sequence of pages with the given options.
 /// </summary>
 /// <returns>A collection of pages that haven't been successfully purged, because of either missing or invalid titles.</returns>
 public static Task <IReadOnlyCollection <PurgeFailureInfo> > PurgeAsync(this IEnumerable <WikiPage> pages,
                                                                         PagePurgeOptions options)
 {
     return(PurgeAsync(pages, options, CancellationToken.None));
 }
示例#5
0
        /// <summary>
        /// Asynchronously purges the current page with the given options.
        /// </summary>
        /// <returns><c>true</c> if the page has been successfully purged.</returns>
        public async Task <bool> PurgeAsync(PagePurgeOptions options, CancellationToken cancellationToken)
        {
            var failure = await RequestHelper.PurgePagesAsync(new[] { this }, options, cancellationToken);

            return(failure.Count == 0);
        }
示例#6
0
 /// <summary>
 /// Asynchronously purges the current page with the given options.
 /// </summary>
 /// <returns><c>true</c> if the page has been successfully purged.</returns>
 public Task <bool> PurgeAsync(PagePurgeOptions options)
 {
     return(PurgeAsync(options, CancellationToken.None));
 }
示例#7
0
 /// <summary>
 /// Asynchronously purges the current page with the given options.
 /// </summary>
 /// <returns><c>true</c> if the page has been successfully purged.</returns>
 public Task <bool> PurgeAsync(PagePurgeOptions options)
 {
     return(PurgeAsync(options, new CancellationToken()));
 }
示例#8
0
        /// <summary>
        /// Asynchronously purges the pages.
        /// </summary>
        /// <returns>A collection of pages that haven't been successfully purged, because of either missing or invalid titles.</returns>
        public static async Task <IReadOnlyCollection <WikiPage> > PurgePagesAsync(IEnumerable <WikiPage> pages,
                                                                                   PagePurgeOptions options, CancellationToken cancellationToken)
        {
            if (pages == null)
            {
                throw new ArgumentNullException(nameof(pages));
            }
            var failedPages = new List <WikiPage>();

            // You can even purge pages from different sites.
            foreach (var sitePages in pages.GroupBy(p => Tuple.Create(p.Site, p.GetType())))
            {
                var site       = sitePages.Key.Item1;
                var titleLimit = site.AccountInfo.HasRight(UserRights.ApiHighLimits)
                    ? 500
                    : 50;
                foreach (var partition in sitePages.Partition(titleLimit).Select(partition => partition.ToList()))
                {
                    site.Logger?.Trace(site, $"Purging {partition.Count} pages.");
                    // We use titles to purge pages.
                    try
                    {
                        var jresult = await site.PostValuesAsync(new
                        {
                            action          = "purge",
                            titles          = string.Join("|", partition.Select(p => p.Title)),
                            forcelinkupdate =
                                (options & PagePurgeOptions.ForceLinkUpdate) == PagePurgeOptions.ForceLinkUpdate,
                            forcerecursivelinkupdate =
                                (options & PagePurgeOptions.ForceRecursiveLinkUpdate) ==
                                PagePurgeOptions.ForceRecursiveLinkUpdate,
                        }, cancellationToken);

                        // Now check whether the pages have been purged successfully.
                        // Process title normalization.
                        var normalized = jresult["normalized"]?.ToDictionary(n => (string)n["from"],
                                                                             n => (string)n["to"]);
                        var purgeStatusDict = jresult["purge"].ToDictionary(o => o["title"]);
                        foreach (var page in partition)
                        {
                            var title = page.Title;
                            // Normalize the title.
                            if (normalized?.ContainsKey(title) ?? false)
                            {
                                title = normalized[title];
                            }
                            // No redirects here ^_^
                            var jpage = purgeStatusDict[title];
                            if (jpage["invalid"] != null || jpage["missing"] != null)
                            {
                                site.Logger?.Warn(site, $"Cannot purge the page: {page}. {jpage["invalidreason"]}");
                                failedPages.Add(page);
                            }
                        }
                    }
                    catch (OperationFailedException ex)
                    {
                        if (ex.ErrorCode == "cantpurge")
                        {
                            throw new UnauthorizedOperationException(ex);
                        }
                        throw;
                    }
                }
            }
            return(failedPages);
        }