diff --git a/UnitTestProject1/Tests/GeneratorTests2.cs b/UnitTestProject1/Tests/GeneratorTests2.cs index 86d4f25c6..52bbbefc0 100644 --- a/UnitTestProject1/Tests/GeneratorTests2.cs +++ b/UnitTestProject1/Tests/GeneratorTests2.cs @@ -408,5 +408,16 @@ public async Task WpLzhSearchTest() new HashSet(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)); + } + } } diff --git a/WikiClientLibrary/Generators/PagesWithPropGenerator.cs b/WikiClientLibrary/Generators/PagesWithPropGenerator.cs new file mode 100644 index 000000000..1a7420884 --- /dev/null +++ b/WikiClientLibrary/Generators/PagesWithPropGenerator.cs @@ -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 +{ + /// + /// List all pages using a given page property. The list of available properties can be found at action=query&list=pagepropnames. + /// + public class PagesWithPropGenerator : WikiPageGenerator + { + public PagesWithPropGenerator(WikiSite site, string propertyName) : base(site) + { + PropertyName = propertyName; + } + + /// + /// Page property for which to enumerate pages (action=query&list=pagepropnames returns page property names in use). + /// + public string PropertyName { get; set; } = string.Empty; + + /// + /// 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 ListName => "pageswithprop"; + + public override IEnumerable> EnumListParameters() + { + return new Dictionary + { + {"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"]); + } + } +} \ No newline at end of file diff --git a/WikiClientLibrary/Generators/PagesWithPropResultItem.cs b/WikiClientLibrary/Generators/PagesWithPropResultItem.cs new file mode 100644 index 000000000..ace4d8d1c --- /dev/null +++ b/WikiClientLibrary/Generators/PagesWithPropResultItem.cs @@ -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; + } + } +} \ No newline at end of file