As preliminary, it's mostly better to check requirements before an action than throwing an exception. If a bot wants to save edits to a protected page (e.g. write by sysops only), the bot should first check the required user rights. The hint in this thread showed a way, but no solution: In DotNetWikiBot version 3.11 I didn't find a method like Site.GetPageHTM() that returns HTML source code.
Now I created a working solution (partial unfinished) using API:Properties and API:Meta.
I added some page properties:
public Dictionary< string, string > ProtectionModes { get; private set; }
// Key contains the "type" (edit, move, etc.), Value contains the "level" (autoconfirmed, sysop, etc.)
public List<string> UserGroups {get; private set; }
// List of user groups the site's owner belongs to.
public bool EditAllowed {get; private set; }
// True if the bot account is allowed to edit the page: If ProtectionModes contains an edit restriction, the user is allowed to edit by the Save() method only if he belongs to the group that is required by the edit protection mode.
public bool MoveAllowed {get; private set; }
// True if the bot account is allowed to move (rename) the page.
The method Page.LoadWithMetadata() in the case (site.useApi) uses an extended query. (This sequence should set the "userinfo" at the beginning of res, but it doesn't.)
Now an application can check EditAllowed before Save() resp. MoveAllowed before RenameTo(). All changes are contained in the uploaded file.
There are some questions left:
What's the best way in Page constructors and Page.Load() method to reset the values?
What can be done in the other case (not site.useApi) via Special:Export? In the resulting xml file, there's no information about user rights and restrictions. On the other side, the xml file is normally used for read-only purposes.
While the browser shows the page source code, there are other elements that give "wgUserGroups" and "wgRestrictionEdit" resp. "wgRestrictionMove". Is this useful?
ProtectionModes and UserGroups are read-only properties, but Add and Remove work public. I didn't look for a way to prohibit such changes. Perhaps it's better to set these values private.
Surely, there's a better way to read the values into ProtectionModes resp. UserGroups. I don't have any knowledge about Linq; my code is copied from MSDN without understanding.
As I said in the preliminary, it may be useful to add such a feature to DotNetWikiBot. Best regards, Juergen
Last edit: Juergen Thomas 2015-01-11
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
As preliminary, it's mostly better to check requirements before an action than throwing an exception. If a bot wants to save edits to a protected page (e.g. write by sysops only), the bot should first check the required user rights. The hint in this thread showed a way, but no solution: In DotNetWikiBot version 3.11 I didn't find a method like Site.GetPageHTM() that returns HTML source code.
Now I created a working solution (partial unfinished) using API:Properties and API:Meta.
I added some page properties:
public Dictionary< string, string > ProtectionModes { get; private set; }
// Key contains the "type" (edit, move, etc.), Value contains the "level" (autoconfirmed, sysop, etc.)
public List<string> UserGroups {get; private set; }
// List of user groups the site's owner belongs to.
public bool EditAllowed {get; private set; }
// True if the bot account is allowed to edit the page: If ProtectionModes contains an edit restriction, the user is allowed to edit by the Save() method only if he belongs to the group that is required by the edit protection mode.
public bool MoveAllowed {get; private set; }
// True if the bot account is allowed to move (rename) the page.
The method Page.LoadWithMetadata() in the case (site.useApi) uses an extended query. (This sequence should set the "userinfo" at the beginning of res, but it doesn't.)
pageXml.Element("protection").Elements("pr") fill ProtectionModes.
XElement.Parse(src).Element("query").Element("userinfo").Element("groups").Elements("g") fill UserGroups.
Check the user rights by:
Now an application can check EditAllowed before Save() resp. MoveAllowed before RenameTo(). All changes are contained in the uploaded file.
There are some questions left:
As I said in the preliminary, it may be useful to add such a feature to DotNetWikiBot. Best regards, Juergen
Last edit: Juergen Thomas 2015-01-11
Upload attachment. (It seems you cannot add an attachment while creating the thread.) Juergen