Skip to content

Commit

Permalink
FilesGenerator (#101)
Browse files Browse the repository at this point in the history
* Add FilesGenerator

* FilesGenerator should use imimages instead of imtitles
  • Loading branch information
ashotjanibekyan authored Aug 27, 2023
1 parent f99a048 commit 94b9c6c
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 4 deletions.
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)},
{"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

0 comments on commit 94b9c6c

Please sign in to comment.