Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FilesGenerator #101

Merged
merged 3 commits into from
Aug 27, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions UnitTestProject1/Tests/GeneratorTests2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
55 changes: 55 additions & 0 deletions WikiClientLibrary/Generators/FilesGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System.Collections.Generic;
using WikiClientLibrary.Generators.Primitive;
using WikiClientLibrary.Infrastructures;
using WikiClientLibrary.Pages;
using WikiClientLibrary.Sites;

namespace WikiClientLibrary.Generators
{
/// <summary>
/// Generates pages from all used files on the provided page.
/// (<a href="https://www.mediawiki.org/wiki/API:Links">mw:API:Links</a>, MediaWiki 1.11+)
/// </summary>
/// <see cref="TransclusionsGenerator"/>
/// <see cref="BacklinksGenerator"/>
/// <see cref="LinksGenerator"/>
public class FilesGenerator : WikiPagePropertyGenerator
{
/// <inheritdoc />
public FilesGenerator(WikiSite site) : base(site)
{
}

/// <inheritdoc />
public FilesGenerator(WikiSite site, WikiPageStub pageStub) : base(site, pageStub)
{
}

/// <summary>
/// Only list these files. Useful for checking whether a certain page has a certain file.
/// (MediaWiki 1.17+)
/// </summary>
/// <value>a sequence of page titles, or <c>null</c> to list all the used files.</value>
public IEnumerable<string>? MatchingTitles { get; set; }

/// <summary>
/// Gets/sets a value that indicates whether the links should be listed in
/// the descending order. (MediaWiki 1.19+)
/// </summary>
public bool OrderDescending { get; set; }

/// <inheritdoc />
public override string PropertyName => "images";

/// <inheritdoc />
public override IEnumerable<KeyValuePair<string, object?>> EnumListParameters()
{
return new Dictionary<string, object?>
{
{"imlimit", PaginationSize},
{"imimages", MatchingTitles == null ? null : MediaWikiHelper.JoinValues(MatchingTitles)},
{"imdir", OrderDescending ? "descending" : "ascending"}
};
}
}
}
9 changes: 5 additions & 4 deletions WikiClientLibrary/Generators/LinksGenerator.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -14,6 +12,7 @@ namespace WikiClientLibrary.Generators
/// </summary>
/// <see cref="TransclusionsGenerator"/>
/// <see cref="BacklinksGenerator"/>
/// <see cref="FilesGenerator"/>
public class LinksGenerator : WikiPagePropertyGenerator
{
/// <inheritdoc />
Expand Down Expand Up @@ -53,7 +52,9 @@ public LinksGenerator(WikiSite site, WikiPageStub pageStub) : base(site, pageStu
{
return new Dictionary<string, object?>
{
{"plnamespace", NamespaceIds == null ? null : MediaWikiHelper.JoinValues(NamespaceIds)}, {"pllimit", PaginationSize}, {"pltitles", MatchingTitles == null ? null : MediaWikiHelper.JoinValues(MatchingTitles)},
{"plnamespace", NamespaceIds == null ? null : MediaWikiHelper.JoinValues(NamespaceIds)},
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just formatting, no meaningful changes.

{"pllimit", PaginationSize},
{"pltitles", MatchingTitles == null ? null : MediaWikiHelper.JoinValues(MatchingTitles)},
{"pldir", OrderDescending ? "descending" : "ascending"}
};
}
Expand Down
11 changes: 11 additions & 0 deletions WikiClientLibrary/Generators/WikiPageExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/// <summary>
/// Creates a <see cref="FilesGenerator"/> instance from the specified page,
/// which generates files from all used files on the page.
/// </summary>
/// <param name="page">The page.</param>
public static FilesGenerator CreateFilesGenerator(this WikiPage page)
{
if (page == null) throw new ArgumentNullException(nameof(page));
return new FilesGenerator(page.Site, page.PageStub);
}

/// <summary>
/// Creates a <see cref="TransclusionsGenerator"/> instance from the specified page,
Expand Down