Archive for January, 2010

Create a custom search engine for Firefox, IE, Chrome

January 26th, 2010

Custom search engines in your browser are all the rage these days. Well, more honestly, they’re not. They won’t make girls swarm in your direction, but they people search your site directly from within in their browser (instead of visiting your site first).

I’ll give you step-by-step directions on how to create your own browser search engine. I put together one for searching the dictionary at Irishionary.com, and you can see it as an addon on Mozilla.org.

In Firefox, my custom search engine look like so:

Custom browser search engine for Irishionary.com.

Custom browser search engine for Irishionary.com.

Creating a browser search engine is quite simple. You write an XML file to describe your search functionality, then you expose that XML file to your site’s visitors. The XML file specification is called OpenSearch, a standard to describe search engines that your browser will understand. We’ll have the search results display as a web page, but you can have the spat back as JSON or whatever.

What you’ll need

  1. A knife.
  2. A rubber band.
  3. A web site with its own search functionality that accepts GET requests (for compatibility with Internet Explorer). For example, you’ll need a page such as http://www.example.com/search.php?query=beer .

Step 1: Create your XML description file

Copy-paste this XML code to a file such as search.xml. You can see mine live in action at http://www.irishionary.com/scripts/browser_search.xml.

<?xml version="1.0" encoding="UTF-8"?>
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/"
                   xmlns:moz="http://www.mozilla.org/2006/browser/search/">
	<ShortName>Irishionary.com</ShortName>
	<Description>Irish language dictionary.</Description>
	<Tags>gaeilge irish gaelic dictionary</Tags>
	<Contact>info@irishionary.com</Contact>
	<InputEncoding>iso-8859-1</InputEncoding>
	<OutputEncoding>iso-8859-1</OutputEncoding>
	<Image width="16" height="16" type="image/x-icon">http://www.irishionary.com/favicon.ico</Image>
        <Image width="64" height="64" type="image/png">http://www.irishionary.com/resources/images/search_logo.png</Image>
	<Url type="text/html" method="get" template="http://www.irishionary.com/process/search.php?search_phrase={searchTerms}">
	</Url>
	<Url type="application/opensearchdescription+xml"
		rel="self"
		template="http://www.irishionary.com/scripts/search.xml" />
	<Query role="example" searchTerms="beer" />
	<Language>en-us</Language>
	<Language>en-gb</Language>
	<Language>ga</Language>
	<moz:SearchForm>http://www.irishionary.com/</moz:SearchForm>
	<AdultContent>false</AdultContent>
	<Attribution>Search data copyright eTeanga (www.eteanga.ie)</Attribution>
</OpenSearchDescription>

Step 2: Customise your XML file (boring)

So the code above describes the Irishionary.com Irish Dictionary search engine. Use your common sense when editing the tags.

Here are some boring details on some of the parts that may need an explanation. There is a full specification of the OpenSearch format available.

  • <InputEncoding> and <OutputEncoding> describe the character encoding that your search engine first accepts, and then spits out (in our case, the character encoding of your HTML search results).
  • The <Image> tags describe two sizes of icons that represent your service. The browser might use the smaller icon to display beside the search box. The bigger image may be used by a directory of search engines, for example.
  • The first <Url> tag is the meat of this whole operation. It describes where to send the search query to get the results. It’s the http://www.example.com/search.php?query=beer URL I mentioned above. In the XML tag, note that {searchTerms} is how you capture what the person typed into the search box. Using our original example, you’d put in http://www.example.com/search.php?query={searchTerms}
  • The second <Url> tag is the URL for where you’re going to publish your XML file online. This will allow the browser to check for updates to your XML in the future.
  • The <Query> is a way to give an example search query. I don’t know where this is displayed.
  • The <Language> tags specify which languages your search engine will accept. It may only be one language. I think you can also use * as a wildcard for “any language”.
  • We’re almost there! <moz:SearchForm> is a Firefox-specific tag which points to where the search form is online.

Step 3: Upload your XML file

Do it. I haven’t been able to get Firefox to recognise a file hosted locally on my machine.

Step 4: Tell your browser about your search engine

There are a couple of ways to test your engine. The first is to let your browser “discover” it. In the HTML of a test page on your site, add the following code inside the <head></head> tags. Customise it to point to where your XML is found.

<link rel="search" type="application/opensearchdescription+xml" title="Test Search" href="http://www.irishionary.com/scripts/browser_search.xml" />

Now *visit* that HTML page in your browser to “auto-discover” the tag you’ve included.

Add to Firefox

When viewing the HTML page that includes the <link> tag above, click the little highlighted arrow beside your default search engine in the search bar. It should look something like the following:

The little arrow gets highlighted when Firefox detects your search engine mentioned in the <link> tag.

The little arrow gets highlighted when Firefox detects your search engine mentioned in the tag.

When you click the arrow, you’ll see the list of installed search engine with the option to “Add” your own engine. If your engine hasn’t been listed, there’s something wrong with your <link> tag above. Once added, you can test your engine!

Add to Chrome

Google give instructions on how to add a detected engine to the browser.

Step 5: Test your search engine

In Firefox, select your engine from the list of search engines, and perform a search. Check out if it brings you to your site’s search results page. If you’ve gotten this far, get someone to pat you on the back. In fact, this may be one of your life’s biggest achievements. Well done.

Bonus step: Direct install link for your visitors

You’ll want to promote the search engine on your site to your visitors. It needs a little Javascript link to get this done:

<a
href="javascript:window.external.AddSearchProvider('http://www.irishionary.com/scripts/browser_search.xml');">Add
engine</a>

