Strainu has submitted this change and it was merged.
Change subject: Ported unusedfiles.py to pywikibot/core, added unicode support.
......................................................................
Ported unusedfiles.py to pywikibot/core, added unicode support.
From unusedfiles.py from pywikibot/copmat to
pywikibot/core. Porting
involved changing the import files. Patched to include
Unicode support
using encode method on relevant strings. Fixed previous way variable
"uploader" was initiated, as it wasusing indexing to access a
dictionary in a list, causing an error, now uses the correct key.
Bug: 56901
follow-up from I8bb5bdcf1518f33a1d40ec807f752bf27a5d73a4
Change-Id: I724c1b8e8ea97be6aac21e4533d303466e1e90a0
---
A scripts/unusedfiles.py
1 file changed, 132 insertions(+), 0 deletions(-)
Approvals:
Strainu: Verified; Looks good to me, approved
jenkins-bot: Checked
diff --git a/scripts/unusedfiles.py b/scripts/unusedfiles.py
new file mode 100644
index 0000000..a94b0ed
--- /dev/null
+++ b/scripts/unusedfiles.py
@@ -0,0 +1,132 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+"""
+This bot appends some text to all unused images and other text to the
+respective uploaders.
+
+Parameters:
+
+-always Don't be asked every time.
+
+"""
+
+#
+# (C) Leonardo Gregianin, 2007
+# (C) Filnik, 2008
+# (c) xqt, 2011
+#
+# Distributed under the terms of the MIT license.
+#
+__version__ = '$Id$'
+#
+
+import pywikibot
+from pywikibot import pagegenerators
+import sys
+
+# ***** SETTINGS *******#
+#
+# - EDIT BELOW - #
+#
+# **********************#
+
+comment = {
+ 'ar': u'صور للاستبعاد',
+ 'en': u'images for elimination',
+ 'fa': u'تصویر استفاده نشده',
+ 'he': u'תמונות להסרה',
+ 'it': u'Bot: segnalo immagine orfana da eliminare',
+ 'pt': u'Bot: marcação de imagens para eliminação',
+}
+
+template_to_the_image = {
+ 'en': u'{{subst:No-use2}}',
+ 'it': u'{{immagine orfana}}',
+ 'fa': u'{{تصاویر بدون استفاده}}',
+}
+template_to_the_user = {
+ 'en': u'\n\n{{img-sem-uso|%(title)s}}',
+ 'fa': u'\n\n{{جا:اخطار به کاربر برای تصاویر بدون
استفاده|%(title)s}}--~~~~',
+ 'it': u'\n\n{{Utente:Filbot/Immagine orfana}}',
+}
+except_text = {
+ 'en': u'<table id="mw_metadata"
class="mw_metadata">',
+ 'it': u'<table id="mw_metadata"
class="mw_metadata">',
+}
+
+# ***** SETTINGS *******#
+#
+# - EDIT ABOVE - #
+#
+# **********************#
+
+
+def appendtext(page, apptext):
+ global always
+ if page.isRedirectPage():
+ page = page.getRedirectTarget().encode("utf-8")
+ if not page.exists():
+ if page.isTalkPage():
+ text = u''
+ else:
+ raise pywikibot.NoPage(u"Page '%s' does not exist" %
page.title())
+ else:
+ text = page.get()
+ # Here you can go editing. If you find you do not
+ # want to edit this page, just return
+ oldtext = text
+ text += apptext
+ if text != oldtext:
+ pywikibot.showDiff(oldtext, text)
+ if not always:
+ choice = pywikibot.inputChoice(
+ u'Do you want to accept these changes?', ['Yes',
'No', 'All'],
+ ['y', 'N', 'a'], 'N')
+ if choice == 'a':
+ always = True
+ if always or choice == 'y':
+ page.put(text, pywikibot.translate(pywikibot.getSite(), comment))
+
+
+def main():
+ global always
+ always = False
+
+ for arg in pywikibot.handleArgs():
+ if arg == '-always':
+ always = True
+ if arg == '-start':
+ start = True
+
+ mysite = pywikibot.getSite()
+ # If anything needs to be prepared, you can do it here
+ template_image = pywikibot.translate(pywikibot.getSite(),
+ template_to_the_image)
+ template_user = pywikibot.translate(pywikibot.getSite(),
+ template_to_the_user).encode("utf-8")
+ except_text_translated = pywikibot.translate(pywikibot.getSite(),
+ except_text).encode("utf-8")
+ basicgenerator = pagegenerators.UnusedFilesGenerator()
+ generator = pagegenerators.PreloadingGenerator(basicgenerator)
+ for page in generator:
+ pywikibot.output("\n\n>>> \03{lightpurple}%s\03{default}
<<<"
+ % page.title())
+ if except_text_translated not in page.getImagePageHtml() and \
+ 'http://' not in page.get():
+ pywikibot.output(u'\n' + page.title())
+ if template_image in page.get():
+ pywikibot.output(u"%s done already"
+ % page.title(asLink=True))
+ continue
+ appendtext(page, u"\n\n" + template_image)
+ uploader = page.getFileVersionHistory().pop(0)['user']
+ usertalkname = 'User Talk:%s' % uploader
+ usertalkpage = pywikibot.Page(mysite, usertalkname)
+ msg2uploader = template_user % {'title': page.title()}
+ # msg2uploader = msg2uploader.encode("utf-8")
+ appendtext(usertalkpage, msg2uploader)
+if __name__ == "__main__":
+ try:
+ main()
+ finally:
+ pywikibot.stopme()
--
To view, visit
https://gerrit.wikimedia.org/r/97016
To unsubscribe, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I724c1b8e8ea97be6aac21e4533d303466e1e90a0
Gerrit-PatchSet: 4
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Gerrit Patch Uploader <gerritpatchuploader(a)gmail.com>
Gerrit-Reviewer: Gerrit Patch Uploader <gerritpatchuploader(a)gmail.com>
Gerrit-Reviewer: Ladsgroup <ladsgroup(a)gmail.com>
Gerrit-Reviewer: Merlijn van Deen <valhallasw(a)arctus.nl>
Gerrit-Reviewer: Qgil <qgil(a)wikimedia.org>
Gerrit-Reviewer: Richardll <lettrich42(a)gmail.com>
Gerrit-Reviewer: Strainu <wiki(a)strainu.ro>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot