WordPress Sitemaps for Multisite Setups

I run several WordPress blogs with one codebase. This allows me to update the codebase once, for all by blogs and also allows me to share plugins. It’s convenient and the way to go if you want to focus and tailor your blogs by topic or audience.

One detractor from this setup is trying to get sitemaps generated for each blog. At the time of posting, current plugins which generate sitemaps were not able to successfully handle this type of multiblog, one code base setup. This is annoying at minimum and possibly damning depending on how well the bots can crawl my page.

My first thought was to go ahead and modify the plugin. How hard could it be? Answer, too hard. I’m sure it can be done, but I have better things to do then swim in that code and figure it out. Next thought, write my own simple plugin. After all, how hard could it be? Answer, too hard. I’d rather figure out the WordPress API later and need sitemaps generated now.

So, I was left to my own crude devices. What I created were 31 lines of simple bliss which gets the job done. The code hinges on two facts: 1) Google accepts simple text files with URLs as a valid sitemap and 2) it’s easier to locate at the tables WordPress employs and figure them out with a few queries then learn an API/workflow or decipher a bunch of code.

Now, detractors will say that it is better to follow the API and build a plugin. That way the code will continue to work as WordPress evolves. And they are correct. That would be the way to go. But hey, this code was quick to write and will tie me over until something better comes along.

Now for the code, written as a php file

  /* Set base url for blog */
  define('BASE_URL', 'http://merit.oryo.us/b'); 
  /* Set table name for posts, could be as simple as wp_posts.  */
  define('TABLE_NAME', 'merit_wp_posts');
 
  /* Set relative paths here to load db credentials */
  require("wp-load.php"); 
  require("../../../wordpress/wp-config.php");
 
  $link = mysql_connect('localhost', DB_USER, DB_PASSWORD);
  if(!$link) {
    die("Couldn't connect: " . mysql_error());
  }
 
 
  mysql_select_db(DB_NAME, $link) || die( mysql_error());
 
  $result = mysql_query("SELECT ID,post_name,post_status FROM " 
    . TABLE_NAME . " ORDER BY post_modified DESC" );
  if(!$result) { die("Error: " . mysql_error()); } 
 
  while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    if($row['post_status'] == "publish") {
      /* Tweak line to display proper url, 
       BASE_URL . "/index.php?id=" . $row['ID'] . "\n" will surely work */
      print BASE_URL . "/" . $row['ID'] . "/" . $row['post_name'] . "\n";
    }
  }
 
  mysql_free_result($result);
 
  mysql_close($link);

As you can see, there’s not much too it. Simple and unsophisticated but works. Of course, to use this you need to have some understanding of what you are doing. If you can’t make since of the code, best to wait until a sitemap plugin comes along that will properly handle a WordPress site with multiple blogs.

Setting up NCBI Blast on Unix System

While installing a precompiled version of NCBI blast on your localbox is fairly straightforward, there are a few gotchas that got me.  Let me eloborate in hopes that they won’t get you too.

If you’re following the instructions kindly provided at NCBI’s site, good.  Continue.  They’re quite adequate and will work for most.  If you’re still reading this message, I take it the instructions were lacking.

One problem you might have is installing at database.  I don’t know why, but the update_blastdb.pl script that is packaged with the binaries did nothing but waste my time.  You can still download the gzipped files and install them yourself.  I chose to go the NONFORMATED DB route because the preformated databases that I downloaded where missing some files.  Formatting the databases is a simple task and can be done with the formatdb command.

NCBI recommends that you place your databases in a seperate directory.  To let blastall, megablast, et al. know about the dbs you must place an .ncbirc file in your home directory or have one in the current working directory when you “blast” a sequence.  If you don’t know who will be using the programs, you can tell blast where the databases are located with the “-d” flag.

megablast -d ../blast/db/nt -i myseq.fasta -o megaoutput.bls

It makes more sence to me to state the db path explicitly since you probably don’t want to make sure that every user has a .ncbirc file in their home directory (assuming this install is for a multi-user system).  Explictly stating the db path also is helpful when calling the program from a script.

Free SSH Hosting

Not too long ago I found myself in a predicament. I wanted to be able to log into my box at home from work but both where behind firewalls and both had dynamic ips allocated. The solution was using ssh and port forwarding.

To get this to work, I needed a third computer that I could log into and set up the port forwarding. I combed the web looking for a free option. I thought, ” There are plenty of free web hosts out there, surely some must be offering ssh access as well.” Wrong. If you don’t believe me, look. If you’re looking right now, keep looking. When your done, come back.

Since there’s not a free option out there, you have to find a reasonable web host that will give you ssh access. I ended up going with bluehost and I can’t complain. I signed up and had ssh access with in hours, I kid you not. Plus, since you’re given unlimited bandwidth, you can setup you ports and forward them one without worrying about gobbling up MBs.

When you consider that bluehost will also give you the ability to host an unlimited number of domains and store an unlimited about of data on their hosts, its a deal hard to pass up. Go bluehost for your free ssh needs and a whole lot more.

Drawing Smooth Curves with Gimp

Paths and the ability to work with them should be in the repertoire of any serious gimper. It’s about the only way to make smooth, complicated curves. In this tutorial, we’ll see just how easy it can be to use paths and in doing so, create an arch.

To start with, create a new image on a white background. In the main Gimp toolbox, select the Path tool. On the image, place anchor points more or less as shown below. Note that the anchor points will automatically be joined by lines and to close the path, you need to hold down on the CTRL key as you click on the first anchor you laid.

With the anchors laid, click on the lines that connect them and drag them up until you get something that looks this.

