User:Lucasbfrbot/CommonsDelinker

From Wikipedia, the free encyclopedia
        public static void OrphanImagesDifferentName(WikiCrawler enwp, WikiCrawler commons, bool forcePurge)
        {

            // get the images:
            var pages = GetImages(enwp, "Category:Wikipedia files with a different name on Wikimedia Commons", Settings.ApplicationSettings.MaxNumberOfItems);
            
            var result = ProcessImages(enwp, commons, pages, true);

            enwp.CreateGallery(result.ImagesToOrphan, Settings.ApplicationSettings.galeryPath + " (Ok)", "commons");
            commons.CreateGallery(result.ImagesToOrphan, Settings.ApplicationSettings.galeryPath + " (Ok)", "en", true);

            enwp.CreateGallery(result.ImagesToOrphanWithWarning, Settings.ApplicationSettings.galeryPath + " (Warn)", "commons");
            commons.CreateGallery(result.ImagesToOrphanWithWarning, Settings.ApplicationSettings.galeryPath + " (Warn)", "en", true);

            enwp.CreateGallery(result.ImagesThatCantbeProcessed, Settings.ApplicationSettings.galeryPath + " (Errors)", "commons");
            commons.CreateGallery(result.ImagesThatCantbeProcessed, Settings.ApplicationSettings.galeryPath + " (Errors)", "en", true);

            Console.Write("Proceed? ");
            if (Console.ReadLine() == "y")
            {

                string gallery = enwp.GetPageText(Settings.ApplicationSettings.galeryPath + " (Ok)");
                // Delete the stuff on OK
                foreach (ImageStatus image in result.ImagesToOrphan)
                {
                    if (gallery.Contains(image.Name))
                    {
                        foreach (string pageName in image.Usages)
                        {
                            enwp.Orphan(image, pageName, forcePurge);
                        }
                    }
                    else
                    {
                        Console.WriteLine("Skipping " + image.Name);
                    }
                }


                // confirm the Warns:
                var imagesConfirmed = new List<ImageStatus>();
                foreach (ImageStatus image in result.ImagesToOrphanWithWarning)
                {
                    if (!String.IsNullOrEmpty(image.Name))
                    {
                        Console.Write(String.Format("Orphan {0} ({1})? (y/n) ", image.Name, image.Status));
                        if (Console.ReadLine() == "y")
                        {
                            imagesConfirmed.Add(image);

                        }
                    }
                }

                foreach (ImageStatus image in imagesConfirmed)
                {
                    foreach (string pageName in image.Usages)
                    {
                        enwp.Orphan(image, pageName, forcePurge);
                    }
                }
            }

        }
        private static ImageStatus GetImageStatus(ImageInformations infosEnwp, ImageInformations infosCommons)
        {
            ImageStatus status = new ImageStatus
            {
                Name = infosEnwp.Name,
                Uploader = infosEnwp.Uploader,
                Duplicate = infosCommons.Name,
                DuplicateUploader = infosCommons.Uploader
            };
            // check the shas are the same:
            if (infosCommons.Hash == infosEnwp.Hash)
            {
                // brain check:
                if (infosCommons.Height == infosEnwp.Height
                    && infosCommons.Width == infosEnwp.Width)
                {
                    if (infosEnwp.Date.AddDays(1) > DateTime.Now)
                    {
                        Console.WriteLine("Error: " + infosEnwp.Name + " was edited today, skipping!");
                        status.StatusCode = StatusCode.Error;
                        status.Status = "Image was edited today, skipping";
                        return status;
                    }
                    else if (!infosEnwp.IsProtected)
                    {
                        status.StatusCode = StatusCode.Ok;
                        status.Status = infosCommons.Date.ToShortDateString();
                        return status;
                    }
                    else
                    {
                        Console.WriteLine("Warning: " + infosEnwp.Name + " is protected!");
                        status.StatusCode = StatusCode.Warn;
                        status.Status = "Image is protected";
                        return status;

                    }
                }
                else
                {
                    Console.WriteLine("ERROR: " + infosEnwp.Name + " SHAS IDENTICAL BUT SIZES DIFFER");
                    status.StatusCode = StatusCode.Error;
                    status.Status = "SHA error";
                    return status;
                }
            }
            else
            {
                if (infosCommons.Date < infosEnwp.Date)
                {
                    Console.WriteLine("Warning: " + infosEnwp.Name + " is newer on en.wp!");
                    status.StatusCode = StatusCode.Warn;
                    status.Status = String.Format("newer on en.wp: en: {0} - c: {1}!", infosEnwp.Date.ToShortDateString(), infosCommons.Date.ToShortDateString());
                    return status;

                }
                else if (infosCommons.Size < infosEnwp.Size)
                {
                    Console.WriteLine("Warning: " + infosEnwp.Name + " is bigger on en.wp!");
                    status.StatusCode = StatusCode.Warn;
                    status.Status = String.Format("bigger on en.wp: en: {0}k - c: {1}k!", ((int)infosEnwp.Size / 1000), ((int)infosCommons.Size / 1000));
                    return status;

                }
                else
                {
                    Console.WriteLine("Warning: " + infosEnwp.Name + " is different!");
                    status.StatusCode = StatusCode.Warn;
                    status.Status = String.Format("different sizes: en: {0}k - c: {1}k!", ((int)infosEnwp.Size / 1000), ((int)infosCommons.Size / 1000));
                    return status;
                }
            }
        }

        private static ImageStatus GetImageStatus(string page, WikiCrawler enwp, WikiCrawler commons, bool considerAlternateNames)
        {
            // check the image is free
            var copyrightStatus = enwp.GetCopyrightStatus(page);
            var infosEnwp = enwp.GetImageProperties(page);
            var infosCommons = commons.GetImageProperties(infosEnwp, considerAlternateNames);
            if (infosCommons.Exists)
            {
                ImageStatus fileStatus = GetImageStatus(infosEnwp, infosCommons);
                if (fileStatus.StatusCode == StatusCode.Ok)
                {
                    var commonsStatus = commons.GetTransfertStatus(infosCommons.Name, infosEnwp.Uploader);
                    copyrightStatus.CopyrightTag += "<br /> " + commonsStatus.CopyrightTag;
                    if (commonsStatus.StatusCode == StatusCode.Error
                        || (commonsStatus.StatusCode == StatusCode.Warn && infosCommons.Uploader != infosEnwp.Uploader))
                    {
                        Console.WriteLine("WARN: " + page + " doesn't seem to have been transfered correctly!");
                        fileStatus.StatusCode = StatusCode.Warn;
                        fileStatus.Status = commonsStatus.Status;
                    }

                }

                fileStatus.CombineWith(copyrightStatus);

                return fileStatus;
            }
            else
            {
                Console.WriteLine("ERROR: " + page + " does not exist on commons!");
                var fileStatus = new ImageStatus(page, StatusCode.Error, "not on commons");
                fileStatus.CombineWith(copyrightStatus);
                return fileStatus;
            }
        }

        struct CheckResults
        {
            public List<ImageStatus> DeletableImages;
            public List<ImageStatus> ImagesToOrphan;
            public List<ImageStatus> ImagesToOrphanWithWarning;
            public List<ImageStatus> DeletableImagesWithWarning;
            public List<ImageStatus> ImagesThatCantbeProcessed;
        }