/// <summary>Undoes all last edits of last page contributor, so page text reverts to
 ///  previous contents. The function doesn't affect other operations
 /// like renaming or protecting.</summary>
 /// <param name="comment">Revert comment.</param>
 /// <param name="isMinorEdit">Minor edit mark (pass true for minor edit).</param>
 public void UndoLastEdits(string comment, bool isMinorEdit)
 {
     if (string.IsNullOrEmpty(title))
         throw new WikiBotException(Bot.Msg("No title specified for page to revert."));
     PageList pl = new PageList(site);
     string lastEditor = "";
     for (int i = 50; i <= 5000; i *= 10) {
         if (Bot.useBotQuery == true && site.botQuery == true &&
             site.botQueryVersions.ContainsKey("ApiQueryRevisions.php"))
                 pl.FillFromPageHistoryEx(title, i, false);
         else
             pl.FillFromPageHistory(title, i);
         lastEditor = pl[0].lastUser;
         foreach (Page p in pl)
             if (p.lastUser != lastEditor) {
                 p.Load();
                 Save(p.text, comment, isMinorEdit);
                 Bot.LogEvent(
                     Bot.Msg("Last edits of page \"{0}\" by user {1} were undone."),
                     title, lastEditor);
                 return;
             }
         pl.Clear();
     }
     Console.Error.WriteLine(Bot.Msg("Can't undo last edits of page \"{0}\" by user {1}."),
         title, lastEditor);
 }
 /// <summary>Undoes the last edit, so page text reverts to previous contents.
 /// The function doesn't affect other operations like renaming.</summary>
 /// <param name="comment">Revert comment.</param>
 /// <param name="isMinorEdit">Minor edit mark (pass true for minor edit).</param>
 public void Revert(string comment, bool isMinorEdit)
 {
     if (string.IsNullOrEmpty(title))
         throw new WikiBotException(Bot.Msg("No title specified for page to revert."));
     PageList pl = new PageList(site);
     if (Bot.useBotQuery == true && site.botQuery == true &&
         site.botQueryVersions.ContainsKey("ApiQueryRevisions.php"))
             pl.FillFromPageHistoryEx(title, 2, false);
     else
         pl.FillFromPageHistory(title, 2);
     if (pl.Count() != 2) {
         Console.Error.WriteLine(Bot.Msg("Can't revert page \"{0}\"."), title);
         return;
     }
     pl[1].Load();
     Save(pl[1].text, comment, isMinorEdit);
     Bot.LogEvent(Bot.Msg("Page \"{0}\" was reverted."), title);
 }
Exemple #3
0
	public static int Main()
	{

		// Se registra en la página y configura el bot
		Site welp = iniciar();

		leerOpciones(Environment.GetCommandLineArgs());

		if (welp == null)
			return 1;

		// Cuenta del número de ediciones
		int cuenta = 0;

		// Obtiene todos los trabajos (de momento en el espacio de nombre Principal)
		PageList todas = new PageList(welp);

		todas.FillFromAllPages("Trabajo:", 0, false, Int32.MaxValue, "Trabajo;");

		foreach (Page pag in todas)
		{
			pag.Load();

			// Si ya hay indicación de curso no hace nada
			List<string> cats = pag.GetCategories();

			if (cats.Exists(patronCCurso.IsMatch))
				continue;

			// Para averiguar el curso obtiene la fecha de la
			// primera edición
			PageList hist = new PageList(welp);

			hist.FillFromPageHistory(pag.title, Int32.MaxValue);

			DateTime fc = hist[hist.Count()-1].timestamp;

			// Distingue en base a ella el curso
			int año = fc.Year;

			// Si es antes del 29 de septiembre (aprox) es que es
			// del curso que empieza en el año anterior
			if (fc.Month < 9 || fc.Month == 9 && fc.Day < 29)
				año--;

			string curso = "Curso " + año + "-" + (año + 1);

			// Muestra información por consola
			Console.Error.WriteLine("«" + pag.title + "» creado en " + fc + " => " + curso);

			cuenta++;

			if (!simulado) {
				pag.AddToCategory(curso);

				pag.Save("bot: categorización de trabajos por curso", true);
			}
		}

		// Resumen de las operaciones
		Console.Error.WriteLine("El bot " + (simulado ? "hubiera realizado " : "realizó ") + cuenta + " ediciones.");

		return 0;
	}
