Xqt submitted this change.

View Change

Approvals: jenkins-bot: Verified Xqt: Looks good to me, approved
Add change_pagelang script

Add script that can set the page language for wikis where
this is enabled (e.g. wikis where $wgPageLanguageUseDB is
enabled – typically this means wikis where the translate
extension is installed).

Change-Id: Ie90869969cf0b453ca5990af2f46d8b3876827b5
---
M docs/scripts/scripts.rst
M scripts/README.rst
A scripts/change_pagelang.py
3 files changed, 191 insertions(+), 0 deletions(-)

diff --git a/docs/scripts/scripts.rst b/docs/scripts/scripts.rst
index faad2ee..af4bab2 100644
--- a/docs/scripts/scripts.rst
+++ b/docs/scripts/scripts.rst
@@ -61,6 +61,11 @@

.. automodule:: scripts.category_redirect

+scripts.change\_pagelang script
+-------------------------------
+
+.. automodule:: scripts.change_pagelang
+
scripts.checkimages script
--------------------------

diff --git a/scripts/README.rst b/scripts/README.rst
index f2a1c30..4062b9c 100644
--- a/scripts/README.rst
+++ b/scripts/README.rst
@@ -46,6 +46,8 @@
+------------------------+---------------------------------------------------------+
| #censure.py | Bad word checker bot. |
+------------------------+---------------------------------------------------------+
+ | change_pagelang.py | Changes the content language of pages. |
+ +------------------------+---------------------------------------------------------+
| checkimages.py | Check recently uploaded files. Checks if a file |
| | description is present and if there are other problems |
| | in the image's description. |
diff --git a/scripts/change_pagelang.py b/scripts/change_pagelang.py
new file mode 100755
index 0000000..4e03253
--- /dev/null
+++ b/scripts/change_pagelang.py
@@ -0,0 +1,184 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+"""
+This script changes the content language of pages.
+
+These command line parameters can be used to specify which pages to work on:
+
+&params;
+
+Furthermore, the following command line parameters are supported:
+
+-setlang What language the pages should be set to
+
+-always If a language is already set for a page, always change it
+ to the one set in -setlang.
+
+-never If a language is already set for a page, never change it to
+ the one set in -setlang (keep the current language).
+"""
+#
+# (C) Pywikibot team, 2018-2020
+#
+# Distributed under the terms of the MIT license.
+#
+import pywikibot
+
+from pywikibot import pagegenerators
+
+from pywikibot.bot import SingleSiteBot
+from pywikibot.tools.formatter import color_format
+
+docuReplacements = { # noqa: N816
+ '&params;': pagegenerators.parameterHelp,
+}
+
+
+class ChangeLangBot(SingleSiteBot):
+
+ """Change page language bot."""
+
+ def __init__(self, **kwargs):
+ """Initializer."""
+ self.available_options.update({
+ 'always': False,
+ 'never': False,
+ 'setlang': ''
+ })
+ super().__init__(**kwargs)
+
+ assert not (self.opt.always and self.opt.never), \
+ 'Either "always" or "never" must be set but not both'
+
+ def changelang(self, page):
+ """Set page language."""
+ token = self.site.get_tokens(['csrf']).get('csrf')
+ parameters = {'action': 'setpagelanguage',
+ 'title': page.title(),
+ 'lang': self.opt.setlang,
+ 'token': token}
+ r = self.site._simple_request(**parameters)
+ r.submit()
+ pywikibot.output(color_format(
+ '{lightpurple}{0}{default}: Setting '
+ 'page language to {green}{1}{default}',
+ page.title(as_link=True), self.opt.setlang))
+
+ def treat(self, page):
+ """Treat a page."""
+ # Current content language of the page and site language
+ parameters = {'action': 'query',
+ 'prop': 'info',
+ 'titles': page.title(),
+ 'meta': 'siteinfo'}
+ r = self.site._simple_request(**parameters)
+ langcheck = r.submit()['query']
+
+ currentlang = ''
+ for k in langcheck['pages']:
+ currentlang = langcheck['pages'][k]['pagelanguage']
+ sitelang = langcheck['general']['lang']
+
+ if self.opt.setlang == currentlang:
+ pywikibot.output(color_format(
+ '{lightpurple}{0}{default}: This page is already set to '
+ '{green}{1}{default}; skipping.',
+ page.title(as_link=True), self.opt.setlang))
+ elif currentlang == sitelang or self.opt.always:
+ self.changelang(page)
+ elif self.opt.never:
+ pywikibot.output(color_format(
+ '{lightpurple}{0}{default}: This page already has a '
+ 'different content language {yellow}{1}{default} set; '
+ 'skipping.', page.title(as_link=True), currentlang))
+ else:
+ pywikibot.output(color_format(
+ '\n\n>>> {lightpurple}{0}{default} <<<', page.title()))
+ choice = pywikibot.input_choice(color_format(
+ 'The content language for this page is already set to '
+ '{yellow}{0}{default}, which is different from the '
+ 'default ({1}). Change it to {green}{2}{default} anyway?',
+ currentlang, sitelang, self.opt.setlang),
+ [('Always', 'a'), ('Yes', 'y'), ('No', 'n'),
+ ('Never', 'v')], default='Y')
+ if choice == 'y':
+ self.changelang(page)
+ if choice == 'n':
+ pywikibot.output('Skipping ...\n')
+ if choice == 'a':
+ self.opt.always = True
+ self.changelang(page)
+ if choice == 'v':
+ self.opt.never = True
+ pywikibot.output('Skipping ...\n')
+
+
+def main(*args):
+ """
+ Process command line arguments and invoke bot.
+
+ If args is an empty list, sys.argv is used.
+
+ @param args: command line arguments
+ @type args: unicode
+ """
+ options = {}
+
+ # Process global args and prepare generator args parser
+ local_args = pywikibot.handle_args(args)
+ gen_factory = pagegenerators.GeneratorFactory()
+
+ for arg in local_args:
+ opt, _, value = arg.partition(':')
+ if opt == '-setlang':
+ options[opt[1:]] = value
+ elif arg in ('-always', '-never'):
+ options[opt[1:]] = True
+ else:
+ gen_factory.handleArg(arg)
+
+ if not options.get('setlang'):
+ pywikibot.error('No -setlang parameter given.')
+ return
+
+ site = pywikibot.Site()
+ specialpages = site.siteinfo['specialpagealiases']
+ specialpagelist = {item['realname'] for item in specialpages}
+ allowedlanguages = site._paraminfo.parameter(module='setpagelanguage',
+ param_name='lang')['type']
+ # Check if the special page PageLanguage is enabled on the wiki
+ # If it is not, page languages can't be set, and there's no point in
+ # running the bot any further
+ if 'PageLanguage' not in specialpagelist:
+ pywikibot.error("This site doesn't allow changing the "
+ 'content languages of pages; aborting.')
+ return
+ # Check if the account has the right to change page content language
+ # If it doesn't, there's no point in running the bot any further.
+ if 'pagelang' not in site.userinfo['rights']:
+ pywikibot.error("Your account doesn't have sufficient "
+ 'rights to change the content language of pages; '
+ "aborting.\n\nYou must have the 'pagelang' right "
+ 'in order to use this script.')
+ return
+
+ # Check if the language you are trying to set is allowed.
+ if options['setlang'] not in allowedlanguages:
+ pywikibot.error('"{}" is not in the list of allowed language codes; '
+ 'aborting.\n\n The following is the list of allowed '
+ 'languages. Using "default" will unset any set '
+ 'language and use the default language for the wiki '
+ 'instead.\n\n'.format(options['setlang'])
+ + ', '.join(allowedlanguages))
+ return
+
+ gen = gen_factory.getCombinedGenerator(preload=True)
+ if gen:
+ bot = ChangeLangBot(generator=gen, **options)
+ bot.run()
+ else:
+ pywikibot.bot.suggest_help(missing_generator=True)
+
+
+if __name__ == '__main__':
+ main()

To view, visit change 478611. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: Ie90869969cf0b453ca5990af2f46d8b3876827b5
Gerrit-Change-Number: 478611
Gerrit-PatchSet: 16
Gerrit-Owner: Jon Harald Søby <jhsoby@gmail.com>
Gerrit-Reviewer: John Vandenberg <jayvdb@gmail.com>
Gerrit-Reviewer: Jon Harald Søby <jhsoby@gmail.com>
Gerrit-Reviewer: Xqt <info@gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-CC: Mpaa <mpaa.wiki@gmail.com>
Gerrit-MessageType: merged