Skip to content

Commit

Permalink
Add delete page support (#312) (#313)
Browse files Browse the repository at this point in the history
  • Loading branch information
LeMyst authored Apr 9, 2022
1 parent 7e91564 commit bfa2800
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 6 deletions.
49 changes: 47 additions & 2 deletions wikibaseintegrator/entities/baseentity.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from wikibaseintegrator.models.claims import Claim, Claims
from wikibaseintegrator.wbi_enums import ActionIfExists
from wikibaseintegrator.wbi_exceptions import MWApiError, NonUniqueLabelDescriptionPairError
from wikibaseintegrator.wbi_helpers import mediawiki_api_call_helper
from wikibaseintegrator.wbi_helpers import delete_page, mediawiki_api_call_helper
from wikibaseintegrator.wbi_login import _Login

if TYPE_CHECKING:
Expand All @@ -23,7 +23,8 @@
class BaseEntity:
ETYPE = 'base-entity'

def __init__(self, api: 'WikibaseIntegrator' = None, lastrevid: int = None, type: str = None, id: str = None, claims: Claims = None, is_bot: bool = None, login: _Login = None):
def __init__(self, api: 'WikibaseIntegrator' = None, title: str = None, pageid: int = None, lastrevid: int = None, type: str = None, id: str = None, claims: Claims = None,
is_bot: bool = None, login: _Login = None):
if not api:
from wikibaseintegrator import WikibaseIntegrator
self.api = WikibaseIntegrator()
Expand All @@ -33,6 +34,8 @@ def __init__(self, api: 'WikibaseIntegrator' = None, lastrevid: int = None, type
self.api.is_bot = is_bot or self.api.is_bot
self.api.login = login or self.api.login

self.title = title
self.pageid = pageid
self.lastrevid = lastrevid
self.type = str(type or self.ETYPE)
self.id = id
Expand All @@ -46,6 +49,22 @@ def api(self) -> WikibaseIntegrator:
def api(self, value: WikibaseIntegrator):
self.__api = value

@property
def title(self) -> Optional[str]:
return self.__title

@title.setter
def title(self, value: Optional[str]):
self.__title = value

@property
def pageid(self) -> Optional[int]:
return self.__pageid

@pageid.setter
def pageid(self, value: Optional[int]):
self.__pageid = value

@property
def lastrevid(self) -> Optional[int]:
return self.__lastrevid
Expand Down Expand Up @@ -107,6 +126,8 @@ def from_json(self, json_data: Dict[str, Any]) -> BaseEntity:
if 'missing' in json_data:
raise ValueError('Entity is nonexistent')

self.title = str(json_data['title'])
self.pageid = int(json_data['pageid'])
self.lastrevid = int(json_data['lastrevid'])
self.type = str(json_data['type'])
self.id = str(json_data['id'])
Expand Down Expand Up @@ -217,6 +238,30 @@ def _write(self, data: Dict = None, summary: str = None, login: _Login = None, a

return json_result['entity']

def delete(self, login: _Login = None, allow_anonymous: bool = False, is_bot: bool = None, **kwargs: Any):
"""
Delete the current entity. Use the pageid first if available and fallback to the page title.
:param login: A wbi_login.Login instance
:param allow_anonymous: Allow an unidentified edit to the Mediawiki API (default False)
:param is_bot: Flag the edit as a bot
:param reason: Reason for the deletion. If not set, an automatically generated reason will be used.
:param deletetalk: Delete the talk page, if it exists.
:param kwargs: Any additional keyword arguments to pass to mediawiki_api_call_helper and requests.request
:return: The data returned by the API as a dictionary
"""

if not self.pageid and self.title:

This comment has been minimized.

Copy link
@dpriskorn

dpriskorn Apr 13, 2022

Contributor

Text has "or" but the logic has "and"

This comment has been minimized.

Copy link
@LeMyst

LeMyst Apr 14, 2022

Author Owner

Fixed in 1f6b7d5, thanks

raise ValueError("A pageid or a page title attribute must be set before deleting an entity object.")

if not self.pageid:
return delete_page(title=self.title, pageid=None, login=login, allow_anonymous=allow_anonymous, is_bot=is_bot, **kwargs)
else:
if isinstance(self.pageid, int):
raise ValueError("The entity must have a pageid attribute correctly set")

return delete_page(title=None, pageid=self.pageid, login=login, allow_anonymous=allow_anonymous, is_bot=is_bot, **kwargs)

def write_required(self, base_filter: List[BaseDataType | List[BaseDataType]] = None, action_if_exists: ActionIfExists = ActionIfExists.REPLACE, **kwargs: Any) -> bool:
fastrun_container = wbi_fastrun.get_fastrun_container(base_filter=base_filter, **kwargs)

Expand Down
57 changes: 53 additions & 4 deletions wikibaseintegrator/wbi_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,15 +287,16 @@ def merge_items(from_id: str, to_id: str, login: _Login = None, ignore_conflicts
if is_bot:
params.update({'bot': ''})

return mediawiki_api_call_helper(data=params, login=login, **kwargs)
return mediawiki_api_call_helper(data=params, login=login, is_bot=is_bot, **kwargs)


def merge_lexemes(source: str, target: str, summary: str = None, is_bot: bool = False, **kwargs: Any) -> Dict:
def merge_lexemes(source: str, target: str, login: _Login = None, summary: str = None, is_bot: bool = False, **kwargs: Any) -> Dict:
"""
A static method to merge two lexemes
:param source: The ID to merge from. This parameter is required.
:param target: The ID to merge to. This parameter is required.
:param login: A wbi_login.Login instance
:param summary: Summary for the edit.
:param is_bot: Mark this edit as bot.
"""
Expand All @@ -313,7 +314,7 @@ def merge_lexemes(source: str, target: str, summary: str = None, is_bot: bool =
if is_bot:
params.update({'bot': ''})

return mediawiki_api_call_helper(data=params, is_bot=is_bot, **kwargs)
return mediawiki_api_call_helper(data=params, login=login, is_bot=is_bot, **kwargs)


def remove_claims(claim_id: str, summary: str = None, baserevid: int = None, is_bot: bool = False, **kwargs: Any) -> Dict:
Expand Down Expand Up @@ -341,7 +342,55 @@ def remove_claims(claim_id: str, summary: str = None, baserevid: int = None, is_
if is_bot:
params.update({'bot': ''})

return mediawiki_api_call_helper(data=params, **kwargs)
return mediawiki_api_call_helper(data=params, is_bot=is_bot, **kwargs)


def delete_page(title: str = None, pageid: int = None, reason: str = None, deletetalk: bool = False, watchlist: str = 'preferences', watchlistexpiry: str = None,
login: _Login = None, **kwargs: Any) -> Dict:
"""
Delete a page
:param title: Title of the page to delete. Cannot be used together with pageid.
:param pageid: Page ID of the page to delete. Cannot be used together with title.
:param reason: Reason for the deletion. If not set, an automatically generated reason will be used.
:param deletetalk: Delete the talk page, if it exists.
:param watchlist: Unconditionally add or remove the page from the current user's watchlist, use preferences (ignored for bot users) or do not change watch.
One of the following values: nochange, preferences, unwatch, watch
:param watchlistexpiry: Watchlist expiry timestamp. Omit this parameter entirely to leave the current expiry unchanged.
:param login: A wbi_login.Login instance
:param kwargs:
:return:
"""

if not title and not pageid:
raise ValueError("A title or a pageid must be specified.")

if title and pageid:
raise ValueError("You can't specify a title and a pageid at the same time.")

if pageid and not isinstance(pageid, int):
raise ValueError("pageid must be an integer.")

params = {
'action': 'delete',
'deletetalk': deletetalk,
'watchlist': watchlist,
'format': 'json'
}

if title:
params.update({'title': title})

if pageid:
params.update({'pageid': pageid})

if reason:
params.update({'reason': reason})

if watchlistexpiry:
params.update({'watchlistexpiry': watchlistexpiry})

return mediawiki_api_call_helper(data=params, login=login, **kwargs)


def search_entities(search_string: str, language: str = None, strict_language: bool = False, search_type: str = 'item', max_results: int = 50, dict_result: bool = False,
Expand Down

0 comments on commit bfa2800

Please sign in to comment.