Exemple #4
0
        private static bool UploadOriginalVersion(out string failureReason, Page page, string mediaUrl,
                                                  string wikiImageFilename, string picasaImageFilename,
                                                  bool fetchThumbnailVersion, bool allowWikiBigger)
        {
            // if (!wikiImageFilename.ToLower().EndsWith(".jpg") && !wikiImageFilename.ToLower().EndsWith(".jpeg") &&
            //     !wikiImageFilename.ToLower().EndsWith(".png"))
            // {
            //     failureReason = "Cannot compare non-bitmap files to original source - requires manual validation";
            //     return false;
            // }

            failureReason = null;

            Bitmap wikiBitmap = new Bitmap(wikiImageFilename);

            // First have the Picasa server resize to the desired size - this will
            // ensure exactly the same resizing algorithm is used.
            string thumbnailUrl =
                new Regex("^(.*)/([^/]*)$").Replace(mediaUrl, "${1}/s" + wikiBitmap.Width + "/${2}");

            string filename = "temp_picasa_image";
            if (!fetchThumbnailVersion || !WgetToFile(thumbnailUrl, filename))
            {
                filename = picasaImageFilename;
            }
            Bitmap picasaBitmap = new Bitmap(filename);

            if (wikiBitmap.Width < picasaBitmap.Width ||
                wikiBitmap.Height < picasaBitmap.Height)
            {
                // Couldn't get version of same size from server - stretch to fit
                Bitmap newPicasaBitmap = new Bitmap(picasaBitmap, wikiBitmap.Width, wikiBitmap.Height);
                picasaBitmap.Dispose();
                picasaBitmap = newPicasaBitmap;
            }

            bool wikiBitmapIsBigger = false;
            if (wikiBitmap.Width >= picasaBitmap.Width ||
                wikiBitmap.Height >= picasaBitmap.Height)
            {
                if (allowWikiBigger)
                {
                    wikiBitmapIsBigger = true;
                    Bitmap newWikiBitmap = new Bitmap(wikiBitmap, picasaBitmap.Width, picasaBitmap.Height);
                    wikiBitmap.Dispose();
                    wikiBitmap = newWikiBitmap;
                }
                else
                {
                    // Wiki version is bigger, something odd going on, skip it
                    wikiBitmap.Dispose();
                    picasaBitmap.Dispose();
                    failureReason = "license matches and is valid - but Commons version is of a different size than the Picasa version, may have been edited locally";
                    return false;
                }
            }

            double avgDiff = ImagesMeanSquareDifference(wikiBitmap, picasaBitmap);

            wikiBitmap.Dispose();
            picasaBitmap.Dispose();

            if (avgDiff >= 0.032 && avgDiff < 0.10)
            {
                failureReason = "license matches and is valid - but Picasa and Commons image were not a close enough match";
                return false;
            }
            else if (avgDiff < 0.032)
            {
                // Got an approximate match, need to upload the full-size version
                // (unless we've done so before and were reverted)
                PageList pageHistory = new PageList(page.site);
                pageHistory.FillFromPageHistory(page.title, int.MaxValue);
                bool alreadyUploaded = false;
                foreach (Page revision in pageHistory)
                {
                    if (revision.lastUser == username && revision.comment.Contains("uploaded a new version"))
                    {
                        alreadyUploaded = true;
                    }
                }

                if (!alreadyUploaded && !wikiBitmapIsBigger)
                {
                    string saveText = page.text;
                    page.UploadImage(picasaImageFilename,
                                     "Uploading version from source, revert me if incorrect",
                                     "", "", "");
                    // Set back to current wikitext
                    page.Load();
                    page.text = saveText;
                }
                return true;
            }
            else
            {
                failureReason = "license matches and is valid - but Picasa and Commons image do not match";
                return false;
            }
        }
 /// <summary>Undoes all last edits made by last contributor.
 /// The function doesn't affect other operations
 /// like renaming or protecting.</summary>
 /// <param name="comment">Comment.</param>
 /// <param name="minorEditByDefault">Minor edit mark (pass true for minor edit).</param>
 /// <returns>Returns true if last edits were undone.</returns>
 public bool UndoLastEdits(string comment, bool isMinorEdit)
 {
     if (string.IsNullOrEmpty(title))
         throw new WikiBotException(Bot.Msg("No title specified for page to revert."));
     PageList pl = new PageList(site);
     string lastEditor = "";
     for (int i = 50; i <= 5000; i *= 10) {
         pl.FillFromPageHistory(title, i);
         lastEditor = pl[0].lastUser;
         foreach (Page p in pl)
             if (p.lastUser != lastEditor) {
                 p.Load();
                 Save(p.text, comment, isMinorEdit);
                 Console.WriteLine(
                     Bot.Msg("Last edits of page \"{0}\" by user {1} were undone."),
                     title, lastEditor);
                 return true;
             }
         if (pl.pages.Count < i)
             break;
         pl.Clear();
     }
     Console.Error.WriteLine(Bot.Msg("Can't undo last edits of page \"{0}\" by user {1}."),
         title, lastEditor);
     return false;
 }
 /// <summary>Undoes the last edit, so page text reverts to previous contents.
 /// The function doesn't affect other actions like renaming.</summary>
 /// <param name="comment">Revert comment.</param>
 /// <param name="minorEditByDefault">Minor edit mark (pass true for minor edit).</param>
 public void Revert(string comment, bool isMinorEdit)
 {
     if (string.IsNullOrEmpty(title))
         throw new WikiBotException(Bot.Msg("No title specified for page to revert."));
     PageList pl = new PageList(site);
     pl.FillFromPageHistory(title, 2);
     if (pl.Count() != 2) {
         Console.Error.WriteLine(Bot.Msg("Can't revert page \"{0}\"."), title);
         return;
     }
     pl[1].Load();
     Save(pl[1].text, comment, isMinorEdit);
     Console.WriteLine(Bot.Msg("Page \"{0}\" was reverted."), title);
 }