| 1 |
# Author: Steven N. Severinghaus <sns@severinghaus.org> |
|---|
| 2 |
# Last Modified: 2008-09-23 by Bill Coffman <bill.coffman@gmail.com> |
|---|
| 3 |
# to support trac v0.11 -- tested on 0.11.1 |
|---|
| 4 |
# Home: http://trac-hacks.org/wiki/LastModifiedMacro |
|---|
| 5 |
# |
|---|
| 6 |
# The LastModified macro is free software; you can redistribute it and/or |
|---|
| 7 |
# modify it under the terms of the GNU General Public License as published |
|---|
| 8 |
# by the Free Software Foundation; either version 2 of the License, or (at |
|---|
| 9 |
# your option) any later version. |
|---|
| 10 |
# |
|---|
| 11 |
# Created for the BFGFF project, http://chimp.acm.uiuc.edu/ |
|---|
| 12 |
# |
|---|
| 13 |
# Shows the last modification date of the specified page, or the page the |
|---|
| 14 |
# macro appears in if not specified. An optional argument, delta, can be |
|---|
| 15 |
# given to show the time elapsed since the last modification. The output is |
|---|
| 16 |
# placed in span with a title that gives the exact modification date and |
|---|
| 17 |
# the author of the change. |
|---|
| 18 |
# |
|---|
| 19 |
# For example, [[LastModified(CustomMacros)]] produces: |
|---|
| 20 |
# 09/06 20:34 |
|---|
| 21 |
# |
|---|
| 22 |
# Alternatively, [[LastModified(CustomMacros,delta)]] produces: |
|---|
| 23 |
# 5 weeks |
|---|
| 24 |
# |
|---|
| 25 |
|
|---|
| 26 |
from StringIO import StringIO |
|---|
| 27 |
from trac.util.html import Markup |
|---|
| 28 |
from trac.wiki.formatter import Formatter |
|---|
| 29 |
|
|---|
| 30 |
from datetime import datetime |
|---|
| 31 |
from trac.util.datefmt import format_datetime, utc |
|---|
| 32 |
from trac.wiki.macros import WikiMacroBase |
|---|
| 33 |
from trac.resource import get_resource_name |
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
class LastModifiedMacro(WikiMacroBase): |
|---|
| 37 |
|
|---|
| 38 |
"""Displays the last modified time of the specified wiki page.""" |
|---|
| 39 |
revision = "$Rev$" |
|---|
| 40 |
url = "$URL$" |
|---|
| 41 |
|
|---|
| 42 |
def expand_macro(self, formatter, name, arguments): |
|---|
| 43 |
|
|---|
| 44 |
args = arguments.split(',') |
|---|
| 45 |
|
|---|
| 46 |
if not arguments: |
|---|
| 47 |
page_name = get_resource_name( self.env, formatter.resource ) |
|---|
| 48 |
mode='normal' |
|---|
| 49 |
elif len(args) == 1: |
|---|
| 50 |
page_name=args[0] |
|---|
| 51 |
mode='normal' |
|---|
| 52 |
else: |
|---|
| 53 |
page_name=args[0] |
|---|
| 54 |
mode='delta' |
|---|
| 55 |
|
|---|
| 56 |
db=self.env.get_db_cnx() |
|---|
| 57 |
cursor=db.cursor() |
|---|
| 58 |
cursor.execute("SELECT author, time FROM wiki WHERE name = '%s' " |
|---|
| 59 |
"ORDER BY version DESC LIMIT 1" % page_name) |
|---|
| 60 |
row = cursor.fetchone() |
|---|
| 61 |
if not row: |
|---|
| 62 |
out = StringIO('cannot find "' + page_name + '"') |
|---|
| 63 |
return Markup(out.getvalue()) |
|---|
| 64 |
|
|---|
| 65 |
author = row[0] |
|---|
| 66 |
time_int = row[1] |
|---|
| 67 |
|
|---|
| 68 |
last_mod = datetime.fromtimestamp(time_int, utc) |
|---|
| 69 |
now = datetime.now(utc) |
|---|
| 70 |
elapsed = now - last_mod |
|---|
| 71 |
if mode=='delta': |
|---|
| 72 |
if elapsed.days==0: |
|---|
| 73 |
if elapsed.seconds/3600>1.5: |
|---|
| 74 |
count=elapsed.seconds/3600 |
|---|
| 75 |
unit='hour' |
|---|
| 76 |
elif elapsed.seconds/60>1.5: |
|---|
| 77 |
count=elapsed.seconds/60 |
|---|
| 78 |
unit='minute' |
|---|
| 79 |
else: |
|---|
| 80 |
count=elapsed.seconds |
|---|
| 81 |
unit='second' |
|---|
| 82 |
elif elapsed.days/3650>1.5: |
|---|
| 83 |
count=elapsed.days/3650 |
|---|
| 84 |
unit='decade' |
|---|
| 85 |
elif elapsed.days/365>1.5: |
|---|
| 86 |
count=elapsed.days/365 |
|---|
| 87 |
unit='year' |
|---|
| 88 |
elif elapsed.days/30>1.5: |
|---|
| 89 |
count=elapsed.days/30 |
|---|
| 90 |
unit='month' |
|---|
| 91 |
elif elapsed.days/7>1.5: |
|---|
| 92 |
count=elapsed.days/7 |
|---|
| 93 |
unit='week' |
|---|
| 94 |
else: |
|---|
| 95 |
count=elapsed.days |
|---|
| 96 |
unit='day' |
|---|
| 97 |
text =""+repr(count)+" "+unit |
|---|
| 98 |
if (count != 1 and count != -1): text+="s" |
|---|
| 99 |
else: |
|---|
| 100 |
text = format_datetime(last_mod, '%c') |
|---|
| 101 |
text = format_datetime(last_mod, '%m/%d %k:%M') |
|---|
| 102 |
|
|---|
| 103 |
out = StringIO(text) |
|---|
| 104 |
return Markup(out.getvalue()) |
|---|