Category

Tutorials

Descriptive Enumerations In C#

I had the desire to create an enumeration in C# the other day and I wanted to be able to attach a description to each item in the enumeration. I did the searching on the internets and put together examples from several sources that I found. Here is an example that ties it all together in a Windows Console applicaton in C#.

Reason 4 + Virtual MIDI Driver + 64bit Vista

I’ve been a user of Propellerhead Reason in the past, but recently I wanted to dust off my chops and try to create some tunes again. My problem is that I don’t have a MIDI interface to attach my keyboard to this PC (yet). So I figured I’d install a software keyboard and hook it up through a virtual MIDI driver. Easy, right?

The propellerhead site pointed me to a software keyboard called Bome’s Mouse Keyboard, which actually had a page dedicated to hooking it up to Reason. Well, I started going down the list of drivers they had listed on that site and each failed in turn. Why? Because I’m running 64bit Vista and none of the drivers were built for 64bit OS. That is … all except one called MIDI Yoke. This one had an install program which ran fine and did not complain. So here are the steps to getting it up and working:

  1. Download and install Bome’s Mouse Keyboard
  2. Download and install MIDI Yoke
  3. Reboot
  4. Get coffee (depending on the speed of your machine)
  5. Launch Bome’s Mouse Keyboard and configure it’s MIDI Output by selecting one of the Out To MIDI Yoke: X settings from the Midi Out file menu.
    bomesmidiout
  6. Open Reason (I’m using version 4) (hopefully you already have this installed)
  7. Select Edit -> Preferences then select the Keyboards and Control Surfaces page.
    keyboardsandsurfaces
  8. Click the Add button under the Attached Surfaces box.
  9. You will now be presented with the Control Surface dialog box.
  10. From the Manufacturer drop-down select <Other>.
  11. From the Model drop-down select MIDI Control Keyboard.
  12. Enter a name in the Name text box (I used Bome’s Mouse Keyboard as the name).
  13. From the MIDI Input drop-down select In From MIDI Yoke: X where X matches the channel number that you selected for the MIDI Out of the software keyboard. I used 1 in both cases.
    controlsurface
  14. Now just click the OK button and close the Preferences dialog and you should be ready to go!

Well, I hope this help someone out. When I was going through the pain of trying to find a Virtual MIDI Driver that would run on a 64bit OS I found nothing but complaints and forum postings of users who could not get it to work… but no solutions. Well, hopefully that has changed now and there is a FREE virtual MIDI driver that will run on 64bit OS. Enjoy!

A WordPress Plugin – Hello XML-RPC World

In a previous post (How Can I Modify The WordPress XML-RPC System?) I outlined how one might create a plugin that adds functionality to the WordPress XML-RPC server. I’ve had some questions from some readers arise so I wanted to take the opportunity to provide a more concrete and usable example.

WordPress Plugin

First you are going to create your plugin. This can be a single file that follows all of the regular WordPress plugin flow. I have a complete plugin example listed below.

Of course, for simplicity sake in this post I’ve eliminated comment blocks. You’ll need those in order for your plugin to be properly recognized by WordPress. Other than that this is it. The wpHelloXMLRPCWorld method is the actual method that does the work of whatever you want. This one simply returns a hello world string. The attach_new_xmlrpc method is used to do the work of registering your new XML-RPC method. And finally we just need to add the action to tell WordPress to use our attach_new_xmlrpc method to attach our new XML-RPC function.

Testing With Java

I personally like the Redstone XML-RPC library, which I’ve written on before (Would You Like To Post To WordPress From Java?). This is a very simple Java class which can test the new XML-RPC function.

Download

This is a complete example that includes:

  1. The PHP file needed for the plugin. Just upload wpHelloXMLRPC.php to your wp-content/plugins directory and activate as usual for WordPress plugins.
  2. The Java program which can run your new XML-RPC method.
  3. A bat file (dos/Windows) that will compile the Java test program.
  4. A jar file containing the required Redstone libraries

wpHelloXMLRPC.zip

How To Manually Merge WordPress Tags

With the advent of “tags” in the WordPress blogging platform there have been several very good plugins introduced which can handle management of your tags. This includes merging tags.

Why Would I Want To Merge Tags?

One word… duplicates. In my case I created a tag for my Friday Song Of The Day posts. The original post slug was /friday-sotd/ however as time went on something happened and I noticed that somehow I had created a second tag with the same title but with /friday-song-of-the-day/ as the slug. Well, that’s a problem if I want to link to all of the posts in that tag, because there are really two different tags.

