Skip to content

Commit

Permalink
Merge pull request #87 from User123698745/feature/FileusageGenerator
Browse files Browse the repository at this point in the history
FileusageGenerator

_TranscludedInGenerator_ can not be used for files.

This PR adds _FileusageGenerator_ ([API docs](https://www.mediawiki.org/wiki/API:Imageusage)) which is basically the same as the _TranscludedInGenerator_ ([API docs](https://www.mediawiki.org/wiki/API:Embeddedin)) just for files.

Note: The API endpoint is called "Imageusage", but it works with any file (see test case).
  • Loading branch information
CXuesong authored May 25, 2021
2 parents d8ffbbe + 311b991 commit 8ce3870
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 6 deletions.
14 changes: 12 additions & 2 deletions UnitTestProject1/Tests/GeneratorTests2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using WikiClientLibrary;
using WikiClientLibrary.Generators;
using WikiClientLibrary.Generators.Primitive;
using WikiClientLibrary.Pages;
using WikiClientLibrary.Pages.Queries.Properties;
using WikiClientLibrary.Tests.UnitTestProject1.Fixtures;
using Xunit;
using Xunit.Abstractions;
Expand Down Expand Up @@ -264,6 +262,18 @@ public async Task WpTranscludedInGeneratorTest()
Assert.Contains(pages, p => p.Title == "Template:Portal bar");
}

[Theory]
[InlineData("File:This.png", "User:Pbm/gallery")] // image
[InlineData("File:Ae Fond Kiss local.ogg", "User:JanGerber/sandbox")] // audio
public async Task WpFileUsageGeneratorTest(string target, string expected)
{
var site = await WpTest2SiteAsync;
var fug = new FileUsageGenerator(site, target) { PaginationSize = 100 };
var pages = await fug.EnumPagesAsync().Take(100).ToListAsync();
ShallowTrace(pages, 1);
Assert.Contains(pages, p => p.Title == expected);
}

[Fact]
public async Task WpLzhEnumPageLinksTest()
{
Expand Down
60 changes: 60 additions & 0 deletions WikiClientLibrary/Generators/FileUsageGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System.Collections.Generic;
using WikiClientLibrary.Generators.Primitive;
using WikiClientLibrary.Infrastructures;
using WikiClientLibrary.Sites;

namespace WikiClientLibrary.Generators
{
/// <summary>
/// Generates all the pages that transclude the specified file.
/// </summary>
/// <seealso cref="BacklinksGenerator"/>
/// <seealso cref="TranscludedInGenerator"/>
/// <seealso cref="TransclusionsGenerator"/>
public class FileUsageGenerator : WikiPageGenerator
{

/// <inheritdoc />
public FileUsageGenerator(WikiSite site) : base(site)
{
}

/// <inheritdoc />
/// <param name="targetTitle">List pages transclude this file. The file does not need to exist.</param>
public FileUsageGenerator(WikiSite site, string targetTitle) : base(site)
{
TargetTitle = targetTitle;
}

/// <summary>
/// List pages transcluding this file. The file does not need to exist.
/// </summary>
public string TargetTitle { get; set; } = "";

/// <summary>
/// Only list pages in these namespaces.
/// </summary>
/// <value>Selected ids of namespace, or <c>null</c> if all the namespaces are selected.</value>
public IEnumerable<int>? NamespaceIds { get; set; }

/// <summary>
/// How to filter redirects in the results.
/// </summary>
public PropertyFilterOption RedirectsFilter { get; set; }

/// <inheritdoc />
public override string ListName => "imageusage";

/// <inheritdoc />
public override IEnumerable<KeyValuePair<string, object?>> EnumListParameters()
{
return new Dictionary<string, object?>
{
{"iutitle", TargetTitle},
{"iunamespace", NamespaceIds == null ? null : MediaWikiHelper.JoinValues(NamespaceIds)},
{"iufilterredir", RedirectsFilter.ToString("redirects", "nonredirects")},
{"iulimit", PaginationSize}
};
}
}
}
6 changes: 2 additions & 4 deletions WikiClientLibrary/Generators/TranscludedInGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Generic;
using WikiClientLibrary.Generators.Primitive;
using WikiClientLibrary.Infrastructures;
using WikiClientLibrary.Pages;
using WikiClientLibrary.Sites;

namespace WikiClientLibrary.Generators
Expand All @@ -12,6 +9,7 @@ namespace WikiClientLibrary.Generators
/// Generates all the pages that transclude the specified title.
/// </summary>
/// <seealso cref="BacklinksGenerator"/>
/// <seealso cref="FileUsageGenerator"/>
/// <seealso cref="TransclusionsGenerator"/>
public class TranscludedInGenerator : WikiPageGenerator
{
Expand Down

0 comments on commit 8ce3870

Please sign in to comment.