📄 rcsfileprovider.java
字号:
catch( Exception e ) { log.error("Unable to close streams!"); } } return result; } /** * Puts the page into RCS and makes sure there is a fresh copy in * the directory as well. * * @param page {@inheritDoc} * @param text {@inheritDoc} * @throws {@inheritDoc} */ public void putPageText( WikiPage page, String text ) throws ProviderException { Process process = null; String pagename = page.getName(); // Writes it in the dir. super.putPageText( page, text ); log.debug( "Checking in text..." ); try { String cmd = m_checkinCommand; String author = page.getAuthor(); if( author == null ) author = "unknown"; // Should be localized but cannot due to missing WikiContext String changenote = (String)page.getAttribute(WikiPage.CHANGENOTE); if( changenote == null ) changenote = ""; cmd = TextUtil.replaceString( cmd, "%s", mangleName(pagename)+FILE_EXT ); cmd = TextUtil.replaceString( cmd, "%u", TextUtil.urlEncodeUTF8(author) ); cmd = TextUtil.replaceString( cmd, "%c", TextUtil.urlEncodeUTF8(changenote) ); log.debug("Command = '"+cmd+"'"); process = Runtime.getRuntime().exec( cmd, null, new File(getPageDirectory()) ); process.waitFor(); // // Collect possible error output // BufferedReader error = null; String elines = ""; error = new BufferedReader(new InputStreamReader(process.getErrorStream())); String line = null; while ((line = error.readLine()) != null) { elines = elines + line +"\n"; } log.debug("Done, returned = "+process.exitValue()); log.debug(elines); if (process.exitValue() != 0) { throw new ProviderException(cmd+"\n"+"Done, returned = "+process.exitValue()+"\n"+elines); } } catch( Exception e ) { log.error("RCS checkin failed",e); ProviderException pe = new ProviderException("RCS checkin failed"); pe.initCause(e); throw pe; } finally { // we must close all by exec(..) opened streams: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4784692 if (process != null) { try { if( process.getOutputStream() != null ) process.getOutputStream().close(); } catch( Exception e ) {} try { if( process.getInputStream() != null ) process.getInputStream().close(); } catch( Exception e ) {} try { if( process.getErrorStream() != null ) process.getErrorStream().close(); } catch( Exception e ) {} } } } /** * {@inheritDoc} */ // FIXME: Put the rcs date formats into properties as well. public List getVersionHistory( String page ) { PatternMatcher matcher = new Perl5Matcher(); PatternCompiler compiler = new Perl5Compiler(); BufferedReader stdout = null; log.debug("Getting RCS version history"); ArrayList<WikiPage> list = new ArrayList<WikiPage>(); try { Pattern revpattern = compiler.compile( PATTERN_REVISION ); Pattern datepattern = compiler.compile( PATTERN_DATE ); // This complicated pattern is required, since on Linux RCS adds // quotation marks, but on Windows, it does not. Pattern userpattern = compiler.compile( PATTERN_AUTHOR ); Pattern notepattern = compiler.compile( PATTERN_CHANGENOTE ); String cmd = TextUtil.replaceString( m_fullLogCommand, "%s", mangleName(page)+FILE_EXT ); Process process = Runtime.getRuntime().exec( cmd, null, new File(getPageDirectory()) ); // FIXME: Should this use encoding as well? stdout = new BufferedReader( new InputStreamReader(process.getInputStream()) ); String line; WikiPage info = null; while( (line = stdout.readLine()) != null ) { if( matcher.contains( line, revpattern ) ) { info = new WikiPage( m_engine, page ); MatchResult result = matcher.getMatch(); int vernum = Integer.parseInt( result.group(1) ); info.setVersion( vernum ); list.add( info ); } if( matcher.contains( line, datepattern ) && info != null ) { MatchResult result = matcher.getMatch(); Date d = parseDate( result.group(1) ); info.setLastModified( d ); } if( matcher.contains( line, userpattern ) && info != null ) { MatchResult result = matcher.getMatch(); info.setAuthor( TextUtil.urlDecodeUTF8(result.group(1)) ); } if( matcher.contains( line, notepattern ) && info != null ) { MatchResult result = matcher.getMatch(); info.setAttribute( WikiPage.CHANGENOTE, TextUtil.urlDecodeUTF8(result.group(1)) ); } } process.waitFor(); // we must close all by exec(..) opened streams: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4784692 process.getInputStream().close(); process.getOutputStream().close(); process.getErrorStream().close(); // // FIXME: This is very slow // for( Iterator i = list.iterator(); i.hasNext(); ) { WikiPage p = (WikiPage) i.next(); String content = getPageText( p.getName(), p.getVersion() ); p.setSize( content.length() ); } } catch( Exception e ) { log.error( "RCS log failed", e ); } finally { try { if( stdout != null ) stdout.close(); } catch( IOException e ) {} } return list; } /** * Removes the page file and the RCS archive from the repository. * This method assumes that the page archive ends with ",v". * * @param page {@inheritDoc} * @throws {@inheritDoc} */ public void deletePage( String page ) throws ProviderException { log.debug( "Deleting page "+page ); super.deletePage( page ); File rcsdir = new File( getPageDirectory(), "RCS" ); if( rcsdir.exists() && rcsdir.isDirectory() ) { File rcsfile = new File( rcsdir, mangleName(page)+FILE_EXT+",v" ); if( rcsfile.exists() ) { if( rcsfile.delete() == false ) { log.warn( "Deletion of RCS file "+rcsfile.getAbsolutePath()+" failed!" ); } } else { log.info( "RCS file does not exist for page: "+page ); } } else { log.info( "No RCS directory at "+rcsdir.getAbsolutePath() ); } } /** * {@inheritDoc} */ public void deleteVersion( String page, int version ) { String line = "<rcs not run>"; BufferedReader stderr = null; boolean success = false; String cmd = m_deleteVersionCommand; log.debug("Deleting version "+version+" of page "+page); cmd = TextUtil.replaceString( cmd, "%s", mangleName(page)+FILE_EXT ); cmd = TextUtil.replaceString( cmd, "%v", Integer.toString( version ) ); log.debug("Running command "+cmd); Process process = null; try { process = Runtime.getRuntime().exec( cmd, null, new File(getPageDirectory()) ); // // 'rcs' command outputs to stderr methinks. // // FIXME: Should this use encoding as well? stderr = new BufferedReader( new InputStreamReader(process.getErrorStream() ) ); while( (line = stderr.readLine()) != null ) { log.debug( "LINE="+line ); if( line.equals("done") ) { success = true; } } } catch( IOException e ) { log.error("Page deletion failed: ",e); } finally { try { if( stderr != null ) stderr.close(); if( process != null ) { process.getInputStream().close(); process.getOutputStream().close(); } } catch( IOException e ) { log.error("Cannot close streams for process while deleting page version."); } } if( !success ) { log.error("Version deletion failed. Last info from RCS is: "+line); } } /** * util method to parse a date string in Local and UTC formats. This method is synchronized * because SimpleDateFormat is not thread-safe. * * @see <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4228335">Sun bug 4228335</a> */ private synchronized Date parseDate( String str ) { Date d = null; try { d = m_rcsdatefmt.parse( str ); return d; } catch ( ParseException pe ) { } try { d = m_rcsdatefmtUTC.parse( str ); return d; } catch ( ParseException pe ) { } return d; } /** * {@inheritDoc} */ public void movePage( String from, String to ) throws ProviderException { // XXX: Error checking could be better throughout this method. File fromFile = findPage( from ); File toFile = findPage( to ); fromFile.renameTo( toFile ); String fromRCSName = "RCS/"+mangleName( from )+FILE_EXT+",v"; String toRCSName = "RCS/"+mangleName( to )+FILE_EXT+",v"; File fromRCSFile = new File( getPageDirectory(), fromRCSName ); File toRCSFile = new File( getPageDirectory(), toRCSName ); fromRCSFile.renameTo( toRCSFile ); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -