Jump to content

User:John Vandenberg/unusedfiles.py.diff

From Meta, a Wikimedia project coordination wiki
Index: unusedfiles.py
===================================================================
--- unusedfiles.py      (revision 4195)
+++ unusedfiles.py      (working copy)
@@ -31,25 +31,50 @@
             page.put(text, msg)
 
 def main():
+    ext = None
+    tag = u'Orphan media'
+    noticetag = None
+
+    # Read commandline parameters.
     for arg in wikipedia.handleArgs():
-        start.append(arg)
+        print 'Processing arg ' + arg
+        if arg.startswith('-ext'):
+            if len(arg) == 4:
+               raise wikipedia.Error, 'Error in arg -ext'
+            else:
+               ext = arg[5:]
+        elif arg.startswith('-tag'):
+            if len(arg) == 4:
+               raise wikipedia.Error, 'Error in arg -tag'
+            else:
+              tag = arg[5:]
+        elif arg.startswith('-noticetag'):
+            if len(arg) == 10:
+               raise wikipedia.Error, 'Error in arg -notice'
+            else:
+              noticetag = arg[11:]
+        else:
+            raise wikipedia.Error, 'Unknown arg'
 
     mysite = wikipedia.getSite()
     # If anything needs to be prepared, you can do it here
 
-    basicgenerator = pagegenerators.UnusedFilesGenerator()
+    basicgenerator = pagegenerators.UnusedFilesGenerator(extension = ext)
     generator = pagegenerators.PreloadingGenerator(basicgenerator)
     for page in generator:
-        #print page.title()
+        print page.title()
         if '<table id="mw_metadata" class="mw_metadata">' not in page.getImagePageHtml() and 'http://' not in page.get():
             wikipedia.output(u'\n' + page.title())
-            appendtext(page,u'\n\n{{subst:No-use2}}')
-            uploader = page.getFileVersionHistory().pop()[1]
-            usertalkname = u'User Talk:' + uploader
-            usertalkpage = wikipedia.Page(mysite,usertalkname)
-            msg2uploader = u'\n\n{{img-sem-uso|' + page.title() + u'}}'
-            appendtext(usertalkpage,msg2uploader)
+            appendtext(page,u'\n\n{{' + tag + u'}}')
 
+            if noticetag:
+                uploader = page.getFileVersionHistory().pop()[1]
+                if wikipedia.input(u'notify %s [Yn]?' % uploader) in 'yY':
+                    usertalkname = u'User Talk:' + uploader
+                    usertalkpage = wikipedia.Page(mysite,usertalkname)
+                    msg2uploader = u'\n\n{{subst:' + noticetag + u'|' + page.title() + u'}} ~~~~'
+                    appendtext(usertalkpage,msg2uploader)
+
 if __name__ == "__main__":
     try:
         main()