CXuesong / WikiClientLibrary

/*🌻*/ Wiki Client Library is an asynchronous MediaWiki API client library targeting modern .NET platforms
https://github.com/CXuesong/WikiClientLibrary/wiki
Apache License 2.0
80 stars 16 forks source link

How do you get, say, the wanted templates? #91

Closed rwv37 closed 2 years ago

rwv37 commented 2 years ago

I'm new to WikiClientLibrary. I'd like to fetch information that can be found on various Special:* pages, for example Special:WantedTemplates. Preferably via the API, not from the page itself (so that the returned data would be easier to get the info from).

tl/dr: I'd like to see an example code snippet of retrieving the same thing I can get via a web browser at api.php?action=query&format=json&generator=querypage&gqppage=Wantedtemplates.

ntl/r: At first, I just tried fetching the Special:* page in the same manner as I have previously fetched normal pages:

        page = new(site, "Special:WantedTemplates");
        await page.RefreshAsync(PageQueryOptions.FetchContent | PageQueryOptions.ResolveRedirects)
            .ConfigureAwait(false);
        Console.WriteLine(page.Content);

But that doesn't give anything in page.Content. So, I poked around a bit more and found WikiSite.InvokeMediaWikiApiAsync, which seemed like a better choice since, like I said, I'd prefer to get the info via the API. But I was unable to figure out how to create an appropriate WikiMessageRequest object to pass to it.

So, I poked around some more, and found that WikiPage.RefreshAsync has overloads taking an IWikiPageQueryProvider parameter, which looks like it can be used to add the various HTTP request params for api.php. But I have run into two issues:

(1) I don't understand how to instantiate an appropriate WikiPageQueryProvider; I guess I have to set the Properties field to... something? Somehow? (2) I also don't know what page title to use when creating the WikiPage object. The "api.php" that I'd normally use through a web browser isn't a "title".

CXuesong commented 2 years ago

Thanks for using WCL!

Since you are working with querypage, searching this keyword in WCL repo might yield something you are looking for. Luckily, it does 😏: QueryPageGenerator. You might wish to take a look at the "Remarks" section of this documentation page.

Searching for this class in unit test project, you may get something like this: https://github.com/CXuesong/WikiClientLibrary/blob/d793a7dfc2ee542229e774f4317e3f934d1d5b5f/UnitTestProject1/Tests/GeneratorTests1.cs#L101-L112 which basically demonstrates how to use this class.

Please refer to [MediaWiki] Generators for conceptual topic on how to consume MediaWiki generators with WCL.


CIL:

At first, I just tried fetching the Special:* page in the same manner as I have previously fetched normal pages:

WikiPage does not work for special pages -- they actually does not contain any "editable content".

So, I poked around a bit more and found WikiSite.InvokeMediaWikiApiAsync, which seemed like a better choice since, like I said, I'd prefer to get the info via the API.

You are now entering WCL infrastructure zone… While by searching function calls to InvokeMediaWikiApiAsync in this repo plus cross-referencing library documentation (c.f. MediaWikiFormRequestMessage), you might get some rough idea on how to perform some bare-bone MediaWiki API calls (and yes you can do this from outside WCL!), please try if you can find a suitable higher-level API in WCL first.

So, I poked around some more, and found that WikiPage.RefreshAsync has overloads taking an IWikiPageQueryProvider parameter, which looks like it can be used to add the various HTTP request params for api.php. But I have run into two issues:

(1) I don't understand how to instantiate an appropriate WikiPageQueryProvider; I guess I have to set the Properties field to... something? Somehow?

My bad. The documentation for this IWikiPageQueryProvider looks obscure, but this interface actually intends to selectively combine multiple page properties (e.g. revision / fileinfo / categoryinfo / etc.) to fetch from MW server, so that WCL users can choose what page properties they want to retrieve from MW server.

(2) I also don't know what page title to use when creating the WikiPage object. The "api.php" that I'd normally use through a web browser isn't a "title".

Please refer to [MediaWiki] Getting started#WikiPages to get started on this ^_^

Again, I suppose there is no need to dig into this interface right now. Anyway, if you have any further questions, I'd be glad to help~

rwv37 commented 2 years ago

Great. Thank you!