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

Add PagesWithPropGenerator #103

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 11 additions & 0 deletions UnitTestProject1/Tests/GeneratorTests2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -408,5 +408,16 @@ public async Task WpLzhSearchTest()
new HashSet<string>(pages.Select(p => p.Title!)));
}

[Fact]
public async Task WpEnPageWithPropTest()
{
var site = await WpEnSiteAsync;
var generator = new PagesWithPropGenerator(site, "defaultsort");
var result = await generator.EnumItemsAsync().Take(10).ToListAsync();
var pages = await generator.EnumPagesAsync().Take(10).ToListAsync();
Utility.AssertTitlesDistinct(pages);
Assert.All(result, r => Assert.NotNull(r.Value));
}

}
}
48 changes: 48 additions & 0 deletions WikiClientLibrary/Generators/PagesWithPropGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using WikiClientLibrary.Generators.Primitive;
using WikiClientLibrary.Infrastructures;
using WikiClientLibrary.Sites;

namespace WikiClientLibrary.Generators
{
/// <summary>
/// List all pages using a given page property. The list of available properties can be found at action=query&amp;list=pagepropnames.
/// </summary>
public class PagesWithPropGenerator : WikiPageGenerator<PagesWithPropResultItem>
{
public PagesWithPropGenerator(WikiSite site, string propertyName) : base(site)
{
PropertyName = propertyName;
}

/// <summary>
/// Page property for which to enumerate pages (action=query&list=pagepropnames returns page property names in use).

Check warning on line 20 in WikiClientLibrary/Generators/PagesWithPropGenerator.cs

View workflow job for this annotation

GitHub Actions / Linux Build & Test

XML comment has badly formed XML -- 'Reference to undefined entity 'list'.'

Check warning on line 20 in WikiClientLibrary/Generators/PagesWithPropGenerator.cs

View workflow job for this annotation

GitHub Actions / Windows Build & Test (Debug)

XML comment has badly formed XML -- 'Reference to undefined entity 'list'.'

Check warning on line 20 in WikiClientLibrary/Generators/PagesWithPropGenerator.cs

View workflow job for this annotation

GitHub Actions / Windows Build & Test (Release)

XML comment has badly formed XML -- 'Reference to undefined entity 'list'.'
/// </summary>
public string PropertyName { get; set; } = string.Empty;

/// <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; }

public override string ListName => "pageswithprop";

public override IEnumerable<KeyValuePair<string, object?>> EnumListParameters()
{
return new Dictionary<string, object?>
{
{"pwpprop", "ids|title|value"},
{"pwppropname", PropertyName},
{"pwplimit", PaginationSize},
{"pwpdir", OrderDescending ? "descending" : "ascending"},
};
}

protected override PagesWithPropResultItem ItemFromJson(JToken json)
{
return new PagesWithPropResultItem(MediaWikiHelper.PageStubFromJson((JObject)json), (string)json["value"]);
}
}
}
16 changes: 16 additions & 0 deletions WikiClientLibrary/Generators/PagesWithPropResultItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using WikiClientLibrary.Pages;

namespace WikiClientLibrary.Generators
{
public class PagesWithPropResultItem
{
public WikiPageStub Page { get; }
public string Value { get; set; }

public PagesWithPropResultItem(WikiPageStub wikiPageStub, string value)
{
Page = wikiPageStub;
Value = value;
}
}
}
Loading