Changeset 4722

Show
Ignore:
Timestamp:
11/03/08 04:45:17 (2 months ago)
Author:
merlimat
Message:

Caches pages content and modifications on local disks.
closes: #1678

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • eclipsetracplugin/eclipse/trunk/mm.eclipse.trac/src/mm/eclipse/trac/Activator.java

    r2016 r4722  
    55import java.net.URL; 
    66import java.util.ResourceBundle; 
     7 
    78 
    89import org.eclipse.jface.resource.ImageDescriptor; 
     
    3132    private ContributionTemplateStore       templateStore; 
    3233    private ContributionContextTypeRegistry contextTypeRegistry; 
     34     
     35    private static WikiPageCache wikiPageCache = new WikiPageCache(); 
    3336     
    3437    /** 
     
    7174    { 
    7275        return plugin; 
     76    } 
     77     
     78    public static WikiPageCache wikiPageCache() 
     79    { 
     80        return wikiPageCache; 
    7381    } 
    7482     
  • eclipsetracplugin/eclipse/trunk/mm.eclipse.trac/src/mm/eclipse/trac/editors/WikiEditorStorage.java

    r2003 r4722  
    11package mm.eclipse.trac.editors; 
    22 
    3 import java.io.ByteArrayInputStream; 
    4 import java.io.ByteArrayOutputStream; 
    5 import java.io.IOException; 
    63import java.io.InputStream; 
    7 import java.io.OutputStreamWriter; 
    8 import java.io.Reader; 
    9 import java.io.StringReader; 
    10 import java.io.Writer; 
    114 
    125import mm.eclipse.trac.Log; 
     
    2821    } 
    2922     
    30     private class StreamAdapter extends InputStream 
    31     { 
    32         private ByteArrayInputStream istream; 
    33          
    34         public StreamAdapter( Reader reader ) 
    35         { 
    36             ByteArrayOutputStream ostream = new ByteArrayOutputStream(); 
    37             try 
    38             { 
    39                 Writer writer = new OutputStreamWriter( ostream, Encoding ); 
    40                 while ( true ) 
    41                 { 
    42                     int c = reader.read(); 
    43                     if ( c == -1 ) 
    44                         break; 
    45                     writer.write( c ); 
    46                 } 
    47                  
    48                 writer.close(); 
    49             } catch ( Exception e ) 
    50             { 
    51                 Log.error( "Conversion error.", e ); 
    52             } 
    53              
    54             Log.info( "Size: " + ostream.size() ); 
    55              
    56             istream = new ByteArrayInputStream( ostream.toByteArray() ); 
    57         } 
    58          
    59         @Override 
    60         public int read() throws IOException 
    61         { 
    62             return istream.read(); 
    63         } 
    64     } 
    65      
    6623    public String getCharset() throws CoreException 
    6724    { 
     
    7128    public InputStream getContents() throws CoreException 
    7229    { 
    73         StringReader reader = new StringReader( page.getContent() ); 
    74         return new StreamAdapter( reader ); 
     30        return page.getContent(); 
    7531    } 
    7632     
     
    9147    } 
    9248     
     49    @SuppressWarnings("unchecked") 
    9350    public Object getAdapter( Class adapter ) 
    9451    { 
  • eclipsetracplugin/eclipse/trunk/mm.eclipse.trac/src/mm/eclipse/trac/models/TracServer.java

    r2579 r4722  
    4646        problems = false; 
    4747         
    48         rootWikiPage = new WikiPage( this, "", true, true ); 
     48        rootWikiPage = new WikiPage( this, "", 0, true, true ); 
    4949        rootWikiPage.setRoot( true ); 
    5050    } 
  • eclipsetracplugin/eclipse/trunk/mm.eclipse.trac/src/mm/eclipse/trac/models/WikiPage.java

    • Property svn:mergeinfo set
    r2016 r4722  
    11package mm.eclipse.trac.models; 
    22 
     3import java.io.IOException; 
     4import java.io.InputStream; 
     5import java.io.InputStreamReader; 
     6import java.io.Reader; 
    37import java.util.ArrayList; 
    48import java.util.Collection; 
     
    1014import java.util.TreeSet; 
    1115 
     16import mm.eclipse.trac.Activator; 
     17import mm.eclipse.trac.Log; 
    1218import mm.eclipse.trac.xmlrpc.WikiExt; 
    1319 
     
    1622public class WikiPage extends ModelBase implements IAdaptable 
    1723{ 
    18     private TracServer            server; 
    19      
    20     private String                fullName; 
    21     private boolean               exists      = false; 
    22     private boolean               hasChildren = false; 
    23     private List<WikiPage>        children    = null; 
    24     private WikiPage              parent      = null; 
    25     private boolean               root        = false; 
    26     private boolean               dirty       = false; 
    27     private String                content     = null; 
    28      
    29     private List<WikiPageVersion> versions    = null; 
    30      
    31     public WikiPage( TracServer server, String fullName, boolean exists ) 
    32     { 
    33         this( server, fullName, exists, server.getWikiExt().hasChildren( fullName ) ); 
    34     } 
    35      
    36     public WikiPage( TracServer server, String fullName, boolean exists, 
    37                      boolean hasChildren ) 
     24    private final TracServer server; 
     25 
     26    private final String fullName; 
     27    private int version; 
     28    private boolean exists = false; 
     29    private boolean hasChildren = false; 
     30    private List<WikiPage> children = null; 
     31    private WikiPage parent = null; 
     32    private boolean root = false; 
     33    private boolean dirty = false; 
     34 
     35    private List<WikiPageVersion> versions = null; 
     36 
     37    public WikiPage( TracServer server, String fullName, int version, 
     38            boolean exists ) 
     39    { 
     40        this( server, fullName, version, exists, server.getWikiExt() 
     41                .hasChildren( fullName ) ); 
     42    } 
     43 
     44    public WikiPage( TracServer server, String fullName, int version, 
     45            boolean exists, boolean hasChildren ) 
    3846    { 
    3947        this.server = server; 
    4048        this.fullName = fullName; 
     49        this.version = version; 
    4150        this.exists = exists; 
    4251        this.hasChildren = hasChildren; 
    43     } 
    44      
     52        Activator.wikiPageCache().checkPage( this ); 
     53    } 
     54 
    4555    public WikiPage( TracServer server, String fullName ) 
    4656    { 
    47         this( server, fullName, true ); 
    48     } 
    49      
     57        this( server, fullName, 0, true ); 
     58        Activator.wikiPageCache().checkPage( this ); 
     59    } 
     60 
    5061    /** 
    5162     * @return the server 
     
    5566        return server; 
    5667    } 
    57      
     68 
    5869    /** 
    5970     * @return the fullName of the page 
     
    6475        return path[path.length - 1]; 
    6576    } 
    66      
     77 
    6778    /** 
    6879     * @return wheter the page exists in Trac or if is only a tree node. 
     
    7283        return exists; 
    7384    } 
    74      
     85 
    7586    /** 
    7687     * @return the fullName of the page 
     
    8091        return fullName; 
    8192    } 
    82      
     93 
     94    public int getVersion() 
     95    { 
     96        return version; 
     97    } 
     98 
    8399    public void addChild( WikiPage child ) 
    84100    { 
    85         if ( children == null ) children = new ArrayList<WikiPage>(); 
    86          
     101        if ( children == null ) 
     102            children = new ArrayList<WikiPage>(); 
     103 
    87104        children.add( child ); 
    88105        child.parent = this; 
    89106        notifyChanged(); 
    90107    } 
    91      
     108 
    92109    public boolean hasChildren() 
    93110    { 
    94111        return hasChildren; 
    95112    } 
    96      
     113 
    97114    public Collection<WikiPage> getChildren() 
    98115    { 
    99         if ( children != null ) return children; 
    100          
     116        if ( children != null ) 
     117            return children; 
     118 
    101119        children = new ArrayList<WikiPage>(); 
    102120        WikiExt wikiExt = server.getWikiExt(); 
    103          
    104         Map<String, Map<String, Boolean>> childrenMap = wikiExt.getChildren( fullName ); 
     121 
     122        Map<String, Map<String, Object>> childrenMap = wikiExt 
     123                .getChildren( fullName ); 
    105124        SortedSet<String> names = new TreeSet<String>( childrenMap.keySet() ); 
    106          
     125 
    107126        for ( String name : names ) 
    108127        { 
    109             Map<String, Boolean> attrs = childrenMap.get( name ); 
    110             boolean exists = attrs.get( "exists" ); 
    111             boolean hasChildren = attrs.get( "hasChildren" ); 
    112             children.add( new WikiPage( server, name, exists, hasChildren ) ); 
    113         } 
    114          
     128            Map<String, Object> attrs = childrenMap.get( name ); 
     129            int version = (Integer) attrs.get( "version" ); 
     130            boolean exists = (Boolean) attrs.get( "exists" ); 
     131            boolean hasChildren = (Boolean) attrs.get( "hasChildren" ); 
     132            children.add( new WikiPage( server, name, version, exists, 
     133                    hasChildren ) ); 
     134        } 
     135 
    115136        return children; 
    116137    } 
    117      
     138 
    118139    public WikiPage getParent() 
    119140    { 
    120141        return parent; 
    121142    } 
    122      
     143 
    123144    public String toString() 
    124145    { 
    125146        return getFullName(); 
    126147    } 
    127      
    128     public String getContent() 
    129     { 
    130         if ( content == null ) content = server.getWiki().getPage( fullName ); 
    131         return content; 
    132     } 
    133      
     148 
     149    public InputStream getContent() 
     150    { 
     151        return Activator.wikiPageCache().getPage( this ); 
     152    } 
     153 
     154    public String getStringContent() 
     155    { 
     156        InputStream is = getContent(); 
     157 
     158        StringBuilder sb = new StringBuilder(); 
     159        try 
     160        { 
     161            while (true) 
     162            { 
     163                char c = (char) is.read(); 
     164                if ( c == (char)-1 ) 
     165                    break; 
     166                else 
     167                    sb.append( c ); 
     168            } 
     169        } 
     170        catch ( IOException e ) 
     171        { 
     172            Log.error( "Error reading content", e ); 
     173        } 
     174 
     175        return sb.toString(); 
     176    } 
     177 
    134178    public void putContent( String content ) 
    135179    { 
    136         this.content = content; 
    137180        setDirty( true ); 
    138         // TODO: Should cache the content on local disk 
    139     } 
    140      
     181        Activator.wikiPageCache().putPage( this, content ); 
     182    } 
     183 
    141184    /** 
    142185     * Save the page in the Trac database 
     
    149192        Map<String, String> attributes = new HashMap<String, String>(); 
    150193        attributes.put( "comment", comment ); 
     194         
     195        String content = getStringContent(); 
    151196        server.getWiki().putPage( fullName, content, attributes ); 
     197 
     198        // Should refresh with newly created version 
     199        version += 1; 
     200        versions = null; 
     201        setDirty( false ); 
    152202         
    153         // Should refresh with newly created version 
    154         versions = null; 
    155          
    156         setDirty( false ); 
    157     } 
    158      
     203        Activator.wikiPageCache().putPage( this, content ); 
     204    } 
     205 
    159206    public List<WikiPageVersion> getVersions() 
    160207    { 
    161         if ( versions != null ) return versions; 
    162          
     208        if ( versions != null ) 
     209            return versions; 
     210 
    163211        versions = new ArrayList<WikiPageVersion>(); 
    164         Object[] remoteVersions = server.getWikiExt().getPageVersions( fullName ); 
    165          
     212        Object[] remoteVersions = server.getWikiExt() 
     213                .getPageVersions( fullName ); 
     214 
    166215        for ( Object o : remoteVersions ) 
    167216        { 
    168217            Map a = (Map) o; 
    169218            WikiPageVersion version; 
    170             version = new WikiPageVersion( this, (Integer) a.get( "version" ), (String) a 
    171                     .get( "author" ), (String) a.get( "comment" ), (Date) a 
    172                     .get( "lastModified" ) ); 
     219            version = new WikiPageVersion( this, (Integer) a.get( "version" ), 
     220                    (String) a.get( "author" ), (String) a.get( "comment" ), 
     221                    (Date) a.get( "lastModified" ) ); 
    173222            versions.add( version ); 
    174223        } 
    175          
     224 
    176225        return versions; 
    177226    } 
    178      
     227 
    179228    public Object getAdapter( Class adapter ) 
    180229    { 
     
    182231        return null; 
    183232    } 
    184      
     233 
    185234    public void setRoot( boolean root ) 
    186235    { 
    187236        this.root = root; 
    188237    } 
    189      
     238 
    190239    public boolean isRoot() 
    191240    { 
    192241        return root; 
    193242    } 
    194      
     243 
    195244    public boolean isDirty() 
    196245    { 
    197246        return dirty; 
    198247    } 
    199      
     248 
    200249    public void setDirty( boolean dirty ) 
    201250    { 
     
    203252        notifyChanged(); 
    204253    } 
    205      
     254 
    206255} 
  • eclipsetracplugin/eclipse/trunk/mm.eclipse.trac/src/mm/eclipse/trac/wiki/NewWikiPage.java

    r2161 r4722  
    5050         
    5151        TracServer server = TracServerList.getInstance().getServerByName( serverName ); 
    52         WikiPage wikiPage = new WikiPage( server, pageName, true, false ); 
     52        WikiPage wikiPage = new WikiPage( server, pageName, 0, true, false ); 
    5353        wikiPage.setDirty( true ); 
    5454         
  • eclipsetracplugin/eclipse/trunk/mm.eclipse.trac/src/mm/eclipse/trac/xmlrpc/Trac.java

    r2579 r4722  
    1616import org.apache.xmlrpc.client.XmlRpcClientConfigImpl; 
    1717import org.apache.xmlrpc.client.XmlRpcCommonsTransportFactory; 
    18 import org.apache.xmlrpc.client.XmlRpcSunHttpTransportFactory; 
    1918 
    2019public class Trac 
  • eclipsetracplugin/eclipse/trunk/mm.eclipse.trac/src/mm/eclipse/trac/xmlrpc/WikiExt.java

    r2003 r4722  
    1616    boolean hasChildren( String pagename ); 
    1717     
    18     Map<String, Map<String,Boolean>> getChildren( String pagename ); 
     18    Map<String, Map<String,Object>> getChildren( String pagename ); 
    1919     
    2020    /** 
  • eclipsetracplugin/tracrpcext/0.10/tracrpcext/wiki.py

    r4694 r4722  
    1111 
    1212    implements(IXMLRPCHandler) 
     13     
     14    VERSION = '1.0' 
    1315 
    1416    def __init__(self): 
     
    2325        yield ('WIKI_VIEW', ((list, str), ), self.getChildren) 
    2426        yield ('WIKI_VIEW', ((dict,), ), self.getMacros) 
     27        yield ('WIKI_VIEW', ((dict,), ), self.getVersion) 
    2528 
    2629    def _page_info(self, name, time, author, version, comment=''): 
     
    6467    def getChildren(self, req, pagename): 
    6568        """ Returns a list of all pages. The result is an array of utf8 pagenames. """ 
     69         
     70        cond  = '' 
     71        # params = ()  
     72         
    6673        if pagename: 
    6774            pagename += '/' 
    68         pages = list( self.wiki.get_pages( pagename ) ) 
    69         pages.sort() 
     75            cond = "WHERE name LIKE '%s%%'" % pagename 
     76            # params = (pagename,) 
     77             
     78        db = self.env.get_db_cnx() 
     79        cursor = db.cursor() 
     80        cursor.execute("SELECT name, MAX(version) FROM wiki %s GROUP BY name" % cond ) 
     81 
     82        pages = [] 
     83        for name, version in cursor: 
     84            pages.append( (name, version) ) 
     85             
    7086        children = {} 
    71         for page in pages: 
    72             # print 'Complete name: "%s"' % page 
     87        for page, version in pages: 
    7388            relname = page.replace( pagename, '' ) 
    74             # print 'RELNAME: "%s"' % relname 
    75               
     89             
    7690            if relname.find( '/' ) == -1: 
    7791                # We only look for direct children 
    7892                children[ page ] = { 'exists' : True, 
    79                                      'hasChildren' : False } 
     93                                     'hasChildren' : False,  
     94                                     'version' : version } 
    8095            else: 
    8196                # The page does not really exists, but it does have 
     
    8499                if not children.has_key( name ): 
    85100                    children[ name ] = { 'exists' : False,  
    86                                         'hasChildren' : True } 
     101                                         'hasChildren' : True,  
     102                                         'version' : version } 
    87103                else: 
    88104                    children[ name ][ 'hasChildren' ] = True 
    89105                     
    90         return children 
     106        return children     
    91107     
    92108    def getMacros(self, req): 
     
    100116                macros[ macro ] = desc  
    101117        return macros 
     118     
     119    def getVersion( self, req ): 
     120        '''Return the module version''' 
     121        return { 'version' : self.VERSION } 
    102122