From: axelcl Date: Wed, 22 Dec 2004 22:58:02 +0000 (+0000) Subject: Refresh Wiki Editor Texct from MySQL via JDBC interface X-Git-Url: http://secure.phpeclipse.com Refresh Wiki Editor Texct from MySQL via JDBC interface --- diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/DownloadWikiLinkEditorAction.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/DownloadWikiLinkEditorAction.java index e68ba72..38080a0 100644 --- a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/DownloadWikiLinkEditorAction.java +++ b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/DownloadWikiLinkEditorAction.java @@ -158,7 +158,7 @@ public final class DownloadWikiLinkEditorAction implements IEditorActionDelegate IEditorPart part = IDE.openEditor(WikiEditorPlugin.getDefault().getActivePage(), file, true); if (part != null && (part instanceof AbstractTextEditor)) { AbstractTextEditor newEditor = (AbstractTextEditor) part; - word = word.replaceAll(" ", "_"); + word = Util.titleToDB(word); String wikiText = MediaWikiConnector.getWikiText(word, "http://en.wikibooks.org/w/wiki.phtml"); if (wikiText!=null) { IDocument doc = newEditor.getDocumentProvider().getDocument(newEditor.getEditorInput()); diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/LoadWikipediaSQLAction.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/LoadWikipediaSQLAction.java new file mode 100644 index 0000000..06d7894 --- /dev/null +++ b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/LoadWikipediaSQLAction.java @@ -0,0 +1,204 @@ +package net.sourceforge.phpeclipse.wiki.actions.mediawiki; + +import java.io.ByteArrayInputStream; +import java.io.StringWriter; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import net.sourceforge.phpeclipse.wiki.editor.WikiEditor; +import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin; +import net.sourceforge.phpeclipse.wiki.internal.Configuration; +import net.sourceforge.phpeclipse.wiki.internal.ConfigurationManager; +import net.sourceforge.phpeclipse.wiki.internal.IConfiguration; +import net.sourceforge.phpeclipse.wiki.preferences.Util; +import net.sourceforge.phpeclipse.wiki.sql.WikipediaDB; +import net.sourceforge.phpeclipse.wiki.velocity.EditorText; + +import org.apache.velocity.VelocityContext; +import org.apache.velocity.app.Velocity; +import org.eclipse.core.resources.IContainer; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IFolder; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.resources.IResourceStatus; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.SubProgressMonitor; +import org.eclipse.jface.action.IAction; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.ITextSelection; +import org.eclipse.jface.text.TextSelection; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.LabelProvider; +import org.eclipse.jface.window.Window; +import org.eclipse.ui.IEditorActionDelegate; +import org.eclipse.ui.IEditorPart; +import org.eclipse.ui.IFileEditorInput; +import org.eclipse.ui.IWorkbenchWindow; +import org.eclipse.ui.dialogs.ListSelectionDialog; +import org.eclipse.ui.internal.dialogs.ListContentProvider; +import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; +import org.eclipse.ui.texteditor.AbstractTextEditor; + +public class LoadWikipediaSQLAction implements IEditorActionDelegate { + + private AbstractTextEditor fEditor; + + private EditorText text; + + private IWorkbenchWindow window; + + private void createFolder(IFolder folderHandle, IProgressMonitor monitor) throws CoreException { + try { + // Create the folder resource in the workspace + // Recursive to create any folders which do not exist already + if (!folderHandle.exists()) { + IContainer parent = folderHandle.getParent(); + if (parent instanceof IFolder && (!((IFolder) parent).exists())) { + createFolder((IFolder) parent, monitor); + } + folderHandle.create(false, true, monitor); + } + } catch (CoreException e) { + // If the folder already existed locally, just refresh to get contents + if (e.getStatus().getCode() == IResourceStatus.PATH_OCCUPIED) + folderHandle.refreshLocal(IResource.DEPTH_INFINITE, new SubProgressMonitor(monitor, 500)); + else + throw e; + } + } + + /** + * Creates a folder resource handle for the folder with the given workspace path. This method does not create the folder resource; + * this is the responsibility of createFolder. + * + * @param folderPath + * the path of the folder resource to create a handle for + * @return the new folder resource handle + * @see #createFolder + */ + private IFolder createFolderHandle(IPath folderPath) { + return IDEWorkbenchPlugin.getPluginWorkspace().getRoot().getFolder(folderPath); + } + + private void createNewFileIfNeeded(IFile file, String word) throws CoreException { + if (!file.exists()) { + createWikiFile(file, word); + } + } + + private void createWikiFile(IFile file, String word) throws CoreException { + IContainer parent = file.getParent(); + if (parent instanceof IFolder && (!((IFolder) parent).exists())) { + createFolder((IFolder) parent, null); + } + String newText = ""; + byte[] buffer = newText.getBytes(); + ByteArrayInputStream source = new ByteArrayInputStream(buffer); + file.create(source, true, null); + } + + public void dispose() { + } + + protected Configuration getConfiguration() { + List allConfigsList = ConfigurationManager.getInstance().getConfigurations(); + ArrayList configsList = new ArrayList(); + for (int i = 0; i < allConfigsList.size(); i++) { + IConfiguration temp = (IConfiguration) allConfigsList.get(i); + if (temp.getType().equals(WikiEditorPlugin.WIKIPEDIA_GET_TEXT)) { + configsList.add(temp); + } + } + Collections.sort(configsList); + Configuration configuration = null; + ListSelectionDialog listSelectionDialog = new ListSelectionDialog(WikiEditorPlugin.getDefault().getWorkbench() + .getActiveWorkbenchWindow().getShell(), configsList, new ListContentProvider(), new LabelProvider(), + "Select the refresh URL."); + listSelectionDialog.setTitle("Multiple active configuration found"); + if (listSelectionDialog.open() == Window.OK) { + Object[] locations = listSelectionDialog.getResult(); + if (locations != null) { + for (int i = 0; i < locations.length; i++) { + configuration = (Configuration) locations[i]; + break; + } + } + } + return configuration; + } + + public IDocument getDocument() { + IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput()); + return doc; + } + + private String getWikiFile(IFile file) { + return Util.getFileWikiName(file, WikiEditorPlugin.HTML_OUTPUT_PATH); + } + + public void init(IWorkbenchWindow window) { + this.window = window; + } + + void openWikiFile(IFile cfile) { + String wikiName = getWikiFile(cfile); + try { + if (fEditor != null) { + selectWiki(wikiName); + } + } catch (Exception e) { + } + + } + + public void openWikiLinkOnSelection() { + IDocument doc = getDocument(); + ITextSelection selection = (ITextSelection) fEditor.getSelectionProvider().getSelection(); + int pos = selection.getOffset(); + IFileEditorInput ei = (IFileEditorInput) fEditor.getEditorInput(); + openWikiFile(ei.getFile()); + } + + public void run(IAction action) { + if (fEditor == null) { + IEditorPart targetEditor = window.getActivePage().getActiveEditor(); + if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) { + fEditor = (AbstractTextEditor) targetEditor; + } + } + if (fEditor != null) { + openWikiLinkOnSelection(); + } + } + + public void selectionChanged(IAction action, ISelection selection) { + if (selection.isEmpty()) { + return; + } + if (selection instanceof TextSelection) { + action.setEnabled(true); + return; + } + if (window.getActivePage() != null && window.getActivePage().getActivePart() != null) { + action.setEnabled(window.getActivePage().getActivePart().getClass().equals(WikiEditor.class)); + } + } + + private void selectWiki(String wikiName) { + String wikiContent = WikipediaDB.getExactText(wikiName); + if (wikiContent != null) { + IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput()); + doc.set(wikiContent); + } + } + + public void setActiveEditor(IAction action, IEditorPart targetEditor) { + if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) { + fEditor = (AbstractTextEditor) targetEditor; + text = new EditorText(targetEditor); + } + } +} \ No newline at end of file diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/editor/WikiCompletionProcessor.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/editor/WikiCompletionProcessor.java index d5d4866..28661b9 100644 --- a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/editor/WikiCompletionProcessor.java +++ b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/editor/WikiCompletionProcessor.java @@ -125,7 +125,7 @@ public class WikiCompletionProcessor implements IContentAssistProcessor { } if (WikiEditorPlugin.fWikiDB != null) { try { - ArrayList list = WikiEditorPlugin.fWikiDB.queryPrefix(prefix); + ArrayList list = WikiEditorPlugin.fWikiDB.queryPrefixTitle(prefix); ICompletionProposal[] titleProposals = computeTitleProposals(list, region, viewer); result.addAll(Arrays.asList(titleProposals)); } catch (Exception ex1) { diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/preferences/Util.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/preferences/Util.java index 241867a..417165e 100644 --- a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/preferences/Util.java +++ b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/preferences/Util.java @@ -21,6 +21,15 @@ import org.eclipse.ui.PlatformUI; import org.plog4u.wiki.filter.FilterUtil; public class Util { + public static String titleToDB(String in) { + return in.replaceAll(" ", "_"); + } + public static String db2Title(String in) { + return in.replaceAll("_", " "); + } + public static String db2TitleLink(String in) { + return "[["+in.replaceAll("_", " ")+"]]"; + } public static Shell findShell() { IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); if (window != null) { diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/sql/WikipediaDB.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/sql/WikipediaDB.java index 9bbad44..4ae5980 100644 --- a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/sql/WikipediaDB.java +++ b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/sql/WikipediaDB.java @@ -1,9 +1,5 @@ package net.sourceforge.phpeclipse.wiki.sql; -import java.io.BufferedReader; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; @@ -14,10 +10,10 @@ import java.sql.Statement; import java.util.ArrayList; import java.util.List; -import net.sourceforge.phpeclipse.wiki.editor.LineTokenizer; import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin; import net.sourceforge.phpeclipse.wiki.internal.ConfigurationManager; import net.sourceforge.phpeclipse.wiki.internal.IConfiguration; +import net.sourceforge.phpeclipse.wiki.preferences.Util; public class WikipediaDB { @@ -75,6 +71,29 @@ public class WikipediaDB { return list; } + public static String getFirstRow(ResultSet rs) throws SQLException { + // the order of the rows in a cursor + // are implementation dependent unless you use the SQL ORDER statement + ResultSetMetaData meta = rs.getMetaData(); + int colmax = meta.getColumnCount(); + int i; + Object o = null; + + // the result set is a cursor into the data. You can only + // point to one row at a time + // assume we are pointing to BEFORE the first row + // rs.next() points to next row and returns true + // or false if there is no next row, which breaks the loop + for (; rs.next();) { + for (i = 0; i < colmax; ++i) { + o = rs.getObject(i + 1); // Is SQL the first column is indexed + // with 1 not 0 + return o.toString(); + } + } + return null; + } + public static void main(String[] args) { WikipediaDB db = null; @@ -87,7 +106,7 @@ public class WikipediaDB { try { // do a query - ArrayList list = db.queryPrefix("Programming:PHP"); + ArrayList list = db.queryPrefixTexts("Programming:PHP"); // db.query("SELECT * FROM cur WHERE cur_title like 'Programming:PHP%'"); // WHERE num_col < 250"); for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i).toString()); @@ -130,9 +149,13 @@ public class WikipediaDB { // } // } - Connection conn; + private final Connection conn; + + private final PreparedStatement fGetPrefixTitles; - PreparedStatement fGetPrefixTitles; + private final PreparedStatement fGetPrefixTexts; + + private final PreparedStatement fGetExactText; public WikipediaDB() throws Exception // note more general exception { @@ -141,16 +164,20 @@ public class WikipediaDB { // mysql-connector.jar should be in the class path or made part of the current jar Class.forName("com.mysql.jdbc.Driver"); - // determine the foirst SQL configuration - List allConfigsList = ConfigurationManager.getInstance().getConfigurations(); - ArrayList configsList = new ArrayList(); + // determine the first SQL configuration IConfiguration configuration = null; - for (int i = 0; i < allConfigsList.size(); i++) { - configuration = (IConfiguration) allConfigsList.get(i); - if (configuration.getType().equals(WikiEditorPlugin.WIKIPEDIA_SQL)) { - break; + try { + List allConfigsList = ConfigurationManager.getInstance().getConfigurations(); + ArrayList configsList = new ArrayList(); + for (int i = 0; i < allConfigsList.size(); i++) { + configuration = (IConfiguration) allConfigsList.get(i); + if (configuration.getType().equals(WikiEditorPlugin.WIKIPEDIA_SQL)) { + break; + } + configuration = null; } - configuration = null; + } catch (Throwable th) { + // } // connect to the database. This will load the db files and start the @@ -168,6 +195,8 @@ public class WikipediaDB { ""); // password } fGetPrefixTitles = conn.prepareStatement("SELECT cur_title FROM cur WHERE LOWER( cur_title ) like ?"); + fGetPrefixTexts = conn.prepareStatement("SELECT cur_text FROM cur WHERE LOWER( cur_title ) like ?"); + fGetExactText = conn.prepareStatement("SELECT cur_text FROM cur WHERE cur_title = ?"); } //use for SQL commands CREATE and SELECT @@ -191,15 +220,60 @@ public class WikipediaDB { // completely examined. } - public synchronized ArrayList queryPrefix(String prefix) throws SQLException { + public synchronized ArrayList queryPrefixTitle(String prefix) throws SQLException { fGetPrefixTitles.setString(1, prefix.toLowerCase() + '%'); ResultSet rs = null; rs = fGetPrefixTitles.executeQuery(); // run the query // do something with the result set. + ArrayList list = getResultAsString(rs); + if (list != null) { + // convert to editor format + for (int i = 0; i < list.size(); i++) { + list.set(i, Util.db2TitleLink((String) list.get(i))); + } + } + return list; + // st.close(); // NOTE!! if you close a statement the associated ResultSet is + } + + public synchronized ArrayList queryPrefixTexts(String prefix) throws SQLException { + fGetPrefixTexts.setString(1, prefix.toLowerCase() + '%'); + ResultSet rs = null; + rs = fGetPrefixTexts.executeQuery(); // run the query + // do something with the result set. return getResultAsString(rs); // st.close(); // NOTE!! if you close a statement the associated ResultSet is } + public synchronized String queryExactText(String prefix) throws SQLException { + fGetExactText.setString(1, prefix); + ResultSet rs = null; + rs = fGetExactText.executeQuery(); // run the query + // do something with the result set. + return getFirstRow(rs); + // st.close(); // NOTE!! if you close a statement the associated ResultSet is + } + + public static String getExactText(String prefix) { + WikipediaDB db = null; + + try { + db = new WikipediaDB(); + } catch (Exception ex1) { + ex1.printStackTrace(); // could not start db + return null; // bye bye + } + + try { + String text = db.queryExactText(prefix); + db.shutdown(); + return text; + } catch (SQLException ex3) { + ex3.printStackTrace(); + } + return null; + } + public void shutdown() throws SQLException { conn.close(); // if there are no other open connection