Ok, so you’d figure I would go out and get one of these shiny new tag plugins and be done with it in a few minutes, right? Heck no! I’m an under-the-hood kind of guy. I like to know how and why stuff works. So I set out to discover the changes that would be needed in my database to perform the merge. Why? Well, now I have the capability to write my own plugin if I so desire… or I could write a blog post to teach other people how to do it. 🙂

So How Do I Do It?

Well, as it turns out this is really quite simple. There are just a few queries that you’ll need to run using the command line MySQL client or a graphical client such as phpMyAdmin.

Step 1) Backup Your Database!
This should go without saying… but I’ll say it. Before making any major modifications to any live system you should make a backup. I wrote an article on creating a Bash Script To Backup Your Website which you can use or one of any other numerous methods.

Step 2) Get The Tag IDs
Next you are going to need to know the IDs of the tags which you are going to be merging. The “tags” are stored in the wp_terms table. In my case I performed a SQL search in the table such as this:

As you can see in the following screenshot of the query results, the two IDs that I am interested in merging are 47 and 91. I note this and move to the next step.

Step 3) What Posts Are Affected?
This step is not really required. You could simply head off to the UPDATE step and trust that everything will be fine. I myself like to know what I will be affecting though, so this query will return some information about all of the posts that are tagged with our two tags:

Here is a screenshot of the results:

Yeah, that gives us somewhat of a picture but there are just too many results to see all at once. Let’s refine that query a bit to just give us a count of the posts related to each tag. Now the query has changed to:

And here is the screenshot of those results. As you can see now I have a nice count. 116 records are tagged with the /friday-song-of-the-day/ slug and 25 are tagged with the /friday-sotd/ slug.

Step 4) Merging Tags… Commence!
Well, you may be disappointed, but the query to perform the merge is actually a simple UPDATE query. Here it is:

Simple, right? All we are doing is changing the listings in the relationships table that are ID 47 to 91. So now there should be no posts that are tagged with the “tag ID” 47. Below is a screenshot after running the query and you can see that 25 rows were affected. This is the same number that you can see above when we counted the posts under each tag.

Now if we run our query to count the number of posts under each tag we get 141, which is the sum of the original two counts 116 + 25. Great!

Step 5) Cleanup
Well, we could stop here, but there are still a few leftover artifacts from the old tag (ID 47). Let’s go ahead and get rid of them:

Step 6) Redirect
What! We’re not done yet? Almost. After making the above changes I went to double check my work by visiting the actual blog. I pulled up the tag archive page for the old tag by going to http://www.franzone.com/tag/friday-sotd/. It works! Now I’m presented with a “No Posts Found” page and a search box. But… that’s not very appealing. I believe I’ve actually linked to this URL in past posts, which could be a problem.

htaccess file to the rescue! This is actually a pretty simple fix. I just opened up my .htaccess file in a text editor and added a file like this:

Now if you visit that URL you get redirected to http://www.franzone.com/tag/friday-song-of-the-day/ instead. Perfect!

Final Notes

So there you have it. You can go about merging your own tags and hacking up your WordPress database. Of course you could take a scripted approach to the above steps. Or you could create your own tag merging WordPress Plugin. Or you could leave a comment below if you were so inclined! Thanks for stopping by.

PHP Init Style Status Message

Yesterday I talked about created SUCCESS and FAILED status messages in a Bash script (Bash Script Init Style Status Message). Well, on occasion I use PHP for shell/system scripting and of course I thought it would be nice to incorporate my new status messages in those scripts as well. Turns out that it was quite easy to port over.

The Functions

You can simply include the following functions in your PHP script/class.

If you read my previous article then you can readily see that the code is almost verbatim, it just uses PHP syntax instead of Bash.

Usage

The above functions were taken out of a class that I was using. If you wanted to include these functions in your class then you could simply call them as follows:

Well, I hope you can find some use for these functions and thank you so much for stopping by! Please leave comments below (if you have any).

Bash Script Init Style Status Message

Have you ever wondered how the OK and/or FAILED messages are printed out in the initialization of services on a Linux system during bootup? Well… I have. Anyway, I’ve always thought that it was really cool how the OK and FAILED messages printed out at the same column on the screen and in different colors. So of course I wanted to know how to do that so I could use it in my own scripts.

The Source

If you look inside some of your systm init scripts you’ll notice that many of them source the /etc/rc.d/init.d/functions script. This is actually where I found the code to produce neato status messages.

The Code

I could have just sourced that file (included) in my scripts and been fine, but I wanted my scripts to be self-contained so I took the liberty of pulling out the applicable code. Here it is below with comments:

Usage

So you can just drop the above at the top of your script and then call the appropriate functions where needed. For example you could do something like this:

Pretty simple, eh? Now what type of script might you need something like this in? Check out my previous article How To Create A Bash Install Script, which is a perfect example of the type of script in which you may want these status type messages.