Customise the URL in there to point to your own XML file. Now visitors can install the search engine directly from your site.

(Yet Another) Bonus step: Submit to Mozilla

A good way to promote your search engine is to submit it to Mozilla’s addons repository for Firefox. It takes some effort. You’ll need to create a developer profile, then submit your addon for testing. Once you move it out of the “sandbox” it will be available publicly and marked as “experimental”. Have your users test it out for a while, then you can ask for it to be fully released. Get started on Mozilla’s developers’ guide.

Congrats, you’re created a browser search engine. I hope you’ve learned some bits here. There is a good best practices guide over at OpenSearch’s site.

<Image

Creating automatic PDFs with PHP, MySQL

January 18th, 2010

My collaborative Irish dictionary, Irishionary.com, creates a nightly PDF version of the dictionary. A cron job calls a PHP script that generates the PDF. There are examples of great looking PDF publications and mine isn’t one of them ;) All the same, it’s not that hard to create your own PDF files though PHP scripts pulling information from your database if you wish. If you figure out how to make them look nice, let me know!

An example of a PDF created through my PHP script:

This is the automated PDF of the Irishionary.com dictionary.

This is the automated PDF of the Irishionary.com dictionary.

mPDF is the PHP library to use for creating this PDF. There are lots of other libraries available, but it’s hard to find one that instantly suits. mPDF seemed to be good in several ways, especially given that you can style the PDF using CSS (Disclaimer: not all CSS tags are supported. Each sold separately. Batteries not included.) Another good reason to use it is that there are a range of fonts that you can use.

The official PDF to show off what mPDF is below. However, I will not allow you to see a full size version of it in the likely chance that it would negatively affect your view of humanity.

Official show-off PDF by mPDF.

Official show-off PDF by mPDF.

Stop messing around, let’s get coding. Download the mPDF library above, and put it somewhere in your web directory. Start editing a new script called mypdf.php.

Step 1

Include the library in your script.

define('_MPDF_PATH','./mpdf/');
include('./mpdf/mpdf.php');

Step 2

Initiate the object that will output the PDF for you.

$mpdf=new mPDF();

Step 3

Write some HTML to the PDF. This is where your literary prowess comes in handy.

$mpdf->WriteHTML('<h1>My PDF</h1><p>Yes, this ebook was totally
worth $29.95 of your hard-earned cash.</p>', 2);
$mpdf->SetFooter('Copyright: me.');

Step 4

Write the PDF to your file system. This step may require some permissions setting magic that your Auntie Maggy definitely couldn’t do. Check out the documentation for more pointers.

$file_path = $_SERVER['DOCUMENT_ROOT'].'/pdf/mypdf.pdf';
$mpdf->Output($file_path,'F');

Step 5

Either a) give away your PDF for the sake of humanity, or, b) charge a $29.95 price for your Amazing New York Times and Life changing My Mammy” PHP-generated PDF.

Web development on Ubuntu

January 11th, 2010

I get it. You’re a Windows person, always have been and always will be. Ok, you’re not a total n00b either. You’ll use the latest Firefox (even Chrome, but you’re a little suspicious about its privacy implications). For LAMP coding you’ve set up XAMPP and code with Notepad++. You’ll use TortoiseSVN to commit coding changes, and you’ll load up Putty to finally update your product web site. And you’re actually pretty happy with your Windows 7 setup (hey, I like it too).

But I’ve made the switch, and you and your pet dog Pudgie could too. Given that you’re not a total n00b, you should consider using Ubuntu for your daily life, and particularly for coding that amaaaazing web site idea you have that lets users schedule their coffee breaks with long lost school friends. Oh, and it has an AJAX-y frond end with widgets and lots of buttons. Ubuntu is nicely set up for this, whatever your online project. Actually, you’ll even come to like that you can SSH and SVN directly through the terminal.

Apache and MySQL feel more at home here, while XAMPP on Windows has always acted a bit like a shady character that you’re never too sure what it’s going to do next. I was sick of the start-stop services of XAMPP. It’s like trying to use Safari under Windows, it just doesn’t feel right. And try letting Apache start with the rest of your crawling Windows installation, haha no thanks!

To give you a flavour, here’s what my Ubuntu desktop could look like if I was doing web development and happened to keep my windows nice and orderly:

An (orderly) view of my LAMP development on Ubuntu.

An (orderly) view of my LAMP development on Ubuntu.

Still a bit unsure about this Linux thing? It’s easier than it used to be. Just set up a virtual machine with a copy of Ubuntu running on it. No need to do a nasty dual-boot setup. (Later on, I bet you’ll be installing Ubuntu and virtualising Windows).

Ok, you’re almost there.You’ll need to install some extra components (although if you install Ubuntu server edition I think a lot of these are already set up for you).

Coding editor. Ubuntu already comes with a text editor called gEdit. Change the colour scheme to Oblivion, show the File Browser menu on the left and you’ll have an instant coding environment:

Gedit text editing program that's shipped with Ubuntu.

Gedit text editing program that's shipped with Ubuntu.

Apache, MySQL, phpmyadmin: With Ubuntu, you don’t have to trawl the web for software downloads of supported packages. Just install them directly through the system’s package manager.

Subversion: In the same manner as above, install SVN. You’ll be able to update/commit directly in the terminal. Now that you can use the Synaptic Package Manager, grab yourself a copy of Firefox too.

That wasn’t so bad, was it? Now you’re running on a free operating system that’s being constantly updated. If you’ve gotten this far, I now declare you an Ubuntu fanboy :P The only reason I still use Windows 7 is when I need to use Lightroom for my photo collection, but that’s a different (virtualisation) story!