What we’ve got now is very rough. Click on the anchors and some handles should pop out. These control how the path enters the anchor. Play around with them until you get a feel for how they work. If you want to control both handles at once, hold down on the shift key. Sometimes you’ll miss the mark and might create another anchor point. You can remove it easily by holding down SHIFT+CTRL and click on it.

For the next couple of steps, we’ll need the Path Dialog. It can be opened underneath the Dialog menu (Dialog -> Paths). In this dialog, you should see our new path. At the bottom of the dialog, there’s a button “Path to Selection”. It’s the one that resembles a selected rectangle. Hit this button once to convert your path to a selection. Then fill it with a gradient.

Clear the selection and have a look. Not to shabby. Finally, finish it off with by stroking the path with a darker color and perhaps a reflection.

Inner Shadow with Gimp

A common technique used to add some depth to an object is an inner shadow. You’ll see it used quite a bit, especially in Photoshop tutorials (Layer->Layer Style->Inner Shadow). It turns out that we can achieve more or less the same effect with Gimp. It just takes more work.

We’ll start with a red rectangle. You can make it any size you want and pick a background color.

Now, select the rectangle. This can easily be done using the fuzzy select tool (magic wand). Next, shrink the selection by about 10px (Select->Shrink).

And then feather the selection by 5px (Select->Feather).

We almost have the selection we need. All we need to do is invert it (Select->Invert). There it is. This selection we’ll fill with a dark gray.

But wait. We don’t want to wipe out the rectangle. Create a new layer (Layer->New Layer) and call it inner shadow. With this layer active, go ahead and fill in the selected region.

We’re almost there. Reduce the opacity some on the inner shadow layer to let the rectangle underneath come through some. You can see that our shadow extends far beyond where it should. It should be confined to the box. To fix this, select the base layer (the one with the box).

Again, select the box and then invert the selection. Finally, switch back to the inner shadow layer and cut away the selection (Edit->Cut).

We’re done.

As illustrated, you can achieve an effect similar to that of Photoshop’s inner shadow with Gimp. It just takes a few more clicks of the mouse.

Advanced Javascript By Example: A First Look Inside JQuery UI

Note: This is not a tutorial for the jQuery UI. This is a look at the code of the UI itself in hopes of better understanding javascript and some techniques jQuery developers use to craft better code.

Efficient learning takes place when there is a master involved in the process. It’s simply much faster to learn and assimilate knowledge and technique when we “see it in action”. One observes the master at his craft and tries to imitate his or her action. In doing so, much is gained. Following this learning paradigm, let’s see what we can learn from the javascript masters over at jQuery.

Now we’re not going to be so presumptuousness as tackle the core jQuery library. We’ll start with something a little more manageable and fresh, the new jQuery UI. In particular we will look at the file ui.core.js which provides some of the basic functionality of the UI. Before proceeding, I suggest you get the code and take a look at it first.

Very well, now let’s see what we can make of the code. First, let’s look at what the code is wrapped in.

;(function($) {
 
  /* bunch o' code */
 
})(jQuery);

Now looking at this piece of code… Whoa, what’s that semicolon doing there! We’re usually accustomed to seeing semicolons at the end of a line. Out of place and pointless as it might seem, it actually can be quite helpful. Some web applications in production try to package your code together with other scripts to speed up downloads (the more curious can read on at semicolon at the start of jQuery). Putting a semicolon at the beginning assures that the script proceeding yours will be tied off nicely regardless of how the scripts are concatenated together.

Next we notice that the code is wrapped in some sort of anonymous function. Before we address why this is the case, first let’s ascertain what’s going on. Consider a simpler example…

( function(message) {
   alert(message);
})('Peek-a-boo');

If you were to run this, you see a pop-up with ‘Peek-a-boo’. This bit of code creates a function and then calls it. Going back to the UI code, we see that everything was wrapped in an anonymous function and then it’s called right away, being passed the jQuery object.

Now, why would we want to do this? The rationale is to prevent conflicts with other libraries. The “$” is a shortcut that some other libraries also employ. By encapsulating the code in an anonymous function we create a local scope. The “$” which exists in this local scope (since its the function’s argument) will take precedence over any global “$” floating around. Becasue we pass the jQuery object to the anonymous function, we can be assured that any reference to “$” in this code refers to the jQuery object and not something else.

Moving on, next we see the $.ui object.

$.ui = {
   plugin: {
             add: function(module, option, set) {
                   var proto = $.ui[module].prototype;
                   for(var i in set) {
                           proto.plugins[i] = proto.plugins[i] || [];
                           proto.plugins[i].push([option, set[i]]);
                   }
             },
             call: function(instance, name, args) {
                   var set = instance.plugins[name];
                    if(!set) { return; }
 
                    for (var i = 0; i < set.length; i++) {
                          if (instance.options[set[i][0]]) {
                                   set[i][1].apply(instance.element, args);
                          }
                    }
             }
        },
   cssCache: {},
   css: function(name) {
        if ($.ui.cssCache[name]) { return $.ui.cssCache[name]; }
                  /* ... */
   },
   hasScroll: function(e, a) {
        /* .... */
        if (e[scroll] > 0) return true; e[scroll] = 1;
             has = e[scroll] > 0 ? true : false; e[scroll] = 0;
             return has;
        }
};

The $.ui object is defined using something akin to JSON. At it’s heart, this notation is nothing more that a list of name/value pairs. The names are, well, the names and the values can be literals, functions or objects. From the code, we can see that $.ui.plugin is an object that has two methods, add and call. $.ui.cssCache is initialized to be an empty object and $ui.css is a method.

That’s it for this first edition. Next time, we’ll look at the widget factory and try to understand factories and prototyping.