Z Wikipedii
#!/usr/bin/python
# Script based on editarticle.py
# adds warning to the named User about "Vandalism"
__metaclass__ = type
__version__ = "$Id: witam.py,v 1.31 2006/03/07 00:23:33 wikipedian Exp $"
sig = u" (edited with witam.py 0.2)"
import sys
import os
import optparse
import string
import tempfile
import wikipedia
import config
class ArticleEditor:
joinchars = string.letters + '[]' + string.digits # join lines if line starts with this ones
def __init__(self, args):
"""Takes one argument, usually this is sys.argv[1:]"""
self.all_args = args
self.set_options()
self.setpage()
self.site = wikipedia.getSite()
def set_options(self):
"""Parse commandline and set options attribute"""
my_args = []
for arg in self.all_args:
arg = wikipedia.argHandler(arg, 'vandal')
if arg:
my_args.append(arg)
parser = optparse.OptionParser()
parser.add_option("-u","--user", action="store_true", default=False, help="Name of user to be warn.");
self.options = parser.parse_args(args=my_args)[0]
def setpage(self):
"""Sets page and page title"""
site = wikipedia.getSite()
if (self.options.user):
pageTitle = "Dyskusja_Wikipedysty:" + self.options.user
else:
pageTitle = "Dyskusja_Wikipedysty:" + wikipedia.input(u"User_name:")
self.page = wikipedia.Page(site, pageTitle)
if self.page.isRedirectPage():
self.page = wikipedia.Page(site, self.page.getRedirectTarget())
def repair(self, content):
"""
Removes single newlines.
"""
#####
# This method was disabled because its functionality belong into
# cosmetic_changes.py, not here.
return content
#
#####
if self.options.join_lines:
lines = content.splitlines()
result = []
for i, line in enumerate(lines):
try:
nextline = lines[i+1]
except IndexError:
nextline = "last"
result.append(line)
if line.strip() == "" or line[0] not in self.joinchars or \
nextline.strip() == "" or nextline[0] not in self.joinchars:
result.append('\n')
else:
result.append(" ")
s = "".join(result)
else:
s = content
return s
def handle_edit_conflict(self):
fn = os.path.join(tempfile.gettempdir(), self.page.title())
fp = open(fn, 'w')
fp.write(new)
fp.close()
wikipedia.output(u"An edit conflict has arisen. Your edit has been saved to %s. Please try again." % fn)
def run(self):
try:
old = self.page.get()
except wikipedia.NoPage:
old = ""
#cheking does user already has warning
#(his Talk page wont be empty then)
print old
if ( old == "" ):
new = "{{subst:Witaj}}\n\nPozdrawiam [[Wikipedysta:Egon|Egon]] [[Dyskusja_Wikipedysty:Egon|Moja Strona Kontaktowa]]"
if new and old != new:
new = self.repair(new)
wikipedia.showDiff(old, new)
#comment = wikipedia.input(u"For what? ") + sig
comment = "Przywitwanie. " + sig
try:
self.page.put(new, comment = comment, minorEdit = False)
except wikipedia.EditConflict:
self.handle_edit_conflict(new)
else:
wikipedia.output(u"Nothing changed")
def main():
app = ArticleEditor(sys.argv[1:])
app.run()
if __name__ == "__main__":
try:
main()
except:
wikipedia.stopme()
raise
wikipedia.stopme()