Software Freedom Law Center

Changeset 108

Show
Ignore:
Timestamp:
05/26/08 11:05:14 (3 months ago)
Author:
bkuhn
Message:

r139@hughes: bkuhn | 2008-05-07 17:25:52 -0400

  • In api.py, macros,py, and model.py, I found all the instances where
    calls were made to the database for the wiki operation and replaced
    them with calls to a WikiStorage? object stored in self._storage.


  • I impelemented much of the WikiStorage? API and the WikiStorageDB class
    implementation, mostly by copying code from the other functions in
    question in api, macros, and model.


Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/trac/trac/wiki/api.py

    r103 r108  
    195195            if now > self._last_index_update + WikiSystem.INDEX_UPDATE_INTERVAL: 
    196196                self.log.debug('Updating wiki page index') 
    197                 db = self.env.get_db_cnx() 
    198                 cursor = db.cursor() 
    199                 cursor.execute("SELECT DISTINCT name FROM wiki") 
    200                 self._index = {} 
    201                 for (name,) in cursor: 
     197                for (name,) in self._storage.get_page_list: 
    202198                    self._index[name] = True 
    203199                self._last_index_update = now 
  • trunk/trac/trac/wiki/macros.py

    r103 r108  
    22# 
    33# Copyright (C) 2005-2008 Edgewall Software 
     4# Copyright (C) 2003-2005 Jonas Borgström <jonas@edgewall.com> 
    45# Copyright (C) 2005-2006 Christopher Lenz <cmlenz@gmx.de> 
     6# Copyright (C) 2008 Bradley M. Kuhn <bkuhn@ebb.org> 
    57# All rights reserved. 
    68# 
     
    159161                    limit = int(argv[1]) 
    160162 
    161         cursor = formatter.db.cursor() 
    162  
    163         sql = 'SELECT name, ' \ 
    164               '  max(version) AS max_version, ' \ 
    165               '  max(time) AS max_time ' \ 
    166               'FROM wiki' 
    167         args = [] 
    168         if prefix: 
    169             sql += ' WHERE name LIKE %s' 
    170             args.append(prefix + '%') 
    171         sql += ' GROUP BY name ORDER BY max_time DESC' 
    172         if limit: 
    173             sql += ' LIMIT %s' 
    174             args.append(limit) 
    175         cursor.execute(sql, args) 
    176  
    177163        entries_per_date = [] 
    178164        prevdate = None 
    179         for name, version, ts in cursor: 
     165        pages = self._storage.get_pages_by_prefix_mod_time_desc(prefix, limit) 
     166        for name, version, ts in pages: 
    180167            if not 'WIKI_VIEW' in formatter.perm('wiki', name, version): 
    181168                continue 
  • trunk/trac/trac/wiki/model.py

    r103 r108  
    5050 
    5151    def _fetch(self, name, version=None, db=None): 
    52         if not db: 
    53             db = self.env.get_db_cnx() 
    54         cursor = db.cursor() 
    55         if version: 
    56             cursor.execute("SELECT version,time,author,text,comment,readonly " 
    57                            "FROM wiki " 
    58                            "WHERE name=%s AND version=%s", 
    59                            (name, int(version))) 
    60         else: 
    61             cursor.execute("SELECT version,time,author,text,comment,readonly " 
    62                            "FROM wiki " 
    63                            "WHERE name=%s ORDER BY version DESC LIMIT 1", 
    64                            (name,)) 
    65         row = cursor.fetchone() 
    66         if row: 
    67             version,time,author,text,comment,readonly = row 
    68             self.version = int(version) 
    69             self.author = author 
    70             self.time = datetime.fromtimestamp(time, utc) 
    71             self.text = text 
    72             self.comment = comment 
    73             self.readonly = readonly and int(readonly) or 0 
    74         else: 
    75             self.version = 0 
    76             self.text = self.comment = self.author = '' 
    77             self.time = None 
    78             self.readonly = 0 
     52        (self.version, self.author, self.time, self.text, self.comment, \ 
     53             self.readonly) = self._storage.fetch_page(name, version) 
    7954 
    8055    exists = property(fget=lambda self: self.version > 0) 
     
    8257    def delete(self, version=None, db=None): 
    8358        assert self.exists, 'Cannot delete non-existent page' 
     59 
    8460        if not db: 
    8561            db = self.env.get_db_cnx() 
     
    8864            handle_ta = False 
    8965 
    90         cursor = db.cursor() 
    91         if version is None: 
    92             # Delete a wiki page completely 
    93             cursor.execute("DELETE FROM wiki WHERE name=%s", (self.name,)) 
    94             self.env.log.info('Deleted page %s' % self.name) 
    95         else: 
    96             # Delete only a specific page version 
    97             cursor.execute("DELETE FROM wiki WHERE name=%s and version=%s", 
    98                            (self.name, version)) 
    99             self.env.log.info('Deleted version %d of page %s' 
    100                               % (version, self.name)) 
     66        self._storage.delete_page(self.name, version, db) 
    10167 
    10268        if version is None or version == self.version: 
     
    12894            handle_ta = False 
    12995 
    130         if t is None: 
    131             t = datetime.now(utc) 
    132  
    133         if self.text != self.old_text: 
    134             cursor = db.cursor() 
    135             cursor.execute("INSERT INTO wiki (name,version,time,author,ipnr," 
    136                            "text,comment,readonly) VALUES (%s,%s,%s,%s,%s,%s," 
    137                            "%s,%s)", (self.name, self.version + 1, 
    138                                       to_timestamp(t), author, remote_addr, 
    139                                       self.text, comment, self.readonly)) 
    140             self.version += 1 
    141         elif self.readonly != self.old_readonly: 
    142             cursor = db.cursor() 
    143             cursor.execute("UPDATE wiki SET readonly=%s WHERE name=%s", 
    144                            (self.readonly, self.name)) 
    145         else: 
    146             raise TracError(_('Page not modified')) 
     96        self._storage.save_page(self, author, comment, remote_addr, t, db) 
    14797 
    14898        if handle_ta: 
     
    160110 
    161111    def get_history(self, db=None): 
    162         if not db: 
    163             db = self.env.get_db_cnx() 
    164         cursor = db.cursor() 
    165         cursor.execute("SELECT version,time,author,comment,ipnr FROM wiki " 
    166                        "WHERE name=%s AND version<=%s " 
    167                        "ORDER BY version DESC", (self.name, self.version)) 
    168         for version,ts,author,comment,ipnr in cursor: 
    169             time = datetime.fromtimestamp(ts, utc) 
    170             yield version,time,author,comment,ipnr 
     112        return self._storage.get_page_history(self.name, self.version, db) 
  • trunk/trac/trac/wiki/storage.py

    r107 r108  
    22# 
    33# Copyright (C) 2005 Edgewall Software 
     4# Copyright (C) 2003-2005 Jonas Borgström <jonas@edgewall.com> 
    45# Copyright (C) 2005 Christopher Lenz <cmlenz@gmx.de> 
    5 # Copyright (C) 2007 Software Freedom Law Center  
    66# Copyright (C) 2007, 2008 Bradley M. Kuhn <bkuhn@ebb.org> 
    77# 
     
    6262       None, the most recent version of page named 'name' is returned.""" 
    6363 
    64    def delete_page(name, version=None): 
     64   # Note that db is an option for delet_page, save_page and 
     65   # get_page_history, even in the super class.  This is to give parity 
     66   # with what is expected in trac.wiki.model.WikiPage functions of 
     67   # similar names, which takes a db as an option.  When writing this, I 
     68   # was a bit confused as to what the meaning of the db being None was, 
     69   # and was unclear about whether the db would always be the same db 
     70   # object as the one returned by self.env.get_db_cnx().  Someone who 
     71   # understands the reasoning on this better in model.py could likely 
     72   # make this better. 
     73 
     74   def delete_page(name, version=None, db=None): 
    6575       """Deletes a page from the wiki""" 
    6676 
     77   def save_page(wikipage, author, comment, remote_addr, t=None, db=None): 
     78      """Saves wikipage with a comment of comment, recording remote_addr 
     79      as the IP address of the contributor, and t as the time stamp.""" 
     80 
     81   def get_page_history(name, version, db=None): 
     82      """Returns a generator that gives the values (version, time, author, 
     83      comment, and ipnumber) for each item in the page's history that is 
     84      specified by name and version numbers less than or equal to 
     85      version""" 
    6786 
    6887class WikiStorageDB(): 
     
    125144       (version,time,author,text,comment,readonly) 
    126145 
     146   def delete_page(name, version=None, db=None): 
     147      """Deletes a page from the wiki""" 
     148      if not db: 
     149         db = self.env.get_db_cnx() 
     150 
     151      cursor = db.cursor() 
     152      if version is None: 
     153         # Delete a wiki page completely 
     154         cursor.execute("DELETE FROM wiki WHERE name=%s", (name,)) 
     155         self.env.log.info('Deleted page %s' % name) 
     156      else: 
     157         # Delete only a specific page version 
     158         cursor.execute("DELETE FROM wiki WHERE name=%s and version=%s", 
     159                        (name, version)) 
     160         self.env.log.info('Deleted version %d of page %s' 
     161                           % (version, name)) 
     162 
     163   def save_page(wikipage, author, comment, remote_addr, t=None, db=None): 
     164      """Saves wikipage with a comment of comment, recording remote_addr 
     165      as the IP address of the contributor, and t as the time stamp.""" 
     166      if not db: 
     167         db = self.env.get_db_cnx() 
     168 
     169      if t is None: 
     170         t = datetime.now(utc) 
     171 
     172      if self.text != self.old_text: 
     173         cursor = db.cursor() 
     174         cursor.execute("INSERT INTO wiki (name,version,time,author,ipnr," 
     175                        "text,comment,readonly) VALUES (%s,%s,%s,%s,%s,%s," 
     176                        "%s,%s)", (wikipage.name, wikpage.version + 1, 
     177                                   to_timestamp(t), author, remote_addr, 
     178                                   wikipage.text, comment, wikipage.readonly)) 
     179         wikipage.version += 1 
     180      elif wikipage.readonly != wikipage.old_readonly: 
     181         cursor = db.cursor() 
     182         cursor.execute("UPDATE wiki SET readonly=%s WHERE name=%s", 
     183                        (wikipage.readonly, wikipage.name)) 
     184      else: 
     185         raise TracError(_('Page not modified')) 
     186 
     187   def get_page_history(name, version, db=None): 
     188      """Returns a generator that gives the values (version, time, author, 
     189      comment, and ipnumber) for each item in the page's history that is 
     190      specified by name and version numbers less than or equal to 
     191      version""" 
     192      if not db: 
     193         db = self.env.get_db_cnx() 
     194      cursor.execute("SELECT version,time,author,comment,ipnr FROM wiki " 
     195                     "WHERE name=%s AND version<=%s " 
     196                     "ORDER BY version DESC", (name, version)) 
     197      for version,ts,author,comment,ipnr in cursor: 
     198            time = datetime.fromtimestamp(ts, utc) 
     199            yield version,time,author,comment,ipnr 
     200 
    127201class WikiStorageVC(): 
    128202    implements(WikiStorage) 

SFLC Main Page

[frdm] Support SFLC