<commit_before># -*- coding: utf-8 -*-
#
# Copyright (C) 2003-2009 Edgewall Software
# Copyright (C) 2003-2005 <NAME> <<EMAIL>>
# Copyright (C) 2005 <NAME> <<EMAIL>>
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at http://trac.edgewall.org/wiki/TracLicense.
#
# This software consists of voluntary contributions made by many
# individuals. For the exact contribution history, see the revision
# history and logs, available at http://trac.edgewall.org/log/.
#
# Author: <NAME> <<EMAIL>>
#         <NAME> <<EMAIL>>

from datetime import datetime

from trac.core import *
from trac.db.util import with_transaction
from trac.resource import Resource
from trac.util.datefmt import utc, to_timestamp
from trac.util.translation import _
from trac.wiki.api import WikiSystem


class WikiPage(object):
    """Represents a wiki page (new or existing)."""

    realm = 'wiki'

    def __init__(self, env, name=None, version=None, db=None):
        self.env = env
        if isinstance(name, Resource):
            self.resource = name
            name = self.resource.id
        else:
            if version:
                version = int(version) # must be a number or None
            self.resource = Resource('wiki', name, version)
        self.name = name
        if name:
            self._fetch(name, version, db)
        else:
            self.version = 0
            self.text = self.comment = self.author = ''
            self.time = None
            self.readonly = 0
        self.old_text = self.text
        self.old_readonly = self.readonly

    def _fetch(self, name, version=None, db=None):
        if not db:
            db = self.env.get_db_cnx()
        cursor = db.cursor()
        if version is not None:
            cursor.execute("SELECT version,time,author,text,comment,readonly "
                           "FROM wiki "
                           "WHERE name=%s AND version=%s",
                           (name, int(version)))
        else:
            cursor.execute("SELECT version,time,author,text,comment,readonly "
                           "FROM wiki "
                           "WHERE name=%s ORDER BY version DESC LIMIT 1",
                           (name,))
        row = cursor.fetchone()
        if row:
            version, time, author, text, comment, readonly = row
            self.version = int(version)
            self.author = author
            self.time = datetime.fromtimestamp(time, utc)
            self.text = text
            self.comment = comment
            self.readonly = readonly and int(readonly) or 0
        else:
            self.version = 0
            self.text = self.comment = self.author = ''
            self.time = None
            self.readonly = 0
            
    exists = property(fget=lambda self: self.version > 0)

    def delete(self, version=None, db=None):
        assert self.exists, 'Cannot delete non-existent page'
        
        @with_transaction(self.env, db)
        def do_delete(db):
            cursor = db.cursor()
            if version is None:
                # Delete a wiki page completely
                cursor.execute("DELETE FROM wiki WHERE name=%s", (self.name,))
                self.env.log.info('Deleted page %s' % self.name)
            else:
                # Delete only a specific page version
                cursor.execute("DELETE FROM wiki WHERE name=%s and version=%s",
                               (self.name, version))
                self.env.log.info('Deleted version %d of page %s'
                                  % (version, self.name))

            if version is None or version == self.version:
                self._fetch(self.name, None, db)

            if not self.exists:
                # Invalidate page name cache
                WikiSystem(self.env).pages.invalidate(db)
                # Delete orphaned attachments
                from trac.attachment import Attachment
                Attachment.delete_all(self.env, 'wiki', self.name, db)

        # Let change listeners know about the deletion
        if not self.exists:
            for listener in WikiSystem(self.env).change_listeners:
                listener.wiki_page_deleted(self)
        else:
            for listener in WikiSystem(self.env).change_listeners:
                if hasattr(listener, 'wiki_page_version_deleted'):
                    listener.wiki_page_version_deleted(self)

    def save(self, author, comment, remote_addr, t=None, db=None):
        new_text = self.text != self.old_text
        if not new_text and self.readonly == self.old_readonly:
            raise TracError(_('Page not modified'))
        t = t or datetime.now(utc)

        @with_transaction(self.env, db)
        def do_save(db):
            cursor = db.cursor()
            if new_text:
                cursor.execute("INSERT INTO wiki (name,version,time,"
                               "author,ipnr,text,comment,readonly)"
                               "VALUES (%s,%s,%s,%s,%s,%s,%s,%s)",
                               (self.name, self.version + 1, to_timestamp(t),
                                author, remote_addr, self.text, comment,
                                self.readonly))
                self.version += 1
                self.resource = self.resource(version=self.version)
            else:
                cursor.execute("UPDATE wiki SET readonly=%s WHERE name=%s",<commit_msg>db: Added a missing space in an SQL statement. Related to #8751.<commit_after># -*- coding: utf-8 -*-
#
# Copyright (C) 2003-2009 Edgewall Software
# Copyright (C) 2003-2005 <NAME> <<EMAIL>>
# Copyright (C) 2005 <NAME> <<EMAIL>>
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at http://trac.edgewall.org/wiki/TracLicense.
#
# This software consists of voluntary contributions made by many
# individuals. For the exact contribution history, see the revision
# history and logs, available at http://trac.edgewall.org/log/.
#
# Author: <NAME> <<EMAIL>>
#         <NAME> <<EMAIL>>

from datetime import datetime

from trac.core import *
from trac.db.util import with_transaction
from trac.resource import Resource
from trac.util.datefmt import utc, to_timestamp
from trac.util.translation import _
from trac.wiki.api import WikiSystem


class WikiPage(object):
    """Represents a wiki page (new or existing)."""

    realm = 'wiki'

    def __init__(self, env, name=None, version=None, db=None):
        self.env = env
        if isinstance(name, Resource):
            self.resource = name
            name = self.resource.id
        else:
            if version:
                version = int(version) # must be a number or None
            self.resource = Resource('wiki', name, version)
        self.name = name
        if name:
            self._fetch(name, version, db)
        else:
            self.version = 0
            self.text = self.comment = self.author = ''
            self.time = None
            self.readonly = 0
        self.old_text = self.text
        self.old_readonly = self.readonly

    def _fetch(self, name, version=None, db=None):
        if not db:
            db = self.env.get_db_cnx()
        cursor = db.cursor()
        if version is not None:
            cursor.execute("SELECT version,time,author,text,comment,readonly "
                           "FROM wiki "
                           "WHERE name=%s AND version=%s",
                           (name, int(version)))
        else:
            cursor.execute("SELECT version,time,author,text,comment,readonly "
                           "FROM wiki "
                           "WHERE name=%s ORDER BY version DESC LIMIT 1",
                           (name,))
        row = cursor.fetchone()
        if row:
            version, time, author, text, comment, readonly = row
            self.version = int(version)
            self.author = author
            self.time = datetime.fromtimestamp(time, utc)
            self.text = text
            self.comment = comment
            self.readonly = readonly and int(readonly) or 0
        else:
            self.version = 0
            self.text = self.comment = self.author = ''
            self.time = None
            self.readonly = 0
            
    exists = property(fget=lambda self: self.version > 0)

    def delete(self, version=None, db=None):
        assert self.exists, 'Cannot delete non-existent page'
        
        @with_transaction(self.env, db)
        def do_delete(db):
            cursor = db.cursor()
            if version is None:
                # Delete a wiki page completely
                cursor.execute("DELETE FROM wiki WHERE name=%s", (self.name,))
                self.env.log.info('Deleted page %s' % self.name)
            else:
                # Delete only a specific page version
                cursor.execute("DELETE FROM wiki WHERE name=%s and version=%s",
                               (self.name, version))
                self.env.log.info('Deleted version %d of page %s'
                                  % (version, self.name))

            if version is None or version == self.version:
                self._fetch(self.name, None, db)

            if not self.exists:
                # Invalidate page name cache
                WikiSystem(self.env).pages.invalidate(db)
                # Delete orphaned attachments
                from trac.attachment import Attachment
                Attachment.delete_all(self.env, 'wiki', self.name, db)

        # Let change listeners know about the deletion
        if not self.exists:
            for listener in WikiSystem(self.env).change_listeners:
                listener.wiki_page_deleted(self)
        else:
            for listener in WikiSystem(self.env).change_listeners:
                if hasattr(listener, 'wiki_page_version_deleted'):
                    listener.wiki_page_version_deleted(self)

    def save(self, author, comment, remote_addr, t=None, db=None):
        new_text = self.text != self.old_text
        if not new_text and self.readonly == self.old_readonly:
            raise TracError(_('Page not modified'))
        t = t or datetime.now(utc)

        @with_transaction(self.env, db)
        def do_save(db):
            cursor = db.cursor()
            if new_text:
                cursor.execute("""
                    INSERT INTO wiki (name,version,time,author,ipnr,text,
                                      comment,readonly)
                    VALUES (%s,%s,%s,%s,%s,%s,%s,%s)
                    """, (self.name, self.version + 1, to_timestamp(t),
                          author, remote_addr, self.text, comment,
                          self.readonly))
                self.version += 1
                self.resource = self.resource(version=self.version)
            else:
                cursor.execute("UPDATE wiki SET readonly=%s WHERE name=%s",
{'fraction_non_alphanumeric': 0.0982836495031617, 'fraction_numerical': 0.005781391147244806, 'mean_word_length': 1.7410250061896508, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2440870', 'n_tokens_mistral': 3060, 'n_tokens_neox': 2852, 'n_words': 855}