Xqt has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/478611 )
Change subject: Add change_pagelang script
......................................................................
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(-)
Approvals:
jenkins-bot: Verified
Xqt: Looks good to me, approved
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:
+
+¶ms;
+
+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
+ '¶ms;': 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 https://gerrit.wikimedia.org/r/c/pywikibot/core/+/478611
To unsubscribe, or for help writing mail filters, visit https://gerrit.wikimedia.org/r/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(a)gmail.com>
Gerrit-Reviewer: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: Jon Harald Søby <jhsoby(a)gmail.com>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-CC: Mpaa <mpaa.wiki(a)gmail.com>
Gerrit-MessageType: merged
jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/636164 )
Change subject: [bugfix] upload a file that ends with the '\r' byte
......................................................................
[bugfix] upload a file that ends with the '\r' byte
Workaround (hack) for T132676.
Append another '\r' so that one is the payload and the second is used
for newline when mangled by email package.
Bug: T132676
Change-Id: I06d4c512a9cc1c29c5aa242f098ac2f533f6a59f
---
M pywikibot/site/__init__.py
1 file changed, 8 insertions(+), 0 deletions(-)
Approvals:
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/site/__init__.py b/pywikibot/site/__init__.py
index f4408a1..9356600 100644
--- a/pywikibot/site/__init__.py
+++ b/pywikibot/site/__init__.py
@@ -5700,6 +5700,14 @@
while True:
f.seek(offset)
chunk = f.read(chunk_size)
+ # workaround (hack) for T132676
+ # append another '\r' so that one is the payload and
+ # the second is used for newline when mangled by email
+ # package.
+ if (len(chunk) < chunk_size
+ or (offset + len(chunk)) == filesize
+ and chunk[-1] == b'\r'[0]):
+ chunk += b'\r'
req = self._request(
throttle=throttle, mime=True,
parameters={
--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/636164
To unsubscribe, or for help writing mail filters, visit https://gerrit.wikimedia.org/r/settings
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I06d4c512a9cc1c29c5aa242f098ac2f533f6a59f
Gerrit-Change-Number: 636164
Gerrit-PatchSet: 2
Gerrit-Owner: Mpaa <mpaa.wiki(a)gmail.com>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: Zhuyifei1999 <zhuyifei1999(a)gmail.com>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged