• This forum has been archived. New threads and replies may not be made. All add-ons/resources that are active should be migrated to the Resource Manager. See this thread for more information.

[SchmitzIT] Smilies Bulk Importer

SchmitzIT

Well-known member
Well, not an add-on as such, but perhaps still something that people might find useful. The attached program basically offers a quick way to bulk-import smilies from a folder on your harddrive.The thought there is that if you want the smileys on the server, you'll probably have them stored locally somewhere.

Basically, it will loop through the designated folder (and its subfolders) looking for .gif and .png files. It's coded in C#, and currently quite straight-forward, without too much error handling or options.

I've added two different files, one a zip containing a plain executable. I'm honestly not 100% sure if it will work out of the box for everyone, as it has a dependency on both the .NET framework, as well on the mysql connector. In my case, I have that file residing here:

C:\Program Files (x86)\MySQL\MySQL Connector Net 6.3.6\Assemblies\v4.0\MySql.Data.dll

You can obtain the mysql connector yourself by downloading it here:

http://dev.mysql.com/downloads/connector/net/6.3.html#downloads

In case you prefer not to meddle with DLLs, I also included an installation package, which should take care of everything, including a start menu installation and an uninstall option.

I tested the script both on my local setup as well as on my test server-side XF installation, and it went flawlessly. Should something not work, let me know and I'll try to address the issue ASAP.

Ideas for future updates:

[*] Include an option to "skip" smilies with the same name as the stock XF ones.
[*] Include an option to remove the existing stock XF smilies (in case one wants to overwrite all the smileys with a completely different set).

Ideas and/or suggestions welcome.
 

Attachments

For those interested, below is the full source code:


Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.IO;

