Back to index

moin  1.9.0~rc2
Functions
MoinMoin.action.diff Namespace Reference

Functions

def execute

Function Documentation

def MoinMoin.action.diff.execute (   pagename,
  request 
)
Handle "action=diff"
    checking for either a "rev=formerrevision" parameter
    or rev1 and rev2 parameters

Definition at line 14 of file diff.py.

00014 
00015 def execute(pagename, request):
00016     """ Handle "action=diff"
00017         checking for either a "rev=formerrevision" parameter
00018         or rev1 and rev2 parameters
00019     """
00020     if not request.user.may.read(pagename):
00021         Page(request, pagename).send_page()
00022         return
00023 
00024     try:
00025         date = request.values['date']
00026         try:
00027             date = long(date) # must be long for py 2.2.x
00028         except StandardError:
00029             date = 0
00030     except KeyError:
00031         date = 0
00032 
00033     try:
00034         rev1 = int(request.values.get('rev1', -1))
00035     except StandardError:
00036         rev1 = 0
00037     try:
00038         rev2 = int(request.values.get('rev2', 0))
00039     except StandardError:
00040         rev2 = 0
00041 
00042     if rev1 == -1 and rev2 == 0:
00043         rev1 = request.rev
00044         if rev1 is None:
00045             rev1 = -1
00046 
00047     # spacing flag?
00048     ignorews = int(request.values.get('ignorews', 0))
00049 
00050     _ = request.getText
00051 
00052     # get a list of old revisions, and back out if none are available
00053     currentpage = Page(request, pagename)
00054     currentrev = currentpage.current_rev()
00055     if currentrev < 2:
00056         request.theme.add_msg(_("No older revisions available!"), "error")
00057         currentpage.send_page()
00058         return
00059 
00060     if date: # this is how we get called from RecentChanges
00061         rev1 = 0
00062         log = editlog.EditLog(request, rootpagename=pagename)
00063         for line in log.reverse():
00064             if date >= line.ed_time_usecs and int(line.rev) != 99999999:
00065                 rev1 = int(line.rev)
00066                 break
00067         else:
00068             rev1 = 1
00069         rev2 = 0
00070 
00071     # Start output
00072     # This action generates content in the user language
00073     request.setContentLanguage(request.lang)
00074 
00075     request.theme.send_title(_('Diff for "%s"') % (pagename, ), pagename=pagename, allow_doubleclick=1)
00076 
00077     if rev1 > 0 and rev2 > 0 and rev1 > rev2 or rev1 == 0 and rev2 > 0:
00078         rev1, rev2 = rev2, rev1
00079 
00080     if rev1 == -1:
00081         oldrev = currentrev - 1
00082         oldpage = Page(request, pagename, rev=oldrev)
00083     elif rev1 == 0:
00084         oldrev = currentrev
00085         oldpage = currentpage
00086     else:
00087         oldrev = rev1
00088         oldpage = Page(request, pagename, rev=oldrev)
00089 
00090     if rev2 == 0:
00091         newrev = currentrev
00092         newpage = currentpage
00093     else:
00094         newrev = rev2
00095         newpage = Page(request, pagename, rev=newrev)
00096 
00097     edit_count = abs(newrev - oldrev)
00098 
00099     f = request.formatter
00100     request.write(f.div(1, id="content"))
00101 
00102     oldrev = oldpage.get_real_rev()
00103     newrev = newpage.get_real_rev()
00104 
00105     revlist = currentpage.getRevList()
00106 
00107     # code below assumes that the page exists and has at least
00108     # one revision in the revlist, just bail out if not. Users
00109     # shouldn't really run into this anyway.
00110     if not revlist:
00111         request.write(f.div(0)) # end content div
00112         request.theme.send_footer(pagename)
00113         request.theme.send_closing_html()
00114         return
00115 
00116     title = _('Differences between revisions %d and %d') % (oldrev, newrev)
00117     if edit_count > 1:
00118         title += ' ' + _('(spanning %d versions)') % (edit_count, )
00119     title = f.text(title)
00120 
00121     # Revision list starts from 2...
00122     if oldrev <= min(revlist):
00123         disable_prev = u' disabled="disabled"'
00124     else:
00125         disable_prev = u''
00126 
00127     if newrev >= max(revlist):
00128         disable_next = u' disabled="disabled"'
00129     else:
00130         disable_next = u''
00131 
00132     page_url = wikiutil.escape(currentpage.url(request), True)
00133 
00134     revert_html = ""
00135     if request.user.may.revert(pagename):
00136         revert_html = """
00137  <td style="border:0">
00138   <form action="%s" method="get">
00139    <div style="text-align:center">
00140     <input name="action" value="revert" type="hidden">
00141     <input name="rev" value="%d" type="hidden">
00142     <input value="%s" type="submit"%s>
00143    </div>
00144   </form>
00145  </td>
00146  """ % (page_url, rev2, _("Revert to this revision"), disable_next)
00147 
00148     navigation_html = """
00149 <span class="diff-header">%s</span>
00150 <table class="diff">
00151 <tr>
00152  <td style="border:0">
00153   <form action="%s" method="get">
00154    <div style="text-align:left">
00155     <input name="action" value="diff" type="hidden">
00156     <input name="rev1" value="%d" type="hidden">
00157     <input name="rev2" value="%d" type="hidden">
00158     <input value="%s" type="submit"%s>
00159    </div>
00160   </form>
00161  </td>
00162  %s
00163  <td style="border:0">
00164   <form action="%s" method="get">
00165    <div style="text-align:right">
00166     <input name="action" value="diff" type="hidden">
00167     <input name="rev1" value="%d" type="hidden">
00168     <input name="rev2" value="%d" type="hidden">
00169     <input value="%s" type="submit"%s>
00170    </div>
00171   </form>
00172  </td>
00173 </tr>
00174 </table>
00175 """ % (title,
00176        page_url, oldrev - 1, oldrev, _("Previous change"), disable_prev,
00177        revert_html,
00178        page_url, newrev, newrev + 1, _("Next change"), disable_next, )
00179 
00180     request.write(f.rawHTML(navigation_html))
00181 
00182     if request.user.show_fancy_diff:
00183         from MoinMoin.util import diff_html
00184         request.write(f.rawHTML(diff_html.diff(request, oldpage.get_raw_body(), newpage.get_raw_body())))
00185         newpage.send_page(count_hit=0, content_only=1, content_id="content-below-diff")
00186     else:
00187         from MoinMoin.util import diff_text
00188         lines = diff_text.diff(oldpage.getlines(), newpage.getlines())
00189         if not lines:
00190             msg = f.text(" - " + _("No differences found!"))
00191             if edit_count > 1:
00192                 msg = msg + f.paragraph(1) + f.text(_('The page was saved %(count)d times, though!') % {
00193                     'count': edit_count}) + f.paragraph(0)
00194             request.write(msg)
00195         else:
00196             if ignorews:
00197                 request.write(f.text(_('(ignoring whitespace)')), f.linebreak())
00198             else:
00199                 qstr = {'action': 'diff', 'ignorews': '1', }
00200                 if rev1:
00201                     qstr['rev1'] = str(rev1)
00202                 if rev2:
00203                     qstr['rev2'] = str(rev2)
00204                 request.write(f.paragraph(1), Page(request, pagename).link_to(request,
00205                     text=_('Ignore changes in the amount of whitespace'),
00206                     querystr=qstr, rel='nofollow'), f.paragraph(0))
00207 
00208             request.write(f.preformatted(1))
00209             for line in lines:
00210                 if line[0] == "@":
00211                     request.write(f.rule(1))
00212                 request.write(f.text(line + '\n'))
00213             request.write(f.preformatted(0))
00214 
00215     request.write(f.div(0)) # end content div
00216     request.theme.send_footer(pagename)
00217     request.theme.send_closing_html()
00218