Skip to content

Commit

Permalink
Merge pull request #102 from ashotjanibekyan/feature/MyWatchlistGener…
Browse files Browse the repository at this point in the history
…ator

Add MyWatchlistGenerator
  • Loading branch information
CXuesong committed Aug 30, 2023
2 parents 94b9c6c + 87ab1da commit 6be126a
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 8 deletions.
3 changes: 0 additions & 3 deletions WikiClientLibrary/Generators/GeoSearchGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Text;
using Newtonsoft.Json.Linq;
using WikiClientLibrary.Generators.Primitive;
using WikiClientLibrary.Infrastructures;
using WikiClientLibrary.Pages;
using WikiClientLibrary.Pages.Queries.Properties;
using WikiClientLibrary.Sites;

Expand Down
6 changes: 1 addition & 5 deletions WikiClientLibrary/Generators/GeoSearchResultItem.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json;
using WikiClientLibrary.Pages;
using WikiClientLibrary.Pages;

namespace WikiClientLibrary.Generators
{
Expand Down
72 changes: 72 additions & 0 deletions WikiClientLibrary/Generators/MyWatchlistGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using WikiClientLibrary.Generators.Primitive;
using WikiClientLibrary.Infrastructures;
using WikiClientLibrary.Sites;

namespace WikiClientLibrary.Generators
{
/// <summary>
/// Get all pages on the current user's watchlist.
/// </summary>
public class MyWatchlistGenerator : WikiPageGenerator<MyWatchlistResultItem>
{
public MyWatchlistGenerator(WikiSite site) : base(site)
{
}

/// <summary>
/// Only list pages in the given namespaces.
/// </summary>
public IEnumerable<int>? NamespaceIds { get; set; }

/// <summary>
/// Only show pages that have not been changed.
/// </summary>
public PropertyFilterOption NotChangedPagesFilter { 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; }

/// <summary>
/// Title (with namespace prefix) to begin enumerating from.
/// </summary>
public string? FromTitle { get; set; }

/// <summary>
/// Title (with namespace prefix) to stop enumerating at.
/// </summary>
public string? ToTitle { get; set; }

public override IEnumerable<KeyValuePair<string, object?>> EnumListParameters()
{
return new Dictionary<string, object?>
{
{"wrnamespace", NamespaceIds == null ? null : MediaWikiHelper.JoinValues(NamespaceIds)},
{"wrlimit", PaginationSize},
{"wrprop", "changed"},
{"wrshow", NotChangedPagesFilter.ToString("!changed", "changed", null)},
{"wrdir", OrderDescending ? "descending" : "ascending"},
{"wrfromtitle", FromTitle},
{"wrtotitle", ToTitle},
};
}

protected override MyWatchlistResultItem ItemFromJson(JToken json)
{
DateTime? changedTime = null;
if (json["changed"] != null)
{
changedTime = DateTime.Parse((string)json["changed"]);
}

return new MyWatchlistResultItem(MediaWikiHelper.PageStubFromJson((JObject)json), json["changed"] != null, changedTime);
}

public override string ListName => "watchlistraw";
}
}
19 changes: 19 additions & 0 deletions WikiClientLibrary/Generators/MyWatchlistResultItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using WikiClientLibrary.Pages;

namespace WikiClientLibrary.Generators
{
public sealed class MyWatchlistResultItem
{
public WikiPageStub Page { get; }
public bool IsChanged { get; }
public DateTime? ChangedTime { get; }

public MyWatchlistResultItem(WikiPageStub page, bool isChanged, DateTime? changedTime = null)
{
Page = page;
IsChanged