Tutorial: Index PHP Script Contents

15 10 2006

Note: The modifications described in this document require working with system files, so be careful and make sure you have backups of any files you change, just in case.

Until recently I ran an online strategy game set in space, which was written in PHP. When I switched, the latest version of Mac OS X was 10.3 (Panther). Searching files in Panther was a lot different, namely in that Spotlight wasn’t around back then.

Take for example, a situation which used to happen quite regularly, I would need to do a search for all PHP files in my web development folder, which contained certain text strings, such as ‘FIX THIS’ and so on. Basically text I had put there so I could come back to later.

In Panther, this worked a treat. If I did a search for “all php files in this folder, which contain the phrase ‘FIX THIS'”, the relevant PHP files would be listed.

When I upgraded to Tiger, I realised that the same search was not working – no PHP files were being listed at all, even ones I knew for sure contained that phrase. I had to do some reading to find out why Spotlight was evidently messing up.

In order to provide the almost instantaneous search function, Spotlight maintains a database of index files, along with their meta data and other things. Spotlight indexes most things, such as the file name, creation and modification dates, file size and so on for all files, but it doesn’t index the content of any files it does not know the type of.

To allow developers to add Spotlight support to their applications, Apple use what are known as ‘mdimporters’. These bits of code tell Spotlight how to behave with file types it doesn’t understand by default.

One of the existing Apple mdimporters it turned out was a plain text importer called ‘RichText.mdimporter’. Since PHP scripts are really just plain text files, surely this importer should be covering them?

Mdimporters, like standard applications, are actually special types of folders containing the required resources. Poking about inside the RichText mdimporter I found there was an ‘Info.plist’ file which seemed to contain various pieces of information, but most importantly it was defining which file types the mdimporter would cover!

So I added another entry to this list, to include PHP files, saved the plist and (after making a backup) replaced the original RichText.mdimporter.

Retrying my search nevertheless showed the same problem, despite the changes I had made. I decided the best way to see if my changes were working was to tell Spotlight to completely rebuild it’s index of my computer. A few Terminal commands later and Spotlight was busy at work doing just that.
Note that it can take Spotlight a while to rebuild a complete index, especially if you have lots of files on your computer.

When Spotlight was finished, I retried my search for the second time, and finally I had success!

So the following steps are necessary for Spotlight to begin indexing your PHP files:

  • In the Finder, click the ‘Go’ menu and choose ‘Go to Folder…’.
  • Type ‘/System/Library/Spotlight/’ and click ‘Go’.
  • Copy and paste the file ‘RichText.mdimporter’ to your desktop to edit.
  • Right click on the newly copied file, and choose ‘Show Package Contents’. A folder will appear.
  • Double click on ‘Contents’ within this new folder.
  • Right-click on ‘Info.plist’ and choose ‘Open With’ and then ‘Other…’.
  • Select TextEdit or your text editor of choice.
  • Locate the part of the file that looks like this:

PHP Spotlight Image 1

  • Add the code “public.php-script”, positioned as shown here:

PHP Spotlight Image 2

  • Save the file and close the ‘Contents’ folder.
  • You now need to replace the original ‘RichText.mdimporter’ with your updated version on your desktop. Make sure you backup your original first in case.
  • Finally, open up ‘Terminal’ – you can find ‘Terminal’ in ‘Applications’ under ‘Utilities’.
  • Type the following command and press enter:
    • mdimport -r /System/Library/Spotlight/RichText.mdimporter

After Spotlight completes the operation, you should find that PHP scripts are being indexed properly and can now be searched for by content!

Note: This post was originally created as an article. It has now been recreated as a normal post as part of my rearranging.




8 responses

22 11 2006

Hey, thanks for this! I’ve search for a whole afternoon trying to find how to make Spotlight recognise the contents of php files. I knew it was searching the directories as ‘name contains php’ found all my files, but as soon as I entered the first letter in the contents box the results were blanked.

Your instructions are excellent – thanks very much.

23 11 2006

Glad to see it came in useful, it was a real pain when I upgraded from Panther to Tiger and realised that my usual searches for PHP files weren’t working anymore!

4 12 2006

I’ve just “upgraded” to Mac from 18 years of using PCs.

Do you know the text I should use inside to achieve the same thing for .js javascript files. I’ve tried public.js-script, public.js, public.javascript, but none are correct.

Thanks for your help so far.

8 12 2006

Hey there, glad to hear it, hope you’re enjoying the experience so far.

With regards to the .js files, you should be on the right lines since .js files are just plain text files.

I think the correct MIME type to use for .js files should be either




That’s according to an online list of file extensions and their MIME types, so give it a whirl and let me know how you get on?

Sorry for the delay in my reply.

13 12 2006

mmm, damn, don’t have RichText.mdimporter for some reason. Any thoughts?

13 12 2006

Hey there, are you sure you don’t have it? I thought it was installed with the system.

Ahhhh just took a look, and the importer file in the Finder has a Microsoft Word icon on it, so it must’ve been installed along with Office.

30 03 2007

Ryan, Hello again – sorry for losing this thread.
Back to javascript files. I tried text/javascript and x-javascript inside the string tags, but neither of these produce .js files which are searchable by content. Most of the other entries start with public. or com.apple.
Have you managed to get .js files searchable in spotlight? if so, what did you put in the string tag in the info.plist?


5 04 2007

Hey again, welcome back!

Sorry I’m afraid I’ve never really needed to search the contents of javascript files in Spotlight, but if it’s not either application/x-javascript or text/javascript I’m not really sure what else it could be.

I will have a look around for some answers, see if I can shed some light.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: