--- /dev/null
+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 <code>createFolder</code>.
+ *
+ * @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 = "<!--" + word + "-->";
+ 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
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;
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 {
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;
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());
// }
// }
- 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
{
// 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
""); // 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
// 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