Archives

Dump A MySQL Database To YAML Using PHP

The Problem

I was recently working on a project that is built on WordPress / BuddyPress. I built up some content using the CMS features of WordPress in my development server and then I transfered the WordPress content to a beta server using the export/import functionality. That kind of stinks having to do that every time I want to push updates to my beta server. So I looked into using Doctrine to generate data fixtures which I could repeatedly run against the different database. Doctrine supports YAML files, which are extremely easy to edit by hand, so it seemed like a good idea.

So what’s the problem? Well, I didn’t currently have any of my data in YAML files. I didn’t want to had edit them or do some sort of copy and paste madness. So, I wrote a script in PHP to dump my data into a YAML file.

Continue Reading

Modify PHP CLI Include Path Dynamically

I was working with an application platform that I had just downloaded today. I just wanted to check it out and play with it a bit. In the application framework was an executable script that provided some command line utility. I fired up the script and received an error like “PHP Fatal error: require_once(): Failed opening required…”. Of course non of the files from the downloaded archived were in the include path for my system, but I didn’t want to just go adding it to my php.ini file just to play around with it short term. What to do.

Command Line Options

First I wanted to see if there was a command line option that I could pass to PHP to tell it what include_path to use for just this one execution. If you execute the following you’ll see the usage:
php --help
There is no include_path command line option, but there is the -d foo[=bar] option which allows us to modify any of the INI entries by key/value.

Continue Reading

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).

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!

How Do I Get Meta Data From MySQL Using PHP?

I know you’ve all asked that question before, right? Well maybe not, but I recently had the need to detect what the DATATYPE for arbitrary columns in arbitrary tables were inside a MySQL database using PHP. Turns out that it isn’t all that hard to get.

The main thing about using this method is that we are simply sending SQL queries to MySQL which instruct it to give us the required information. The first one is to retrieve a list of tables for the currently selected database:

The next time is when we retrieve the columns for the table and the column definitions:

Notice that I looped through the results of the first query (the table names). I then use the value of the table names returned from the query to build the query for extracting the column definitions.

Finally, you might ask why I decided to throw all of the results into an array? I could just as easily (probably more easily) have output the results to the console as I ran through the queries. Well, in my situation I needed to actually store and compare the results at a later time. So that’s what I did! Following is an entire code sample that should work; just fill in your login credentials and have fun!

Inserting A Picture Into Your WordPress RSS 2.0 Feed

I use NetVibes for my browser home page and RSS feed reader and I’ve noticed over time that some of the feeds that I subscribe to have pictures embedded within their feed summary. Why doesn’t my feed have a picture in it? I thought that this was really neat because it gives you a quick visual queue as to what the article may be about. It is also eye-catching so that among 5 or so feeds on a page you are instantly drawn to the one with pictures. So how can you get a picture into your feed?

RSS 2.0 Specification

The RSS 2.0 specification has an <enclosure> element available at the <item> level. This element can specify various types of media that you want attached to your feed item. In our case we want that to be an image. The element should look something like this:

<enclosure url=”[URL To Media File]” length=”[Size Of Media File]” type=”[Mime Type Of Media File]” />

All three attributes are required:

  1. url – This tells the feed reader where the media file is located
  2. length – This is how big the media file is in bytes
  3. type – This is the standard MIME type of the file

WordPress Action Hooks

So, how do I get this information into my RSS 2.0 feed? Luckily the good folks at WordPress have included a little action hook in the file that generates your feed. So all we have to do is write a function and hook the action! For my purposes I was writing a new theme so I just included all of this in my functions.php file. You could modify the functions.php file of your existing theme or this could actually be made into a plugin.

First we have the function that does the heavy lifting. The first thing I do in my function is get the image URL. For my purposes I store the image URL in a custom field attached to each post. I also include a default image inside my theme in case I forget to supply an image for a post.

Next I calculate the MIME type of the file. Now there are some really cool functions out there that actually look inside the file to get to the real MIME type. I don’t really need to be that sophisticated since I’m in control of the image so I’ll just use the file extension. I’m supporting PNG, GIF and JPEG (default) image formats.

The final piece of information we need is the filesize. That is a quick and simple function call if you have the absolute path to the file. I found a good function to get that information on the PHP site.

Now just echo out the enclosure element.

The action hook looks like this and should come outside of the function you define. The first argument is the action hook in WordPress. The second argument is the name of our function.

Putting it all together we have.

Conclusion

This article describes part of the reason I love WordPress so much. They were so thoughtful to leave so many useful and meaningful hooks into the platform and that makes my job as a web developer that much easier. Oh and by the way, the above trick won’t work on every RSS reader. That is implementation specific as to whether the reader decides to display the image or not. All you can do is server it up to them.

Looping Through Pages In WordPress

In a recent article I outlined how to modify the output of the wp_list_pages() template function in WordPress. Later on, however, I found that having the <span> tags that I added in every single title was not desirable. So instead of using a filter I wrote a small bit of custom code to iterate through my pages instead of using wp_list_pages(). Here is how to do it.

Wherever you need to output your pages just use some code like this:

Writing A WordPress Filter To Modify Post/Page Titles

I was recently working on a new WordPress theme when I had need to modify the title of pages before output. The style I was attempting to use involved wrapping <span> tags around the title (inside of the href tags). The wp_list_pages() function returns pages (which are basically the same as posts) and has several arguments that you can pass to customize it’s output. However, modifying the title output is not one of the arguments.

Filters to the rescue! I simply opened up the functions.php file and added a function like this:

Pretty simple, eh? All that is left to do is to register this function as an appropriate filter, which you can do like this:

The first parameter tell WordPress which function/activity you want to “hook” into. The second parameter is the name of your function. The final parameter is a priority specifier. The lower this priority number the earlier it will execute.

Done! Now instead of output like this:

I get this:

For extra credit you actually throw all of this into a plugin. That way you wouldn’t necessarily have to modify any of your theme files.