This class edits MediaWiki sites using api.php
MediaWiki API manual: https://www.mediawiki.org/wiki/API Site prerequisites: MediaWiki 1.13+ with the following settings: * $wgEnableAPI = true; (enabled by default in DefaultSettings.php) * $wgEnableWriteAPI = true; * AssertEdit extension installed (https://www.mediawiki.org/wiki/Extension:Assert_Edit)
Inheritance: IApiEdit
Beispiel #1
0
        /// <summary>
        /// Creates a new instance of the ApiEdit class by cloning the current instance
        /// ATTENTION: the clones will share the same cookie container, so logging off or logging under another username
        /// with one instance will automatically make another one do the same
        /// </summary>
        public ApiEdit Clone()
        {
            ApiEdit clone = new ApiEdit();
            clone.m_URL = m_URL;
            clone.m_Maxlag = m_Maxlag;
            clone.m_Cookies = m_Cookies;
            clone.ProxySettings = ProxySettings;

            return clone;
        }
Beispiel #2
0
        private void btnLogin_Click(object sender, EventArgs e)
        {
            groupBox2.Enabled = false;
            try
            {
                Editor = new ApiEdit(txtURL.Text);
                Editor.Login(txtUsername.Text, txtPassword.Text);

                groupBox2.Enabled = true;
                txtEdit.Text = "";
                btnSave.Enabled = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, ex.GetType().Name);
            }
        }
 public ApiException(ApiEdit editor, string message)
     : base(message)
 {
     Editor = editor;
 }
Beispiel #4
0
 public SpamlistException(ApiEdit editor, string url)
     : base(editor, "The link '" + url + "' is blocked by spam blacklist")
 {
     URL = url;
 }
Beispiel #5
0
 public NewMessagesException(ApiEdit editor)
     : base(editor, "You have new messages")
 {
 }
Beispiel #6
0
 public MaxlagException(ApiEdit editor, int maxlag, int retryAfter)
     : base(editor, "maxlag", "Maxlag exceeded by " + maxlag + " seconds, retry in " + retryAfter + " seconds")
 {
     Maxlag = maxlag;
     RetryAfter = retryAfter;
 }
Beispiel #7
0
 public InvalidTitleException(ApiEdit editor, string title)
     : base(editor, "Invalid title: \"" + title + "\"")
 {
     InvalidTitle = title;
 }
Beispiel #8
0
 public BrokenXmlException(ApiEdit editor, Exception innerException)
     : base(editor, "Error parsing data returned by server: " + innerException.Message, innerException)
 {
 }
Beispiel #9
0
 public ApiXmlException(ApiEdit editor, XmlException innerException, string get, string post, string content)
     : base(editor, "XmlException in ApiEdit.CheckForErrors", innerException)
 {
     GetUrl = get;
     PostQuery = post;
     Content = content;
 }
 public MediaWikiSaysNoException(ApiEdit editor, string message)
     : base(editor, message)
 {
 }
 public SharedRepoException(ApiEdit editor, string message)
     : base(editor, message)
 {
 }
 public TranslationPageEditException(ApiEdit editor)
     : base(editor, "tpt-target-page", "Can't edit Translation Pages")
 {
 }
 public MediaWikiReadOnlyException(ApiEdit editor, string errorMessage)
     : base(editor, "readonly", errorMessage)
 {
 }
 public ApiOperationFailedException(ApiEdit editor, string action, string result)
     : base(editor, "Operation '" + action + "' ended with result '" + result + "'.")
 {
     Action = action;
     Result = result;
 }
 public ApiException(ApiEdit editor, string message, Exception innerException)
     : base(message, innerException)
 {
     Editor = editor;
 }
 public ApiException(ApiEdit editor, string message, Exception innerException)
     : base(message, innerException)
 {
     Editor = editor;
 }
 public Login()
 {
     editor = new ApiEdit("http://" + _sourceWiki + ".wikipedia.org/w");
     bot    = new ApiEdit("http://" + _destinationWiki + ".wikipedia.org/w");
 }
Beispiel #18
0
 public ApiBlankException(ApiEdit editor)
     : base(editor, "The result returned by server was blank")
 {
 }
Beispiel #19
0
 public BrokenXmlException(ApiEdit editor, string message)
     : base(editor, message)
 {
 }
Beispiel #20
0
 public BrokenXmlException(ApiEdit editor, string message)
     : base(editor, message)
 {
 }
Beispiel #21
0
 public FeatureDisabledException(ApiEdit editor, string errorCode, string errorMessage)
     : base(editor, errorCode, errorMessage)
 {
     DisabledFeature = errorCode.Replace("-disabled", "");
 }
