From 5681c756b64e036c44f394f35f6a308f50781d61 Mon Sep 17 00:00:00 2001 From: Ashot Janibekyan Date: Sun, 27 Aug 2023 02:47:02 +0400 Subject: [PATCH 1/2] Add FilesGenerator --- UnitTestProject1/Tests/GeneratorTests2.cs | 14 +++++ .../Generators/FilesGenerator.cs | 55 +++++++++++++++++++ .../Generators/LinksGenerator.cs | 9 +-- .../Generators/WikiPageExtensions.cs | 11 ++++ 4 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 WikiClientLibrary/Generators/FilesGenerator.cs diff --git a/UnitTestProject1/Tests/GeneratorTests2.cs b/UnitTestProject1/Tests/GeneratorTests2.cs index 3dfc18903..dfbe11ac4 100644 --- a/UnitTestProject1/Tests/GeneratorTests2.cs +++ b/UnitTestProject1/Tests/GeneratorTests2.cs @@ -298,6 +298,20 @@ public async Task WpLzhEnumPageLinksTest() Assert.Equal(links.ToHashSet(), linkPages.ToHashSet()); } + [Fact] + public async Task WpEnEnumPageFilesTest() + { + var site = await WpEnSiteAsync; + var gen = new FilesGenerator(site, site.SiteInfo.MainPage) { PaginationSize = 20 }; + var files = await gen.EnumPagesAsync().Select(p => p.Title).ToListAsync(); + ShallowTrace(files); + Utility.AssertNotNull(files); + Assert.Contains("File:Wikidata-logo.svg", files); + Assert.Contains("File:Wikibooks-logo.svg", files); + Assert.Contains("File:Commons-logo.svg", files); + Assert.Contains("File:Wikisource-logo.svg", files); + } + [Fact] public async Task WpEnGeoSearchTest1() { diff --git a/WikiClientLibrary/Generators/FilesGenerator.cs b/WikiClientLibrary/Generators/FilesGenerator.cs new file mode 100644 index 000000000..99dfb22d5 --- /dev/null +++ b/WikiClientLibrary/Generators/FilesGenerator.cs @@ -0,0 +1,55 @@ +using System.Collections.Generic; +using WikiClientLibrary.Generators.Primitive; +using WikiClientLibrary.Infrastructures; +using WikiClientLibrary.Pages; +using WikiClientLibrary.Sites; + +namespace WikiClientLibrary.Generators +{ + /// + /// Generates pages from all used files on the provided page. + /// (mw:API:Links, MediaWiki 1.11+) + /// + /// + /// + /// + public class FilesGenerator : WikiPagePropertyGenerator + { + /// + public FilesGenerator(WikiSite site) : base(site) + { + } + + /// + public FilesGenerator(WikiSite site, WikiPageStub pageStub) : base(site, pageStub) + { + } + + /// + /// Only list links to these titles. Useful for checking whether a certain page links to a certain title. + /// (MediaWiki 1.17+) + /// + /// a sequence of page titles, or null to list all the linked pages. + public IEnumerable? MatchingTitles { get; set; } + + /// + /// Gets/sets a value that indicates whether the links should be listed in + /// the descending order. (MediaWiki 1.19+) + /// + public bool OrderDescending { get; set; } + + /// + public override string PropertyName => "images"; + + /// + public override IEnumerable> EnumListParameters() + { + return new Dictionary + { + {"imlimit", PaginationSize}, + {"imtitles", MatchingTitles == null ? null : MediaWikiHelper.JoinValues(MatchingTitles)}, + {"imdir", OrderDescending ? "descending" : "ascending"} + }; + } + } +} \ No newline at end of file diff --git a/WikiClientLibrary/Generators/LinksGenerator.cs b/WikiClientLibrary/Generators/LinksGenerator.cs index d3e94c689..1ff8132c8 100644 --- a/WikiClientLibrary/Generators/LinksGenerator.cs +++ b/WikiClientLibrary/Generators/LinksGenerator.cs @@ -1,6 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Text; +using System.Collections.Generic; using WikiClientLibrary.Generators.Primitive; using WikiClientLibrary.Infrastructures; using WikiClientLibrary.Pages; @@ -14,6 +12,7 @@ namespace WikiClientLibrary.Generators /// /// /// + /// public class LinksGenerator : WikiPagePropertyGenerator { /// @@ -53,7 +52,9 @@ public LinksGenerator(WikiSite site, WikiPageStub pageStub) : base(site, pageStu { return new Dictionary { - {"plnamespace", NamespaceIds == null ? null : MediaWikiHelper.JoinValues(NamespaceIds)}, {"pllimit", PaginationSize}, {"pltitles", MatchingTitles == null ? null : MediaWikiHelper.JoinValues(MatchingTitles)}, + {"plnamespace", NamespaceIds == null ? null : MediaWikiHelper.JoinValues(NamespaceIds)}, + {"pllimit", PaginationSize}, + {"pltitles", MatchingTitles == null ? null : MediaWikiHelper.JoinValues(MatchingTitles)}, {"pldir", OrderDescending ? "descending" : "ascending"} }; } diff --git a/WikiClientLibrary/Generators/WikiPageExtensions.cs b/WikiClientLibrary/Generators/WikiPageExtensions.cs index 67e46236a..f28f76f2f 100644 --- a/WikiClientLibrary/Generators/WikiPageExtensions.cs +++ b/WikiClientLibrary/Generators/WikiPageExtensions.cs @@ -21,6 +21,17 @@ public static LinksGenerator CreateLinksGenerator(this WikiPage page) if (page == null) throw new ArgumentNullException(nameof(page)); return new LinksGenerator(page.Site, page.PageStub); } + + /// + /// Creates a instance from the specified page, + /// which generates files from all used files on the page. + /// + /// The page. + public static FilesGenerator CreateFilesGenerator(this WikiPage page) + { + if (page == null) throw new ArgumentNullException(nameof(page)); + return new FilesGenerator(page.Site, page.PageStub); + } /// /// Creates a instance from the specified page, From 5215c356ee3476b4162045e46f573c4ec4edd90c Mon Sep 17 00:00:00 2001 From: Ashot Janibekyan Date: Sun, 27 Aug 2023 02:50:58 +0400 Subject: [PATCH 2/2] FilesGenerator should use imimages instead of imtitles --- WikiClientLibrary/Generators/FilesGenerator.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/WikiClientLibrary/Generators/FilesGenerator.cs b/WikiClientLibrary/Generators/FilesGenerator.cs index 99dfb22d5..ccdafb6e8 100644 --- a/WikiClientLibrary/Generators/FilesGenerator.cs +++ b/WikiClientLibrary/Generators/FilesGenerator.cs @@ -26,10 +26,10 @@ public FilesGenerator(WikiSite site, WikiPageStub pageStub) : base(site, pageStu } /// - /// Only list links to these titles. Useful for checking whether a certain page links to a certain title. + /// Only list these files. Useful for checking whether a certain page has a certain file. /// (MediaWiki 1.17+) /// - /// a sequence of page titles, or null to list all the linked pages. + /// a sequence of page titles, or null to list all the used files. public IEnumerable? MatchingTitles { get; set; } /// @@ -47,7 +47,7 @@ public FilesGenerator(WikiSite site, WikiPageStub pageStub) : base(site, pageStu return new Dictionary { {"imlimit", PaginationSize}, - {"imtitles", MatchingTitles == null ? null : MediaWikiHelper.JoinValues(MatchingTitles)}, + {"imimages", MatchingTitles == null ? null : MediaWikiHelper.JoinValues(MatchingTitles)}, {"imdir", OrderDescending ? "descending" : "ascending"} }; }