<s> from trac . core import * <EOL> from trac . resource import Resource <EOL> from trac . util . datefmt import datetime_now , from_utimestamp , to_utimestamp , utc <EOL> from trac . util . translation import _ <EOL> from trac . wiki . api import WikiSystem , validate_page_name <EOL> class WikiPage ( object ) : <EOL> """<STR_LIT>""" <EOL> realm = WikiSystem . realm <EOL> @ property <EOL> def resource ( self ) : <EOL> return Resource ( self . realm , self . name , self . _resource_version ) <EOL> def __init__ ( self , env , name = None , version = None ) : <EOL> """<STR_LIT>""" <EOL> self . env = env <EOL> if version : <EOL> try : <EOL> version = int ( version ) <EOL> except ValueError : <EOL> version = None <EOL> if isinstance ( name , Resource ) : <EOL> resource = name <EOL> name = resource . id <EOL> if version is None and resource . version is not None : <EOL> try : <EOL> version = int ( resource . version ) <EOL> except ValueError : <EOL> version = None <EOL> self . name = name <EOL> self . _resource_version = version <EOL> if name : <EOL> self . _fetch ( name , version ) <EOL> else : <EOL> self . version = <NUM_LIT:0> <EOL> self . text = self . comment = self . author = '<STR_LIT>' <EOL> self . time = None <EOL> self . readonly = <NUM_LIT:0> <EOL> self . old_text = self . text <EOL> self . old_readonly = self . readonly <EOL> def _fetch ( self , name , version = None ) : <EOL> if version is not None : <EOL> sql = """<STR_LIT>""" <EOL> args = ( name , int ( version ) ) <EOL> else : <EOL> sql = """<STR_LIT>""" <EOL> args = ( name , ) <EOL> for version , time , author , text , comment , readonly in self . env . db_query ( sql , args ) : <EOL> self . version = int ( version ) <EOL> self . author = author <EOL> self . time = from_utimestamp ( time ) <EOL> self . text = text <EOL> self . comment = comment <EOL> self . readonly = int ( readonly ) if readonly else <NUM_LIT:0> <EOL> break <EOL> else : <EOL> self . version = <NUM_LIT:0> <EOL> self . text = self . comment = self . author = '<STR_LIT>' <EOL> self . time = None <EOL> self . readonly = <NUM_LIT:0> <EOL> def __repr__ ( self ) : <EOL> if self . name is None : <EOL> name = self . name <EOL> else : <EOL> name = u'<STR_LIT>' % ( self . name , self . version ) <EOL> return '<STR_LIT>' % ( self . __class__ . __name__ , name ) <EOL> exists = property ( lambda self : self . version > <NUM_LIT:0> ) <EOL> def delete ( self , version = None ) : <EOL> """<STR_LIT>""" <EOL> if not self . exists : <EOL> raise TracError ( _ ( "<STR_LIT>" ) ) <EOL> with self . env . db_transaction as db : <EOL> if version is None : <EOL> db ( "<STR_LIT>" , ( self . name , ) ) <EOL> self . env . log . info ( "<STR_LIT>" , self . name ) <EOL> else : <EOL> db ( "<STR_LIT>" , <EOL> ( self . name , version ) ) <EOL> self . env . log . info ( "<STR_LIT>" , version , <EOL> self . name ) <EOL> if version is None or version == self . version : <EOL> self . _fetch ( self . name , None ) <EOL> if not self . exists : <EOL> del WikiSystem ( self . env ) . pages <EOL> from trac . attachment import Attachment <EOL> Attachment . delete_all ( self . env , self . realm , self . name ) <EOL> if not self . exists : <EOL> for listener in WikiSystem ( self . env ) . change_listeners : <EOL> listener . wiki_page_deleted ( self ) <EOL> else : <EOL> for listener in WikiSystem ( self . env ) . change_listeners : <EOL> if hasattr ( listener , '<STR_LIT>' ) : <EOL> listener . wiki_page_version_deleted ( self ) <EOL> def save ( self , author , comment , remote_addr = None , t = None ) : <EOL> """<STR_LIT>""" <EOL> if not validate_page_name ( self . name ) : <EOL> raise TracError ( _ ( "<STR_LIT>" , <EOL> name = self . name ) ) <EOL> new_text = self . text != self . old_text <EOL> if not new_text and self . readonly == self . old_readonly : <EOL> raise TracError ( _ ( "<STR_LIT>" ) ) <EOL> t = t or datetime_now ( utc ) <EOL> with self . env . db_transaction as db : <EOL> if new_text : <EOL> db ( """<STR_LIT>""" , ( self . name , self . version + <NUM_LIT:1> , to_utimestamp ( t ) , <EOL> author , remote_addr , self . text , comment , <EOL> self . readonly ) ) <EOL> self . version += <NUM_LIT:1> <EOL> else : <EOL> db ( "<STR_LIT>" , <EOL> ( self . readonly , self . name ) ) <EOL> if self . version == <NUM_LIT:1> : <EOL> del WikiSystem ( self . env ) . pages <EOL> self . author = author <EOL> self . comment = comment <EOL> self . time = t <EOL> for listener in WikiSystem ( self . env ) . change_listeners : <EOL> if self . version == <NUM_LIT:1> : <EOL> listener . wiki_page_added ( self ) <EOL> else : <EOL> from trac . util import arity <EOL> if arity ( listener . wiki_page_changed ) == <NUM_LIT:6> : <EOL> listener . wiki_page_changed ( self , self . version , t , <EOL> comment , author , remote_addr ) <EOL> else : <EOL> listener . wiki_page_changed ( self , self . version , t , <EOL> comment , author ) <EOL> self . old_readonly = self . readonly <EOL> self . old_text = self . text <EOL> def rename ( self , new_name ) : <EOL> """<STR_LIT>""" <EOL> if not self . exists : <EOL> raise TracError ( _ ( "<STR_LIT>" ) ) <EOL> if not validate_page_name ( new_name ) : <EOL> raise TracError ( _ ( "<STR_LIT>" , <EOL> name = new_name ) ) <EOL> old_name = self . name <EOL> with self . env . db_transaction as db : <EOL> new_page = WikiPage ( self . env , new_name ) <EOL> if new_page . exists : <EOL> raise TracError ( _ ( "<STR_LIT>" , <EOL> name = new_name ) ) <EOL> db ( "<STR_LIT>" , ( new_name , old_name ) ) <EOL> del WikiSystem ( self . env ) . pages <EOL> from trac . attachment import Attachment <EOL> Attachment . reparent_all ( self . env , self . realm , old_name , <EOL> self . realm , new_name ) <EOL> self . name = new_name <EOL> self . env . log . info ( "<STR_LIT>" , old_name , new_name ) <EOL> for listener in WikiSystem ( self . env ) . change_listeners : <EOL> if hasattr ( listener , '<STR_LIT>' ) : <EOL> listener . wiki_page_renamed ( self , old_name ) <EOL> def edit_comment ( self , new_comment ) : <EOL> """<STR_LIT>""" <EOL> if not self . exists : <EOL> raise TracError ( _ ( "<STR_LIT>" ) ) <EOL> old_comment = self . comment <EOL> with self . env . db_transaction as db : <EOL> db ( "<STR_LIT>" , <EOL> ( new_comment , self . name , self . version ) ) <EOL> self . comment = new_comment <EOL> self . env . log . info ( "<STR_LIT>" , <EOL> self . name , self . version , new_comment ) <EOL> for listener in WikiSystem ( self . env ) . change_listeners : <EOL> if hasattr ( listener , '<STR_LIT>' ) : <EOL> listener . wiki_page_comment_modified ( self , old_comment ) <EOL> def get_history ( self ) : <EOL> """<STR_LIT>""" <EOL> for version , ts , author , comment , ipnr in self . env . db_query ( """<STR_LIT>""" , ( self . name , self . version ) ) : <EOL> yield version , from_utimestamp ( ts ) , author , comment , ipnr </s>