Beispiel #22
0
 public BrokenXmlException(ApiEdit editor, string message, Exception innerException)
     : base(editor, message, innerException)
 {
 }
Beispiel #23
0
 public LoggedOffException(ApiEdit editor)
     : base(editor, "User is logged off")
 {
 }
Beispiel #24
0
 public BrokenXmlException(ApiEdit editor, Exception innerException)
     : base(editor, "Error parsing data returned by server: " + innerException.Message, innerException)
 {
 }
Beispiel #25
0
 public MediaWikiSaysNoException(ApiEdit editor, string message)
     : base(editor, message)
 {
 }
Beispiel #26
0
 public LoginException(ApiEdit editor, string status)
     : base(editor, GetErrorMessage(status))
 {
     StatusCode = status;
 }
Beispiel #27
0
 public RedirectToSpecialPageException(ApiEdit editor)
     : base(editor, "Redirect target is special page")
 {
 }
Beispiel #28
0
 public MaxlagException(ApiEdit editor, int maxlag, int retryAfter)
     : base(editor, "maxlag", "Maxlag exceeded by " + maxlag + " seconds, retry in " + retryAfter + " seconds")
 {
     Maxlag     = maxlag;
     RetryAfter = retryAfter;
 }
Beispiel #29
0
 public ApiErrorException(ApiEdit editor, string errorCode, string errorMessage)
     : base(editor, "API returned the following error: '" + errorMessage + "'")
 {
     ErrorCode = errorCode;
     ApiErrorMessage = errorMessage;
 }
Beispiel #30
0
 public AssertionFailedException(ApiEdit editor, string assertion)
     : base(editor, "Assertion '" + assertion + "' failed")
 {
 }
 public ApiException(ApiEdit editor, string message)
     : base(message)
 {
     Editor = editor;
 }
Beispiel #32
0
 public SpamlistException(ApiEdit editor, string url)
     : base(editor, "The link '" + url + "' is blocked by spam blacklist")
 {
     URL = url;
 }
 public ApiOperationFailedException(ApiEdit editor, string action, string result)
     : base(editor, "Operation '" + action + "' ended with result '" + result + "'.")
 {
     Action = action;
     Result = result;
 }
Beispiel #34
0
 public LoggedOffException(ApiEdit editor)
     : base(editor, "User is logged off")
 {
 }
Beispiel #35
0
 public ApiException(ApiEdit editor, string message, Exception innerException)
     : base(message, innerException)
 {
     Editor = editor;
     ThrowingThread = Thread.CurrentThread;
 }
Beispiel #36
0
 public CaptchaException(ApiEdit editor)
     : base(editor, "Captcha required")
 {
 }
Beispiel #37
0
 public AssertionFailedException(ApiEdit editor, string assertion)
     : base(editor, "Assertion '" + assertion + "' failed")
 {
 }
Beispiel #38
0
 public InterwikiException(ApiEdit editor)
     : base(editor, "Page title contains interwiki")
 {
 }
Beispiel #39
0
 public BrokenXmlException(ApiEdit editor, string message, Exception innerException)
     : base(editor, message, innerException)
 {
 }
Beispiel #40
0
 public NewMessagesException(ApiEdit editor)
     : base(editor, "You have new messages")
 {
 }
Beispiel #41
0
 public CaptchaException(ApiEdit editor)
     : base(editor, "Captcha required")
 {
 }
Beispiel #42
0
 public InvalidTitleException(ApiEdit editor, string title)
     : base(editor, "Invalid title: \"" + title + "\"")
 {
     InvalidTitle = title;
 }
Beispiel #43
0
 public InterwikiException(ApiEdit editor)
     : base(editor, "Page title contains interwiki")
 {
 }
Beispiel #44
0
 public RedirectToSpecialPageException(ApiEdit editor)
     : base(editor, "Redirect target is special page")
 {
 }
Beispiel #45
0
 public AbortedException(ApiEdit editor)
     : base(editor, "API operation aborted")
 {
 }
Beispiel #46
0
 public ApiException(ApiEdit editor, string message, Exception innerException)
     : base(message, innerException)
 {
     Editor         = editor;
     ThrowingThread = Thread.CurrentThread;
 }
Beispiel #47
0
 public LoginException(ApiEdit editor, string status)
     : base(editor, GetErrorMessage(status))
 {
     StatusCode = status;
 }
Beispiel #48
0
 public AbortedException(ApiEdit editor)
     : base(editor, "API operation aborted")
 {
 }
