Skip to content

Commit

Permalink
[IMPR] Recover unlink.py
Browse files Browse the repository at this point in the history
- update and improve restored unlink.py
- update documentation
- update tests after adding unlink messages

Bug: T223826
Change-Id: Id93308289f398e664d6fd777b53b5bcaa1394b6c
  • Loading branch information
xqt committed Aug 10, 2024
1 parent b0d979c commit a1cf30a
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 33 deletions.
6 changes: 0 additions & 6 deletions docs/scripts/archive.rst
Original file line number Diff line number Diff line change
Expand Up @@ -254,12 +254,6 @@ table2wiki script
**Nifty script to convert HTML-tables to MediaWiki's own syntax**


unlink script
=============

**This bot unlinks a page on every page that links to it**


wikisourcetext script
=====================

Expand Down
7 changes: 7 additions & 0 deletions docs/scripts/unsorted.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ replicate\_wiki script
:no-members:
:noindex:

unlink script
=============

.. automodule:: scripts.unlink
:no-members:
:noindex:

watchlist script
================

Expand Down
5 changes: 5 additions & 0 deletions docs/scripts_ref/scripts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,11 @@ transwikiimport script

.. automodule:: scripts.transwikiimport

unlink script
=============

.. automodule:: scripts.unlink

unusedfiles script
==================

Expand Down
4 changes: 2 additions & 2 deletions pywikibot/i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -876,11 +876,11 @@ def bundles(stem: bool = False) -> Generator[Path | str, None, None]:
>>> from pywikibot import i18n
>>> bundles = sorted(i18n.bundles(stem=True))
>>> len(bundles)
38
39
>>> bundles[:4]
['add_text', 'archivebot', 'basic', 'blockpageschecker']
>>> bundles[-5:]
['undelete', 'unprotect', 'unusedfiles', 'weblinkchecker', 'welcome']
['unlink', 'unprotect', 'unusedfiles', 'weblinkchecker', 'welcome']
>>> 'pywikibot' in bundles
True
Expand Down
9 changes: 9 additions & 0 deletions scripts/CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
Scripts Changelog
=================

9.4.0
-----

unlink
^^^^^^

* unlink script was recovered


9.3.1
-----

Expand Down
2 changes: 2 additions & 0 deletions scripts/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ Bots and scripts
| unusedfiles.py | Bot appends some text to all unused images and other |
| | text to the respective uploaders. |
+--------------------------+---------------------------------------------------------+
| unlink.py | This bot unlinks a page on every page that links to it. |
+--------------------------+---------------------------------------------------------+
| upload.py | Upload an image to a wiki. |
+--------------------------+---------------------------------------------------------+
| watchlists.py | Allows access to the account's watchlist. |
Expand Down
54 changes: 29 additions & 25 deletions scripts/unlink.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
#!/usr/bin/python
"""
This bot unlinks a page on every page that links to it.
#!/usr/bin/env python3
"""This bot unlinks a page on every page that links to it.
This script understands this command-line argument:
-namespace:n Number of namespace to process. The parameter can be used
multiple times. It works in combination with all other
parameters, except for the -start parameter. If you e.g.
want to iterate over all user pages starting at User:M, use
-start:User:M.
-always Don't prompt you for each replacement.
-namespace:n Number of namespace to process. The parameter can be used
multiple times.
Any other parameter will be regarded as the title of the page
that should be unlinked.
Expand All @@ -20,12 +18,21 @@
descriptions:
python pwb.py unlink "Foo bar" -namespace:0 -namespace:6
.. versionchanged:: 6.0
script was archived.
.. versionchanged:: 7.0
script was deleted.
.. versionchanged:: 9.4
script was recovered.
"""
#
# (C) Pywikibot team, 2007-2020
# (C) Pywikibot team, 2007-2024
#
# Distributed under the terms of the MIT license.
#
from __future__ import annotations

import pywikibot
from pywikibot.bot import SingleSiteBot
Expand All @@ -38,11 +45,11 @@ class UnlinkBot(SingleSiteBot, BaseUnlinkBot):

summary_key = 'unlink-unlinking'

def __init__(self, pageToUnlink, **kwargs):
def __init__(self, page_title: str, **kwargs):
"""Initialize a UnlinkBot instance with the given page to unlink."""
super().__init__(**kwargs)
self.pageToUnlink = pageToUnlink
self.generator = pageToUnlink.getReferences(
self.pageToUnlink = pywikibot.Page(self.site, page_title) # noqa: N803
self.generator = self.pageToUnlink.getReferences(
namespaces=self.opt.namespaces, content=True)

@property
Expand All @@ -55,36 +62,33 @@ def treat_page(self):
self.unlink(self.pageToUnlink)


def main(*args):
"""
Process command line arguments and invoke bot.
def main(*args: str) -> None:
"""Process command line arguments and invoke bot.
If args is an empty list, sys.argv is used.
@param args: command line arguments
@type args: str
:param args: command line arguments
"""
# This temporary string is used to read the title
# of the page that should be unlinked.
page_title = None
page_title: str = ''
options = {}

for arg in pywikibot.handle_args(args):
if arg.startswith('-namespace:'):
if 'namespaces' not in options:
options['namespaces'] = []
opt, _, value = arg.partition(':')
if opt == '-namespace':
options.setdefault('namespaces', [])
try:
options['namespaces'].append(int(arg[11:]))
options['namespaces'].append(int(value))
except ValueError:
options['namespaces'].append(arg[11:])
options['namespaces'].append(value)
elif arg == '-always':
options['always'] = True
else:
page_title = arg

if page_title:
page = pywikibot.Page(pywikibot.Site(), page_title)
bot = UnlinkBot(page, **options)
bot = UnlinkBot(page_title, **options)
bot.run()
else:
pywikibot.bot.suggest_help(missing_parameters=['page title'])
Expand Down

0 comments on commit a1cf30a

Please sign in to comment.