Thanks for stopping by! I hope that you’ll leave any comments that you have below.

How To Create A Bash Install Script

Have you ever wanted to create an installer program on a Linux system, but didn’t want all the hassle of an actual install builder? I have. I recently had need of a simple, no frills installation that could:

  1. ask the user a few questions
  2. extract some binary files and
  3. do some stuff with those binary files.

So I’m going to show you how you can create such an installer with very little hassle. I can’t actually claim credit for this method though; I actually got the idea from Sun’s JDK installer for the Linux platform. You download a “.bin” file, change the file mode so that it is executable and then run it. It displays the end user license agreement, gets some feedback and then goes about installing Java for you. Well, if you open that .bin file up in a text editor (say vi) you’ll see that it’s nothing more than a shell script with a binary chunked onto the end. Thus my plans for world domination were born…


Bash Command Line Pro Tips

As someone that has used the Bash shell almost daily for over 15 years, Jason Cannon has accumulated several command line “tricks” that have saved him time and frustration. This guide is a collection of techniques that you can put to use right away to increase your efficiency at the command line. Start using these 10 tactics today to improve your effectiveness.

Click to download this resource for FREE.


The Script

The first part of this process is to create your install script. At the beginning of my script I put any environment variables and setup that I need for the script to run propertly.

For this exercise I chose a zip file as my binary distribution media. The ZIP_FILENAME variable holds the name of the file after extracting it from the binary installer. SCRIPT_LINES holds the number of lines in this script file. Actually it’s the number of lines plus one (more on this later). SUM1 and SUM2 are the two numbers that I got from running /usr/bin/sum on my zip file. We’ll use these numbers to verify the file after extracting it from the archive.

The next line uses the trap statement to execute some instructions in case the file exits. This just lets the script clean up after itself in the event that the user exits the file prematurely (ie using CTRL-C to close).

Now for the unpacking (or extracting) of our binary distribution media (a zip file in this case). Basically we’re using the tail command to do the job. The -n option indicates that our number argument is the number of lines that we want. Of course you see we’re using the $SCRIPT_LINES variable that we defined at the start of the script. We prepend the plus (+) operator in front of that number which tells the tail command that we want the last lines of the file starting at the number indicated. So we’ll get all of the file starting at the line just past our script, which will be the start of our zip file.

This little bit performs a checksum on the unpacked zip file and checks that against the values we set when building the installer.

The last little bit to this script does not really do that much. We’ve unpacked and verified our zip file so now we just unzip it. To finish up the script removes the zip file (we’ve already extracted it’s contents) and exits. Of course at this stage you could do any number of things that you wanted. You could compile some code if you wanted, prompt the user for feedback in order to build a configuration… whatever you need to do (that can be scripted).

Building The Installer

Ok, so we have our install script ready to go and we have a zip file (or binary distribution media). Now what? Building the final installer is actually a trivial matter from here. All we need to do is concatenate the script file (first) and the zip file into our final installer. We could also change the file mode so that it is executable.

Or if you want to get extremely fancy you can create a makefile! With this example a makefile is like hanging a photo on the wall with a jackhammer, but for more complex projects it may actually be warranted.

With this in a file called Makefile all you have to do is type make and it builds the installer for you.

Download

Thank you so much for stopping by and reading. I sincerely hope that I’ve helped someone out there. To conclude here is a download that includes a) the install script b) the zip file I used and c) the Makefile to build the final installer. Enjoy!

BashInstallerTutorial.zip (3 KB)

Before you go… please feel to leave comments below. Especially if you have any suggestions as to how this method could be improved. Thanks!

How To Use Subversion Over SSH

The Problem

I recently had a problem. I am a software developer and as such I use source control. For this purpose I generally prefer Subversion. I have been keeping my source repositories on an external drive with the intention to back them up to another secure location periodically (yeah, that never happens). I looked into online subversion hosting, but it seems a bit pricey for my few meager projects. I have a web host, but they don’t host subversion repositories over HTTPS as I’ve used in other situations. What to do?

The Solution

Subversion over SSH to the rescue! I actually have shell access to my web host (I can login to the server via SSH) and doing some quick research I discovered that I can run Subversion through an SSH tunnel. Cool! So how might one set this up?

Setting Up The Server

Prerequisites for the server are that you can login to your server via ssh. Got that? Good. Now while logged into your (Linux) server make sure that subversion is installed using the following commands:

Can’t find them? Well, my host has them and they have great rates. You can check out 1and1 hosting here. Once you are sure that subversion is installed you just need to create your repository. I did it like this:

Now just note (or write down) the full path to your repository from the pwd command (print working directory) and your off to setup the client.

Setting Up The Client