namespace XF_Smilies_Bulk_Importer
{
    public partial class SmileyImporter : Form
    {
        String insertCommand = "INSERT INTO xf_smilie (title, smilie_text, image_url) VALUES";
        public SmileyImporter()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void Form1_DoubleClick(object sender, EventArgs e)
        {
            // Info-message
            MessageBox.Show("XenForo smilies bulk importer by www.SchmitzIT.com, developed exclusively for XenForo.com. Have a great day!");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Show the folder selector, and if a path was selected, add it to the textbox
            DialogResult result = folderBrowserDialog1.ShowDialog();
            if (result == DialogResult.OK) {
                txtLocalFilepath.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        private void btnTestConnection_Click(object sender, EventArgs e)
        {
            // Check that all server settings are filled in
            VerifyTextBoxesNotEmpty(groupBox2);
            // Try and create a connection.
            TestConnection();
        }

        private Boolean TestConnection()
        {
            // Build a connection
            MySqlConnection conn = new MySqlConnection();
            conn.ConnectionString =
                "server=" + txtServer.Text.ToString() + ";"
                + "port=" + txtPort.Text.ToString() + ";"
                + "database=" + txtDBName.Text.ToString() + ";"
                + "uid=" + txtDBUser.Text.ToString() + ";"
                + "password=" + txtDBPassword.Text.ToString() + ";";
            try
            {
                conn.Open();
            } catch (MySqlException e) {
                MessageBox.Show("An error has occurred trying to open the connection: " + e.ErrorCode + " - " + e.Message);
                return false;
            }
            finally {
                conn.Close();
            }
            MessageBox.Show("Connection successfully established!");
            return true;
        }

        private Boolean VerifyTextBoxesNotEmpty(GroupBox gb)
        {
            // Check that all textboxes have been filled in
            foreach (TextBox inputfield in gb.Controls.OfType<TextBox>()) {
                if (inputfield.Text == "") {
                    MessageBox.Show("Please fill in all the required fields!");
                    return false;
                }
            }
            return true;
        }

        private void btnGo_Click(object sender, EventArgs e)
        {
            // Everything needs to be filled in
            if (VerifyTextBoxesNotEmpty(groupBox1) && VerifyTextBoxesNotEmpty(groupBox2)) {
                // loop through the local folder and grab all images.
                String[] pngSmilies = Directory.GetFiles(txtLocalFilepath.Text, "*.png", SearchOption.AllDirectories);
                String[] gifSmilies = Directory.GetFiles(txtLocalFilepath.Text, "*.gif", SearchOption.AllDirectories);
                ProcessSmilies(pngSmilies);

                // Add a comma to the insertCommand and then process the gifs
                insertCommand += ", ";
                ProcessSmilies(gifSmilies);

                // Build a connection
                MySqlConnection conn = new MySqlConnection();
                conn.ConnectionString =
                    "server=" + txtServer.Text.ToString() + ";"
                    + "port=" + txtPort.Text.ToString() + ";"
                    + "database=" + txtDBName.Text.ToString() + ";"
                    + "uid=" + txtDBUser.Text.ToString() + ";"
                    + "password=" + txtDBPassword.Text.ToString() + ";";

                try {
                    conn.Open();
                    MySqlCommand cmd = conn.CreateCommand();
                    cmd.CommandText = insertCommand;
                    cmd.ExecuteNonQuery();
                } catch (MySqlException ex) {
                    MessageBox.Show("An error has occurred trying to open the connection: " + ex.ErrorCode + " - " + ex.Message);
                    return;
                } finally {
                    conn.Close();
                    MessageBox.Show("All done. Have a great day!");
                }
            }
        }
        private void ProcessSmilies(String[] files) {
            Boolean isFirst = true;
            foreach (String filename in files) {
                // First, strip off the path
                String onlyFilename = filename.Substring(filename.LastIndexOf("\\") + 1, filename.Length - (filename.LastIndexOf("\\") + 1));

                // Create a variable holding the plain name, without extension or dot
                String compactFileName = onlyFilename.Substring(0, onlyFilename.IndexOf("."));

                if (isFirst == true) {
                    // First time we loop. Do not add a comma to the string
                    insertCommand += "('"
                        // Capitalize the first letter
                        + System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(compactFileName)
                        + "', ':" + compactFileName + ":', '" + txtServerFilepath.Text + "/" + onlyFilename + "')";
                    isFirst = false;
                } else {
                    // Add a comma
                    insertCommand += ", ('"
                        + System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(compactFileName)
                        + "', ':" + compactFileName + ":', '" + txtServerFilepath.Text + "/" + onlyFilename + "')";
                }
            }
        }
    }
}
 
There is no instruction I'm afraid and, and than install where? on machine or server? if so how to install it? (no manual)
smile.png

error.webp
 
I think I'll wait for an admincp backend system for uploading smilies i don't want a hundred add-ons for certain smiley features that should be all part of the core. I'm not fussed manually adding them one by one.

Nice release regardless.
 
There is no instruction I'm afraid and, and than install where? on machine or server? if so how to install it? (no manual) :)
View attachment 10041

Hey,
Did you try running the executable only, or the installer package? In case it was the executable, try installing the MySQL Connector (I hate that's neccessary, but the .NET framework does not offer native support for MySQL). Otherwise, the installer package ought to take care of registering it for you.Let me know if you still have any trouble?

You install it on your local machine, NOT on the server. The executable is just an executable. After installing the connector, it should just run and show you a GUI like in my screenshot where you enter a few bits of info.

In case of the installer, run the setup, and that ought to take care of everything. Then you just fire it up from the start menu.

Oh you still have to add them one by one with this?

No. What this does is it will loop through all .png and .gif files in the folder you point it to. Then it will generate the names and replacement for it automatically. So if you have a folder containing smilies called bat.gif, bear.png, etc, it will generate them as Bat, replacing :bat: and Bear replacing :bear:, and so on.

I'd also prefer a back-end integrated solution like Shelley suggested, but lacking that and finding myself in need of quickly importing several dozen smilies, I figured it would take me about as long to write this little app than it would to manually do them one by one, and with the former I'd at least get a little exercise out of it.
 
I've tried it but it didn't work unfortunately. I suspected it was maybe because my host wouldn't allow remote access to mysql so I added my IP adress but that didn't help either:

caps.webp
 
I've tried it but it didn't work unfortunately. I suspected it was maybe because my host wouldn't allow remote access to mysql so I added my IP adress but that didn't help either:

View attachment 10313

The error message seems (I have to admit that I have no idea what the Turkish means) to state it cannot find the MySQL.Data file. Did you try to install the mySQL connector as indicated above? If so, try and see if copying it to the location C:\Program Files (x86)\MySQL\MySQL Connector Net 6.3.6\Assemblies\v4.0\MySql.Data.dll will do the trick?
 
The error message seems (I have to admit that I have no idea what the Turkish means) to state it cannot find the MySQL.Data file. Did you try to install the mySQL connector as indicated above? If so, try and see if copying it to the location C:\Program Files (x86)\MySQL\MySQL Connector Net 6.3.6\Assemblies\v4.0\MySql.Data.dll will do the trick?

Sorry fot that EntropiaPlanets, I couldn't installed the program on win7 it says "the program was finished immaturely" so the reason is that it wasn't installed at the first place. Is there somehow a wourkaround to fix it ? Thank you.

Edit: Installing an earlier version strangely worked. But the importer is still not working. I even try to copy the files and rename it to MySQL Connector Net 6.3.6 no luck.
 
Sorry fot that EntropiaPlanets, I couldn't installed the program on win7 it says "the program was finished immaturely" so the reason is that it wasn't installed at the first place. Is there somehow a wourkaround to fix it ? Thank you.

Edit: Installing an earlier version strangely worked. But the importer is still not working. I even try to copy the files and rename it to MySQL Connector Net 6.3.6 no luck.

Sorry to hear that. Have you tried using the other zip-file containing the installer?
 
Top Bottom