Beispiel #49
0
 public MediaWikiReadOnlyException(ApiEdit editor, string errorMessage)
     : base(editor, "readonly", errorMessage)
 {
 }
Beispiel #50
0
 public AsyncApiEdit(string url, Control parentControl, bool php5)
 {
     Editor        = new ApiEdit(url, php5);
     State         = EditState.Ready;
     ParentControl = parentControl;
 }
Beispiel #51
0
 public ApiBlankException(ApiEdit editor)
     : base(editor, "The result returned by server was blank")
 {
 }
Beispiel #52
0
        public void CopyTemplatesMethod()
        {
            #region resetting things in articlecache
            articleContentCache.ArticleName         = "";
            articleContentCache.Article_WikiContent = "";
            #endregion

            List <string> articleList = this.ListOfArticles;
            string        sourceWiki  = SourceWiki.Trim();
            if (editor == null)
            {
                editor = new ApiEdit("http://" + sourceWiki + ".wikipedia.org/w/", false);
                editor.Login(FormMainWindowUserName, FormMainWindowPassword);
            }

            if (editor.User == null)
            {
                editor.Login(FormMainWindowUserName, FormMainWindowPassword);
            }

            if (editor.User.Name == null)
            {
                editor.Login(FormMainWindowUserName, FormMainWindowPassword);
            }


            if (bot == null)
            {
                bot = new ApiEdit("http://" + m_destinationWiki + ".wikipedia.org/w/", false);
                bot.Login(FormMainWindowUserName, FormMainWindowPassword);
            }
            if (bot.User == null)
            {
                bot.Login(FormMainWindowUserName, FormMainWindowPassword);
            }
            if (bot.User.Name == null)
            {
                bot.Login(FormMainWindowUserName, FormMainWindowPassword);
            }



            int _counter = 0;
            int maxValue = articleList.Count;
            var completedArticlePercentage = 0;
            foreach (string articleName in articleList)
            {
                _counter++;

                try{
                    #region progressbar data
                    if (maxValue > 0)
                    {
                        completedArticlePercentage = ((_counter * 100) / maxValue);
                    }
                    else
                    {
                        completedArticlePercentage = 5;
                    }
                    if (OnRobotPercentageFeedback != null)
                    {
                        OnRobotPercentageFeedback(completedArticlePercentage);
                    }

                    #endregion

                    string TemplateName = "Template:" + articleName;


                    Debug.WriteLine(_counter + ")ArticleName:" + articleName);
                    if (OnRobotLogFeedBack != null)
                    {
                        OnRobotLogFeedBack("\n----------------------------------------------\n");
                        OnRobotLogFeedBack("Opening article:" + articleName + " from " + editor.ApiURL);
                    }
                    string englishArticle = editor.Open(TemplateName, false);

                    //clearing cache
                    articleContentCache.ClearCache();
                    articleContentCache.ArticleName         = TemplateName;
                    articleContentCache.Article_WikiContent = englishArticle;



                    if (!string.IsNullOrEmpty(articleContentCache.Article_WikiContent))
                    {
                        if (OnRobotLogFeedBack != null)
                        {
                            OnRobotLogFeedBack("Opening article:" + articleContentCache.ArticleName + " from " + bot.ApiURL);
                        }
                        string nepaliArticle = bot.Open(articleContentCache.ArticleName, false); //Don't know what to do for content "nepaliArticle"

                        //if(sourceWiki.ToLower().Trim()=="hi"){
                        //this.richTextBoxHindi.Text = englishArticle;
                        //richTextBoxHindi.Invoke(this.OnHindiTextObtained, englishArticle);

                        if (EnableTranslation)
                        {
                            this.TranslateTemplateThread();
                            if (OnRobotLogFeedBack != null)
                            {
                                if (templateTranslatorThread != null)
                                {
                                    OnRobotLogFeedBack("\nThread started : " + templateTranslatorThread.Name);
                                }
                            }

                            //Thread Joining here
                            if (templateTranslatorThread != null)
                            {
                                templateTranslatorThread.Join();
                            }
                            //end thread join


                            if (OnRobotLogFeedBack != null)
                            {
                                OnRobotLogFeedBack("\nText to translate : " + articleContentCache.Article_WikiContent);
                            }


                            if (OnRobotLogFeedBack != null)
                            {
                                OnRobotLogFeedBack("\ntranslated text : " + articleContentCache.TranslatedArticle_Content);
                            }

                            if (OnArticleCacheReady != null)
                            {
                                if (OnRobotLogFeedBack != null)
                                {
                                    OnRobotLogFeedBack("Sending article cache");
                                }
                                OnArticleCacheReady(articleContentCache);
                            }

                            while (this.templateTranslatorThread.IsAlive)
                            {
                                //MessageBox.Show("Editor is busy");
                                if (OnRobotLogFeedBack != null)
                                {
                                    OnRobotLogFeedBack("\n Editor is busy");
                                }
                                Thread.Sleep(5000);
                            }
                        }



                        if (!bot.URL.ToLower().Contains("http://en.wikipedia.org/w/") || (!bot.URL.ToLower().Contains("http://" + sourceWiki + ".wikipedia.org/w/"))) //Don't mess with en wikipedia and sourceWikipedia
                        {                                                                                                                                             //Write protect for english wikipedia
                            string editSummary = string.IsNullOrEmpty(EditSummary)
                                                 ? "Copy template from " + sourceWiki
                                                 : EditSummary;
                            editSummary = editSummary + " from  " + editor.URL + "index.php?oldid=" + editor.Page.RevisionID.ToString() + " [[:" + sourceWiki + ":" + editor.Page.Title + "]]";
                            bool _templateSaveSuccess = true;
                            if (!bot.Page.Exists)
                            {
                                if (OnRobotLogFeedBack != null)
                                {
                                    OnRobotLogFeedBack("Going to save " + bot.Page.Title);
                                }
                                string _nepaliArticleToSave = articleContentCache.Article_WikiContent;
                                if (articleContentCache.IsTranslatedArticle)
                                {
                                    _nepaliArticleToSave = articleContentCache.TranslatedArticle_Content;
                                }

                                if (OnArticleCacheReady != null)
                                {
                                    OnArticleCacheReady(articleContentCache);
                                }

                                SaveInfo saveInfo = bot.Save(_nepaliArticleToSave, editSummary, false, WatchOptions.NoChange);

                                if (OnRobotLogFeedBack != null)
                                {
                                    OnRobotLogFeedBack("Saved : " + bot.Page.Title + saveInfo.ResponseXml.InnerText);
                                }
                                if (!TemplateName.EndsWith("/doc"))
                                {
                                    string templateDocName    = TemplateName.Trim() + "/doc";
                                    string englishTemplateDoc = editor.Open(templateDocName);
                                    if (!string.IsNullOrEmpty(englishTemplateDoc))
                                    {
                                        string nepaliArticleDoc = bot.Open(templateDocName, false);
                                        if (!bot.Page.Exists)
                                        {
                                            SaveInfo docSaveInfo = bot.Save(englishTemplateDoc, editSummary, false,
                                                                            WatchOptions.NoChange);
                                            if (OnRobotLogFeedBack != null)
                                            {
                                                OnRobotLogFeedBack("\n Found a template doc, Copied Template doc as well : " + docSaveInfo.ResponseXml);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if (OnArticleProcessed != null)
                    {
                        OnArticleProcessed(articleContentCache);
                    }
                    Thread.Sleep(m_ThreadSleepTime); //Let Wiki Breathe
                }catch (Exception e) {
                    WikiFunctions.ErrorHandler.Handle(e);
                }
            }
        }
Beispiel #53
0
 public OperationFailedException(ApiEdit editor, string action, string result, string xml)
     : base(editor, "Operation '" + action + "' ended with result '" + result + "'.\r\nXml: '" + xml + "'")
 {
     Action = action;
     Result = result;
     Xml = xml;
 }
 private AsyncApiEdit(ApiEdit editor, Control parentControl)
 {
     SynchronousEditor = editor;
     ParentControl     = parentControl;
     State             = EditState.Ready;
 }
Beispiel #55
0
 public SharedRepoException(ApiEdit editor, string message)
     : base(editor, message)
 {
 }
 public ApiErrorException(ApiEdit editor, string errorCode, string errorMessage)
     : base(editor, "Bot API returned the following error: '" + errorMessage + "'")
 {
     ErrorCode       = errorCode;
     ApiErrorMessage = errorMessage;
 }
Beispiel #57
0
 public TranslationPageEditException(ApiEdit editor)
     : base(editor, "tpt-target-page", "Can't edit Translation Pages")
 {
 }
 public FeatureDisabledException(ApiEdit editor, string errorCode, string errorMessage)
     : base(editor, errorCode, errorMessage)
 {
     DisabledFeature = errorCode.Replace("-disabled", "");
 }
 public AsyncApiEdit(string url, Control parentControl, bool php5)
 {
     Editor = new ApiEdit(url, php5);
     State = EditState.Ready;
     m_ParentControl = parentControl;
 }
 public ApiLoggedOffException(ApiEdit editor)
     : base(editor, "You are currently logged off")
 {
 }