Commit 43156e56 authored by Liang Ding's avatar Liang Ding

Fix #12284

parent 0a93151b
...@@ -31,6 +31,7 @@ import org.b3log.latke.logging.Logger; ...@@ -31,6 +31,7 @@ import org.b3log.latke.logging.Logger;
import org.b3log.latke.model.Plugin; import org.b3log.latke.model.Plugin;
import org.b3log.latke.model.User; import org.b3log.latke.model.User;
import org.b3log.latke.plugin.ViewLoadEventData; import org.b3log.latke.plugin.ViewLoadEventData;
import org.b3log.latke.repository.jdbc.util.Connections;
import org.b3log.latke.service.LangPropsService; import org.b3log.latke.service.LangPropsService;
import org.b3log.latke.service.ServiceException; import org.b3log.latke.service.ServiceException;
import org.b3log.latke.servlet.HTTPRequestContext; import org.b3log.latke.servlet.HTTPRequestContext;
...@@ -60,13 +61,16 @@ import java.io.File; ...@@ -60,13 +61,16 @@ import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.*; import java.util.*;
/** /**
* Admin console render processing. * Admin console render processing.
* *
* @author <a href="http://88250.b3log.org">Liang Ding</a> * @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.3.2.11, Mar 31, 2017 * @version 1.4.2.11, May 8, 2017
* @since 0.4.1 * @since 0.4.1
*/ */
@RequestProcessor @RequestProcessor
...@@ -75,7 +79,7 @@ public class AdminConsole { ...@@ -75,7 +79,7 @@ public class AdminConsole {
/** /**
* Logger. * Logger.
*/ */
private static final Logger LOGGER = Logger.getLogger(AdminConsole.class.getName()); private static final Logger LOGGER = Logger.getLogger(AdminConsole.class);
/** /**
* Language service. * Language service.
...@@ -231,7 +235,8 @@ public class AdminConsole { ...@@ -231,7 +235,8 @@ public class AdminConsole {
final Map<String, String> langs = langPropsService.getAll(locale); final Map<String, String> langs = langPropsService.getAll(locale);
final Map<String, Object> dataModel = renderer.getDataModel(); final Map<String, Object> dataModel = renderer.getDataModel();
dataModel.put("isMySQL", RuntimeDatabase.MYSQL == Latkes.getRuntimeDatabase()); dataModel.put("supportExport", RuntimeDatabase.MYSQL == Latkes.getRuntimeDatabase()
|| RuntimeDatabase.H2 == Latkes.getRuntimeDatabase());
dataModel.putAll(langs); dataModel.putAll(langs);
Keys.fillRuntime(dataModel); Keys.fillRuntime(dataModel);
dataModel.put(Option.ID_C_LOCALE_STRING, locale.toString()); dataModel.put(Option.ID_C_LOCALE_STRING, locale.toString());
...@@ -308,19 +313,23 @@ public class AdminConsole { ...@@ -308,19 +313,23 @@ public class AdminConsole {
return; return;
} }
if (!Latkes.runsWithJDBCDatabase() || RuntimeDatabase.MYSQL != Latkes.getRuntimeDatabase()) { if (!Latkes.runsWithJDBCDatabase()) {
context.renderJSON().renderMsg("Just support MySQL export now"); context.renderJSON().renderMsg("Just support MySQL/H2 export now");
return; return;
} }
final RuntimeDatabase runtimeDatabase = Latkes.getRuntimeDatabase();
final String dbUser = Latkes.getLocalProperty("jdbc.username"); final String dbUser = Latkes.getLocalProperty("jdbc.username");
final String dbPwd = Latkes.getLocalProperty("jdbc.password"); final String dbPwd = Latkes.getLocalProperty("jdbc.password");
final String dbURL = Latkes.getLocalProperty("jdbc.URL"); final String dbURL = Latkes.getLocalProperty("jdbc.URL");
String sql; // exported SQL script
if (RuntimeDatabase.MYSQL == runtimeDatabase) {
String db = StringUtils.substringAfterLast(dbURL, "/"); String db = StringUtils.substringAfterLast(dbURL, "/");
db = StringUtils.substringBefore(db, "?"); db = StringUtils.substringBefore(db, "?");
String sql;
try { try {
if (StringUtils.isNotBlank(dbPwd)) { if (StringUtils.isNotBlank(dbPwd)) {
sql = Execs.exec("mysqldump -u" + dbUser + " -p" + dbPwd + " --databases " + db); sql = Execs.exec("mysqldump -u" + dbUser + " -p" + dbPwd + " --databases " + db);
...@@ -333,16 +342,52 @@ public class AdminConsole { ...@@ -333,16 +342,52 @@ public class AdminConsole {
return; return;
} }
} else if (RuntimeDatabase.H2 == runtimeDatabase) {
final Connection connection = Connections.getConnection();
final Statement statement = connection.createStatement();
try {
final StringBuilder sqlBuilder = new StringBuilder();
final ResultSet resultSet = statement.executeQuery("SCRIPT");
while (resultSet.next()) {
final String stmt = resultSet.getString(1);
sqlBuilder.append(stmt).append(Strings.LINE_SEPARATOR);
}
resultSet.close();
statement.close();
sql = sqlBuilder.toString();
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Export failed", e);
context.renderJSON().renderMsg("Export failed, please check log");
return;
} finally {
if (null != connection) {
connection.close();
}
}
} else {
context.renderJSON().renderMsg("Just support MySQL/H2 export now");
return;
}
if (StringUtils.isBlank(sql)) {
context.renderJSON().renderMsg("Export failed, please check log");
return;
}
final String tmpDir = System.getProperty("java.io.tmpdir"); final String tmpDir = System.getProperty("java.io.tmpdir");
String localFilePath = tmpDir + "/b3_solo_" + UUID.randomUUID().toString() + ".sql"; String localFilePath = tmpDir + File.separator + "b3_solo_" + UUID.randomUUID().toString() + ".sql";
LOGGER.info(localFilePath); LOGGER.trace(localFilePath);
final File localFile = new File(localFilePath); final File localFile = new File(localFilePath);
try { try {
final byte[] data = sql.getBytes("UTF-8"); final byte[] data = sql.getBytes("UTF-8");
OutputStream output = new FileOutputStream(localFile); final OutputStream output = new FileOutputStream(localFile);
IOUtils.write(data, output); IOUtils.write(data, output);
IOUtils.closeQuietly(output); IOUtils.closeQuietly(output);
...@@ -364,6 +409,7 @@ public class AdminConsole { ...@@ -364,6 +409,7 @@ public class AdminConsole {
context.renderJSON().renderMsg("Export failed, please check log"); context.renderJSON().renderMsg("Export failed, please check log");
return; return;
} }
} }
......
...@@ -47,7 +47,7 @@ ...@@ -47,7 +47,7 @@
</div> </div>
<div id="tabOthersPanel_other" class="none"> <div id="tabOthersPanel_other" class="none">
<button class="margin12" onclick="admin.others.removeUnusedTags();">${removeUnusedTagsLabel}</button> <button class="margin12" onclick="admin.others.removeUnusedTags();">${removeUnusedTagsLabel}</button>
<#if isMySQL> <#if supportExport>
<button class="margin12" onclick="admin.others.exportSQL();">${exportSQLLabel}</button> <button class="margin12" onclick="admin.others.exportSQL();">${exportSQLLabel}</button>
</#if> </#if>
</div> </div>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment