Exemplo n.º 1
0
 /// <summary>
 /// Moves (renames) a page. (MediaWiki 1.12)
 /// </summary>
 public Task MoveAsync(string newTitle, string reason, PageMovingOptions options)
 {
     return(MoveAsync(newTitle, reason, options, AutoWatchBehavior.Default, CancellationToken.None));
 }
Exemplo n.º 2
0
        /// <summary>
        /// Moves (renames) a page. (MediaWiki 1.12)
        /// </summary>
        public async Task MoveAsync(string newTitle, string reason, PageMovingOptions options, AutoWatchBehavior watch,
                                    CancellationToken cancellationToken)
        {
            if (newTitle == null)
            {
                throw new ArgumentNullException(nameof(newTitle));
            }
            if (newTitle == Title)
            {
                return;
            }
            using (Site.BeginActionScope(this))
                using (await Site.ModificationThrottler.QueueWorkAsync("Move: " + this, cancellationToken))
                {
                    // When passing this to the Edit API, always pass the token parameter last
                    // (or at least after the text parameter). That way, if the edit gets interrupted,
                    // the token won't be passed and the edit will fail.
                    // This is done automatically by mw.Api.
                    JToken jresult;
                    try
                    {
                        jresult = await Site.InvokeMediaWikiApiAsync(new MediaWikiFormRequestMessage(new
                        {
                            action = "move",
                            token = WikiSiteToken.Move,
                            from = PageStub.HasTitle ? PageStub.Title : null,
                            fromid = PageStub.HasTitle ? null : (int?)PageStub.Id,
                            to = newTitle,
                            maxlag = 5,
                            movetalk = (options & PageMovingOptions.LeaveTalk) != PageMovingOptions.LeaveTalk,
                            movesubpages = (options & PageMovingOptions.MoveSubpages) == PageMovingOptions.MoveSubpages,
                            noredirect = (options & PageMovingOptions.NoRedirect) == PageMovingOptions.NoRedirect,
                            ignorewarnings = (options & PageMovingOptions.IgnoreWarnings) ==
                                             PageMovingOptions.IgnoreWarnings,
                            watchlist = watch,
                            reason = reason,
                        }), cancellationToken);
                    }
                    catch (OperationFailedException ex)
                    {
                        switch (ex.ErrorCode)
                        {
                        case "cantmove":
                        case "protectedpage":
                        case "protectedtitle":
                            throw new UnauthorizedOperationException(ex);

                        default:
                            if (ex.ErrorCode.StartsWith("cantmove"))
                            {
                                throw new UnauthorizedOperationException(ex);
                            }
                            throw;
                        }
                    }
                    var fromTitle = (string)jresult["move"]["from"];
                    var toTitle   = (string)jresult["move"]["to"];
                    Site.Logger.LogInformation("Page [[{fromTitle}]] has been moved to [[{toTitle}]].", fromTitle, toTitle);
                    var link = WikiLink.Parse(Site, toTitle);
                    PageStub = new WikiPageStub(PageStub.Id, toTitle, link.Namespace.Id);
                }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Moves (renames) a page. (MediaWiki 1.12)
        /// </summary>
        public async Task MoveAsync(string newTitle, string reason, PageMovingOptions options, AutoWatchBehavior watch,
                                    CancellationToken cancellationToken)
        {
            if (newTitle == null)
            {
                throw new ArgumentNullException(nameof(newTitle));
            }
            if (newTitle == Title)
            {
                return;
            }
            using (await Site.ModificationThrottler.QueueWorkAsync("Move: " + this, cancellationToken))
            {
                var token = await Site.GetTokenAsync("move", cancellationToken);

                // When passing this to the Edit API, always pass the token parameter last
                // (or at least after the text parameter). That way, if the edit gets interrupted,
                // the token won't be passed and the edit will fail.
                // This is done automatically by mw.Api.
                JToken jresult;
                try
                {
                    jresult = await Site.PostValuesAsync(new
                    {
                        action         = "move",
                        from           = Title,
                        to             = newTitle,
                        maxlag         = 5,
                        movetalk       = (options & PageMovingOptions.LeaveTalk) != PageMovingOptions.LeaveTalk,
                        movesubpages   = (options & PageMovingOptions.MoveSubpages) == PageMovingOptions.MoveSubpages,
                        noredirect     = (options & PageMovingOptions.NoRedirect) == PageMovingOptions.NoRedirect,
                        ignorewarnings = (options & PageMovingOptions.IgnoreWarnings) ==
                                         PageMovingOptions.IgnoreWarnings,
                        watchlist = watch,
                        reason    = reason,
                        token     = token,
                    }, cancellationToken);
                }
                catch (OperationFailedException ex)
                {
                    switch (ex.ErrorCode)
                    {
                    case "cantmove":
                    case "protectedpage":
                    case "protectedtitle":
                        throw new UnauthorizedOperationException(ex);

                    default:
                        if (ex.ErrorCode.StartsWith("cantmove"))
                        {
                            throw new UnauthorizedOperationException(ex);
                        }
                        throw;
                    }
                }
                var fromTitle = (string)jresult["move"]["from"];
                var toTitle   = (string)jresult["move"]["to"];
                Site.Logger?.Info(this, $"Page {fromTitle} has been moved to {toTitle} .");
                Title = toTitle;
            }
        }