Gerrit Patch Uploader has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/101472


Change subject: Port create_categories.py to core
......................................................................

Port create_categories.py to core

Change-Id: Id170af5a909918a56f4868665ff38333e30e37ca
---
A scripts/create_categories.py
1 file changed, 96 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core 
refs/changes/72/101472/1

diff --git a/scripts/create_categories.py b/scripts/create_categories.py
new file mode 100755
index 0000000..4305bb9
--- /dev/null
+++ b/scripts/create_categories.py
@@ -0,0 +1,96 @@
+# -*- coding: utf-8 -*-
+"""
+Program to batch create categories.
+
+The program expects a generator containing a list of page titles to be used as
+base.
+
+The following command line parameters are supported:
+
+-always         (not implemented yet) Don't ask, just do the edit.
+
+-overwrite      (not implemented yet).
+
+-parent         The name of the parent category.
+
+-basename       The base to be used for the new category names.
+
+Example:
+create_categories.py
+    -lang:commons
+    -family:commons
+    -links:User:Multichill/Wallonia
+    -parent:"Cultural heritage monuments in Wallonia"
+    -basename:"Cultural heritage monuments in"
+
+"""
+__version__ = '$Id$'
+#
+# (C) Multichill, 2011
+# (C) xqt, 2011
+#
+#   Distributed under the terms of the MIT license.
+#
+#
+import pywikibot
+from pywikibot import catlib, config, pagegenerators
+import sys
+
+
+def createCategory(page, parent, basename):
+    title = page.title(withNamespace=False)
+
+    newpage = pywikibot.Page(pywikibot.getSite(u'commons', u'commons'),
+                                 u'Category:' + basename + u' ' + title)
+    newtext = u''
+    newtext += u'[[Category:' + parent + u'|' + title + u']]\n'
+    newtext += u'[[Category:' + title + u']]\n'
+
+    if not newpage.exists():
+        #FIXME: Add user prompting and -always option
+        pywikibot.output(newpage.title())
+        pywikibot.showDiff(u'', newtext)
+
+        comment = u'Creating new category'
+        #FIXME: Add exception handling
+        newpage.put(newtext, comment)
+    else:
+        #FIXME: Add overwrite option
+        pywikibot.output(u'%s already exists, skipping' % (newpage.title(),))
+
+
+def main(args):
+    '''
+    Main loop. Get a generator and options.
+    '''
+    generator = None
+    parent = u''
+    basename = u''
+    always = False
+
+    genFactory = pagegenerators.GeneratorFactory()
+
+    for arg in pywikibot.handleArgs():
+        if arg == '-always':
+            always = True
+        elif arg.startswith('-parent:'):
+            parent = arg[len('-parent:'):].strip()
+        elif arg.startswith('-basename'):
+            basename = arg[len('-basename:'):].strip()
+        else:
+            genFactory.handleArg(arg)
+
+    generator = genFactory.getCombinedGenerator()
+    if generator:
+        for page in generator:
+            createCategory(page, parent, basename)
+    else:
+        pywikibot.output(u'No pages to work on')
+
+    pywikibot.output(u'All done')
+
+if __name__ == "__main__":
+    try:
+        main(sys.argv[1:])
+    finally:
+        pywikibot.stopme()

-- 
To view, visit https://gerrit.wikimedia.org/r/101472
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Id170af5a909918a56f4868665ff38333e30e37ca
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Gerrit Patch Uploader <[email protected]>
Gerrit-Reviewer: Guoguo12 <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to