예제 #1
0
        /// <summary>
        /// Deletes the current page.
        /// </summary>
        public async Task <bool> DeleteAsync(string reason, AutoWatchBehavior watch, CancellationToken cancellationToken)
        {
            using (Site.BeginActionScope(this))
                using (await Site.ModificationThrottler.QueueWorkAsync("Delete: " + this, cancellationToken))
                {
                    JToken jresult;
                    try
                    {
                        jresult = await Site.InvokeMediaWikiApiAsync(new MediaWikiFormRequestMessage(new
                        {
                            action = "delete",
                            token = WikiSiteToken.Delete,
                            title = PageStub.HasTitle ? PageStub.Title : null,
                            pageid = PageStub.HasTitle ? null : (int?)PageStub.Id,
                            maxlag = 5,
                            watchlist = watch,
                            reason = reason,
                        }), cancellationToken);
                    }
                    catch (OperationFailedException ex)
                    {
                        switch (ex.ErrorCode)
                        {
                        case "cantdelete": // Couldn't delete "title". Maybe it was deleted already by someone else
                        case "missingtitle":
                            return(false);

                        case "permissiondenied":
                            throw new UnauthorizedOperationException(ex);
                        }
                        throw;
                    }
                    var title = (string)jresult["delete"]["title"];
                    PageStub       = new WikiPageStub(WikiPageStub.MissingPageIdMask, PageStub.Title, PageStub.NamespaceId);
                    LastRevision   = null;
                    LastRevisionId = 0;
                    Site.Logger.LogInformation("[[{Page}]] has been deleted.", title);
                    return(true);
                }
        }
 internal PurgeFailureInfo(WikiPageStub page, string invalidReason)
 {
     Page          = page;
     InvalidReason = invalidReason;
 }
예제 #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 (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);
                }
        }
예제 #4
0
        /// <summary>
        /// Submits content contained in <see cref="Content"/>, making edit to the page.
        /// (MediaWiki 1.16)
        /// </summary>
        /// <param name="summary">The edit summary. Leave it blank to use the default edit summary.</param>
        /// <param name="minor">Whether the edit is a minor edit. (See <a href="https://meta.wikimedia.org/wiki/Help:Minor_edit">m:Help:Minor Edit</a>)</param>
        /// <param name="bot">Whether to mark the edit as bot; even if you are using a bot account the edits will not be marked unless you set this flag.</param>
        /// <param name="watch">Specify how the watchlist is affected by this edit.</param>
        /// <param name="cancellationToken">A token used to cancel the operation.</param>
        /// <returns><c>true</c> if page content has been changed; <c>false</c> otherwise.</returns>
        /// <remarks>
        /// This action will refill <see cref="Id" />, <see cref="Title"/>,
        /// <see cref="ContentModel"/>, <see cref="LastRevisionId"/>, and invalidate
        /// <see cref="ContentLength"/>, <see cref="LastRevision"/>, and <see cref="LastTouched"/>.
        /// You should call <see cref="RefreshAsync()"/> again if you're interested in them.
        /// </remarks>
        /// <exception cref="InvalidOperationException">Cannot create actual page in the specified namespace.</exception>
        /// <exception cref="OperationConflictException">Edit conflict detected.</exception>
        /// <exception cref="UnauthorizedOperationException">You have no rights to edit the page.</exception>
        public async Task <bool> UpdateContentAsync(string summary, bool minor, bool bot, AutoWatchBehavior watch,
                                                    CancellationToken cancellationToken)
        {
            using (Site.BeginActionScope(this))
                using (await Site.ModificationThrottler.QueueWorkAsync("Edit: " + 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 = "edit",
                            token = WikiSiteToken.Edit,
                            title = PageStub.HasTitle ? PageStub.Title : null,
                            pageid = PageStub.HasTitle ? null : (int?)PageStub.Id,
                            minor = minor,
                            bot = bot,
                            recreate = true,
                            maxlag = 5,
                            basetimestamp = LastRevision?.TimeStamp,
                            watchlist = watch,
                            summary = summary,
                            text = Content,
                        }), cancellationToken);
                    }
                    catch (OperationFailedException ex)
                    {
                        switch (ex.ErrorCode)
                        {
                        case "protectedpage":
                            throw new UnauthorizedOperationException(ex);

                        case "pagecannotexist":
                            throw new InvalidOperationException(ex.ErrorMessage, ex);

                        default:
                            throw;
                        }
                    }
                    var jedit  = jresult["edit"];
                    var result = (string)jedit["result"];
                    if (result == "Success")
                    {
                        if (jedit["nochange"] != null)
                        {
                            Site.Logger.LogInformation("Submitted empty edit to page.");
                            return(false);
                        }
                        ContentModel   = (string)jedit["contentmodel"];
                        LastRevisionId = (int)jedit["newrevid"];
                        // jedit["ns"] == null
                        PageStub = new WikiPageStub((int)jedit["pageid"], (string)jedit["title"], PageStub.NamespaceId);
                        Site.Logger.LogInformation("Edited page. New revid={RevisionId}.", LastRevisionId);
                        return(true);
                    }
                    // No "errors" in json result but result is not Success.
                    throw new OperationFailedException(result, (string)null);
                }
        }