There are two requirements for the client setup. First you should install subversion. If you just install a client (ie TortoiseSVN) you may be missing some required files so go ahead and install the real deal. The second requirement is an SSH client that can handle tunneling. If you’ve performed the server setup over SSH then chances are you already have this. I use cygwin with OpenSSH installed so this how-to will be based on that.

Just so you know, subversion does not support connecting to the svn+ssh protocol “out of the box”. If it did then I wouldn’t be writing this article! The actual steps are mostly taken care of in the installation of the two requirements. Now you just need to edit the subversion config file which can be found at %USERPROFILE%/Application Data/Subversion/config. Mine is actually under %USERPROFILE%/AppData/Roaming/Subversion/config for Windows Vista. Do a search in this file for the [tunnels] section. You will actually see a nice description of the svn+ssh protocol there which I highly suggest reading. After that add the following line:

If you installed cygwin in another location then use that instead. Make sure that you use forward slashes too as the backslashes act as escape characters and then you have to use double backslashes and it just looks ugly. Save and close the file and you should be good! Yeah, that was easy.

Testing The Connection

Now to see if it actually works. Open up a DOS prompt (if you’re using Windows). Try to query your subversion repository like so:

Now obviously use the values in that URL that make sense for your setup. If you don’t know how to do that then press the red box with the X in it at the top right corner of your web browser and then turn off your computer! 🙂 Hopefully all will go well. The client will try to authenticate with your USERNAME and prompt you for the password. Then it will connect and execute the appropriate svn client calls on your remote repository and tunnel the results back to you. Awesome!

Now if you want to kick it up a notch (as Emeril would say) then you can try integrating your new remote repository with a nice graphical client. I highly suggest TortoiseSVN as it integrates right into your Windows shell. You could also try using Subclipse or any others that you like.

PHP Script To Monitor FTP Directory Changes

I recently had the need to be able to monitor a directory on an FTP server for changes and to then be notified of those changes. I’ve been working a lot in PHP lately so I decided to use that to implement my script.

FTP Access

FTP (file transfer protocol) access in PHP is a breeze. It’s as simple as this:

Easy, right? Of course I left out a few pieces of the puzzle, but I just wanted to show how easy it is to actually get to an FTP server, login and get a directory listing.

Comparisons

After retrieving the listing which is an array I remove out the “.” and “..” listings as they are special system directories meaning “the current directory” and “the parent directory” respectively.

Now the header for this section is Comparisons, so what exactly are we comparing to? Each time this script runs we store a serialized version of this array out to file. I read the file into an array and compare it with the one we just created from the FTP directory.

Now that we have an array of files (and directories) that we just pulled from the FTP server and an array that we saved from the last time we ran we can just compare the two.

Finally, I write the contents of the new array back out to the cache file so we can use it to compare on the next run.

Summary and Download

So that’s it! Pretty simple. On my server I pre-pended the following to the file:
#!/usr/bin/env php
and changed the file mode so that it could be executed. I then setup a cron job to run at 8 o’clock every morning like this:

> crontab -e

# This is added to crontab
0 8 * * * ~/ftpMonitor.php

Here is the complete file in a zip file. Open the file in any text editor and you will see a configuration section at the top where you can set your FTP host information and email information.

ftpMonitor.zip

I hope you have enjoyed this short tutorial and may possibly find it useful. If you have any comments or suggestions for improvement please feel free to leave those in the comments section below. Thanks for stopping by!

Bash Script Naming – Say My Name, Say My Name!

I use Java as my main Linux server programming language these days and so I usually have a Bash script to go along with each program. The Bash script help encapsulate creating the proper CLASSPATH needed to run the Java program, calls the correct Java class and also passes through any command line parameters that I might need. Recently I had several Java programs that I wrote and bundled inside the same jar file which I needed to call separately. They had all of the same CLASSPATH and environment requirements, so why should I create separate Bash scripts for each one?

What’s In A Name?

Instead of a separate Bash script for each Java program I used a neat little feature of Linux called symbolic linking. I create in a “bin” subdirectory my main Bash script and then link to it using distinct names from the main program directory. The script then detects what names is being used to call it and executes the appropriate Java class. Some of the Bash code looks like this:

Notice the $@ command line parameter in the call to the Java class? That simply passes on the command line parameters used to call the script to the Java program.

So you can see, if I had a Java program in the com.franzone package who’s class name was TestOne, then I could create a symbolic link to my JavaCaller script to call it like this:

ln -s bin/JavaCaller TestOne

Now from the main directory I could call the ./TestOne script (symbolic link) which would actually execute the JavaCaller script with the TestOne name. That would cause my Bash script to call the TestOne Java class. Cool, eh?