comparison MoinMoin/action/diff.py @ 1018:f5b796658f93

moved edit and diff code to own files, strange: needs a http_headers() call at end of edit.py - why?
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Mon, 24 Jul 2006 00:19:55 +0200
parents
children 6488692b1eb8
comparison
equal deleted inserted replaced
1017:2408963718c8 1018:f5b796658f93
1 # -*- coding: iso-8859-1 -*-
2 """
3 MoinMoin - show diff between 2 page revisions
4
5 @copyright: 2000-2004 by Jürgen Hermann <jh@web.de>,
6 2006 by MoinMoin:ThomasWaldmann
7 @license: GNU GPL, see COPYING for details.
8 """
9
10 from MoinMoin.logfile import editlog
11 from MoinMoin.Page import Page
12
13 def execute(pagename, request):
14 """ Handle "action=diff"
15 checking for either a "rev=formerrevision" parameter
16 or rev1 and rev2 parameters
17 """
18 if not request.user.may.read(pagename):
19 Page(request, pagename).send_page(request)
20 return
21
22 try:
23 date = request.form['date'][0]
24 try:
25 date = long(date) # must be long for py 2.2.x
26 except StandardError:
27 date = 0
28 except KeyError:
29 date = 0
30
31 try:
32 rev1 = int(request.form.get('rev1', [-1])[0])
33 except StandardError:
34 rev1 = 0
35 try:
36 rev2 = int(request.form.get('rev2', [0])[0])
37 except StandardError:
38 rev1 = 0
39
40 if rev1 == -1 and rev2 == 0:
41 try:
42 rev1 = int(request.form.get('rev', [-1])[0])
43 except StandardError:
44 rev1 = -1
45
46 # spacing flag?
47 ignorews = int(request.form.get('ignorews', [0])[0])
48
49 _ = request.getText
50
51 # get a list of old revisions, and back out if none are available
52 currentpage = Page(request, pagename)
53 revisions = currentpage.getRevList()
54 if len(revisions) < 2:
55 currentpage.send_page(request, msg=_("No older revisions available!"))
56 return
57
58 if date: # this is how we get called from RecentChanges
59 rev1 = 0
60 log = editlog.EditLog(request, rootpagename=pagename)
61 for line in log.reverse():
62 if date >= line.ed_time_usecs and int(line.rev) != 99999999:
63 rev1 = int(line.rev)
64 break
65 else:
66 rev1 = 1
67 rev2 = 0
68
69 # Start output
70 # This action generate content in the user language
71 request.setContentLanguage(request.lang)
72
73 request.http_headers()
74 request.theme.send_title(_('Diff for "%s"') % (pagename,), pagename=pagename, allow_doubleclick=1)
75
76 if rev1 > 0 and rev2 > 0 and rev1 > rev2 or rev1 == 0 and rev2 > 0:
77 rev1, rev2 = rev2, rev1
78
79 oldrev1, oldcount1 = None, 0
80 oldrev2, oldcount2 = None, 0
81
82 # get the filename of the version to compare to
83 edit_count = 0
84 for rev in revisions:
85 edit_count += 1
86 if rev <= rev1:
87 oldrev1, oldcount1 = rev, edit_count
88 if rev2 and rev >= rev2:
89 oldrev2, oldcount2 = rev, edit_count
90 if oldrev1 and oldrev2 or oldrev1 and not rev2:
91 break
92
93 if rev1 == -1:
94 oldpage = Page(request, pagename, rev=revisions[1])
95 oldcount1 -= 1
96 elif rev1 == 0:
97 oldpage = currentpage
98 # oldcount1 is still on init value 0
99 else:
100 if oldrev1:
101 oldpage = Page(request, pagename, rev=oldrev1)
102 else:
103 oldpage = Page(request, "$EmptyPage$") # hack
104 oldpage.set_raw_body("") # avoid loading from disk
105 oldrev1 = 0 # XXX
106
107 if rev2 == 0:
108 newpage = currentpage
109 # oldcount2 is still on init value 0
110 else:
111 if oldrev2:
112 newpage = Page(request, pagename, rev=oldrev2)
113 else:
114 newpage = Page(request, "$EmptyPage$") # hack
115 newpage.set_raw_body("") # avoid loading from disk
116 oldrev2 = 0 # XXX
117
118 edit_count = abs(oldcount1 - oldcount2)
119
120 # this should use the formatter, but there is none?
121 request.write('<div id="content">\n') # start content div
122 request.write('<p class="diff-header">')
123 request.write(_('Differences between revisions %d and %d') % (oldpage.get_real_rev(), newpage.get_real_rev()))
124 if edit_count > 1:
125 request.write(' ' + _('(spanning %d versions)') % (edit_count,))
126 request.write('</p>')
127
128 if request.user.show_fancy_diff:
129 from MoinMoin.util.diff import diff
130 request.write(diff(request, oldpage.get_raw_body(), newpage.get_raw_body()))
131 newpage.send_page(request, count_hit=0, content_only=1, content_id="content-below-diff")
132 else:
133 lines = wikiutil.linediff(oldpage.getlines(), newpage.getlines())
134 if not lines:
135 msg = _("No differences found!")
136 if edit_count > 1:
137 msg = msg + '<p>' + _('The page was saved %(count)d times, though!') % {
138 'count': edit_count}
139 request.write(msg)
140 else:
141 if ignorews:
142 request.write(_('(ignoring whitespace)') + '<br>')
143 else:
144 qstr = 'action=diff&ignorews=1'
145 if rev1: qstr = '%s&rev1=%s' % (qstr, rev1)
146 if rev2: qstr = '%s&rev2=%s' % (qstr, rev2)
147 request.write(Page(request, pagename).link_to(request,
148 text=_('Ignore changes in the amount of whitespace'),
149 querystr=qstr, rel='nofollow') + '<p>')
150
151 request.write('<pre>')
152 for line in lines:
153 if line[0] == "@":
154 request.write('<hr>')
155 request.write(wikiutil.escape(line)+'\n')
156 request.write('</pre>')
157
158 request.write('</div>\n') # end content div
159 request.theme.send_footer(pagename)
160 request.theme.send_closing_html()
161