-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Description
LeMyst/WikibaseIntegrator#917 (comment)
code is here
def create_redirect(from_id: str, to_id: str, login: _Login | None = None) -> dict[str, Any]:
"""
Clear the item `from_id` and create a redirect to `to_id`.
"""
log.info(f"Creating redirect from {from_id} → {to_id}")
try:
# Step 1: Clear the item
clear_payload = {
"action": "wbeditentity",
"id": from_id,
"clear": "true",
"summary": f"Clearing {from_id} to prepare for redirect",
"format": "json"
}
clear_response = mediawiki_api_call_helper(data=clear_payload, login=login, is_bot=True)
if "error" in clear_response:
log.error(f"Failed to clear {from_id}: {clear_response['error']}")
return clear_response
else:
log.debug(f"Cleared successfully: {clear_response}")
# Step 2: Create the redirect
redirect_payload = {
"action": "wbcreateredirect",
"from": from_id,
"to": to_id,
"data": "{}",
"format": "json"
}
redirect_response = mediawiki_api_call_helper(data=redirect_payload, login=login, is_bot=True)
if "error" in redirect_response:
log.error(f"Failed to create redirect {from_id} → {to_id}: {redirect_response['error']}")
else:
log.info(f"Redirect created successfully: {from_id} → {to_id}")
return redirect_response
except Exception as e:
log.error(f"Exception when creating redirect {from_id} → {to_id}: {e}")
return {"error": str(e)}
def merge_items_and_create_redirect(
qids: List[str],
login: _Login | None = None,
ignore_conflicts: List[str] | None = None,
is_bot: bool = False,
tags: Union[str, List[str]] | None = None,
**kwargs: Any
) -> str:
"""
Merge multiple Wikibase items into the lowest QID.
:param qids: List of item QIDs to merge. The lowest QID will be kept.
:param login: A wbi_login.Login instance
:param ignore_conflicts: List of elements to ignore conflicts for. Can contain "description", "sitelink", "statement"
:param is_bot: Mark this edit as bot
:param tags: Single tag string or list of tags to attach to the edit
:param kwargs: Additional parameters to pass to mediawiki_api_call_helper
"""
if not qids or len(qids) < 2:
raise ValueError("You must provide at least two QIDs to merge")
# Sort QIDs numerically to keep the lowest
sorted_qids = sorted(qids, key=lambda x: int(x.lstrip('Q')))
to_id = sorted_qids[0] # keep the lowest QID
from_ids = sorted_qids[1:] # merge all other QIDs into to_id
# Prepare tags string if provided
tags_str = None
if tags:
if isinstance(tags, list):
tags_str = '|'.join(tags)
else:
tags_str = str(tags)
for from_id in from_ids:
params = {
'action': 'wbmergeitems',
'fromid': from_id,
'toid': to_id,
'format': 'json'
}
if ignore_conflicts is not None:
params['ignoreconflicts'] = '|'.join(ignore_conflicts)
if is_bot:
params['bot'] = ''
if tags_str:
params['tags'] = tags_str
try:
mediawiki_api_call_helper(data=params, login=login, is_bot=is_bot, **kwargs)
print(f"Merged {from_id} into {to_id}")
create_redirect(from_id=from_id, to_id=to_id, login=login)
return to_id
except Exception as e:
print(f"Error merging {from_id} into {to_id}: {e}")
Metadata
Metadata
Assignees
Labels
No labels