<commit_before># -*- coding: iso8859-1 -*-
#
# Copyright (C) 2003, 2004, 2005 Edgewall Software
# Copyright (C) 2003, 2004, 2005 <NAME> <<EMAIL>>
#
# Trac is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# Trac is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# Author: <NAME> <<EMAIL>>

from trac import perm
from trac.util import enum, escape, shorten_line
from trac.Module import Module
from trac.versioncontrol.svn_authz import SubversionAuthorizer
from trac.web.main import add_link
from trac.WikiFormatter import wiki_to_oneliner, wiki_to_html

import time

AVAILABLE_FILTERS = ('ticket', 'changeset', 'wiki', 'milestone')


class Timeline(Module):

    def get_info(self, req, start, stop, maxrows, filters=AVAILABLE_FILTERS):
        perm_map = {'ticket': perm.TICKET_VIEW, 'changeset': perm.CHANGESET_VIEW,
                    'wiki': perm.WIKI_VIEW, 'milestone': perm.MILESTONE_VIEW}
        filters = list(filters) # copy list so we can make modifications
        for k,v in perm_map.items():
            if k in filters and not self.perm.has_permission(v):
                filters.remove(k)
        if not filters:
            return []

        sql, params = [], []
        if 'changeset' in filters:
            sql.append("SELECT time,rev,'','changeset',message,author"
                       " FROM revision WHERE time>=%s AND time<=%s")
            params += (start, stop)
        if 'ticket' in filters:
            sql.append("SELECT time,id,'','newticket',summary,reporter"
                       " FROM ticket WHERE time>=%s AND time<=%s")
            params += (start, stop)
            sql.append("SELECT time,ticket,'','reopenedticket','',author "
                       "FROM ticket_change WHERE field='status' "
                       "AND newvalue='reopened' AND time>=%s AND time<=%s")
            params += (start, stop)
            sql.append("SELECT t1.time,t1.ticket,t2.newvalue,'closedticket',"
                       "t3.newvalue,t1.author"
                       " FROM ticket_change t1"
                       "   INNER JOIN ticket_change t2 ON t1.ticket = t2.ticket"
                       "     AND t1.time = t2.time"
                       "   LEFT OUTER JOIN ticket_change t3 ON t1.time = t3.time"
                       "     AND t1.ticket = t3.ticket AND t3.field = 'comment'"
                       " WHERE t1.field = 'status' AND t1.newvalue = 'closed'"
                       "   AND t2.field = 'resolution'"
                       "   AND t1.time >= %s AND t1.time <= %s")
            params += (start,stop)
        if 'wiki' in filters:
            sql.append("SELECT time,-1,name,'wiki',comment,author"
                       " FROM wiki WHERE time>=%s AND time<=%s")
            params += (start, stop)
        if 'milestone' in filters:
            sql.append("SELECT completed AS time,-1,name,'milestone','',''" 
                       " FROM milestone WHERE completed>=%s AND completed<=%s")
            params += (start, stop)

        sql = ' UNION ALL '.join(sql) + ' ORDER BY time DESC'
        if maxrows:
            sql += ' LIMIT %d'
            params += (maxrows,)

        cursor = self.db.cursor()
        cursor.execute(sql, params)

        # Make the data more HDF-friendly
        info = []
        for idx, row in enum(cursor):
            if idx == 0:
                req.check_modified(int(row[0]))
            t = time.localtime(int(row[0]))
            gmt = time.gmtime(int(row[0]))
            item = {
                'time': time.strftime('%H:%M', t),
                'date': time.strftime('%x', t),
                'datetime': time.strftime('%a, %d %b %Y %H:%M:%S GMT', gmt),
                'idata': int(row[1]),
                'tdata': escape(row[2]),
                'type': row[3],
                'message': row[4] or '',
                'author': escape(row[5] or 'anonymous')
            }
            info.append(item)
        return info

    def render(self, req):
        self.perm.assert_permission(perm.TIMELINE_VIEW)
        self.authzperm = SubversionAuthorizer(self.env, req.authname) # Kludge

        # Parse the from date and adjust the timestamp to the last second of
        # the day
        t = time.localtime()
        if req.args.has_key('from'):
            try:
                t = time.strptime(req.args.get('from'), '%x')
            except:
                pass
        fromdate = time.mktime((t[0], t[1], t[2], 23, 59, 59, t[6], t[7], t[8]))

        try:
            daysback = max(0, int(req.args.get('daysback', '')))
        except ValueError:
            daysback = 30
        req.hdf['timeline.from'] = time.strftime('%x', time.localtime(fromdate))
        req.hdf['timeline.daysback'] = daysback

        stop = fromdate
        start = stop - (daysback + 1) * 86400
        maxrows = int(req.args.get('max', 0))

        filters = [k for k in AVAILABLE_FILTERS if k in req.args.keys()]
        if not filters:
            filters = AVAILABLE_FILTERS[:]

        req.hdf['title'] = 'Timeline'
        format = req.args.get('format')

        events = self.get_info(req, start, stop, maxrows, filters)
        for idx, event in enum(events):
            render_func = getattr(self, '_render_%s' % event['type'])
            event = render_func(req, event)
            if not event:
                continue

            if format == 'rss':
                # For RSS, author must be an email address
                if item['author'].find('@') != -1:
                    item['author.email'] = item['author']

            req.hdf['timeline.items.%d' % idx] = event

        if format == 'rss':
            self.display_rss(req)
        else:
            self.display_html(req, filters)
