ubbexport.java

来自「Jive是基于JSP/JAVA技术构架的一个大型BBS论坛系统,这是Jive论坛」· Java 代码 · 共 1,595 行 · 第 1/5 页

JAVA
1,595
字号
                        } catch (Exception e) {                            e.printStackTrace();                        } // end try switch 1                        done = true;                        break;                    case 2:                        done = true;                        break;                    default:                        println("ERROR - command not recognized. Please try again.");                } // end switch            } catch (Exception e) {                e.printStackTrace();            } // end try catch blok        } // end while loop    }    /**     * Do an export of all forum data.     *     * Ive added comments for my own benefit - martin farrell     */    public static void fullExport() {        // Check the directories and retrieve them        File ubbDir = getUbbDirectory();        File ubbCGIDir = getUbbCGIDirectory();        //Get member info - process membertotal.cgi        int memberCount = 0;        // Create member directory eg ubb/cgi-bin/Members        File memberDir = new File(ubbCGIDir, "Members");        // Get membertotals from memberDir - this is the same for both versions        File memberTotalFile = new File(memberDir, "membertotal.cgi");        // Convert file to an array        String [] memberTotal = fileToArray(memberTotalFile);        // Process files         if (memberTotal.length > 0) {            try {                // Parse value from memberTotals                memberCount = Integer.parseInt(memberTotal[0]);            }            catch (Exception e) {                e.printStackTrace();            }        }        // Get forum info        int forumCount = 0;        File forumsFile = null;        // If the version is 5 then use the old version        // else if 6 use vars_forums.cgi         if (UBB_VERSION == 5) {            println("UBB_VERSION 5 - using forums.cgi");            forumsFile = new File(ubbCGIDir, "forums.cgi");        }        else if (UBB_VERSION == 6) {            println("UBB_VERSION 6 - using vars_forums.cgi");            forumsFile = new File(ubbCGIDir, "vars_forums.cgi");        }        else {            println("There seems to be an error - please check your file structures");        }        // Parse data to an array        String [] forumData = fileToArray(forumsFile);        forumCount = forumData.length;        // Output number of users found to export and number of forums to export.        String foundString = "Found " + forumCount + " forum";        if (forumCount > 1) {            foundString += "s";        }        foundString += " and " + memberCount + " member";        if (memberCount > 1) {            foundString += "s";        }        foundString += " to export.";        println(foundString);        // collect the output filename        println(" ");        println("Enter the name of the file you'd like to write the data out to:");        print("[" + fullFile + "] > ");        String xmlName = readLine();        if (xmlName.equals("")) {            xmlName = fullFile;        }        fullFile = xmlName;        boolean done = false;        while (!done) {            println(" ");            println("Ready to begin export. Enter the number of the command you'd like to perform:");            println(" (1) Proceed with export to file \"" + xmlName + "\".");            println(" (2) Abandon export.");            print("> ");            String response = readLine();            println(" ");            try {                int choice = Integer.parseInt(response);                switch (choice) {                    case 1:                        println("Starting data export...");                        //Output data to file.                        //Write data as UTF-8 so that extended chars turn out correctly.                        try {                            Writer out = new UnicodeFilterWriter(                                    new BufferedWriter(                                            new OutputStreamWriter(                                                    new FileOutputStream(xmlName),                                                    "UTF-8"),2097152));                            out.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");                            out.write("<!DOCTYPE Jive SYSTEM \"http://www.jivesoftware.com/jive.dtd\">");                            out.write("<Jive xmlversion=\"" + XML_VERSION + "\" exportDate=\"" +                                    dateFormatter.format(new Date()) + "\">"                            );                            // Export user data                            print("Exporting user data...");                            int userCount = writeUserXML(memberDir, out);                            out.flush();                            println("\nFinished exporting " + userCount + " users.");                            // Export all forums                            out.write("<ForumList>");                            // Loop thru forums                            for (int i=0; i<forumCount; i++) {                                FullStringTokenizer tokens = new FullStringTokenizer(forumData[i],"|");                                // Split depending on ubb version - tidy up later - martin                                if (UBB_VERSION == 5) {                                    println("processing forums for UBB version 5");                                    tokens.nextToken();                                    String forumName = tokens.nextToken();                                    String forumDescription = tokens.nextToken();                                    for (int j=0; j < 5; j++) {                                        tokens.nextToken();                                    }                                    int forumID = Integer.parseInt(tokens.nextToken());                                    File forumDir = new File(ubbDir, "Forum" + forumID);                                    if( forumDir.exists() ) {                                        println("Exporting data from " + forumDir.getAbsolutePath() );                                        int messageCount = writeForumXML(forumDir, forumID, forumName, forumDescription, out);                                        println("\nFinished exporting " + messageCount + " messages.\n");                                    }                                } else if (UBB_VERSION == 6) {                                    println("processing forums for UBB version 6");                                    // Create default string holder                                    String nt = null;                                    // Create holder strings                                    String forumName = null; // Holder for forumName                                    String forumDescription = null; // Holder for forumDescription                                    int forumID = -1; // Holder for forumID                                    // vars_forum.cgi - each line has 31 vars -> set as 31 max                                    // reset as 18                                    // could change to nextToken                                    for (int j = 0; j < 18; j++) {                                        // Process Required tokens                                        if ( j == 3 ) {                                            forumName = nt;                                            println("forumName " + forumName);                                        } else if (j == 5) {                                            forumDescription = nt;                                            println("forumDescription " + forumDescription);                                        } else if (j==17) {                                            forumID = Integer.parseInt(nt);                                            println("forumId " + forumID);                                        }                                        File forumDir = new File(ubbDir, "Forum" + forumID);                                        // If forumDir exists - then write out contents                                        if (forumDir.exists()) {                                            println("Exporting data from " + forumDir.getAbsolutePath() );                                            int messageCount = writeForumXMLUBBV6(forumDir, forumID, forumName, forumDescription, out);                                            println("\nFinished exporting " + messageCount + " messages.\n");                                        }                                        // Get next token                                        nt = tokens.nextToken();                                    } // End forum line processing                                } // End UBB version if                            }                            out.write("</ForumList>");                            out.write("</Jive>");                            out.close();                        }                        catch(Exception e) {                            e.printStackTrace();                        }                        done = true;                        break;                    case 2:                        done = true;                        break;                    default:                        println("ERROR - command not recognized. Please try again.");                }            }            catch (NumberFormatException nfe) {                println("ERROR - command not recognized. Please try again.");            }        }    }    /**     * Gets the creation date for the specified forum. This date is determined     * by the date of the first message posted in the forum.     */    private static Date getForumCreationDate( File forumDir, int forumNumber )            throws IOException    {        println( "Getting creation date for forum " + forumNumber + "..." );        Date creationDate = new Date();        // Get the file which lists the threads in this forum        File threadList = new File(forumDir, "forum" + forumNumber + ".threads");        BufferedReader listIn = new BufferedReader(new FileReader(threadList));        String threadLine = null;        boolean validDate = false;        // Loop through the list of threads until        //  1) there are no more threads to examine or        //  2) we've found a valid creation date        while ( ((threadLine = listIn.readLine()) != null) && !validDate ) {            FullStringTokenizer tokens = new FullStringTokenizer(threadLine, "|^|");            tokens.nextToken();            String firstThreadID = tokens.nextToken();            File firstThreadFile = new File( forumDir, firstThreadID+".cgi" );            BufferedReader messages = new BufferedReader(                    new FileReader(firstThreadFile)            );            messages.readLine(); // skip first line            String rootMessage = messages.readLine();            if( rootMessage != null ) {                FullStringTokenizer mTokens = new FullStringTokenizer(rootMessage,"||");                // skip the first 3 tokens                mTokens.nextToken(); mTokens.nextToken(); mTokens.nextToken();                String date = mTokens.nextToken();                date += " " + mTokens.nextToken();                try {                    creationDate = ubbMessageDateFormatter.parse(date);                } catch( ParseException e ) {                    System.err.println("(getForumCreationDate): unparseable date: " + date);                }                // found a good date, get out of this loop                validDate = true;            }            messages.close();        }        listIn.close();        return creationDate;    }    /**     *     *     */    private static Date getForumModifiedDateUBBV6( File forumDir, int forumNumber )            throws IOException    {        println("Getting modified date for forum " + forumNumber + "...");        // Set defaults        Date modifiedDate = new Date();        // File containing last accessed date - lasttime.file        File lastTimeFile = new File(forumDir, "lasttime.file");        // process if it exists	if (lastTimeFile.exists()) {        BufferedReader listIn = new BufferedReader(new FileReader(lastTimeFile));        String line = null;        String createDate = null;        while ((line = listIn.readLine()) != null) {            if (createDate == null) {                // first line                createDate = line;            } else {                // Second line                createDate = createDate + " " + line;                // format date                try {                    modifiedDate = ubbMessageDateFormatter.parse(createDate);                } catch ( ParseException e) {                    System.err.println("(getForumModifiedDate): unparseable date: " + createDate);                }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?