Пример #1
0
        private static async Task <WikiLinkList> _generateLinkifyListAsync()
        {
            // Returns a dictionary of substrings that should be turned into page links in page content.

            WikiLinkList list = new WikiLinkList();

            // Add species names to the dictionary.

            Species[] species_list = await SpeciesUtils.GetSpeciesAsync();

            foreach (Species species in species_list)
            {
                list.Add(species.ShortName.ToLower(), species.FullName);
                list.Add(species.FullName.ToLower(), species.FullName);
                list.Add(species.Name.ToLower(), species.FullName);

                if (!string.IsNullOrEmpty(species.CommonName))
                {
                    list.Add(species.CommonName.ToLower(), species.FullName);
                }
            }

            foreach (Species species in species_list)
            {
                // Also linkify binomial names that might be using outdated genera (e.g. Species moved to a new genus since the description was written).
                // Only do this for species that have a unique name-- otherwise, there's no way to know for sure which species to link to!
                // This might create some false-positives, so it could be a good idea to limit matches only to known genera (at the expense of a significantly longer regex).

                if (list.Count(x => x.Value == species.Name.ToLower()) == 1)
                {
                    list.Add(string.Format(WikiPageUtils.UnlinkedWikiTextPatternFormat, @"[A-Z](?:[a-z]+|\.)\s" + Regex.Escape(species.Name.ToLower())), species.FullName, WikiLinkListDataType.Regex);
                }
            }

            // Add zone names to the dictionary.

            Zone[] zones_list = await ZoneUtils.GetZonesAsync();

            foreach (Zone zone in zones_list)
            {
                list.Add(zone.FullName.ToLower(), zone.FullName);
            }

            return(list);
        }
Пример #2
0
        public static string FormatPageLinksIf(string content, WikiLinkList linkifyList, Func <WikiLinkListData, bool> condition)
        {
            // Keys to be replaced are sorted by length so longer strings are replaced before their substrings.
            // For example, "one two" should have higher priority over "one" and "two" individually.

            // Additionally, filter the list so that we only have unique values to avoid performing replacements more than once.
            // This can cause some incorrect mappings, but it's the best we can do.
            // Note that "GroupBy" preserves the order of the elements.

            foreach (WikiLinkListData data in linkifyList.GroupBy(x => x.Value).Select(x => x.First()).OrderByDescending(x => x.Value.Length))
            {
                if (!condition(data))
                {
                    continue;
                }

                string pageTitle = data.Target;
                Regex  regex     = data.Type == WikiLinkListDataType.Find ? new Regex(string.Format(UnlinkedWikiTextPatternFormat, Regex.Escape(data.Value)), RegexOptions.IgnoreCase)
                    : new Regex(data.Value);

                content = regex.Replace(content, m => {
                    string matchValue = m.Value;

                    if (pageTitle == matchValue)
                    {
                        return(string.Format("[[{0}]]", matchValue));
                    }
                    else
                    {
                        return(string.Format("[[{0}|{1}]]", pageTitle, matchValue));
                    }
                });
            }

            return(content);
        }
Пример #3
0
        public async Task MainAsync(string[] args)
        {
            _log("loading configuration");

            Config config = JsonConvert.DeserializeObject <Config>(System.IO.File.ReadAllText("wikibot-config.json"));

            _log("initializing mediawiki client");

            MediaWikiClient client = new MediaWikiClient {
                Protocol  = config.Protocol,
                Server    = config.Server,
                ApiPath   = config.ApiPath,
                UserAgent = config.UserAgent
            };

            client.Log += _log;

            EditHistory history = new EditHistory();

            if (client.Login(config.Username, config.Password).Success)
            {
                _log("generating link dictionary");

                WikiLinkList LinkifyList = await _generateLinkifyListAsync();

                _log("synchronizing species");
                _log("getting species from database");

                Species[] speciesList = await SpeciesUtils.GetSpeciesAsync();

                _log(string.Format("got {0} results", speciesList.Count()));

                foreach (Species species in speciesList)
                {
                    _log(string.Format("synchronizing species {0}", species.ShortName));

                    // Create the page builder.

                    SpeciesPageBuilder pageBuilder = new SpeciesPageBuilder(species, WikiPageTemplate.Open(SpeciesTemplateFilePath))
                    {
                        AllSpecies = speciesList,
                        LinkList   = LinkifyList
                    };

                    // Attempt to upload the species' picture.

                    pageBuilder.PictureFilenames.Add(await UploadSpeciesPictureAsync(client, history, species));
                    pageBuilder.PictureFilenames.AddRange(await UploadSpeciesGalleryAsync(client, history, species));
                    pageBuilder.PictureFilenames.RemoveAll(x => string.IsNullOrWhiteSpace(x));

                    // Generate page content.

                    WikiPage wikiPage = await pageBuilder.BuildAsync();

                    string pageTitle      = wikiPage.Title;
                    bool   createRedirect = pageTitle != species.FullName;

                    // Upload page content.

                    await _editSpeciesPageAsync(client, history, species, pageTitle, wikiPage.Body);

                    // Attempt to create the redirect page for the species (if applicable).

                    if (createRedirect)
                    {
                        string redirect_page_title = species.FullName;

                        if (await _editPageAsync(client, history, redirect_page_title, string.Format("#REDIRECT [[{0}]]", pageTitle) + "\n" + BotFlag))
                        {
                            await history.AddRedirectRecordAsync(redirect_page_title, pageTitle);
                        }
                    }

                    _log(string.Format("finished synchronizing species {0}", species.ShortName));
                }
            }
            else
            {
                _log("mediawiki login failed");
            }

            _log("synchronizing complete");

            await Task.Delay(-1);
        }
Пример #4
0
 public static string FormatPageLinks(string content, WikiLinkList linkifyList)
 {
     return(FormatPageLinksIf(content, linkifyList, x => true));
 }