<commit_msg>Fix another incorrect var ref. Closes #1371.<commit_after># -*- coding: iso8859-1 -*-
#
# Copyright (C) 2003, 2004, 2005 Edgewall Software
# Copyright (C) 2003, 2004, 2005 <NAME> <<EMAIL>>
#
# Trac is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# Trac is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# Author: <NAME> <<EMAIL>>

from trac import perm
from trac.util import enum, escape, shorten_line
from trac.Module import Module
from trac.versioncontrol.svn_authz import SubversionAuthorizer
from trac.web.main import add_link
from trac.WikiFormatter import wiki_to_oneliner, wiki_to_html

import time

AVAILABLE_FILTERS = ('ticket', 'changeset', 'wiki', 'milestone')


class Timeline(Module):

    def get_info(self, req, start, stop, maxrows, filters=AVAILABLE_FILTERS):
        perm_map = {'ticket': perm.TICKET_VIEW, 'changeset': perm.CHANGESET_VIEW,
                    'wiki': perm.WIKI_VIEW, 'milestone': perm.MILESTONE_VIEW}
        filters = list(filters) # copy list so we can make modifications
        for k,v in perm_map.items():
            if k in filters and not self.perm.has_permission(v):
                filters.remove(k)
        if not filters:
            return []

        sql, params = [], []
        if 'changeset' in filters:
            sql.append("SELECT time,rev,'','changeset',message,author"
                       " FROM revision WHERE time>=%s AND time<=%s")
            params += (start, stop)
        if 'ticket' in filters:
            sql.append("SELECT time,id,'','newticket',summary,reporter"
                       " FROM ticket WHERE time>=%s AND time<=%s")
            params += (start, stop)
            sql.append("SELECT time,ticket,'','reopenedticket','',author "
                       "FROM ticket_change WHERE field='status' "
                       "AND newvalue='reopened' AND time>=%s AND time<=%s")
            params += (start, stop)
            sql.append("SELECT t1.time,t1.ticket,t2.newvalue,'closedticket',"
                       "t3.newvalue,t1.author"
                       " FROM ticket_change t1"
                       "   INNER JOIN ticket_change t2 ON t1.ticket = t2.ticket"
                       "     AND t1.time = t2.time"
                       "   LEFT OUTER JOIN ticket_change t3 ON t1.time = t3.time"
                       "     AND t1.ticket = t3.ticket AND t3.field = 'comment'"
                       " WHERE t1.field = 'status' AND t1.newvalue = 'closed'"
                       "   AND t2.field = 'resolution'"
                       "   AND t1.time >= %s AND t1.time <= %s")
            params += (start,stop)
        if 'wiki' in filters:
            sql.append("SELECT time,-1,name,'wiki',comment,author"
                       " FROM wiki WHERE time>=%s AND time<=%s")
            params += (start, stop)
        if 'milestone' in filters:
            sql.append("SELECT completed AS time,-1,name,'milestone','',''" 
                       " FROM milestone WHERE completed>=%s AND completed<=%s")
            params += (start, stop)

        sql = ' UNION ALL '.join(sql) + ' ORDER BY time DESC'
        if maxrows:
            sql += ' LIMIT %d'
            params += (maxrows,)

        cursor = self.db.cursor()
        cursor.execute(sql, params)

        # Make the data more HDF-friendly
        info = []
        for idx, row in enum(cursor):
            if idx == 0:
                req.check_modified(int(row[0]))
            t = time.localtime(int(row[0]))
            gmt = time.gmtime(int(row[0]))
            item = {
                'time': time.strftime('%H:%M', t),
                'date': time.strftime('%x', t),
                'datetime': time.strftime('%a, %d %b %Y %H:%M:%S GMT', gmt),
                'idata': int(row[1]),
                'tdata': escape(row[2]),
                'type': row[3],
                'message': row[4] or '',
                'author': escape(row[5] or 'anonymous')
            }
            info.append(item)
        return info

    def render(self, req):
        self.perm.assert_permission(perm.TIMELINE_VIEW)
        self.authzperm = SubversionAuthorizer(self.env, req.authname) # Kludge

        # Parse the from date and adjust the timestamp to the last second of
        # the day
        t = time.localtime()
        if req.args.has_key('from'):
            try:
                t = time.strptime(req.args.get('from'), '%x')
            except:
                pass
        fromdate = time.mktime((t[0], t[1], t[2], 23, 59, 59, t[6], t[7], t[8]))

        try:
            daysback = max(0, int(req.args.get('daysback', '')))
        except ValueError:
            daysback = 30
        req.hdf['timeline.from'] = time.strftime('%x', time.localtime(fromdate))
        req.hdf['timeline.daysback'] = daysback

        stop = fromdate
        start = stop - (daysback + 1) * 86400
        maxrows = int(req.args.get('max', 0))

        filters = [k for k in AVAILABLE_FILTERS if k in req.args.keys()]
        if not filters:
            filters = AVAILABLE_FILTERS[:]

        req.hdf['title'] = 'Timeline'
        format = req.args.get('format')

        events = self.get_info(req, start, stop, maxrows, filters)
        for idx, event in enum(events):
            render_func = getattr(self, '_render_%s' % event['type'])
            event = render_func(req, event)
            if not event:
                continue

            if format == 'rss':
                # For RSS, author must be an email address
                if event['author'].find('@') != -1:
                    event['author.email'] = event['author']

            req.hdf['timeline.items.%d' % idx] = event

        if format == 'rss':
            self.display_rss(req)
        else:
            self.display_html(req, filters)

{'fraction_non_alphanumeric': 0.12523854961832062, 'fraction_numerical': 0.015267175572519083, 'mean_word_length': 1.93512252042007, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '573546', 'n_tokens_mistral': 3859, 'n_tokens_neox': 3591, 'n_words': 1074}