Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python2..../Demo/cgi
File: wiki.py
"""Wiki main program. Imported and run by cgi3.py."""
[0] Fix | Delete
[1] Fix | Delete
import os, re, cgi, sys, tempfile
[2] Fix | Delete
escape = cgi.escape
[3] Fix | Delete
[4] Fix | Delete
def main():
[5] Fix | Delete
form = cgi.FieldStorage()
[6] Fix | Delete
print "Content-type: text/html"
[7] Fix | Delete
print
[8] Fix | Delete
cmd = form.getvalue("cmd", "view")
[9] Fix | Delete
page = form.getvalue("page", "FrontPage")
[10] Fix | Delete
wiki = WikiPage(page)
[11] Fix | Delete
method = getattr(wiki, 'cmd_' + cmd, None) or wiki.cmd_view
[12] Fix | Delete
method(form)
[13] Fix | Delete
[14] Fix | Delete
class WikiPage:
[15] Fix | Delete
[16] Fix | Delete
homedir = tempfile.gettempdir()
[17] Fix | Delete
scripturl = os.path.basename(sys.argv[0])
[18] Fix | Delete
[19] Fix | Delete
def __init__(self, name):
[20] Fix | Delete
if not self.iswikiword(name):
[21] Fix | Delete
raise ValueError, "page name is not a wiki word"
[22] Fix | Delete
self.name = name
[23] Fix | Delete
self.load()
[24] Fix | Delete
[25] Fix | Delete
def cmd_view(self, form):
[26] Fix | Delete
print "<h1>", escape(self.splitwikiword(self.name)), "</h1>"
[27] Fix | Delete
print "<p>"
[28] Fix | Delete
for line in self.data.splitlines():
[29] Fix | Delete
line = line.rstrip()
[30] Fix | Delete
if not line:
[31] Fix | Delete
print "<p>"
[32] Fix | Delete
else:
[33] Fix | Delete
print self.formatline(line)
[34] Fix | Delete
print "<hr>"
[35] Fix | Delete
print "<p>", self.mklink("edit", self.name, "Edit this page") + ";"
[36] Fix | Delete
print self.mklink("view", "FrontPage", "go to front page") + "."
[37] Fix | Delete
[38] Fix | Delete
def formatline(self, line):
[39] Fix | Delete
words = []
[40] Fix | Delete
for word in re.split('(\W+)', line):
[41] Fix | Delete
if self.iswikiword(word):
[42] Fix | Delete
if os.path.isfile(self.mkfile(word)):
[43] Fix | Delete
word = self.mklink("view", word, word)
[44] Fix | Delete
else:
[45] Fix | Delete
word = self.mklink("new", word, word + "*")
[46] Fix | Delete
else:
[47] Fix | Delete
word = escape(word)
[48] Fix | Delete
words.append(word)
[49] Fix | Delete
return "".join(words)
[50] Fix | Delete
[51] Fix | Delete
def cmd_edit(self, form, label="Change"):
[52] Fix | Delete
print "<h1>", label, self.name, "</h1>"
[53] Fix | Delete
print '<form method="POST" action="%s">' % self.scripturl
[54] Fix | Delete
s = '<textarea cols="70" rows="20" name="text">%s</textarea>'
[55] Fix | Delete
print s % self.data
[56] Fix | Delete
print '<input type="hidden" name="cmd" value="create">'
[57] Fix | Delete
print '<input type="hidden" name="page" value="%s">' % self.name
[58] Fix | Delete
print '<br>'
[59] Fix | Delete
print '<input type="submit" value="%s Page">' % label
[60] Fix | Delete
print "</form>"
[61] Fix | Delete
[62] Fix | Delete
def cmd_create(self, form):
[63] Fix | Delete
self.data = form.getvalue("text", "").strip()
[64] Fix | Delete
error = self.store()
[65] Fix | Delete
if error:
[66] Fix | Delete
print "<h1>I'm sorry. That didn't work</h1>"
[67] Fix | Delete
print "<p>An error occurred while attempting to write the file:"
[68] Fix | Delete
print "<p>", escape(error)
[69] Fix | Delete
else:
[70] Fix | Delete
# Use a redirect directive, to avoid "reload page" problems
[71] Fix | Delete
print "<head>"
[72] Fix | Delete
s = '<meta http-equiv="refresh" content="1; URL=%s">'
[73] Fix | Delete
print s % (self.scripturl + "?cmd=view&page=" + self.name)
[74] Fix | Delete
print "<head>"
[75] Fix | Delete
print "<h1>OK</h1>"
[76] Fix | Delete
print "<p>If nothing happens, please click here:",
[77] Fix | Delete
print self.mklink("view", self.name, self.name)
[78] Fix | Delete
[79] Fix | Delete
def cmd_new(self, form):
[80] Fix | Delete
self.cmd_edit(form, label="Create")
[81] Fix | Delete
[82] Fix | Delete
def iswikiword(self, word):
[83] Fix | Delete
return re.match("[A-Z][a-z]+([A-Z][a-z]*)+", word)
[84] Fix | Delete
[85] Fix | Delete
def splitwikiword(self, word):
[86] Fix | Delete
chars = []
[87] Fix | Delete
for c in word:
[88] Fix | Delete
if chars and c.isupper():
[89] Fix | Delete
chars.append(' ')
[90] Fix | Delete
chars.append(c)
[91] Fix | Delete
return "".join(chars)
[92] Fix | Delete
[93] Fix | Delete
def mkfile(self, name=None):
[94] Fix | Delete
if name is None:
[95] Fix | Delete
name = self.name
[96] Fix | Delete
return os.path.join(self.homedir, name + ".txt")
[97] Fix | Delete
[98] Fix | Delete
def mklink(self, cmd, page, text):
[99] Fix | Delete
link = self.scripturl + "?cmd=" + cmd + "&page=" + page
[100] Fix | Delete
return '<a href="%s">%s</a>' % (link, text)
[101] Fix | Delete
[102] Fix | Delete
def load(self):
[103] Fix | Delete
try:
[104] Fix | Delete
f = open(self.mkfile())
[105] Fix | Delete
data = f.read().strip()
[106] Fix | Delete
f.close()
[107] Fix | Delete
except IOError:
[108] Fix | Delete
data = ""
[109] Fix | Delete
self.data = data
[110] Fix | Delete
[111] Fix | Delete
def store(self):
[112] Fix | Delete
data = self.data
[113] Fix | Delete
try:
[114] Fix | Delete
f = open(self.mkfile(), "w")
[115] Fix | Delete
f.write(data)
[116] Fix | Delete
if data and not data.endswith('\n'):
[117] Fix | Delete
f.write('\n')
[118] Fix | Delete
f.close()
[119] Fix | Delete
return ""
[120] Fix | Delete
except IOError, err:
[121] Fix | Delete
return "IOError: %s" % str(err)
[122] Fix | Delete
[123] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function